SQL stored procedure call Stored Procedure

Source: Internet
Author: User
Studying a project code made by someone else, we can see that there is a piece of stored procedure calling code in the database. The original code is complicated. So I plan to write a simple example to learn it. 1. Create a stored procedure to be called. USE [MSPetShop4] The PetShop's ready-made database GOALTERPROCEDURE [dbo].

Studying a project code made by someone else, we can see that there is a piece of stored procedure calling code in the database. The original code is complicated. So I plan to write a simple example to learn it. 1. Create a stored procedure to be called. USE [MSPetShop4] // USE the PetShop's ready-made database go alter procedure [dbo].

Studying a project code made by someone else, we can see that there is a piece of stored procedure calling code in the database. The original code is complicated. So I plan to write a simple example to learn it.

1. Create a stored procedure to be called.

USE [MSPetShop4] // a ready-to-USE PetShop Database
GO

Alter procedure [dbo]. [uspGetCategoryID]
@ Name varchar (20 ),
@ CateID varchar (20) output // Add output variable and output tag
AS
BEGIN
-- Set nocount on added to prevent extra result sets from
-- Interfering with SELECT statements.
SELECT @ CateID = [CategoryId] // assign values to output variables

FROM [MSPetShop4]. [dbo]. [Category] where Name = @ Name
END

Ii. Call the Stored Procedure

USE [MSPetShop4]
GO

ALTER procedure [dbo]. [saveProduct]
(
@ Prodid char (20 ),
@ Catname char (20 ),
@ ProdName char (20)
)
As
Begin
Set nocount on
Declare @ CategoryID varchar (20) // variable used to store the output result
Exec dbo. uspGetCategoryID @ Name = @ catname, @ CateID = @ CategoryID output // the original code is two variables with the same name. I changed the name to a different one and found that the called stored procedure result variable = the variable name to be received different from what I think

Select @ CategoryID

Insert into Product values (@ prodid, @ CategoryID, @ ProdName ,'','')
End

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.