Sie sind auf Seite 1von 11

Things we will learn

T-SQL is not a general-purpose programming language, but it does sup- port some basic programming ideas and constructs that can be used within data manipulation processes. These are generally not used when working with simple SQL statements, but are often used when working with stored procedures.

Using variables Performing conditional processing Repeating processes (looping)

Using variables

Variable is a named object that stores a value. T-SQL variables have specific rules and requirements: All T-SQL variables names must start with @, local variables are prefixed with @, and global variables (which are used extensively by SQL Server itself and generally should not be used for your own purposes) are prefixed with @@. Before they can be used, T-SQL variables must be declared using the DECLAREstatement. Multiple variables may be defined using individual DECLARE statements, or they may be comma-delimited in a single DECLARE statement. There is no way to undeclare variables. They remain present until they go out of scope. For local variables, this means they will exist until the process completes.

Example
DECLARE @age INT; DECLARE @firstName CHAR(20), @lastName CHAR(20);
To assign values to variables, you use the SET statement, as shown here: DECLARE @age INT; DECLARE @firstName CHAR(20), @lastName CHAR(20); SET @lastName=parashari; SET @firstName=arushi; SET @age=21;

Use of select statement


For assignig values to the variables: SELECT @age=21;

* SET or SELECT? There is one important difference between using SET or SELECT to assign variable values. SET only sets a single variable,and to assign values to multiple variables you must use multiple SET statements. SELECT,on the other hand,can be used to assign values to multiple variables in a single statement.

To check the output of the variables


DECLARE @age INT; DECLARE @firstName CHAR(20), @lastName CHAR(20); SET @lastName=parashari; SET @firstName=arushi; SET @age=21; SELECT @lastName, @firstName, @age T-SQL also supports the PRINTstatement, which is used to display mes- sages along with any returned results. Here is an example: DECLARE @age INT; DECLARE @firstName CHAR(20), @lastName CHAR(20); SET @lastName=parashari; SET @firstName=arushi; SET @age=21; PRINT @lastName + + @firstName; PRINT @age;

Converting Variables to Strings


PRINT Age: + Convert(CHAR, @age); Age: 21

Understanding views

Views are virtual tables. Unlike tables that contain data, views simply contain queries that dynamically retrieve data when used.

Why to use views

To reuse SQL statements. . To simplify complex SQL operations. After the query is written, it can be reused easily, without you having to know the details of the underlying query itself. To expose parts of a table instead of complete tables. To secure data. Users can be given access to specific subsets of tables instead of to entire tables. To change data formatting and representation. Views can return data formatted and presented differently from their underlying tables.

Views rules

Like tables, views must be uniquely named. (They cannot have the name of any other table or view.) There is no limit to the number of views that can be created. . SQL Server views may contain no more than 1,024 columns. To create views, you must have security access. This is usually granted by the database administrator. Views can be nested; that is, a view may be built using a query that retrieves data from another view. ORDER BY may not be used in views, but ORDER BYmay be used in the SELECT statement that retrieves data from the view. Views cannot be indexed, nor can they have triggers or default values associated with them. Views can be used in conjunction with tables (for example, to create a SELECT statement that joins a table and a view).

Using views

Views are created using the CREATE VIEW statement. To remove a view, you use the DROP statement. The syntax is simply DROP VIEW view name;. To update a view, you may use the DROP statement and then the CREATE VIEW statement again.