=号放到WHERE 里去,怎么能放到SELECT里呢?

解决方案 »

  1.   

    我想實現這個功能:
    select @temp=max(id) from replist with(index(pk_replist)) where factory='1'
      

  2.   

    exec("select max(id) from replist  with(index(pk_replist))  where "+@lclass+"='"+@class+"'")
      

  3.   

    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
      

  4.   

    exec("select @temp=max(id) from replist  with(index(pk_replist))  where "+@lclass+"='"+@class+"'")
      

  5.   

    謝謝!
    根據你的代碼:
    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'
    該如何實現呢?請指教!!!!!
      

  6.   

    to  tj_dns(愉快的登山者),還是不可以,出現如下提示:
    Server: Msg 137, Level 15, State 1, Line 1
    Must declare the variable '@temp'.
      

  7.   

    你这种动态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