如何解决INSERT EXEC语句不能嵌套在SQL Server中

时间:2017-10-09 11:08:12

标签: sql-server database stored-procedures java-stored-procedures

我有一个sp,我将返回一个单列结果。我试图将结果存储到表类型中,但我收到此错误:

  

无法嵌套INSERT EXEC语句。

我已经搜索过,但没有找到任何可接受的解决方案。

sp如下: -

    ALTER PROCEDURE [dbo].[Sp_DemographicFilter_booster]
        (

        @FilterSelected FilterSelected READONLY, 
        @CountryCategoryId int=null
    )
AS
BEGIN
    SET NOCOUNT ON;

    DECLARE @WhereCondition varchar(500)  ;
    DECLARE @QueryString Varchar(MAX) ;
    DECLARE @QueryString_booster Varchar(MAX) ;
    DECLARE @Filter table (FilterColumn Varchar(200),FilterValue Varchar(200))
    DECLARE @Result table (SERIAL int)
    DECLARE @Result_booster table (SERIAL int)

    if( select top 1  FilterColumn FROM  @FilterSelected where FilterColumn<>'HISPANIC') is NOT NULL
    BEGIN
    Insert into @Filter
    Select * from @FilterSelected where FilterColumn<>'HISPANIC'
    --DECLARE @DemoTbl TABLE (MetricName VARCHAR(100),CatValue VARCHAR(100))

        SELECT @WhereCondition= COALESCE( @WhereCondition + ' and ', '')+SubjectList FROM (
        SELECT DISTINCT STD.Filtercolumn +' in ('+
        ISNULL(STUFF((SELECT ', '+'''' + ssm.Filtervalue+''''
        FROM @Filter SSM
        INNER JOIN @Filter SUB ON SUB.FilterColumn = SSM.FilterColumn and SUB.FilterColumn=STD.FilterColumn
        WHERE sub.FilterValue = ssm.FilterValue
        FOR XML PATH('')
        ), 1, 1, ''), 'Not Assigned Yet')+')' AS SubjectList
        FROM @Filter STD)A


        print @WhereCondition

    --INSERT INTO @DemoTbl
    --select SUBSTRING(col1,1, CHARINDEX(':',col1,1)-1) MetricName,SUBSTRING(col1, CHARINDEX(':',col1,1)+1,LEN(Col1)) CatValue  
    --from dbo.UF_CSVDataToTable(@FilterSelectedSelected)


    SET @QueryString='SELECT SERIAL FROM Logical.Demographic D
    WHERE '+@WhereCondition+' and CountryCategoryId='+cast(@CountryCategoryId as varchar(10))
    PRINT @QueryString
    insert into @Result
    EXEC(@QueryString)
    --select * from @Result
    END
     IF(select top 1  FilterColumn FROM  @FilterSelected where FilterColumn='HISPANIC') IS NOT NULL
    BEGIN

    Delete from @Filter;

    DECLARE @Response varchar(20)=null;

    Insert into @Filter
    Select * from @FilterSelected where FilterColumn='HISPANIC'

    select @Response=FilterValue from @Filter;

    DECLARE @VariableID int=null;

    select @VariableID=SurrogateKeyCounter from MetaData.Metadata_Screener where DBMetricName='HISPANIC';

    SET @QueryString_booster='SELECT SERIAL FROM Logical.Response R
    WHERE variableid='+cast(@VariableID as varchar(10))+' and CountryCategoryId='+cast(@CountryCategoryId as varchar(10))
    +' and ResponseName='''+@Response+''''

    PRINT @QueryString_booster
    Insert into @Result_booster
    EXEC(@QueryString_booster)

    END
    DECLARE @Final_Result table (SERIAL int)

    insert into @Final_Result
    select * from @Result
    UNION
    select * From @Result_booster

    select * from @Final_Result


    END

我这样称呼这个程序:

declare @ds FilterSelected
insert into @ds values('Hispanic','yes')
@FilterSelected=@ds,@CountryCategoryId=100

 DECLARE @DemoTbl TABLE (Serial INT)


Insert into @DemoTbl    
EXEC Sp_DemographicFilter_booster @FilterSelected=@ds,
@CountryCategoryId=100

1 个答案:

答案 0 :(得分:0)

调用SP Sp_DemographicFilter_booster以及唯一ID。 在Sp_DemographicFilter_booster SP内部,使用相同的唯一ID AS ID字段在全局表中创建存储全局表(##)的结果 现在,当返回主SP访问全局表时,条件为唯一ID