Here I have a collection of some usefull T-SQL snippets.
|
|
This query verifies if the certain database exists on server or not
T-SQL
|
SELECT 'Exists' From Information_schema.Schemata WHERE Catalog_name='AdventureWorks'
|
|
|
Remarks
IF...ELSE constructs can be used in batches, in stored procedures (in which these constructs are often used to test for the existence of some parameter), and in ad hoc queries.
IF tests can be nested after another IF or following an ELSE. There is no limit to the number of nested levels.
Examples
Use one IF...ELSE block
This example shows an IF condition with a statement block. If the average price of the title is not less than $15, it prints the text: Average title price is more than $15.
T-SQL
|
USE pubs GO
IF (SELECT AVG(price) FROM titles WHERE type = 'mod_cook') < $15 BEGIN PRINT 'The following titles are excellent mod_cook books:' PRINT ' ' SELECT SUBSTRING(title, 1, 35) AS Title FROM titles WHERE type = 'mod_cook'
END
ELSE IF (SELECT AVG(price) FROM titles WHERE type = 'mod_cook') > $15 BEGIN PRINT 'The following titles are expensive mod_cook books:' PRINT ' ' SELECT SUBSTRING(title, 1, 35) AS Title FROM titles WHERE type = 'mod_cook' END
|
Origin: MSDN
|
|
|
|
|
|