Transact-SQL
Transact-SQL (T-SQL) is Microsoft's and Sybase's proprietary extension to the SQL (Structured Query Language) used to interact with relational databases. 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. 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. Stored procedures in SQL Server are executable server-side routines. The advantage of stored procedures is the ability to pass parameters. VariablesTransact-SQL provides the following statements to declare and set local variables: DECLARE @var1 NVARCHAR(30);
SET @var1 = 'Some Name';
SELECT @var1 = Name
FROM Sales.Store
WHERE CustomerID = 100;
Flow controlKeywords for flow control in Transact-SQL include
IF DATEPART(dw, GETDATE()) = 7 OR DATEPART(dw, GETDATE()) = 1
PRINT 'It is the weekend.';
ELSE
PRINT 'It is a weekday.';
IF DATEPART(dw, GETDATE()) = 7 OR DATEPART(dw, GETDATE()) = 1
BEGIN
PRINT 'It is the weekend.';
PRINT 'Get some rest on the weekend!';
END;
ELSE
BEGIN
PRINT 'It is a weekday.';
PRINT 'Get to work on a weekday!';
END;
DECLARE @i INT;
SET @i = 0;
WHILE @i < 5
BEGIN
PRINT 'Hello world.';
SET @i = @i + 1;
END;
Changes to DELETE and UPDATE statementsIn Transact-SQL, both the
This example deletes all DELETE u
FROM users AS u INNER JOIN user_flags AS f ON u.id = f.id
WHERE f.name = 'idle';
BULK INSERT
TRY CATCHBeginning with SQL Server 2005,[1] Microsoft introduced additional -- 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
-- roll back the transaction because of error
ROLLBACK TRAN;
END CATCH;
See also
References
External links |
Portal di Ensiklopedia Dunia