Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 PL/SQL vs. Transact-SQL

2 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.

3 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

4 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

5 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 DECLARE @find varchar(30) SET @find = 'Ring%' SELECT au_lname, au_fname, phone FROM authors WHERE au_lname LIKE @find

6 Transact-SQL USE pubs SET NOCOUNT ON GO DECLARE @pub_id char(4), @hire_date datetime SET @pub_id = '0877' SET @hire_date = '1/01/93' -- Here is the SELECT statement syntax to assign values to two local -- variables. -- SELECT @pub_id = '0877', @hire_date = '1/01/93' SET NOCOUNT OFF SELECT fname, lname FROM employee WHERE pub_id = @pub_id and hire_date >= @hire_date

7 Transact-SQL SET @local_variable Sets the specified local variable, previously created with the DECLARE @local_variable statement, to the given value. DECLARE @myvar char(20) SET @myvar = 'This is a test' SELECT @myvar GO

8 Transact-SQL Use a local variable assigned a value with SET in a SELECT statement This example creates a local variable named @state 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 DECLARE @state char(2) SET @state = 'UT' SELECT RTRIM(au_fname) + ' ' + RTRIM(au_lname) AS Name, state FROM authors WHERE state = @state GO

9 Transact-SQL SELECT @local_variable Specifies that the given local variable (created using DECLARE @local_variable) should be set to the specified expression. It is recommended that SET @local_variable be used for variable assignment rather than SELECT @local_variable. USE Northwind DECLARE @var1 nvarchar(30) SELECT @var1 = 'Generic Name' SELECT @var1 = CompanyName FROM Customers WHERE CustomerID = 'ALFKA' SELECT @var1 AS 'Company Name'

10 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())) + '.'

11 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 ] )

12 Transact-SQL DECLARE @myval decimal (5, 2) SET @myval = 193.57 SELECT CAST(CAST(@myval AS varbinary(20)) AS decimal(10,5)) -- Or, using CONVERT SELECT CONVERT(decimal(10,5), CONVERT(varbinary(20), @myval))

13 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

14 Transact-SQL SET NOCOUNT OFF GO USE pubs GO SET NOCOUNT ON GO DECLARE @msg varchar(255) IF (SELECT COUNT(price) FROM titles WHERE title_id LIKE 'BU%' AND price 0 BEGIN SET @msg = 'There are several books that are a good value at under $20. These books are: ' PRINT @msg SET NOCOUNT OFF SELECT title FROM titles WHERE price < 20 END ELSE BEGIN SET @msg = 'There are no books under $20. ' PRINT @msg SELECT title FROM titles WHERE title_id LIKE 'BU%' AND PRICE <10 END

15 Transact-SQL IF...ELSE IF Boolean_expression { sql_statement | statement_block } [ ELSE { sql_statement | statement_block } ]

16 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.'

17 Transact-SQL WHILE Boolean_expression { sql_statement | statement_block } [ BREAK ] { sql_statement | statement_block } [ CONTINUE ]

18 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'

19 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 }

20 Transact-SQL SET NOCOUNT ON DECLARE @message varchar(80), @p_id char(5), @Sp_qty int, @maxqty int DECLARE shipment_cursor CURSOR FOR SELECT p#, sum(qty) FROMsp GROUP BYp# OPEN shipment_cursor FETCH NEXT FROM shipment_cursor INTO @p_id, @sp_qty Set @maxqty=@sp_qty IF @@FETCH_STATUS <> 0 PRINT ' >' WHILE @@FETCH_STATUS = 0 BEGIN FETCH NEXT FROM shipment_cursor INTO @p_id, @sp_qty IF @maxqty <@sp_qty Set @maxqty=@sp_qty END SELECT @message = ' ' + @p_id +' '+ cast (@sp_qty as char(10)) Print @message CLOSE shipment_cursor DEALLOCATE shipment_cursor GO


Download ppt "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."

Similar presentations


Ads by Google