简体   繁体   English

使用第一个声明的变量执行多个查询字符串

[英]Executing multiple query strings using a variable declared at first one

I need to execute a long dynamic query string based on a xml variable, then I needed to split the string on multiple varchar variables, each one containing part of an update in a table inside a cursor. 我需要执行一个基于xml变量的长动态查询字符串,然后需要将该字符串拆分为多个varchar变量,每个变量都包含游标内表中的一部分更新。

I will post part of the query: 我将发布部分查询:

DECLARE @QUERY1 VARCHAR(8000);
DECLARE @QUERY2 VARCHAR(8000);
DECLARE @QUERY3 VARCHAR(8000);
DECLARE @QUERY4 VARCHAR(8000);

DECLARE @cols AS VARCHAR(8000);

DECLARE @begin INT
DECLARE @totalid INT
DECLARE @groupsize INT
SET @groupsize = 0
SELECT @totalid = MAX(IntegrationId) FROM Integration
set @QUERY1 = N'
BEGIN TRANSACTION
DECLARE @xml XML 
DECLARE @XMLReceiverID int
DECLARE XMLRowItem CURSOR FOR
SELECT XMLReceiverID,XMLContent FROM XMLReceiver WHERE FlagImportData = 0 and Typology = ''XXXX''
OPEN XMLRowItem
FETCH XMLRowItem INTO @XMLReceiverID,@xml
WHILE @@Fetch_Status = 0

BEGIN
';
set @QUERY2 = N'
INSERT INTO IntegrationData (XmlReceiverID) 
';
set @QUERY3 = N'
SELECT @XMLReceiverID
';      
--PRINT @QUERY1 + @QUERY2 + @QUERY3
EXEC (@QUERY1 + @QUERY2 + @QUERY3)      
WHILE @groupsize <= @totalid
BEGIN
SET @begin = @groupsize
SET @groupsize = @groupsize + 10
select @cols = 
(select STUFF((SELECT distinct ',',  QUOTENAME(FieldName), ' = ' , CASE WHEN XMLPath IS NULL THEN 'NULL' ELSE '(SELECT TOP 1 c.value(''(' + (REVERSE(LEFT(REVERSE(XMLPath), CHARINDEX('/', REVERSE(XMLPath)) - 1)))  + ')[1]'',''VARCHAR(50)'') FROM  @xml.nodes(''' + REPLACE(XMLPath,'/' + REVERSE(LEFT(REVERSE(XMLPath), CHARINDEX('/', REVERSE(XMLPath)) - 1)), '') + ''') t(c)) ' END 
from IntegracaoOpcaoLayout
where FlagIntegration = 1 
AND XMLPath IS NOT NULL
AND IntegrationId > @begin
AND IntegrationId <= @groupsize
order by QUOTENAME(FieldName) asc

FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')  ,1,1,''))

set @QUERY2 = N'
UPDATE IntegrationData SET ' + @cols + ' WHERE XmlReceiverID = @XMLReceiverID
';
EXEC (@QUERY2)
--PRINT @QUERY2
END 
set @QUERY4 = N'    
UPDATE XMLReceiver SET FlagIntegration = 1 WHERE XMLReceiverID = @XMLReceiverID AND FlagImportData = 0
FETCH NEXT FROM XMLRowItem into @XMLReceiverID,@xml
END
PRINT @XMLReceiverID
CLOSE XMLRowItem

DEALLOCATE XMLRowItem
COMMIT TRANSACTION
';
EXEC (@QUERY4)
--PRINT @QUERY4

If I run this code printing the query strings and execute it all it works fine! 如果我运行此代码打印查询字符串并执行它,那么一切正常!

But if I run normally, I get error bellow (about nor declaring the variable declared at first exec statement): 但是,如果我正常运行,则会收到以下错误消息(关于或不声明在第一个exec语句中声明的变量):

Msg 102, Level 15, State 1, Line 15
Incorrect syntax near '@XMLReceiverID'.
Msg 137, Level 15, State 2, Line 2
Must declare the scalar variable "@xml".

Has anyone a work around or a suggestion? 有没有人解决或提出建议?

Here is the query printed: 这是打印的查询:

BEGIN TRANSACTION
DECLARE @xml XML 
DECLARE @XMLReceiverID int
DECLARE XMLRowItem CURSOR FOR
SELECT XMLReceiverID,XMLContent FROM XMLReceiver WHERE FlagImportData = 0 and Typology = 'XXXX'
OPEN XMLRowItem
FETCH XMLRowItem INTO @XMLReceiverID,@xml
WHILE @@Fetch_Status = 0

BEGIN

INSERT INTO IntegrationData (XmlReceiverID) 

SELECT @XMLReceiverID

UPDATE IntegrationData SET [Field1] = (SELECT TOP 1 c.value('(test1)[1]','VARCHAR(50)') FROM  @xml.nodes('/xmlPath/test1') t(c)) ,
[Field2] = (SELECT TOP 1 c.value('(test2)[1]','VARCHAR(50)') FROM  @xml.nodes('/xmlPath/test2') t(c)) ,
[Field3] = (SELECT TOP 1 c.value('(test3)[1]','VARCHAR(50)') FROM  @xml.nodes('/xmlPath/test3') t(c)) 
UPDATE XMLReceiver SET FlagImportData = 1 WHERE XMLReceiverID = @XMLReceiverID AND FlagImportData = 0
FETCH NEXT FROM XMLRowItem into @XMLReceiverID,@xml
END
PRINT @XMLReceiverID
CLOSE XMLRowItem

DEALLOCATE XMLRowItem
COMMIT 

The reason your code is throwing errors is because all of your code is out of scope. 您的代码抛出错误的原因是因为您所有的代码都超出范围。

This is your problem. 这是你的问题。

 set @QUERY2 = N'
    UPDATE IntegrationData SET ' + @cols + ' WHERE XmlReceiverID = @XMLReceiverID
    ';
    EXEC (@QUERY2)
    --PRINT @QUERY2
    END 
    set @QUERY4 = N'    
    UPDATE XMLReceiver SET FlagIntegration = 1 WHERE XMLReceiverID = @XMLReceiverID AND FlagImportData = 0
    FETCH NEXT FROM XMLRowItem into @XMLReceiverID,@xml
    END
    PRINT @XMLReceiverID
    CLOSE XMLRowItem

    DEALLOCATE XMLRowItem
    COMMIT TRANSACTION
    ';

You can see that you reassign @query2 with a statement that requires @XMLReceiverID, but the variable has not been declared for the new @query2 assignment. 您可以看到您使用要求@XMLReceiverID的语句重新分配了@ query2,但是尚未为新的@ query2分配声明该变量。 The same thing holds for @query 4. The two variables @XML,@XMLReceiverId are not in the same scope as @Query1. @query 4同样适用。两个变量@ XML,@ XMLReceiverId与@ Query1不在同一范围内。

Essentially, you need to concatenate all statements into a single variable or combine them together into a single execute. 本质上,您需要将所有语句连接到一个变量中,或者将它们组合成一个执行语句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM