Term
What are the two parts of a CTE? |
|
Definition
- A WITH clause containing a SELECT that generates a valid table.
- An outer SELECT that references the table expression.
|
|
|
Term
A recursive CTE consists of
- an __(a)__, which is the source of the __(b)__, along with a __(c)__ statement and a __(d)__, which recurses across the anchor query
- An outer query which references the routine and specifies the __(e)__
|
|
Definition
a. anchor query
b. recursion
c. UNION ALL
d. second query
e. number of recursion levels |
|
|
Term
A __________ subquery is independent of the outer query within which it is contained. |
|
Definition
|
|
Term
A __________ subquery depends upon and references columns from the outer query. |
|
Definition
|
|
Term
Use a recursive CTE that will return
'Components Road Frames' and all its children
Reference ProductCategory, ProductSubCategory and Product from AdventureWorks2008R2. |
|
Definition
declare @searchname varchar(50)
set @searchname ='Components Road Frames'
;with
hierarchycte (id, name, parentid) as
(
select ProductCategoryID ID, Name, null ParentID
from Production.ProductCategory
union all
select ProductSubcategoryID ID, c.Name+' '+s.Name Name, s.ProductCategoryID ParentID
from Production.ProductCategory c
join Production.ProductSubcategory s on c.ProductCategoryID = s.ProductCategoryID
union all
select p.ProductID ID, c.Name+' '+s.Name+' '+p.Name Name, p.ProductSubcategoryID ParentID
from Production.ProductCategory c
join Production.ProductSubcategory s on c.ProductCategoryID = s.ProductCategoryID
join Production.Product p on s.ProductSubcategoryID = p.ProductSubcategoryID
),
productcte(ID, Name, ParentID,Level) AS
(
SELECT ID, Name, ParentID , 2 as Level-- level, provide starting point
FROM hierarchycte
WHERE Name = @searchname
union all
select r.id, r.name, r.parentid,Level+ 1
from hierarchycte r
join productcte on productcte.ID = r.parentid
)
select * from productcte order by ID
|
|
|
Term
What is the two-word term when you embed a SELECT statement into a FROM clause? |
|
Definition
|
|
Term
What is the syntax of the RANK function? |
|
Definition
RANK() OVER(PARTITION BY <column_name>
ORDER BY <column_Name>) AS <alias> |
|
|
Term
Which ranking function requires a parameter? |
|
Definition
|
|
Term
What is the difference between RANK()
and DENSE_RANK()? |
|
Definition
If there are ties in values, RANK assigns the same rank value to each row then skips to the next value, leaving a gap in the sequence corresponding to the number of rows that were tied. DENSE_RANK does not leave gaps. |
|
|
Term
What is the difference between ROW_NUMBER()
and RANK()? |
|
Definition
ROW_NUMBER() assigns incrementing sequential numbers based on logical order specified in the ORDER BY subclause. RANK() assigns ranking numbers based on the value of the column specified in the ORDER BY subclause. |
|
|
Term
What does the NTILE function do? |
|
Definition
The NTILE function allows you to associate the rows in the result with equally sized groups of rows (tiles). |
|
|
Term
Could you have a CREATE PROCEDURE or ALTER PROCEDURE command in a stored procedure? |
|
Definition
No, these are 2 of 16 commands that cannot be used in a stored procedure. |
|
|
Term
Which two of the following are allowed in a stored procedure?
- CREATE VIEW
- CREATE TABLE
- ALTER VIEW
- ALTER INDEX
- CREATE TRIGGER
|
|
Definition
2 & 3 (CREATE TABLE & ALTER INDEX) |
|
|
Term
use master go create procedure sp_one as select 'this' go use test2 go create procedure sp_one as select 'that' go
exec sp_one
What will be the output? |
|
Definition
|
|
Term
When do you absolutely need to use the BEGIN and END with an IF or a WHILE? (Select one answer)
a. Never
b. Always
c. When executing 2 or more lines of code
d. When executing 3 or more lines of code |
|
Definition
c, when executing more than 1 line of code. |
|
|
Term
Differentiate between @@IDENTITY
and SCOPE_IDENTITY(). |
|
Definition
@@IDENTITY is a global variable and SCOPE_IDENTITY() is a function. @@IDENTITY contains the last identity value inserted for the connection. SCOPE_DENTITY() returns the last identity value inserted in the scope of the current statement. |
|
|
Term
When defining parameters for a stored procedure, how do you differentiate between input and output parameters? |
|
Definition
An output parameter has the keyword OUT/OUTPUT in the CREATE PROCEDURE <stored procedure> and
the EXEC <stored procedure>.
(e.g. @parm1 INT OUTPUT) |
|
|
Term
What is the range of error numbers that ship with SQL Server? |
|
Definition
|
|
Term
How many severity levels does SQL Server define and what is the number range? |
|
Definition
|
|
Term
Any error with a severity level of ____ or higher is automatically logged to the SQL Server error log and the Windows Application Event Log. |
|
Definition
|
|
Term
What 3 things can you say about errors with a severity level of 20 to 25? |
|
Definition
- Fatal.
- Cause conection to be terminated.
- Open transactions are rolled back.
|
|
|
Term
What numbers must I use to create my own custom error messages and what stored procedure do I use? |
|
Definition
50001 and higher, sp_addmessage. |
|
|
Term
What command do you need to send an error message to an application from a TRY...CATCH block? |
|
Definition
A RAISERROR from within the CATCH block. |
|
|
Term
True or False, I can return the corresponding error
message from error 2627 from within a TRY...CATCH block using the RAISERROR command. |
|
Definition
False, any number less than 50000 is considered a system error and cannot be returned even with the RAISERROR command. You will need to dynamically build a custom message and use error number 50000 to return the message with RAISERROR command. |
|
|
Term
|
Definition
A connection setting that causes a transaction to complete entirely or fail entirely. You can use SET XACT_ABORT ON/OFF to change the setting. |
|
|
Term
What's the difference executing RAISERROR in the TRY block versus the CATCH block? |
|
Definition
In a TRY block, a RAISERROR immediately passes control to the CATCH block without returning an error message to the application. In a CATCH block, a RAISERROR closes the transaction and returns control to the application with the specified message. |
|
|
Term
What function returns the state of the innermost transaction in a TRY...CATCH block? |
|
Definition
XACT_STATE with the values (1) - open transaction to be committed or rolled back, (0) - no open transaction, (-1) open transaction in a doomed state and can only be rolled back. |
|
|
Term
How does XACT_ABORT behave with a TRY block versus a CATCH block? |
|
Definition
With a TRY block, the transaction is not terminated and control is transferred to the CATCH block. If XACT_ABORT is set on, any error is fatal. A transaction inside a CATCH block cannot be committed if XACT_ABORT is turned on. |
|
|
Term
Discuss the 5 components of cursors. |
|
Definition
- DECLARE is used to define the SELECT statement that is the basis for the rows in the cursor.
- OPEN executes the SELECT and loads the row into a memory structure.
- FETCH retrieves one row at a time from the cursor.
- CLOSE closes the processing on the cursor.
- DEALLOCATE removes the cursor and releases the memory structures containing the cursor result sets.
|
|
|
Term
What are the 4 types of cursors? |
|
Definition
- FAST_FORWARD - fastest performing, moves forward one row at a time, same as FORWARD_ONLY and READ_ONLY cursors, default option.
- STATIC - result is retrieved and stored in a temporary table in tempdb, supports scrolling.
- KEYSET - set of keys that uniquely identify each result set row is stored in a temporary table in tempdb.
- DYNAMIC - most expensive, cursor reflects all changes made to result set.
|
|
|
Term
The code within every function is required to complete with a _________ statement. |
|
Definition
|
|
Term
With a scalar function you RETURN a ____(a)____
____(b)____.
With an inline table-valued function you RETURN
a ____(c)____ ____(d)____. |
|
Definition
a. single
b. value
c. SELECT
d. statement |
|
|
Term
With a multi-statement table-valued function, you RETURN __________. |
|
Definition
Nothing. You include only the RETURN keyword at the end of the function. |
|
|
Term
With the exception of a/an ____(a)____ function,
all the code within a function is required to be enclosed
in a ____(b)____. |
|
Definition
a. inline table-valued
b. BEGIN...END block |
|
|
Term
What are the 4 options for a function? |
|
Definition
- ENCRYPTION
- SCHEMABINDING
- RETURNS NULL ON NULL INPUT/CALLED NULL ON NULL INPUT
- EXECUTE AS
|
|
|
Term
An ____(a)____ function behaves like and is interchangeable with a ____(b)____. |
|
Definition
a. inline table-valued
b. view |
|
|
Term
DML triggers are created against a table or a view and are defined for a specific event: ______, ______ or ______. |
|
Definition
|
|
Term
How can you specify the first and last AFTER triggers to fire for the same action? |
|
Definition
|
|
Term
DDL triggers can execute either when a ______ statement is executed or when a user _______. |
|
Definition
DDL, logs on to a SQL Server instance. |
|
|
Term
While DML triggers have access to the inserted and deleted tables, DDL triggers have access to the _______ function. |
|
Definition
|
|
Term
Logon triggers are fired after ________ ________but before the ________ ___________ is ________ ________ |
|
Definition
Logon triggers are fired after authentication succeeds but before the user session is actually established. |
|
|
Term
What are the 6 requirements for updating data through a view? |
|
Definition
- The data modification must reference exactly one table.
- Columns in the view must reference columns in a table directly.
- THe column cannot be derived from an aggregate.
- The column cannot be computed as a result of a UNION/UNION ALL, CROSS JOIN, EXCEPT, INTERSECT.
- The column being modified cannot be affected by the GROUP BY, DISTINCT or HAVING clause.
- The TOP operator is not used.
|
|
|
Term
What is one possible workaround to update an underlying table if a view does not meet the requirements to be updatable ? |
|
Definition
Use an INSTEAD OF trigger on the view. |
|
|
Term
What does the WITH CHECK OPTION clause do in a view? |
|
Definition
It requires that the only data manipulation that can occur through the view must also be retrievable when you select the view. |
|
|
Term
|
Definition
To consider the index covered, it must contain all columns referenced in the query. |
|
|