[英]Exceeding maximum number of variable declarations in script on SQL Server 2005
An application I am currently working on will generate an SQL script to populate a database. 我当前正在处理的应用程序将生成一个SQL脚本来填充数据库。 A single transaction in the script looks like this (note I have changed table/variable names ;-)
脚本中的单个事务如下所示(请注意,我已经更改了表/变量名称;-)
USE MyDatabase
BEGIN TRANSACTION
SET XACT_ABORT ON
DECLARE @Foo int
SET IDENTITY_INSERT Table1 ON
INSERT INTO Table1 [...]
SET IDENTITY_INSERT Table1 OFF
SET IDENTITY_INSERT Table2 ON
INSERT INTO Table2 [...]
SET IDENTITY_INSERT Table2 OFF
INSERT INTO Table3 [...]
-- Here I reference @Foo
SET @Foo = dbo.SomeStoredProcedure()
-- Use @Foo in some query
COMMIT TRANSACTION
GO
SET NOCOUNT ON
This script will then generate n
of these transactions, which will then be excuted on SQL Server 2005 to populate the database with n
records. 然后,此脚本将生成这些事务中的
n
,然后在SQL Server 2005中对其进行处理,以使用n
条记录填充数据库。
The problem I am seeing is with the declaration of the @Foo
variable shown above. 我看到的问题是上面显示的
@Foo
变量的声明。 When running the script, once we have reached 65535 records, I get the following error: 运行脚本时,一旦我们达到65535条记录,就会出现以下错误:
The variable name '@Foo' has already been declared.
Variable names must be unique within a query batch or stored procedure.
I think this is a misleading error message, because everything is fine until I hit 65535, and the significance of this number (2^16-1) leads me to believe I am hitting some sort of script limitation. 我认为这是一个误导性的错误消息,因为在我达到65535之前一切都很好,并且这个数字的重要性(2 ^ 16-1)使我相信我遇到了某种脚本限制。
I have tried defining the @Foo
variable once, at the top of the script, and re-using it within each transaction. 我尝试在脚本顶部定义一次
@Foo
变量,然后在每个事务中重用它。 But this doesn't work as it appears each transaction has its own scope. 但这是行不通的,因为看起来每个事务都有自己的范围。
Would creating an extra level of scope (ie an inner transaction) and declaring the variable within the deeper scope help address this issue? 创建额外级别的范围(即内部事务)并在更深的范围内声明变量是否可以解决此问题?
Any other recommendations about the best way to fix this issue? 还有其他有关解决此问题的最佳方法的建议吗?
Looks like you've missed the GO delimiter, since I have scripts with many more lines. 看起来你已经错过了GO分隔符,因为我有更多的行脚本。 Check your scripting solution
检查您的脚本解决方案
Try The Go delimiter and also change the data type for the dynmc. 尝试使用Go分隔符,并更改dynmc的数据类型。 variable.
变量。 You are going out of its range and an "INT" is a cyclic data type in SQL Servers.
您超出了它的范围,而“ INT”是SQL Server中的循环数据类型。 It will come to the same address when the cycle of -65536 to 65535 completes.
-65536至65535的循环完成时,它将到达相同的地址。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.