Thus far we have used Transact-SQL to write queries that capture information in your database.
This module covers ways in which you can use Transact-SQL to interact with your server in other ways.
I call it the programming module because it covers ways that you can integrate programming concepts into your Transact-SQL statements to give you greater control over your database.
After completing this module, you will be able to:
- Declare and use variables
- Use functions to obtain information and perform SQL Server 2012 operations
- Handle errors generated by SQL Server 2012
- Program your code so that errors are raised to notify you of events
In the next lesson, declaration and use of variables in your Transact-SQL code will be discussed.
Well, you now know how to declare variables, but the question that follows is, How do you change their values?
There are three ways to set the value in a variable. You can initialize it in the DECLARE statement, use a SELECT statement, or use a SET statement.
Functionally, SET and SELECT work almost the same, except that a SELECT statement can do a couple more things:
- SELECT can assign a value from a column in the SELECT statement
- SELECT can assign values to many variables in the same statement
So why have two ways of doing this? SELECT predates SET back in SQL Server history, so why go to the trouble to implement SET at all?
Suffice to say that SET is now part of the ANSI/ISO standard, and that is why it has
been put in there. However, I cannot find anything wrong with the same functionality in SELECT, even ANSI/ISO seems to think that it is okay.