Programmable Objects Flashcards
How do you declare and instantiate a variable in T-SQL?
DECLARE @ AS
How is a variable set using T-SQL?
There are many ways to set a variable, it can be set when it is declared, it can be set using the SET statement after a variable is declared, or multiple variables can be set using the SELECT statement (this option has a larger potential for errors.
Example:
DECLARE @i AS INT = 10;
DECLARE @b AS INT;
SET @b = 10;
SET @B = (SELECT empid FROM HR.Employees WHERE firstname = ‘Josh’ AND lastname = ‘Karr’);
Define batch.
A batch is one or more T-SQL statements sent by a client application to SQL Server for execution as a single unit.
Additionally, a batch is a set of commands that are parsed and executed as a unit.
If the parsing is successful, SQL Server then attempts to execute the batch.
In the event of a syntax error in the batch, the whole batch is not submitted to SQL Server for execution.
What is used to signal the end of a batch?
SSMS provides a client tool command called GO that signals the end of a batch.
Note that the GO command is a client tool command and not a T-SQL Server command.
True or False: A variable is not local to the batch in which it is defined.
False
If you refer to a variable that was defined in another batch, you’ll get an error saying that the variable was not defined.
List statements which require their own batch
REATE DEFAULT, CREATE FUNCTION, CREATE PROCEDURE, CREATE RULE, CREATE SCHEMA, CREATE TRIGGER, and CREATE VIEW.
True or False: One best practice you can follow to avoid such problems is to separate DDL and DML statements into different batches.
True
What is a Flow Element?
Flow elements control the flow of your code.
What are the main flow elements T-SQL provides?
IF, ELSE, WHILE
How are the boundaries of a statement block defined?
BEGIN marks the start of the statement block, and END; marks the end of the block.
True or False: If you need to run more than one statement in the IF or ELSE sections, a statement block is not required.
False
Multiple IF/ELSE statements require a statement block, same as any OOP language.
Example:
IF DAY(SYSDATETIME()) = 1
BEGIN
PRINT ‘Today is the first day of the month.’;
PRINT ‘Starting first-of-month-day process.’;
/* … process code goes here … */
PRINT ‘Finished first-of-month-day database process.’;
END;
True or False: A WHILE statement will continue to execute if the statement evaluates to UNKNOWN, and only terminates if the statement evaluates to FALSE.
False
When the predicate is FALSE or UNKNOWN, the loop terminates.
What is the name of the built-in looping element provided by T-SQL?
Trick question. There is no element provided; however, it’s easy to mimic such an element with a WHILE loop and a variable.
EXAMPLE:
DECLARE @i AS INT = 1; WHILE @i <= 10 BEGIN PRINT @i; SET @i = @i + 1; END;
T-SQL supports the BREAK and CONTINUE statements
True
How is a cursor defined in the context of T-SQL?
A cursor is a non-relational result with order guaranteed among rows.
It is also an object which can be used to process rows from a result of a query one at a time and in a requested order.