SQL - 获取输出参数值

时间:2015-01-06 13:54:57

标签: sql select stored-procedures

我正在尝试从select语句输出一个值,但不确定如何完成此操作。 我打电话给程序通过示例值' mypackage' (package_id)和117(total_cost)。我需要它将total_cost参数输出为INT。

ALTER PROCEDURE [aren1002].[ArenDataGetPackageWithRoomCost]
(
    @room_type as int=NULL,
    @package_id as varchar(30)=NULL,
    @total_cost as INT OUTPUT,
    @sql as varchar(400)
)
AS
SET NOCOUNT ON

BEGIN
SELECT @total_cost AS
    string @sql = 'SELECT ' + @package_id + ' from aren1002.HOLIDAY_ROOM where room_standard_id = ' + @room_type
    exec @sql
END 

2 个答案:

答案 0 :(得分:1)

对于 SQL SERVER

ALTER PROCEDURE [aren1002].[ArenDataGetPackageWithRoomCost]
(
    @room_type AS INT=NULL,
    @package_id AS VARCHAR(30)=NULL,
    @total_cost AS INT OUTPUT,
    @sql AS VARCHAR(400)
)
AS
SET NOCOUNT ON

BEGIN
    DECLARE @sql NVARCHAR(MAX)
    SET @sql = 'SELECT @total_cost = ' + @package_id + ' from aren1002.HOLIDAY_ROOM where room_standard_id = ' + @room_type
    sp_executesql @sql, '@total_cost AS INT OUTPUT', @total_cost AS INT OUTPUT
END 

答案 1 :(得分:0)

ALTER PROCEDURE [aren1002].[ArenDataGetPackageWithRoomCost]
(
    @room_type as int=NULL,
    @package_id as varchar(30)=NULL,
    @total_cost as INT OUTPUT,
    @sql as varchar(400)
)
AS
SET NOCOUNT ON

BEGIN
    DECLARE @sql NVARCHAR(MAX)
    select @sql = 'SELECT @total_cost=' + @package_id + ' from aren1002.HOLIDAY_ROOM 
      where room_standard_id = ' + @room_type
    exec sp_executesql @sql,N'@total_cost INT Output',@total_cost output
END