国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

SQL一個存儲過程調用另一個存儲過程 獲得返回值問題

2024-07-21 02:44:52
字體:
來源:轉載
供稿:網友
第一種方法: 使用output參數

USE AdventureWorks;
GO
IF OBJECT_ID ( 'PRoduction.usp_GetList', 'P' ) IS NOT NULL
    DROP PROCEDURE Production.usp_GetList;
GO
CREATE PROCEDURE Production.usp_GetList @product varchar(40)
    , @maxprice money
    , @compareprice money OUTPUT
    , @listprice money OUT
AS
    SELECT p.name AS Product, p.ListPrice AS 'List Price'
    FROM Production.Product p
    JOIN Production.ProductSubcategory s
      ON p.ProductSubcategoryID = s.ProductSubcategoryID
    WHERE s.name LIKE @product AND p.ListPrice < @maxprice;
-- Populate the output variable @listprice.
SET @listprice = (SELECT MAX(p.ListPrice)
        FROM Production.Product p
        JOIN  Production.ProductSubcategory s
          ON p.ProductSubcategoryID = s.ProductSubcategoryID
        WHERE s.name LIKE @product AND p.ListPrice < @maxprice);
-- Populate the output variable @compareprice.
SET @compareprice = @maxprice;
GO


另一個存儲過程調用的時候:

Create Proc Test
as
DECLARE @compareprice money, @cost money
EXECUTE Production.usp_GetList '%Bikes%', 700,
    @compareprice OUT,
    @cost OUTPUT
IF @cost <= @compareprice
BEGIN
    PRINT 'These products can be purchased for less than
    $'+RTRIM(CAST(@compareprice AS varchar(20)))+'.'
END
ELSE
    PRINT 'The prices for all products in this category exceed
    $'+ RTRIM(CAST(@compareprice AS varchar(20)))+'.'
第二種方法:創建一個臨時表

create proc GetUserName
as
begin
    select 'UserName'
end

Create table #tempTable (userName nvarchar(50))
insert into #tempTable(userName)
exec GetUserName

select #tempTable

--用完之后要把臨時表清空
drop table #tempTable--需要注意的是,這種方法不能嵌套。例如:

  procedure   a   
  begin   
      ...   
      insert   #table   exec   b   
  end   
    
  procedure   b   
  begin   
      ...   
      insert   #table    exec   c   
      select   *   from   #table     
  end   
    
  procedure   c   
  begin   
      ...   
      select   *   from   sometable   
  end  

--這里a調b的結果集,而b中也有這樣的應用b調了c的結果集,這是不允許的,
--會報“INSERT EXEC 語句不能嵌套”錯誤。在實際應用中要避免這類應用的發生。


第三種方法:聲明一個變量,用exec(@sql)執行:

1);EXEC 執行SQL語句

declare @rsql varchar(250)
        declare @csql varchar(300)
        declare @rc nvarchar(500)
        declare @cstucount int
        declare @ccount int
        set @rsql='(select Classroom_id from EA_RoomTime where zc='+@zc+' and xq='+@xq+' and T'+@time+'=''否'') and ClassroomType=''1'''
        --exec(@rsql)
        set @csql='select @a=sum(teststucount),@b=sum(classcount) from EA_ClassRoom where classroom_id in '
        set @rc=@csql+@rsql
        exec sp_executesql @rc,N'@a int output,@b int output',@cstucount output,@ccount output--將exec的結果放入變量中的做法
        --select @csql+@rsql
        --select @cstucount
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 卓尼县| 翼城县| 通州市| 喀什市| 鲁甸县| 昭苏县| 棋牌| 木兰县| 旅游| 瑞昌市| 松阳县| 贵溪市| 石家庄市| 松溪县| 黄梅县| 依兰县| 五河县| 蒙山县| 河曲县| 左权县| 固镇县| 泌阳县| 如皋市| 辉南县| 福鼎市| 富锦市| 西乡县| 大竹县| 利辛县| 阿拉善右旗| 黄平县| 民权县| 汶上县| 同心县| 朝阳县| 古田县| 建始县| 克什克腾旗| 巨鹿县| 开江县| 信阳市|