Home  >  Article  >  Database  >  SQL 存储过程调用存储过程

SQL 存储过程调用存储过程

WBOY
WBOYOriginal
2016-06-07 17:37:151457browse

研究一个别人做的项目代码看到数据库里有一段存储过程调用存储过程的代码,原来的代码比较复杂。 于是自己打算写一个简单的例子学习一下。 一、首先创建了被需要被调用的存储过程。 USE [MSPetShop4] //使用的PetShop的现成数据库 GO ALTER PROCEDURE [dbo].

研究一个别人做的项目代码看到数据库里有一段存储过程调用存储过程的代码,,原来的代码比较复杂。 于是自己打算写一个简单的例子学习一下。

一、首先创建了被需要被调用的存储过程。

USE [MSPetShop4]  //使用的PetShop的现成数据库
GO

ALTER PROCEDURE [dbo].[uspGetCategoryID]
@Name varchar(20),
@CateID varchar(20) output  //输出变量加输出标记output
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SELECT @CateID = [CategoryId]  //输出变量赋值

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

 

二、调用存储过程

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)  //用来存储输出结果的变量
exec dbo.uspGetCategoryID @Name=@catname , @CateID=@CategoryID output //原来的代码是两个一样名字的变量 我换了下不一样的名字  发现是  被调用的存储过程结果变量 =  需要接收结果存储变量名 和我想的不一样

select @CategoryID

 

insert into Product values(@prodid,@CategoryID,@ProdName,'','')
end

 

 

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn