Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with variable output of sp_executesql

Status
Not open for further replies.

cedwards65

Programmer
Mar 23, 2011
1
US
I am building dynamic SQL and want to execute that using the sp_executesql. When I execute the @sql_statement from the print statement in sqlserver I get a result, but when I execute it as below the @customer_id_out variable is still showing as null. Any ideas what I am doing wrong

DECLARE
@sql_statement Nvarchar(4000),
@customer_id_found int

SELECT @customer_id_found = null
select @sql_statement = @Sql_active + @product_list
print @sql_statement
exec sp_executesql @sql_statement,
N'@CID int out',
@customer_id_found out

IF ISNULL(@customer_id_found,'') <> ''
RETURN -1
 
Code:
DECLARE @sql_statement Nvarchar(4000),
        @customer_id_found    int

SELECT @customer_id_found = null

select @sql_statement = @Sql_active + @product_list

print @sql_statement
exec sp_executesql @sql_statement,
     N'@CID int OUTPUT', -- Here
     @CID = @customer_id_found OUTPUT  -- And here
                   
IF ISNULL(@customer_id_found,'') <> ''
    RETURN -1

Borislav Borissov
VFP9 SP2, SQL Server 2000,2005 & 2008.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top