Note that there are some explanatory texts on larger screens.

plurals
  1. POStored Procedure Dynamic SQL stored result in variable
    primarykey
    data
    text
    <p>I have A stored procedure written</p> <pre><code>DECLARE @AreaID AS INT DECLARE @DayPrior AS INT DECLARE @TableName AS VARCHAR(50) DECLARE @StoreQuery AS NVARCHAR(MAX) DECLARE @SQL_ExtractDlSql AS NVARCHAR(MAX) DECLARE @ParameterDefinition AS NVARCHAR(2000) SET @AreaID = 1 SET @DayPrior = 1 SET @TableName = 'Tbl_Lube' SET @SQL_ExtractDlSql = 'SELECT Download_SQL From HDDDataPackage.dbo.tbl_HDD_DataDownloadSetting Where AreaId=@AreaID AND TableName=@TableName' SET @ParameterDefinition = '@AreaID INT,@DayPrior INT,@TableName VARCHAR(50)' EXECUTE sp_executesql @SQL_ExtractDlSql, @ParameterDefinition, @AreaID, @DayPrior, @TableName PRINT @SQL_ExtractDlSql </code></pre> <p>I above Stored procedure, I get What I wanted on print, but instead of doing that, I wanted the selected data stores as a variable. Then what I done is I declared a variable @StoreSql and made some modified into the SQL statement show below:</p> <p>Variable Declaration:</p> <pre><code>DECLARE @StoreSql AS NVARCHAR(MAX) </code></pre> <p>Modified the Code:</p> <pre><code>SET @SQL_ExtractDlSql = 'SELECT @StoreSql = Download_SQL From HDDDataPackage.dbo.tbl_HDD_DataDownloadSetting Where AreaId=@AreaID AND TableName=@TableName' </code></pre> <p>I executed the code, I get error <strong>Must declare the scalar variable "@StoreSql".</strong></p> <p>I have totally no idea how to deal with this.Any one can help.Beside,I am new to sqlserver,new to stored procedures.</p> <p>Please don't hesitate to point out my any mistake,any..such as logic or etc. I learn from mistake.</p> <p>Regards LiangCk</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload