Forum Discussion

amaaiia's avatar
amaaiia
Skilled Sharer
2 years ago
Solved

Parameters for table name in stored procedure

I want to have a stored procedure to call it from Lookup activity in several Data Pipelines. I want the procedure to be as follows: I want the "from" table to be ingested with parameters. I've...
  • govindarajan_d's avatar
    2 years ago

    Hi amaaiia ,

     

    Declare the parameters as NVARCHAR. It will show an error but still it will work. After that use SET to form the string you want to execute and then use 'sp_executesql' with the string you formed. Make sure you use N in front of the strings.

     

    In the below example, I have formed two strings one with Table name and the other with select query using the table name. 

    DECLARE @MergeQuery NVARCHAR(MAX), @TableName NVARCHAR(MAX);
    SET @TableName = N'dbo.Product_Info';
    SET @MergeQuery = N'SELECT * FROM ' + @TableName;
    
    EXEC sp_executesql @MergeQuery;