1--Stored Procedure Learning Chapter2 3--1simple stored procedures with no parameters for learning4IF object_id ('orders_getallorders','P') is not NULL5 DROP PROCEDURE orders_getallorders;6 GO7 CREATE PROC orders_getallorders8 as9 SET NOCOUNT on;TenSELECT *From dbo. Orders One GO A ---2. Simple stored procedure with parameters -IF object_id ('Orders_getallbyname','P') is not NULL the DROP PROCEDURE orders_getallbyname; - GO - CREATE PROCEDURE orders_getallbyname -@ShipName NVARCHAR ( +) + as -SELECT *From dbo. Orders +WHERE shipname=@ShipName A GO at ---executing a stored procedure with parameters -EXEC orders_getallbyname @ShipName =n'Hanari Carnes' - - ---3. Using a simple stored procedure with wildcard parameters in -IF object_id ('Employees_getallbyname','P') is not NULL to DROP PROCEDURE employees_getallbyname; + GO - CREATE PROCEDURE employees_getallbyname the@FirstName NVARCHAR (Ten) =n'%', *@LastName NVARCHAR ( -) =n'D%' $ asPanax NotoginsengSELECT *From dbo. Employees - WHERE FirstName like @FirstName and LastName like @LastName; the +--Executing stored procedures AEXECUTE dbo. Employees_getallbyname @FirstName = N'Nancy',--nvarchar (Ten) the@LastName = N'Davolio'--nvarchar ( -) + - $--4. Returning multiple result sets $IF object_id ('Getmanyresultscount','P') is not NULL - DROP PROCEDURE getmanyresultscount; - GO the CREATE PROCEDURE Getmanyresultscount - asWuyiSELECT COUNT (*) from dbo. Orders; theSELECT COUNT (*) from dbo. Employees; - GO Wu ---Executing stored procedures About EXEC Getmanyresultscount; $ ---stored procedures that use the OUTPUT parameter -IF object_id ('getmanyproducts','P') is not NULL - DROP PROCEDURE getmanyproducts; A DOC + CREATE PROCEDURE getmanyproducts the@ProductName NVARCHAR ( +), - @MaxPrice Money, $ @ComparePrice Money OUTPUT, the @UnitPrice Money OUTPUT the as the SELECT p.productname,p.unitprice from dbo. Products as P theINNER JOIN dbo. Categories as C on C.categoryid =P.categoryid -WHERE p.productname like @ProductName and p.unitprice<@MaxPrice in the--Setting output Parameters theSET @UnitPrice =( About SELECT MAX (p.unitprice) from dbo. Products as P theJOIN dbo. Categories as C on C.categoryid =P.categoryid theWHERE p.productname like @ProductName and p.unitprice<@MaxPrice the ) +SET @[email protected];
SQL Server stored procedure review (i)