Server Interaction  «Prev  Next»

Lesson 7

SQL Server Programming Conclusion

This module discussed ways in which you can use programming concepts to give you greater control over your database.
Having completed this module, you should be able to:
  1. Declare and use variables
  2. Use functions to obtain information and perform SQL Server 2012 operations
  3. Handle errors generated by SQL Server 2012
  4. Program your code so that errors are raised to notify you of events
Variables are declared in the body of a batch or procedure with the DECLARE statement and are assigned values with either a SET or SELECT statement. Cursor variables can be declared with this statement and used with other cursor-related statements. After declaration, all variables are initialized as NULL.

Syntax
DECLARE 
     {{ @local_variable [AS] data_type } 
         | { @cursor_variable_name CURSOR } 
         | { table_type_definition } 
     } [ ,...n]

 < table_type_definition > ::= 
     TABLE ( { < column_definition > | < table_constraint > } [ ,... ] 
             ) 

 < column_definition > ::= 
     column_name scalar_data_type 
     [ COLLATE collation_name ] 
     [ [ DEFAULT constant_expression ] | IDENTITY [ ( seed, increment ) ] ] 
     [ ROWGUIDCOL ] 
     [ < column_constraint > ] 

 < column_constraint > ::= 
     { [ NULL | NOT NULL ] 
     | [ PRIMARY KEY | UNIQUE ] 
     | CHECK ( logical_expression ) 
     } 

 < table_constraint > ::= 
     { { PRIMARY KEY | UNIQUE } ( column_name [ ,... ] ) 
     | CHECK ( search_condition ) 
     } 

Arguments

@local_variable


Glossary terms

This module introduced you to the following terms:
  1. Fatal error
  2. Function
  3. Handling errors
  4. Variable
In the next module, learn about SQL Server 2012 triggers.

SQL Server 2012 - Quiz

Before moving on to the next module, click the Quiz link below to check your knowledge of the material covered in this module with a short, multiple-choice quiz.
SQL Server 2012 - Quiz