SQL Server存储过程调用需要变量声明(但已经声明)

时间:2013-10-18 11:59:08

标签: sql sql-server sql-server-2008 stored-procedures xbrl

我有一些存储过程,我按此顺序调用。

enter image description here

因此,从第一个存储过程importTaxonomy我呼叫parseXBRLparseXBRL我呼叫createTaxonomyStructure

但是在这个流程中,当执行最后一个存储过程的代码时,我得到一个错误。

Msg 1087, Level 15, State 2, Line 1 Must declare the table variable "@temporaryTable".

您可以在下面找到此存储过程的前几行代码:

CREATE PROCEDURE createTaxonomyStructure @taxonomy_table nvarchar(max), @debug bit = 0  
AS

DECLARE @statement NVARCHAR(MAX)
DECLARE @temporaryTable TABLE (taxonomyLine NVARCHAR(MAX))      -- declared a temporary table to avoid creating a Dynamic Query with the entire cursor, but just with the temporary table
DECLARE @taxonomyLine NVARCHAR(MAX)     -- variable that will store one line of the taxonomy

    SET @statement = 'INSERT INTO @temporaryTable SELECT taxText FROM ' + @taxonomy_table   -- statement that will import the taxonomy in the temporary table

EXEC sp_executesql @statement           

DECLARE taxonomyCursor CURSOR READ_ONLY FAST_FORWARD FOR        -- read each line in the taxonomy to parse afterwards
    SELECT taxonomyLine
    FROM @temporaryTable

OPEN taxonomyCursor

FETCH NEXT FROM taxonomyCursor INTO @taxonomyLine               -- parsing each taxonomy line and extracting the values from important attributes
WHILE @@FETCH_STATUS = 0
    BEGIN

        DECLARE @id_element NVARCHAR(MAX)
        DECLARE @leaf_element NVARCHAR(MAX)

        SELECT @id_element = (SELECT dbo.extract_IDElement(@taxonomyLine))
        SELECT @leaf_element = (SELECT dbo.extract_IDLeafElement(@taxonomyLine))

        SET @statement = 'UPDATE ' + @taxonomy_table + ' SET fullName = ''' + @id_element + ''', leafName = ''' + @leaf_element + '''';

        EXEC sp_executesql @statement

    END

我确实声明了这个变量,但我仍然得到错误,我不明白为什么。

如何克服此错误?

谢谢!

1 个答案:

答案 0 :(得分:1)

错误在这里:

SET @statement = 'INSERT INTO @temporaryTable SELECT taxText FROM ' + @taxonomy_table   -- statement that will import the taxonomy in the temporary table

EXEC sp_executesql @statement 

将其更改为

set @statement = 'select taxText from ' + @taxonomy_table

insert into @temporaryTable
exec sp_executesql @statement

发生错误是因为在执行@statement的范围内没有变量表@temporaryTable