我想實現這個功能: select @temp=max(id) from replist with(index(pk_replist)) where factory='1'
exec("select max(id) from replist with(index(pk_replist)) where "+@lclass+"='"+@class+"'")
declare @lclass varchar(8) declare @class char(1) declare @temp int set @lclass='factory' set @class='1'declare @sql nvarchar(500)* set @sql='select @t=max(id) from replist with(index(pk_replist)) where '+@lclass+'='''+@class+'''' exec sp_executesql @sql,N'@t int output',@temp output
exec("select @temp=max(id) from replist with(index(pk_replist)) where "+@lclass+"='"+@class+"'")
謝謝! 根據你的代碼: exec("select max(id) from replist with(index(pk_replist)) where "+@lclass+"='"+@class+"'") 確實沒出錯,但我要實現的是把查詢的結果賦值給@temp,也就是我想實現的功能是: select @temp=max(id) from replist with(index(pk_replist)) where factory='1' 該如何實現呢?請指教!!!!!
to tj_dns(愉快的登山者),還是不可以,出現如下提示: Server: Msg 137, Level 15, State 1, Line 1 Must declare the variable '@temp'.
你这种动态SQL 不能单纯EXEC,用SP_EXECUTESQL 传出参数: set @sql='select @t=max(id) from replist with(index(pk_replist)) where '+@lclass+'='''+@class+'''' exec sp_executesql @sql,N'@t int output',@temp output 以上就把查詢的結果賦值給@temp
select @temp=max(id) from replist with(index(pk_replist)) where factory='1'
declare @class char(1)
declare @temp int
set @lclass='factory'
set @class='1'declare @sql nvarchar(500)*
set @sql='select @t=max(id) from replist with(index(pk_replist)) where '+@lclass+'='''+@class+''''
exec sp_executesql @sql,N'@t int output',@temp output
根據你的代碼:
exec("select max(id) from replist with(index(pk_replist)) where "+@lclass+"='"+@class+"'")
確實沒出錯,但我要實現的是把查詢的結果賦值給@temp,也就是我想實現的功能是:
select @temp=max(id) from replist with(index(pk_replist)) where factory='1'
該如何實現呢?請指教!!!!!
Server: Msg 137, Level 15, State 1, Line 1
Must declare the variable '@temp'.
set @sql='select @t=max(id) from replist with(index(pk_replist)) where '+@lclass+'='''+@class+''''
exec sp_executesql @sql,N'@t int output',@temp output
以上就把查詢的結果賦值給@temp