Declare @reValue varchar(200)
Declare @tmpField varchar(50)
Decalre @id int
...
set @reValue = exec('select '+ @tmpField +' from Table1 where ID= '+@id)
select @reValue
error --在关键字 'exec' 附近有语法错误。
怎样从exec('SQL语句') 中得到一个返回值?
简单的说 执行 select exec('select count(*) from Table1') 也是报同样的错。
MSN: jxfun@hotmail.com 谢谢!
10 个解决方案
#1
Declare @reValue varchar(200)
Declare @tmpField varchar(50)
Decalre @id int
...
exec('select @reValue = ’+ @tmpField +' from Table1 where ID= '+@id)
select @reValue
Declare @tmpField varchar(50)
Decalre @id int
...
exec('select @reValue = ’+ @tmpField +' from Table1 where ID= '+@id)
select @reValue
#2
Declare @reValue varchar(200)
Declare @tmpField varchar(50)
Decalre @id int
...
set @reValue = exec('select '+ @tmpField +' from Table1 where ID= '+@id)
select @reValue
错误的原因很....很....糗,因为楼主使用了全角的括号"("、")",换作半角应该就没有问题了。
Declare @tmpField varchar(50)
Decalre @id int
...
set @reValue = exec('select '+ @tmpField +' from Table1 where ID= '+@id)
select @reValue
错误的原因很....很....糗,因为楼主使用了全角的括号"("、")",换作半角应该就没有问题了。
#3
使用SP_executesql
#4
用 输出参数
eg:
declare @num,
@sqls
set @sqls='select count(*) from ' + @servername + '.a.dbo.b'
exec(@sqls)
我如何能将exec执行的结果存入变量@num中
declare @num int,
@sqls nvarchar(4000)
set @sqls='select @a=count(*) from '+@servername+'.a.dbo.b'
exec sp_executesql @sqls,N'@a int output',@num output
select @num
eg:
declare @num,
@sqls
set @sqls='select count(*) from ' + @servername + '.a.dbo.b'
exec(@sqls)
我如何能将exec执行的结果存入变量@num中
declare @num int,
@sqls nvarchar(4000)
set @sqls='select @a=count(*) from '+@servername+'.a.dbo.b'
exec sp_executesql @sqls,N'@a int output',@num output
select @num
#5
declare @num,
@sqls
set @sqls='select count(*) from ' + @servername + '.a.dbo.b'
exec(@sqls)
我如何能将exec执行的结果存入变量@num中
declare @num int,
@sqls nvarchar(4000)
set @sqls='select @a=count(*) from '+@servername+'.a.dbo.b'
exec sp_executesql @sqls,N'@a int output',@num output
select @num
@sqls
set @sqls='select count(*) from ' + @servername + '.a.dbo.b'
exec(@sqls)
我如何能将exec执行的结果存入变量@num中
declare @num int,
@sqls nvarchar(4000)
set @sqls='select @a=count(*) from '+@servername+'.a.dbo.b'
exec sp_executesql @sqls,N'@a int output',@num output
select @num
#6
或者使用临时表转存一下
create table #temp1(a int)
insert into #temp exec('select count(*) from Table1')
Declare @reValue varchar(200)
select @reValue=a from #temp
print @reValue
create table #temp1(a int)
insert into #temp exec('select count(*) from Table1')
Declare @reValue varchar(200)
select @reValue=a from #temp
print @reValue
#7
汗ing,是偶自己糗了...
#8
谢谢 NinGoo(宁哥)!
#9
宁哥果然强啊~~~~~~~~~~~
#10
受教了...
#1
Declare @reValue varchar(200)
Declare @tmpField varchar(50)
Decalre @id int
...
exec('select @reValue = ’+ @tmpField +' from Table1 where ID= '+@id)
select @reValue
Declare @tmpField varchar(50)
Decalre @id int
...
exec('select @reValue = ’+ @tmpField +' from Table1 where ID= '+@id)
select @reValue
#2
Declare @reValue varchar(200)
Declare @tmpField varchar(50)
Decalre @id int
...
set @reValue = exec('select '+ @tmpField +' from Table1 where ID= '+@id)
select @reValue
错误的原因很....很....糗,因为楼主使用了全角的括号"("、")",换作半角应该就没有问题了。
Declare @tmpField varchar(50)
Decalre @id int
...
set @reValue = exec('select '+ @tmpField +' from Table1 where ID= '+@id)
select @reValue
错误的原因很....很....糗,因为楼主使用了全角的括号"("、")",换作半角应该就没有问题了。
#3
使用SP_executesql
#4
用 输出参数
eg:
declare @num,
@sqls
set @sqls='select count(*) from ' + @servername + '.a.dbo.b'
exec(@sqls)
我如何能将exec执行的结果存入变量@num中
declare @num int,
@sqls nvarchar(4000)
set @sqls='select @a=count(*) from '+@servername+'.a.dbo.b'
exec sp_executesql @sqls,N'@a int output',@num output
select @num
eg:
declare @num,
@sqls
set @sqls='select count(*) from ' + @servername + '.a.dbo.b'
exec(@sqls)
我如何能将exec执行的结果存入变量@num中
declare @num int,
@sqls nvarchar(4000)
set @sqls='select @a=count(*) from '+@servername+'.a.dbo.b'
exec sp_executesql @sqls,N'@a int output',@num output
select @num
#5
declare @num,
@sqls
set @sqls='select count(*) from ' + @servername + '.a.dbo.b'
exec(@sqls)
我如何能将exec执行的结果存入变量@num中
declare @num int,
@sqls nvarchar(4000)
set @sqls='select @a=count(*) from '+@servername+'.a.dbo.b'
exec sp_executesql @sqls,N'@a int output',@num output
select @num
@sqls
set @sqls='select count(*) from ' + @servername + '.a.dbo.b'
exec(@sqls)
我如何能将exec执行的结果存入变量@num中
declare @num int,
@sqls nvarchar(4000)
set @sqls='select @a=count(*) from '+@servername+'.a.dbo.b'
exec sp_executesql @sqls,N'@a int output',@num output
select @num
#6
或者使用临时表转存一下
create table #temp1(a int)
insert into #temp exec('select count(*) from Table1')
Declare @reValue varchar(200)
select @reValue=a from #temp
print @reValue
create table #temp1(a int)
insert into #temp exec('select count(*) from Table1')
Declare @reValue varchar(200)
select @reValue=a from #temp
print @reValue
#7
汗ing,是偶自己糗了...
#8
谢谢 NinGoo(宁哥)!
#9
宁哥果然强啊~~~~~~~~~~~
#10
受教了...