PL/SQL vs. Transact-SQL
Transact-SQL Central to the use of Microsoft® SQL Server™. All applications that communicate with SQL Server do so by sending Transact- SQL statements to the server, regardless of an application's user interface.
Transact-SQL Applications that use a graphical user interface General language sentences Microsoft Visual C++®, Visual Basic®, or Microsoft Visual J++® that such as ADO, OLE DB, and ODBC. Web pages Distributed database systems Data warehouses in which data is extracted from online transaction processing (OLTP) systems
Transact-SQL Applications that use a graphical user interface General language sentences Microsoft Visual C++®, Visual Basic®, or Microsoft Visual J++® that such as ADO, OLE DB, and ODBC. Web pages Distributed database systems Data warehouses in which data is extracted from online transaction processing (OLTP) systems
Transact-SQL Define a variable: –Variables are declared in the body of a batch or procedure with the DECLARE statement and are assigned values with either a SET or SELECT statement. USE pubs varchar(30) = 'Ring%' SELECT au_lname, au_fname, phone FROM authors WHERE au_lname
Transact-SQL USE pubs SET NOCOUNT ON GO datetime = '0877' = '1/01/93' -- Here is the SELECT statement syntax to assign values to two local -- variables. -- = = '1/01/93' SET NOCOUNT OFF SELECT fname, lname FROM employee WHERE pub_id and hire_date
Transact-SQL Sets the specified local variable, previously created with the statement, to the given value. char(20) = 'This is a test' GO
Transact-SQL Use a local variable assigned a value with SET in a SELECT statement This example creates a local variable and uses this local variable in a SELECT statement to find all author first and last names where the author resides in the state of Utah. USE pubs GO char(2) = 'UT' SELECT RTRIM(au_fname) + ' ' + RTRIM(au_lname) AS Name, state FROM authors WHERE state GO
Transact-SQL Specifies that the given local variable (created using should be set to the specified expression. It is recommended that be used for variable assignment rather than USE Northwind nvarchar(30) = 'Generic Name' = CompanyName FROM Customers WHERE CustomerID = 'ALFKA' AS 'Company Name'
Transact-SQL PRINT: Returns a user-defined message to the client.This example converts the results of the GETDATE function to a varchar data type and concatenates it with literal text to be returned by PRINT. PRINT 'This message was printed on ' + RTRIM(CONVERT(varchar(30), GETDATE())) + '.'
Transact-SQL CAST and CONVERT –Explicitly converts an expression of one data type to another. CAST and CONVERT provide similar functionality. CAST ( expression AS data_type ) CONVERT ( data_type [ ( length ) ], expression [, style ] )
Transact-SQL decimal (5, 2) = SELECT AS varbinary(20)) AS decimal(10,5)) -- Or, using CONVERT SELECT CONVERT(decimal(10,5),
Transact-SQL BEGIN...END Encloses a series of Transact-SQL statements so that a group of Transact-SQL statements can be executed. BEGIN and END are control-of- flow language keywords. BEGIN { sql_statement | statement_block } END
Transact-SQL SET NOCOUNT OFF GO USE pubs GO SET NOCOUNT ON GO varchar(255) IF (SELECT COUNT(price) FROM titles WHERE title_id LIKE 'BU%' AND price 0 BEGIN = 'There are several books that are a good value at under $20. These books are: ' SET NOCOUNT OFF SELECT title FROM titles WHERE price < 20 END ELSE BEGIN = 'There are no books under $20. ' SELECT title FROM titles WHERE title_id LIKE 'BU%' AND PRICE <10 END
Transact-SQL IF...ELSE IF Boolean_expression { sql_statement | statement_block } [ ELSE { sql_statement | statement_block } ]
Transact-SQL 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 PRINT 'Average title price is more than $15.'
Transact-SQL WHILE Boolean_expression { sql_statement | statement_block } [ BREAK ] { sql_statement | statement_block } [ CONTINUE ]
Transact-SQL USE pubs GO WHILE (SELECT AVG(price) FROM titles) < $30 BEGIN UPDATE titles SET price = price * 2 SELECT MAX(price) FROM titles IF (SELECT MAX(price) FROM titles) > $50 BREAK ELSE CONTINUE END PRINT 'Too much for the market to bear'
Transact-SQL SET NOCOUNT Stops the message indicating the number of rows affected by a Transact-SQL statement from being returned as part of the results. SET NOCOUNT { ON | OFF }
Transact-SQL SET NOCOUNT ON int DECLARE shipment_cursor CURSOR FOR SELECT p#, sum(qty) FROMsp GROUP BYp# OPEN shipment_cursor FETCH NEXT FROM Set IF <> 0 PRINT ' >' WHILE = 0 BEGIN FETCH NEXT FROM Set END = ' ' +' '+ cast as char(10)) CLOSE shipment_cursor DEALLOCATE shipment_cursor GO