create procedure Upro
@tbname nvarchar(50)
,@YIDValue nvarchar(50)
asexec ('select max(XID) from ' + @tbname + ' where YID=''' +@YIDValue + '''')

解决方案 »

  1.   

    不管什麼條件,你隻要模仿上面用EXEC()動態執行即可!
      

  2.   

    你可以在EXEC前,先print 'select max(XID) from ' + @tbname + ' where YID=''' +@YIDValue + ''''看看此字符表達式是否是你要執行的SQL 語句即可
      

  3.   

    我说的xid,yid,并不是已经确定的名称,xid,yid是随着表名称的不同而不同的,也需要input 到存储过称里。
      

  4.   

    create proc up_test (@tablename varchar(256),@xid varchar(256),@yid varchar(256), @where varchar(800),@out int output)
    as
    begin
      declare @str nvarchar(1000)
      select @str = 'SELECT @out =MAX('+@xid+' from '+@tablename +
    'where '+@yid + '=' +@where
      exec sp_executesql  @str, '@out int output',@out output
    end
      

  5.   

    修改了一下:drop proc up_test
    go
    create proc up_test (@tablename varchar(256),@xid varchar(256),@yid varchar(256), @where varchar(800),@out int output)
    as
    begin
      declare @str nvarchar(1000)
      select @str = 'SELECT @out =MAX('+@xid+') from '+@tablename +
    ' where '+@yid + '=' +@where
      print @str
      exec sp_executesql  @str, N'@out int output',@out output
    end
    go
      

  6.   

    drop proc up_test
    go
    create proc up_test (@tablename varchar(256),@xid varchar(256),@yid varchar(256), @where varchar(800),@out int output)
    as
    begin
      declare @str nvarchar(1000)
      select @str = 'SELECT @out =MAX('+@xid+') from '+@tablename +
    ' where '+@yid + '=' +@where
      exec sp_executesql  @str, N'@out int output',@out output
    end
    go
      

  7.   

    那我要调用的时候要怎么写?
    exec up_test 't_inlist','f_inid','f_drugid','000000'
      

  8.   

    如果YID的条件值是字符型應該是:
    create proc up_test (@tablename varchar(256),@xid varchar(256),@yid varchar(256), @where varchar(800),@out int output)
    as
    begin
      declare @str nvarchar(1000)
      select @str = 'SELECT @out =MAX('+@xid+') from '+@tablename +
    ' where '+@yid + '=''' +@where+''''
      exec sp_executesql  @str, N'@out int output',@out output
    end
    go
    ----記得給字符型的變量加個引號(SQL兩單引號標示一個單引號)
      

  9.   

    最好在在EXEC前調試一下@STR是否是你想要的SQL語句
    begin
      declare @str nvarchar(1000)
      select @str = 'SELECT @out =MAX('+@xid+') from '+@tablename +
    ' where '+@yid + '=''' +@where+''''   PRINT @STR   exec sp_executesql  @str, N'@out int output',@out output
    end
      

  10.   

    declare @out nvarchar(100)
    exec up_test 't_inlist','f_inid','f_drugid','000000',@out outputprint @out
      

  11.   

    调用
    declare @a int
    exec up_test 'test_1' ,'a','b', '4',@a output
    select @aCREATE TABLE [test_1] (
    [id] [int] NULL ,
    [a] [int] NULL ,
    [b] [int] NULL 
    ) ON [PRIMARY]
    GO
      

  12.   

    declare @out nvarchar(100)
    exec p_get_XList_Max_XId 't_inlist','f_inid','f_drugid','000000',@out out结果:
    SELECT @out =MAX(f_inid)as Maxid from t_inlist where f_drugid='000000'
    服务器: 消息 156,级别 15,状态 1,行 1
    在关键字 'as' 附近有语法错误。
      

  13.   

    declare @sql nvarchar(1000)select @sql='select @output =max('+@xid+)'+from '+@table+' where '+@yid+'='+@条件exec sp_executesql @sql,N'@xid int,@yid nvarchar(100) ,@条件 nvarchar(100),output int output' ,@xid,@yid ,@条件,@out output
      

  14.   

    去掉 AS MAXID 即可!
      

  15.   

    1:將@out 類型改為: varchar(50)2: 
     select @output =max(xid) from ......  是賦值語句
     select max(xid) as maxid from ........ 才是查詢語句
      

  16.   

    as附近少了一个空格。加上一个空格就对了。像这样的情况,
    create f up_test (@tablename varchar(256),@xid varchar(256),@yid varchar(256), @where varchar(800),@out int output)
    as
    begin
      declare @str nvarchar(1000)
      select @str = 'SELECT MAX('+@xid+') from '+@tablename +
    ' where '+@yid + '=''' +@where+''''
      print @str
      execute(@str)
    end
    go
      

  17.   

    to hjhing(winding)
    照你说的:1:將@out 類型改為: varchar(50)
    还是数据库中id项是'000001',结果却是'1'