1

I am trying to output a value from a select statement, but not sure how to complete this. I call the procedure passing example values 'mypackage' (package_id) and 117 (total_cost). I need it to output the total_cost parameter as a 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 
1
  • 1
    which rdbms are you using? Commented Jan 6, 2015 at 13:56

2 Answers 2

1

For 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 
Sign up to request clarification or add additional context in comments.

2 Comments

This will throw error you need to use Nvarchar when sp_executesql is used to execute dynamic sql
I don't know what is that OP's requirement. But if you are using sp_executesql then the variable should be declared as Nvarchar else you will get error
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 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.