Download presentation
Presentation is loading. Please wait.
Published byTheodore Norris Modified over 9 years ago
1
1 SQL/PSM Procedures Stored in the Database General-Purpose Programming
2
2 Stored Procedures uPSM, or “persistent, stored modules,” allows us to store procedures as database schema elements. uPSM = a mixture of conventional statements (if, while, etc.) and SQL. uLets us do things we cannot do in SQL alone.
3
3 Functionality Stored procedures can be used to u Return information to the caller u Modify data in databases u Implement business logic in data tier u Control access to data u Improve performance of the system u Reduce network traffic u Perform other actions and operations (such as process e-mail, execute operating usystem commands and processes, and manage other SQL server objects)
4
4 Basic PSM Form CREATE PROC[EDURE] procedure_name [ {@ parameter data_type} [= default] [OUTPUT] ] [,... n] AS sql_statement [... n]
5
5 Example Create Procedure prGetBar @barName varchar(50) as Select * from Bar where name = @barName Bars(name, addr, license)
6
6 Execute the Stored Procedure [ [ EXEC [ UTE ] ] { [ @return_status = ] { procedure_name [ ;number ] | @procedure_name_var } [ [ @parameter = ] { value | @variable [ OUTPUT ] | [ DEFAULT ] ] [,...n ] [ WITH RECOMPILE ] uExample - Execute prGetBar @barName=‘Joe’ - prGetBar @barName=‘Joe’ - prGetBar ‘Joe’
7
7 Receive Information from a Stored Procedure uResult set uOutput parameters uReturn value uGlobal cursor
8
8 Returning Result Sets uTo obtain a result set from a stored procedure, insert a Transact-SQL statement that returns a result set into the body of the stored procedure. The simplest way is by using a Select statement, but you could also call another stored procedure.
9
9 Input and Output Parameters uThe header of the stored procedure contains a list of parameters delimited with a comma (,) character. Each parameter is defined with an identifier and a data type. Parameter identifiers must begin with the at sign (@). uIf the parameter with the keyword Output after the data type, it can return a value to the caller.
10
10 Returning Values with Output Parameters CREATE PROCEDURE dbo.mathtutor @m1 smallint, @m2 smallint, @result smallint OUTPUT AS SET @result = @m1 * @m2 DECLARE @answer smallint EXECUTE mathtutor 5, 6, @answer OUTPUT SELECT 'The result is: ', @answer The result is: 30 CREATE PROCEDURE dbo.mathtutor @m1 smallint, @m2 smallint, @result smallint OUTPUT AS SET @result = @m1 * @m2 DECLARE @answer smallint EXECUTE mathtutor 5, 6, @answer OUTPUT SELECT 'The result is: ', @answer The result is: 30 Creating Stored Procedure Procedure Executing Stored Procedure Procedure Results of Stored Procedure Procedure
11
11 Another Example Create Procedure prGetBeerPrice @bar varchar(50), @beer varchar(50),@price money output as Select @price=price from Sells where bar = @bar and beer=@beer uExecute the stored procedure - Declare @price money - Execute prGetBeerPrice @bar=‘Joe’,@beer=’Bud’,@price Output - Select @price Sells(bar, beer, price)
12
12 Return Value uEach stored procedure can end with a Return statement. The statement can be followed by an integer value that can be read by the caller. If the return value is not explicitly set, the server will return the default value—zero (0). uBecause return values are limited to integer data types, they are most often used to signal an status or error code to the caller.
13
13 Example Create Procedure prGetBeerPrice @bar varchar(50), @beer varchar(50),@price money output as Select @price=price from Sells where bar = @bar and beer=@beer Return @@error uExecute the stored procedure - Declare @price money - Declare @errorCode - Execute @errorCode =prGetBeerPrice @bar=‘Joe’,@beer=’Bud’,@price Output - Select @price result, @errorCode ErrorCode
14
14 Default Values Default values are defined at the end of a parameter definition, behind the data types. All that is needed is an assignment (=) and a value.
15
15 Example if exists (select * from sysobjects where id = object_id(' prGetBar ') and OBJECTPROPERTY(id, 'IsProcedure') = 1) drop procedure prGetBar GO Create Procedure prGetBar @barName varchar(50) = ‘Joe’ as Select * from Bar where name = @barName uExecute prGetBar @barName=‘Smith’ uExecute prGetBar
16
16 Renaming Stored Procedures sp_rename [ @objname = ] 'object_name', [ @newname = ] 'new_name' [, [ @objtype = ] 'object_type' ] uExample Sp_rename prGetBar getBar
17
17 Altering Stored Procedures Alter Procedure prGetBar @barName varchar(50) = ‘Joe’ as Select * from Bar where name = @barName
18
18 Deleting Stored Procedures uDROP PROCEDURE { procedure } [,...n ] uExample Drop Procedure getBar
19
19 T-SQL Identifiers The following are the rules for creating identifiers: u Identifiers in SQL Server 2000 may have between 1 and 128 characters. There are exceptions to this rule: certain objects are limited (for instance, temporary tables can have identifiers up to only 116 characters long). uThe first character of the identifier must be a letter, underscore ( _ ), at sign(@), or number sign (#). The first letter must be defined in the Unicode 2.0 standard. Among other letters, Latin letters a–z and A–Z can be used as a first character. Some characters (@ and #) have special meanings in T-SQL. They act as signals to SQL Server to treat their carriers differently. u Subsequent characters must be letters from the Unicode 2.0 standard, or decimal digits, or one of the special characters @, #, _, or $. u SQL Server reserved words should not be used as object identifiers. u Identifiers cannot contain spaces or other special characters except for @, #, _, or $.
20
20 T_SQL Data Type(1) Data TypeDesc.Max Length charfixed size8000 characters varcharVariable size8000 characters textVariable size2^31 - 1 ncharUnicode, fixed size4000 characters nvarcharUnicode, Variable size4000 characters ntextUnicode, Variable size2^30 - 1
21
21 T_SQL Data Type(2) Data TypeStorage SizeMinimumMaximum int4 bytes-2^312^31– 1 smallint2 bytes-2^152^15– 1 tinyint1 byte0255 bigint8 bytes-2^632^63– 1 bit1 bit01
22
22 T_SQL Data Type(3) Data TypeStorage Size binary<8000 bytes varbinary<8000 bytes image<2G datetime smalldatetime uniqueidentifierGUID,NEWID()
23
23 Variables uLocal variable - begin with an @ prefix. uGlobal variable - begin with an @@ prefix.
24
24 Declaring Variables u They must begin with @: - Declare @LastName varchar(50) u It is possible to define several variables in a single Declare statement. You just need to separate them with commas: - Declare @LastName varchar(50), - @FirstName varchar(30), - @BirthDate smalldatetime u You can also define variables based on user-defined data types: - Declare @OfficePhone phone
25
25 Assigning Values with the Select Statement SELECT @name=name,@manf=manf FROM Beers WHERE manf = ’Anheuser-Busch’; SELECT @name name,@manf manufacture;
26
26 Assigning Values with the Set Statement Set @name=‘ Bud ’,@manf=‘ Anheuser-Busch’
27
27 Displaying the Values of Variables uSelect @LastName uPrint @FirstName
28
28 Global Variables u@@error u@@rowcount select Make, Model, EqTypeid into OldEquipment from Equipment where EqTypeid = 2 if @@rowcount = 0 Print "No rows were copied!"
29
29 Flow-Control Statements u Comments u Statement block u If…Else u While…Break u Break u Continue u GoTo u WaitFor u Begin…End
30
30 Comments uSingle-Line Comments -- - A complete line or part of the line canbe marked as a comment if the user places two hyphens (--) at the beginning. uMultiline Comments: /* … */
31
31 Statement Blocks: Begin…End Begin Insert Order(OrderDate, RequestedById, TargetDate, DestinationLocation) Values(@OrderDate, @ContactId, @TargetDate, @LocId) Select @ErrorCode = @@Error, @OrderId = @@Identity if @ErrorCode <> 0 begin RaiseError('Error occurred while inserting Order!', 16,1) Return @@ErrorCode end End
32
32 Conditional Execution: The If Statement uSyntax 1 If boolean_expression {Transact-SQL_statement | statement_block} [else {Transact-SQL_statement | statement_block}]
33
33 Example Create Procedure prInsertUpdateSells -- store values in Sells table. ( @bar varchar(50), @beer varchar(50), @price money ) As declare @bn varchar(50) Select @bn = beer From sells Where bar = @bar and beer=@beer Sells(bar, beer, price)
34
34 Example(Cont.) -- does such bn already exists in the database If @bn IS NOT NULL --insert a new record Insert sells (bar, beer, price) Values (@bar, @beer, @ price ) Else --if it does not exist Begin -- update the beer’s price Update sells Set price=@price Where bar=@bar and beer=@beer End
35
35 Conditional Execution: The If Statement uSyntax 2 If [NOT] Exists(subquery) {Transact-SQL_statement | statement_block} [else {Transact-SQL_statement | statement_block}]
36
36 Example Create Procedure prInsertUpdateSells_2 @bar varchar(50),@beer varchar(50),@price money As If not exists(Select @bn = beer From sells Where bar = @bar and beer=@beer) --insert a new record Insert sells (bar, beer, price) Values (@bar, @beer, @ price) Else --if it does not exist Begin -- update the beer’s price Update sells Set price=@price Where bar=@bar and beer=@beer End
37
37 Looping: The While Statement While Boolean_expression {sql_statement | statement_block} [Break] {sql_statement | statement_block} [Continue]
38
38 Example Create Procedure prCalcFactorial -- calculate factorial -- 1! = 1 -- 3! = 3 * 2 * 1 -- n! = n * (n-1)*... 5 * 4 * 3 * 2 * 1 @inyN tinyint, @intFactorial int OUTPUT As Set @intFactorial = 1 while @inyN > 1 begin set @intFactorial = @intFactorial * @inyN Set @inyN = @inyN - 1 end return 0
39
39 System Stored Procedure uSP_Databases uSP_Tables uSP_Addlogin uSP_GrantAccess uSP_Password u SP_Attach_DB uSP_Detach_DB uSp_Rename uXP_cmdshell uXp_Fileexist uXp Fixeddrives
40
40 Cursors uA cursor is essentially a tuple-variable that ranges over all tuples in the result of some query. uDeclare a cursor c by: DECLARE c CURSOR FOR ;
41
41 Opening and Closing Cursors uTo use cursor c, we must issue the command: OPEN c; - The query of c is evaluated, and c is set to point to the first tuple of the result. uWhen finished with c, issue command: CLOSE c;
42
42 Fetching Tuples From a Cursor uTo get the next tuple from cursor c, issue command: FETCH FROM c INTO x1, x2,…,xn ; uThe x ’s are a list of variables, one for each component of the tuples referred to by c. uc is moved automatically to the next tuple.
43
43 Breaking Cursor Loops --- (1) uThe usual way to use a cursor is to create a loop with a FETCH statement, and do something with each tuple fetched. uA tricky point is how we get out of the loop when the cursor has no more tuples to deliver.
44
44 Breaking Cursor Loops --- (2) uEach SQL operation returns a status, which is a 5-digit number. - For example, 00000 = “Everything OK,” and 02000 = “Failed to find a tuple.” uIn PSM, we can get the value of the status in a variable called SQLSTATE.
45
45 Breaking Cursor Loops --- (3) uWe may declare a condition, which is a boolean variable that is true if and only if SQLSTATE has a particular value. Example: We can declare condition NotFound to represent 02000 by: DECLARE NotFound CONDITION FOR SQLSTATE ’02000’;
46
46 Breaking Cursor Loops --- (4) uThe structure of a cursor loop is thus: cursorLoop: LOOP … FETCH c INTO … ; IF NotFound THEN LEAVE cursorLoop; END IF; … END LOOP;
47
47 Example: Cursor uLet’s write a procedure that examines Sells(bar, beer, price), and raises by $1 the price of all beers at Joe’s Bar that are under $3. - Yes, we could write this as a simple UPDATE, but the details are instructive anyway.
48
48 The Needed Declarations CREATE PROCEDURE JoeGouge( ) DECLARE theBeer CHAR(20); DECLARE thePrice REAL; DECLARE NotFound CONDITION FOR SQLSTATE ’02000’; DECLARE c CURSOR FOR (SELECT beer, price FROM Sells WHERE bar = ’Joe’’s Bar’); Used to hold beer-price pairs when fetching through cursor c Returns Joe’s menu
49
49 The Procedure Body BEGIN OPEN c; menuLoop: LOOP FETCH c INTO theBeer, thePrice; IF NotFound THEN LEAVE menuLoop END IF; IF thePrice < 3.00 THEN UPDATE Sells SET price = thePrice + 1.00 WHERE bar = ’Joe’’s Bar’ AND beer = theBeer; END IF; END LOOP; CLOSE c; END; Check if the recent FETCH failed to get a tuple If Joe charges less than $3 for the beer, raise it’s price at Joe’s Bar by $1.
50
50 uFunction alternative: CREATE FUNCTION ( ) RETURNS
51
51 example The example could be the export of a group of tables from a database to text files using bcp. Create Procedure prBcpOutTables --loop through tables and export them to text fields @debug int = 0 As Declare @chvTable varchar(128), @chvCommand varchar(255) Declare @curTables Cursor Set @curTables = Cursor FOR select name from sysobjects where xType = 'U'
52
52 Open @curTables -- get first table Fetch Next From @curTables Into @chvTable -- if we successfully read the current record While (@@fetch_status = 0) Begin -- assemble DOS command for exporting table Set @chvCommand = 'bcp "Asset..[' + @chvTable + ']" out C:\sql7\backup\' + @chvTable + '.txt -c -q -Sdejan -Usa -Pdejan' -- during test just display command If @debug <> 0 Select @chvCommand chvCommand -- in production execute DOS command and export table If @debug = 0 Execute xp_cmdshell @chvCommand, NO_OUTPUT Fetch Next From @curTables Into @chvTable End Close @curTables Deallocate @curTables Return 0
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.