2017-05-21 54 views
2

在以下過程中,第二個I F語句中的Select語句返回結果(e_id)。如何將sp_executesql結果轉換爲動態查詢中的變量?

但我想將select語句的結果存儲在一個變量中。我正在從臨時表中刪除記錄,並使用此變量將結果插入到臨時表中。但是@myVariable返回null。

CREATE TYPE TableVariable AS TABLE 
(
    id INT IDENTITY(1,1), 
    field_ids INT, 
    value VARCHAR(MAX) 
) 

Type created successfully. 

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 [email protected]; 
     SELECT @val= value  FROM @TableVar where [email protected]; 
     SELECT @field_Type=type,@is_List=is_list FROM FIELD WHERE [email protected] 


      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 SP_EXECUTESQL @SQL 
      END 
      IF(@is_List = 0 AND @pk>1) 
      BEGIN 
      SELECT @field_type 
      SELECT * FROM #Tempentities 
      Set @SQL = '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 SP_EXECUTESQL @SQL, N'@myVariable INT OUTPUT', @[email protected] OUTPUT 
      --Execute @entityid=SP_EXECUTESQL @SQL 
       Delete from #Tempentities 
       insert into #Tempentities(entity_id) values(@myVariable) 

      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 

回答

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 [email protected]; 
     SELECT @val= value  FROM @TableVar where [email protected]; 
     SELECT @field_Type=type,@is_List=is_list FROM FIELD WHERE [email protected] 


      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 
相關問題