如何在动态查询中将sp_executesql结果导入变量?

时间:2017-05-21 17:27:47

标签: sql sql-server tsql dynamic sp-executesql

在下面的过程中,第二个<font> F语句中的Select语句返回结果(I)。

但我想将select语句的结果存储在一个变量中。我正在从临时表中删除记录,并使用此变量在临时表中插入结果。但是e_id返回null。

@myVariable

1 个答案:

答案 0 :(得分:1)

将sp_executesql与输出参数

一起使用

您可以参考以下答案来解决此问题

您的问题不需要

sp_executesql

如果目标是将值插入#Tempentities,则无需在变量中存储值,您可以在动态查询中使用临时表:

SET @SQL = 'INSERT INTO #Tempentities(entity_id) SELECT DISTINCT v1.e_id from values v1 inner join listings l ON v1.e_id = l.e_id WHERE l.catalog_id = ' +CAST(@C_id AS nvarchar(20))
SET @SQL += ' AND field_id='+CAST(@fid AS nvarchar(20))+ ' AND(value IN(SELECT val FROM spliting('''+CAST(@val AS VARCHAR(2000))+''','',''))) AND (v1.entity_id IN(SELECT * FROM #Tempentities))' 

整个查询将如下所示:

USE [Test]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[Testing_eid]
    (@TableVar TableVariable READONLY,
     @C_id INT)
AS
    DECLARE @maxPK INT
    DECLARE @pk INT 
    DECLARE @fid SMALLINT
    DECLARE @is_List SMALLINT
    DECLARE @val VARCHAR(MAX)
    DECLARE @field_Type VARCHAR(50)
    DECLARE @Where VARCHAR(MAX)
    DECLARE @SQL NVARCHAR(2000)
    DECLARE @myVariable INT
    DECLARE @SQLSTATUS NVARCHAR(2000)

    --Declare @TempEntities table
    --(
    --entity_id int
    --)
   Set @pk = 1
BEGIN
   BEGIN TRY
    SET NOCOUNT ON;
    create table #Tempentities (e_id int)
     Select @maxPK = count(*) From @TableVar 

     While @pk <= @maxPK
    BEGIN
        SELECT @fid= field_ids FROM @TableVar where id=@pk;
        SELECT @val= value     FROM @TableVar where id=@pk;
        SELECT @field_Type=type,@is_List=is_list FROM FIELD WHERE ID=@fid


            BEGIN
            IF(@is_List = 0 AND @pk=1)
            BEGIN
            Set @SQL = 'SELECT DISTINCT v1.e_id from values v1 inner join listings l ON v1.e_id = l.e_id WHERE l.c_id = ' +CAST(@C_id AS nvarchar(20))
            SET @SQL += ' AND field_id='+CAST(@fid AS nvarchar(20))+ ' and(value IN(SELECT val FROM spliting('''+CAST(@val AS VARCHAR(2000))+''','','')))' 
            INSERT INTO #Tempentities(entity_id) EXECUTE @SQL
            END
            IF(@is_List = 0 AND @pk>1)
            BEGIN
            SELECT @field_type
            SELECT * FROM #Tempentities
            Delete from #Tempentities
            Set @SQL = 'insert into #Tempentities(entity_id) SELECT DISTINCT v1.e_id from values v1 inner join listings l ON v1.e_id = l.e_id WHERE l.catalog_id = ' +CAST(@C_id AS nvarchar(20))
            SET @SQL += ' AND field_id='+CAST(@fid AS nvarchar(20))+ ' AND(value IN(SELECT val FROM spliting('''+CAST(@val AS VARCHAR(2000))+''','',''))) AND (v1.entity_id IN(SELECT * FROM #Tempentities))' 
            --INSERT INTO @TempEntities(entity_id) EXECUTE SP_EXECUTESQL @SQL
             EXECUTE (@SQL)



            END
            END
             SELECT * FROM #Tempentities
     select @pk = @pk + 1
     --select @pk

    END

    END TRY
    BEGIN CATCH
        SELECT ERROR_NUMBER() AS ErrorNumber
        ,ERROR_LINE() AS ErrorLine
        ,ERROR_MESSAGE() AS ErrorMessage;
    END CATCH
END