Stored procedures are a set of SQL statements (one or more) typically grouped together to perform...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
Stored procedures are a set of SQL statements (one or more) typically grouped together to perform a specific routine. Stored procedures can be created in any user-defined database and system database except the resource database. Some of the benefits of using stored procedures are as follows: • They offer improved performance because of compiled code. They are easy to maintain because changes are central instead of inline with code. • Since database operations can be performed inside the stored procedures, they provide a strong level of security. Instead of access being granted to the underlying object, permission can be granted only to the stored procedure. Essentially, stored procedures create a level of abstraction for permissions-instead of the user being granted SELECT, INSERT, UPDATE, or DELETE rights, the user can be granted EXECUTE rights to a stored procedure. The basic syntax for creating a stored procedure is follows: CREATE PROC PROCEDURE) [schema nane.] procedure name [number] [(@parameter [type_schena_nane. J data type) [ VARYING][default ] [ OUT | OUTPUT] [READONLY] ] [...] [WITH <procedure option [....]] [FOR REPLICATION] AS ( [BEGIN] sql statement [] [] [END] ) [+] <procedure option> [ENCRYPTION] [RECOMPILE ] EXECUTE AS Clause ] Step 1 Let's create a simple store procedure that returns the Purchase OrderID, Purchase OrderDetailID, OrderDate, TotalDue, ReceivedQty and ProductName information of the products sold. CREATE PROCEDURE sp PurchaseOrderInformation AS BEGIN END SELECT poh. PurchaseOrder ID, pod.PurchaseOrderDetailID, poh.OrderDate, poh. TotalDue, pod. ReceivedQty, p.Nane ProductName FROM Purchasing. PurchaseOrderHeader poh INNER 301N Purchasing. PurchaseOrderDetail pod ON poh. Purchase order 10- pod. Purchase order ID INNER JOIN Production Product p ON pod ProductID p.productID Step 2 - To execute a stored procedure using T-SQL, you use the EXECUTE or EXEC keyword. exec sp Purchase Order Information; 1234 4 5 6 7 8 9 10 1 3 Results 4 PurchaseOrderID PurchaseOrderDetailD 1 6 2 7 2 3 4 5 6 7 7 EXEC sp PurchaseOrder Information WITH RESULT SETS Step 3 - To change the result set, you issue the EXECUTE command as normal, but you add a WITH RESULT SETS statement, and within the parentheses you provide a new column definition for each column in the result set. Messages ** Execute the query and review the results. Results | Messages Purchase Order ID Purchase Order Detail ID Order Date 1 2 3 4 1 2 4 5 6 8 7 9 7 10 7 5 6 7 8 2 3 9 10 [Purchase Order ID] int, [Purchase Order Detail ID] int, [Order Date] datetime, [Total Due] Money, [Received Quantity] float, [Product Name] varchar (50) 1 2 Order Date ReceivedOty 3.00 Total Due 2011-04-16 00:00:00.000 222,1492 2011-04-16 00:00:00.000 300.6721 3.00 2011-04-16 00:00:00.000 300.6721 3.00 2011-04-16 00:00:00.000 9776.2665 550.00 2011-04-16 00:00:00.000 189.0395 2.00 2011-04-30 00:00:00.000 22539.0165 550.00 2011-04-30 00:00:00.000 16164.0229 468.00 2011-04-30 00:00:00.000 64847.5328 550.00 2011-04-30 00:00:00.000 64847.5328 550.00 2011-04-30 00:00:00.000 64847,5328 550.00 3 4 5 6 7 8 9 10 Product Name Adjustable Race Thin-Jam Hex Nut 9 Thin-Jam Hex Nut 10 Seat Post Headset Ball Bearings HL Road Rim Touring Rim LL Crankam ML Crankarm HL Crankam Total Due 2011-04-16 00:00:00.000 222,1492 3 2011-04-16 00:00:00.000 300.6721 3 2011-04-16 00:00:00.000 300,6721 3 2011-04-16 00:00:00.000 9776.2665 550 2011-04-16 00:00:00.000 189.0395 2 2011-04-30 00:00:00.000 22539.0165 550 2011-04-30 00:00:00.000 16164.0229 468 2011-04-30 00:00:00.000 64847.5328 550 2011-04-30 00:00:00.000 64847,5328 550 2011-04-30 00:00:00.000 64847.5328 550 Received Quantity Product Name Adjustable Race Thin-Jam Hex Nut 9 Thin-Jam Hex Nut 10 Seat Post Headset Ball Bearings HL Road Rim Touring Rim LL Crankam ML Crankam HL Crankam ** In the preceding query, the EXEC keyword is used for a standard stored procedure execution However, the columns and data types have been changed to make the column names user-friendly. Step 4 - To create an output parameter for a stored procedure, you use the following syntax: parameter_name data_type OUTPUT For example, the following stored procedure finds products by model year and returns to number of products via the @product_count output parameter: CREATE PROCEDURE uspFindProduct Byflodel C AS BEGIN END 4 5 6 GEDRESSBL 9 10 ** Execute the query and review the results. DECLARE @count INT; EXEC uspFindProduct By Model @model year 2014, @product_count - @count OUTPUT; SELECT @count AS "Number of products found"; 12 @model year SMALLINT, @product_count INT OUTPUT 13 14 SELECT Name ListPrice Production Product 1 FROM WHERE 11 Charming YEAR (ModifiedDate) - @model year; SELECT @product_count - @ROUCOUNT; Resut Messages Lice Headset Ball Bearings Blade LL Crank ML Crankam HL Crankam Charing Bots Chairving Nut Crown Race Chain Stays Decal 1 15 Decal 2 Number of products found 504 Stored procedures are a set of SQL statements (one or more) typically grouped together to perform a specific routine. Stored procedures can be created in any user-defined database and system database except the resource database. Some of the benefits of using stored procedures are as follows: • They offer improved performance because of compiled code. They are easy to maintain because changes are central instead of inline with code. • Since database operations can be performed inside the stored procedures, they provide a strong level of security. Instead of access being granted to the underlying object, permission can be granted only to the stored procedure. Essentially, stored procedures create a level of abstraction for permissions-instead of the user being granted SELECT, INSERT, UPDATE, or DELETE rights, the user can be granted EXECUTE rights to a stored procedure. The basic syntax for creating a stored procedure is follows: CREATE PROC PROCEDURE) [schema nane.] procedure name [number] [(@parameter [type_schena_nane. J data type) [ VARYING][default ] [ OUT | OUTPUT] [READONLY] ] [...] [WITH <procedure option [....]] [FOR REPLICATION] AS ( [BEGIN] sql statement [] [] [END] ) [+] <procedure option> [ENCRYPTION] [RECOMPILE ] EXECUTE AS Clause ] Step 1 Let's create a simple store procedure that returns the Purchase OrderID, Purchase OrderDetailID, OrderDate, TotalDue, ReceivedQty and ProductName information of the products sold. CREATE PROCEDURE sp PurchaseOrderInformation AS BEGIN END SELECT poh. PurchaseOrder ID, pod.PurchaseOrderDetailID, poh.OrderDate, poh. TotalDue, pod. ReceivedQty, p.Nane ProductName FROM Purchasing. PurchaseOrderHeader poh INNER 301N Purchasing. PurchaseOrderDetail pod ON poh. Purchase order 10- pod. Purchase order ID INNER JOIN Production Product p ON pod ProductID p.productID Step 2 - To execute a stored procedure using T-SQL, you use the EXECUTE or EXEC keyword. exec sp Purchase Order Information; 1234 4 5 6 7 8 9 10 1 3 Results 4 PurchaseOrderID PurchaseOrderDetailD 1 6 2 7 2 3 4 5 6 7 7 EXEC sp PurchaseOrder Information WITH RESULT SETS Step 3 - To change the result set, you issue the EXECUTE command as normal, but you add a WITH RESULT SETS statement, and within the parentheses you provide a new column definition for each column in the result set. Messages ** Execute the query and review the results. Results | Messages Purchase Order ID Purchase Order Detail ID Order Date 1 2 3 4 1 2 4 5 6 8 7 9 7 10 7 5 6 7 8 2 3 9 10 [Purchase Order ID] int, [Purchase Order Detail ID] int, [Order Date] datetime, [Total Due] Money, [Received Quantity] float, [Product Name] varchar (50) 1 2 Order Date ReceivedOty 3.00 Total Due 2011-04-16 00:00:00.000 222,1492 2011-04-16 00:00:00.000 300.6721 3.00 2011-04-16 00:00:00.000 300.6721 3.00 2011-04-16 00:00:00.000 9776.2665 550.00 2011-04-16 00:00:00.000 189.0395 2.00 2011-04-30 00:00:00.000 22539.0165 550.00 2011-04-30 00:00:00.000 16164.0229 468.00 2011-04-30 00:00:00.000 64847.5328 550.00 2011-04-30 00:00:00.000 64847.5328 550.00 2011-04-30 00:00:00.000 64847,5328 550.00 3 4 5 6 7 8 9 10 Product Name Adjustable Race Thin-Jam Hex Nut 9 Thin-Jam Hex Nut 10 Seat Post Headset Ball Bearings HL Road Rim Touring Rim LL Crankam ML Crankarm HL Crankam Total Due 2011-04-16 00:00:00.000 222,1492 3 2011-04-16 00:00:00.000 300.6721 3 2011-04-16 00:00:00.000 300,6721 3 2011-04-16 00:00:00.000 9776.2665 550 2011-04-16 00:00:00.000 189.0395 2 2011-04-30 00:00:00.000 22539.0165 550 2011-04-30 00:00:00.000 16164.0229 468 2011-04-30 00:00:00.000 64847.5328 550 2011-04-30 00:00:00.000 64847,5328 550 2011-04-30 00:00:00.000 64847.5328 550 Received Quantity Product Name Adjustable Race Thin-Jam Hex Nut 9 Thin-Jam Hex Nut 10 Seat Post Headset Ball Bearings HL Road Rim Touring Rim LL Crankam ML Crankam HL Crankam ** In the preceding query, the EXEC keyword is used for a standard stored procedure execution However, the columns and data types have been changed to make the column names user-friendly. Step 4 - To create an output parameter for a stored procedure, you use the following syntax: parameter_name data_type OUTPUT For example, the following stored procedure finds products by model year and returns to number of products via the @product_count output parameter: CREATE PROCEDURE uspFindProduct Byflodel C AS BEGIN END 4 5 6 GEDRESSBL 9 10 ** Execute the query and review the results. DECLARE @count INT; EXEC uspFindProduct By Model @model year 2014, @product_count - @count OUTPUT; SELECT @count AS "Number of products found"; 12 @model year SMALLINT, @product_count INT OUTPUT 13 14 SELECT Name ListPrice Production Product 1 FROM WHERE 11 Charming YEAR (ModifiedDate) - @model year; SELECT @product_count - @ROUCOUNT; Resut Messages Lice Headset Ball Bearings Blade LL Crank ML Crankam HL Crankam Charing Bots Chairving Nut Crown Race Chain Stays Decal 1 15 Decal 2 Number of products found 504
Expert Answer:
Answer rating: 100% (QA)
It seems that the images contain information about SQL stored procedures Stored procedures are colle... View the full answer
Related Book For
Accounting Information Systems
ISBN: 9780132871938
11th Edition
Authors: George H. Bodnar, William S. Hopwood
Posted Date:
Students also viewed these databases questions
-
Solve the linear programming problem by the method of corners. (There may be more than one correct answer.) C=5x+by Minimize subject to 4x + y 42 2x+ y 30 x+3y 2 30 x20, y 20 The minimum is C 52.5...
-
Planning is one of the most important management functions in any business. A front office managers first step in planning should involve determine the departments goals. Planning also includes...
-
Googles ease of use and superior search results have propelled the search engine to its num- ber one status, ousting the early dominance of competitors such as WebCrawler and Infos- eek. Even later...
-
Assume that you have been hired as a consultant by CGT, a major producer of chemicals and plastics, including plastic grocery bags, styrofoam cups, and fertilizers, to estimate the firms weighted...
-
Air at 27C with a free stream velocity of 10 m/s is used to cool electronic devices mounted on a printed circuit board. Each device, 4 mm by 4 mm, dissipates 40 mW, which is removed from the top...
-
When building a successful contract between two people, explain the problems that asymmetric information, agency relationships, and adverse selection can introduce.
-
Donating Blood to Grandma? Can young blood help old brains? Several studies in mice indicate that it might. In the studies, old mice (equivalent to about a 70-year-old person) were randomly assigned...
-
HiTech Manufacturing Company has 1,000,000 shares of $1 par value capital stock outstanding on January 1. The following equity transactions occurred during the current year: Apr. 30 Distributed...
-
Image transcription text YAW Dynamics: Char. Velocity = 32 mph or 14 m/s Estimated Front mu = 0.55 Max Speed 130R = 59 mph Roll Dynamics: SSF = 69in / 56.2in = 1.22 Rear Ride Rate = 320lbs/2/ 0.5in =...
-
Pie Corporation acquired 75 percent of Slice Companys ownership on January 1, 20X8, for $96,000. At that date, the fair value of the noncontrolling interest was $32,000. The book value of Slices net...
-
A manager at a car wash company in North Carolina wants to determine which promotions improve sales. He has information on sales, price discounts, and advertising expenditures on Radio and Newspaper...
-
What are the five steps in preparing a state machine to model a user interface?
-
Based on the following information, how many days of supply of inventory is the firm holding (assume 250 days of operation per year)? Interpret your answer if the industry average inventory days...
-
What is supply chain management? How does it differ from logistics?.
-
Make your own list of what you think the advantages and disadvantages could be of structured, ethnographic and scenario-based approaches.
-
Why should the user interface classes be kept separate from the business classes and application logic?
-
1. A decrease in supply is depicted by a A. movement from point x topoint y . B. shift from S 2 to S 1 . C. shift from S 1 to S 2 . D. movement from point y topoint x . 2. Using the same graph above,...
-
Assessing simultaneous changes in CVP relationships Braun Corporation sells hammocks; variable costs are $75 each, and the hammocks are sold for $125 each. Braun incurs $240,000 of fixed operating...
-
What are the objectives of a fixed- asset or investment accounting system?
-
What is a virus program? Give several examples.
-
You decided to use a newly acquired audit software package in auditing accounts payable. The accounts payable system has been computerized for several years, and the transaction records are recorded...
-
A typical timber wolf has a mass of \(40 \mathrm{~kg}\), a typical jackrabbit a mass of \(2.5 \mathrm{~kg}\). Given the scaling law presented in the passage, we'd expect the specific metabolic rate...
-
A standard gold bar stored at Fort Knox, Kentucky, is 7.00 inches long, 3.63 inches wide, and 1.75 inches tall. Gold has a density of \(19,300 \mathrm{~kg} / \mathrm{m}^{3}\). What is the mass of...
-
A typical timber wolf has a mass of \(40 \mathrm{~kg}\), a typical jackrabbit a mass of \(2.5 \mathrm{~kg}\). Given the scaling law presented in the passage, we'd expect the wolf to use times more...
Study smarter with the SolutionInn App