Programmable Objects Flashcards

1
Q

How do you declare and instantiate a variable in T-SQL?

A

DECLARE @ AS

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How is a variable set using T-SQL?

A

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’);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Define batch.

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is used to signal the end of a batch?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

True or False: A variable is not local to the batch in which it is defined.

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

List statements which require their own batch

A

REATE DEFAULT, CREATE FUNCTION, CREATE PROCEDURE, CREATE RULE, CREATE SCHEMA, CREATE TRIGGER, and CREATE VIEW.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

True or False: One best practice you can follow to avoid such problems is to separate DDL and DML statements into different batches.

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is a Flow Element?

A

Flow elements control the flow of your code.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are the main flow elements T-SQL provides?

A

IF, ELSE, WHILE

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How are the boundaries of a statement block defined?

A

BEGIN marks the start of the statement block, and END; marks the end of the block.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

True or False: If you need to run more than one statement in the IF or ELSE sections, a statement block is not required.

A

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;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

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.

A

False

When the predicate is FALSE or UNKNOWN, the loop terminates.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the name of the built-in looping element provided by T-SQL?

A

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;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

T-SQL supports the BREAK and CONTINUE statements

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How is a cursor defined in the context of T-SQL?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Reasons why the cursor object should be avoided if possible.

A

First and foremost, when you use cursors you pretty much go against the relational model, which is based on set theory.

Given a set-based query and cursor code that do similar physical processing behind the scenes, the cursor code is usually many times slower than the set-based code.

With cursors, you write imperative solutions—in other words, you’re responsible for defining how to process the data (declaring the cursor, opening it, looping through the cursor records, closing the cursor, and deallocating the cursor).

17
Q

List the major steps involved when working with a cursor:

A
  1. (optional) Declare variables if any modified or returned based off of operations involving the cursor.
  2. Declare the cursor based on a query.
  3. Open the cursor.
  4. Fetch attribute values from the first cursor record into variables.
  5. Iterate though cursor records while @@FETCH_STATUS = 0
  6. Close the cursor
  7. Deallocate the cursor.
18
Q

SQL Server supports three types of temporary tables, name them.

A
  1. local temporary tables
  2. global temporary tables
  3. table variables
19
Q

How is a local temporary table created?

A

A local temporary table is created by naming it with a pound sign as a prefix, such as #T1.

20
Q

What are the types of Routines which SQL Server supports?

A

user-defined functions, stored procedures, and triggers.

21
Q

Parts of creating a function

A
  1. DROP IF EXISTS
  2. CREATE FUNCTION ( @parameter(s) )
  3. RETURNS
  4. AS
  5. end with GO
22
Q

What does the following statement accomplish?

SET NOCOUNT ON

A

This command is used to suppress messages indicating how many rows were affected by DML statements.

23
Q

What is a trigger?

A

A trigger is a special kind of stored procedure which cannot be executed explicitly; rather, it’s attached to an event. Whenever the event takes place, the trigger fires and the trigger’s code runs.

24
Q

SQL Server supports the associate of triggers with two kinds of events, name these events.

A

DML events such as an INSERT, UPDATE, or DELETE statement; and DDL events such as CREATE TABLE

25
Q

True or False: A trigger is considered part of the transaction that includes the event that caused the trigger to fire.

A

True

Issuing a ROLLBACK TRAN command within the trigger’s code causes a rollback of all changes that took place in the trigger, and also of all changes t hat took place in the transaction associated with the trigger.

26
Q

SQL Server supports two types of DML triggers, name them.

A

after, instead of

An after trigger fires after the event it’s associated with finishes and can be defined only on permanent tables.

An instead of trigger fires instead of the event it’s associated with and can be defined on permanent tables and views.

27
Q

What are the components of creating a new trigger?

A

CREATE TRIGGER FOR AFTER INSERT
AS
SET NOCOUNT ON;

INSERT INTO dbo.T1_Audit(keycol, datacol)
SELECT keycol, datacol FROM inserted;
GO