Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 使用exec时增加计数器_Sql_Sql Server_Sql Server 2008_Sql Server 2005 - Fatal编程技术网

Sql 使用exec时增加计数器

Sql 使用exec时增加计数器,sql,sql-server,sql-server-2008,sql-server-2005,Sql,Sql Server,Sql Server 2008,Sql Server 2005,我想以如下方式使用计数器: exec ('begin insert into ' + @temp07 + ' (FileID,FileName) Select aof_id,aof_fileName from PaFi07(' + @partId + '); sp_executesql @sqlCounter, N'@intCount int output, @intConst int, @intCount = @intCount output, @intConst = @intConst

我想以如下方式使用计数器:

exec
('begin insert into ' + @temp07 + ' (FileID,FileName)
Select aof_id,aof_fileName from PaFi07(' + @partId + ');

sp_executesql @sqlCounter, N'@intCount int output, @intConst int,  @intCount = @intCount output,  @intConst = @intConst 
end')
那我怎么才能让柜台工作呢

任何关于计数器在此EXEC命令内工作的建议
THANX

您不需要
exec

set @inCount = @intCount + @intConst
如果要使用
exec
,请使用字符串中变量的名称,而不是值:

exec('set @inCount = @intCount + @intConst')
编辑: 对于新问题;您可以将这些值连接到字符串中:

exec
('begin insert into ' + @temp07 + ' (FileID,FileName)
Select aof_id,aof_fileName from PaFi07(' + @partId + ');

sp_executesql @sqlCounter, N'@intCount int output, @intConst int,  @intCount = ' + @intCount + ' output,  @intConst = ' + @intConst + ' end')
如果它们是数值,则需要强制转换它们:

... + cast(@intCount as varchar) + ...

由于
EXEC
在其自己的范围内运行,因此变量标识符没有意义,因此不能
EXEC
对它们执行操作

您可以将它们声明为
int
s,将它们与语句字符串一起传递给
sp_executesql
,并将结果作为输出返回

declare @sql nvarchar(256) = 'SET @intCount = @intCount + @intConst'
EXEC sp_executesql @sql, 
    N'@intCount int output, @intConst int', 
    @intCount = @intCount output, 
    @intConst = @intConst

select @intCount 

>6

您的代码段中有几个错误: 首先,您将变量声明为varchar(10),同时打算将它们用作数字。它们应该声明为smallint、int或bigint

然后,您将使用这些varchar变量组合一个字符串,并尝试将1添加到@inCount中存储的值中,同时添加数字1

因为变量是字符串而不是数字,所以+符号尝试将它们连接起来

要了解您的错误,首先,您应该将数字1转换为字符串,这样编写EXEC:

exec ('SET ' + @intCount + ' = ' + @intCount + '1') 
SET 5 = 51
完成此操作后,只需删除EXEC并将要连接的字符串指定给新的字符串变量。因此:

DECLARE @composedQuery varchar(1000)

SET @composedQuery = 'SET ' + @intCount + ' = ' + @intCount + '1'
SELECT @composedQuery
您将看到如下结果:

exec ('SET ' + @intCount + ' = ' + @intCount + '1') 
SET 5 = 51
当然,这不是您打算用EXEC执行的,不是吗?:)

其他答案中已向您提供了正确的解决方案。我重写了完整的片段:

declare @intCount int
declare @intConst int

set @intCount = 5
set @intConst = 1

SET @intCount = @intCount + 1
--OR
SET @intCount = @intCount + @intConst

“不直接”是什么意思?sp_executesql是一种将外部变量传递到动态语句范围的方法。请重新检查修改后的问题并提供帮助。这是一个完全不同的问题。。报表中的计数器在哪里?你想数什么?什么是
PaFi07
?计数器在while循环中位于exec之外,这只是代码的一部分;)如果它在exec之外,为什么需要以字符串形式递增计数器?你只会得到部分问题的部分答案…好吧,如果你想站在EXEC一边,那么Alex K.给出的解决方案就是这样做的。但是,请检查变量的声明,因为它们似乎是数字而不是字符串。请您重新检查修改后的问题和帮助您可以重新检查修改后的问题和帮助吗?请您如何编写?这是一个系统sp,它需要一个参数。。。