From Wikipedia
From Wikipedia
Jump to: navigation, search Transact-SQL (T-SQL) is Microsoft's and Sybase's proprietary extension to SQL. SQL, often expanded to Structured Query Language, is a standardized computer language that was originally developed by IBM for querying, altering and defining relational databases, using declarative statements. T-SQL expands on the SQL standard to include procedural programming, local variables, various support functions for string processing, date processing, mathematics, etc. and changes to the DELETE and UPDATE statements. These additional features make Transact-SQL Turing complete. Transact-SQL is central to using Microsoft SQL Server. All applications that communicate with an instance of SQL Server do so by sending Transact-SQL statements to the server, regardless of the user interface of the application.
Contents
[hide]
y y y y y y
1 Flow control 2 Changes to DELETE and UPDATE statements 3 BULK INSERT 4 TRY CATCH 5 See also 6 External links
the current date is a weekend day, or "It is a weekday" if the current date is a weekday.
IF DATEPART(dw, GETDATE()) = 7 OR DATEPART(dw, GETDATE()) = 1 PRINT 'It is the weekend.' ELSE PRINT 'It is a weekday.' BEGIN and END mark a block of statements. If more than one statement is to be controlled by the
conditional in the example above, we can use BEGIN and END like this:
IF DATEPART(dw, GETDATE()) = 7 OR DATEPART(dw, GETDATE()) = 1 BEGIN PRINT 'It is the weekend.'
PRINT 'Get some rest in weekend!' END ELSE BEGIN PRINT 'It is a weekday.' PRINT 'Get to work in weekday!' END WAITFOR will wait for a given amount of time, or until a particular time of day. The statement can
Beginning with SQL Server 2008, Microsoft introduced additional TRY CATCH logic to support exception type behaviour. This behaviour enables developers to simplify their code and leave out @@ERROR checking after each SQL execution statement.
-- begin transaction BEGIN TRAN BEGIN TRY -- execute each statement INSERT INTO MYTABLE(NAME) VALUES ('ABC') INSERT INTO MYTABLE(NAME) VALUES ('123') -- commit the transaction COMMIT TRAN END TRY BEGIN CATCH -- rollback the transaction because of error ROLLBACK TRAN END CATCH