Chapter 14 Flashcards

1
Q

What’s the name of the system database that stores temporary tables?

A

tempdb

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

A series of SQL statements that you can store in a file is called a

A

script

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

Which statement can you use to handle errors caused by one or more SQL statements?

A

TRY…CATCH

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

What do you call a local variable that can store a single value?

A

scalar

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

Which utility can you use to execute Transact-SQL scripts from a command line?

A

SQLCMD

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

Which statement changes the database context to a database named TestDB?

A

USE TestDB;

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

Which statement do you use to return a message to the client?

A

PRINT

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

Which system function can you use to return the value of the most recently assigned identity column?

A

@@IDENTITY

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

What statement can you use to divide a script into multiple batches?

A

GO

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

Which statement assigns the value “Test” to a scalar variable named @Name that’s declared with the varchar data type?

A

SET @Name = ‘Test’;

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

Which statement can you use to control the flow of execution based on a true/false condition?

A

IF…ELSE

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

Which statement creates a table variable named @TestTable?

A

DECLARE @TestTable table;

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

What statement do you use to execute a dynamic SQL statement?

A

EXEC

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

Which statement can you use to repeatedly execute a statement or set of statements?

A

WHILE

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

Which statement tests whether the database named TestDB exists?

A

IF DB_ID (‘TestDB’) IS NOT NULL

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

The scope of a local variable is limited to what?

A

the batch in which it’s defined

17
Q

Which of the following statements can be coded in a batch with other statements?

A

CREATE TABLE

18
Q

The scope of a derived table is limited to what?

A

the statement in which it’s defined

19
Q

The scope of a temporary table is limited to what?

A

the database session in which it’s defined

20
Q

Given the following statements that declare a local variable and set its value, which of the following will cause an error?

DECLARE @Example1 varchar(128);
SET @Example1 = ‘Invoices’;

A

SELECT *

FROM @Example1;

21
Q

Consider the following code:

USE AP;

SELECT * INTO #InvoiceCopy FROM Invoices;

DECLARE @InvoiceID int, @InvoiceTotal money;
DECLARE @Total money;
SET @Total = 0;

WHILE @Total + (SELECT TOP 1 InvoiceTotal
FROM #InvoiceCopy
ORDER BY InvoiceTotal DESC) <= 200000
BEGIN
SELECT TOP 1 @InvoiceID = InvoiceID, @InvoiceTotal = InvoiceTotal
FROM #InvoiceCopy
ORDER BY InvoiceTotal DESC;
IF @InvoiceTotal < 1000
BREAK;
ELSE
BEGIN
SET @Total = @Total + @InvoiceTotal;
DELETE FROM #InvoiceCopy
WHERE InvoiceID = @InvoiceID;
END;
END;

PRINT ‘Total: $’ + CONVERT(varchar, @Total, 1);

What can cause the WHILE loop in this script to end other than the expression on the statement becoming true?

A

When the value of the @InvoiceTotal variable becomes less than 1000

22
Q

Consider the following code:

USE AP;
DECLARE @Date1 date;
DECLARE @Date2 date;
SELECT @Date1 = MIN(InvoiceDueDate), @Date2 = MAX(InvoiceDueDate)
FROM Invoices
WHERE InvoiceTotal - PaymentTotal - CreditTotal > 0;
IF @Date1 < GETDATE()
IF @Date2 < GETDATE()
BEGIN
PRINT ‘Earliest past due date: ‘ + CONVERT(varchar, @Date1, 1);
PRINT ‘Latest past due date: ‘ + CONVERT(varchar, @Date2, 1);
END;
ELSE
PRINT ‘Earliest past due date: ‘ + CONVERT(varchar, @Date1, 1);
ELSE
PRINT ‘No invoices past due’;

If the current date is 03/15/20, the earliest invoice due date for invoices with unpaid balances is 02/09/20, and the latest invoice due date for invoices with unpaid balances is 03/20/20, what will be printed by this script?

A

Earliest past due date: 02/09/20