• SQL 存储过程调用存储过程


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

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

    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

  • 相关阅读:
    POJ-1330 Nearest Common Ancestors(倍增的LCA)
    POJ-1442 Black Box(手写堆优化)
    POJ-2442 Sequence(手写堆优化)
    BZOJ2506 calc
    BZOJ3396 [Usaco2009 Jan]Total flow 水流
    BZOJ3570 DZY Loves Physics I
    BZOJ1101 [POI2007]Zap
    BZOJ1110 [POI2007]砝码Odw
    BZOJ1555 KD之死
    BZOJ3476 [Usaco2014 Mar]The Lazy Cow
  • 原文地址:https://www.cnblogs.com/dangkei/p/3461595.html
Copyright © 2020-2023  润新知