Data modeling using ER
Subqueries and scripts & batches1Definition of a
subqueryNested subqueriesCorrelated subqueries The ISNULL
functionDerived tablesThe EXISTS operatorMixing data types: CAST
& CONVERTPerformance considerationsWriting scripts Retrieving
identity values@@ROWCOUNTBatches Control-of-flow statements
Steen Jensen, autumn 2013
Definition of a subqueryA subquery is a query nested inside
another query
Generally used for one of a few needs:To break a query into a
series of logical stepsTp provide a listing to be the target of a
WHERE clauseTo provide a lookup driven by each individual record in
a parent query
Most subqueries (but not all) can also be written as a
join2Nested subqueriesA nested subquery is a query, which only goes
in one direction returning either a single value for use in the
outer query, or a full list of values to bused with the IN
operator
See examples page 215 218bot3Correlated subqueriesA correlated
subquery works in two directions
In a correlated subquery the inner query runs on information
provided by the outer query, and vice versa
See examples page 218bot 223bot4The ISNULL functionThe ISNULL
function accepts a variable or expression and tests it for a NULL
value
If the value is NULL, the function returns some other specified
value
If the value is not NULL, the original value is returned
See examples page 223bot 225bot
5Derived tablesA derived table is made up of the columns and
rows of a result set from a query
To create a derived table, you must do two things:Enclose the
query that generates the result set in parenthesesAlias the results
of the query, so the alias can be referenced as a table
See examples page 215bot 227bot
6Parts, which can be skipped7Using common table expressions
(CTEs): page 228top 232top
The EXISTS operatorWhen you use EXISTS, you dont return data but
a simple TRUE/FALSE
See examples page 232 234bot
8Mixing data types: CAST & CONVERTBoth CAST and CONVERT
perform data type conversions
You would use CAST and CONVERT in situations, when SQL Server
would not implicitly make the conversions for you
See examples page 236top 239top
9Performance considerations10
Exercise in subqueriesExperiment running & changing the
different subqueries at SQLZOO:
http://sqlzoo.net/wiki/SELECT_.._SELECT
11Writing scriptsA script isnt a script until you store it in a
file, where it can be pulled up and reused
Scripts are usually treated as a unit
Scripts can use both system functions and local variables
You can declare one variable (a scalar variable) at a time or
several
The are three ways to set the value of a variable:Initialize it
in the DECLARE statementUse a SELECT statementUse a SET
statement
See examples page 384 388bot
12Common system functions13
Retrieving identity valuesBy using the system function called
SCOPE_IDENTITY() the value of the last inserted key can be
obtained
See examples page 391 394mid
14Parts, which can be skipped15Generating sequences: page 395
398bot
Running from the command prompt - SQLCMD: page 405top 409bot
Dynamic SQL using the EXEC command: page 409bot 415bot
@@ROWCOUNTBy using the system function called @@ROWCOUNT the
number of affected rows can be obtained
See examples page 398bot 399mid
16Grouping statements into batchesA batch is a grouping of SQL
statements into one logical unit
If a statement fails at parsetime, nothing runsIf a statement
fails at runtime, all statements until the erroneous statement have
already been run
To separate a script into multiple batches, you make use of the
GO statement
The GO statement:Must be on a separate lineCauses all statements
prior to the GO statement to be sent independently to the serverIs
not a SQL statement
See examples page 400mid 401top + 404 405top
17When to use batchesBatches are used, when something has to
happen either before or separately from everything else in a
script
The following commands require their own batch:CREATE
DEFAULTCREATE PROCEDURECREATE RULECREATE TRIGGERCREATE VIEW
Batches can be used to establish precedence
18Control-of-flow statementsControl-of-flow statements makes it
possible to use programming language constructs in SQL
The control-of-flow statements
include:IF...ELSEGOTOWHILEWAITFORTRY/CATCH
See examples page 417-418 + 419bot 420top + 421bot 423top + 425
426mid + 427mid + 429bot 430mid
19Exercise in scriptMake ex.1 on page 432 (chapter 11) in the
book without looking at the answer
Then check your answer up against the indicative answer on page
790
20