create proc 过程名
@cTableName varchar(100),
@cFieldName varchar(100)
as
declare @sSql nvarchar(200),@GetMaxId int set @sSql = N' select @GetMaxId = max( '+ @cFieldName + ' ) from '+ @cTableName
exec sp_executesql @ssql,N'@GetMaxId int output',@GetMaxId output
if (@GetMaxId = 0 )
   set @GetMaxId = 1
else
   set @GetMaxId = @GetMaxId + 1
Return @GetMaxId
go

解决方案 »

  1.   

    调用:declare @a int
    exec @a=过程名 'xx','tt'select @a
      

  2.   

    1: 
      先别骂人,其实主管一定有他的道理,我就比较喜欢用日期加流水号的方式
    2:
    http://expert.csdn.net/Expert/topic/2364/2364046.xml?temp=.829693
    [交流]动态SQL语句
      

  3.   

    Alter Function dbo.bf_GetIncreaseId (
    @cFieldName  char(20),
    @cTableName char(20)
    )
    Returns  int
    as
    begin--第一种方法
    /* 错误提示
    服务器: 消息 443,级别 16,状态 2,过程 bf_GetIncreaseId,行 35
    在函数内不正确地使用了 'EXECUTE'。
    */ 
    declare @GetMaxId int
            declare @sSql  varchar(200)
    -------这里错了----------
    set @sSql = ' select '+@GetMaxId + '= max( '+ @cFieldName + ' ) from '+ @cTableName+''
    -------已改正----------- exec (@sSql) if (@GetMaxId = 0 )
       set @GetMaxId = 1
    else
       set @GetMaxId = @GetMaxId + 1

    Return @GetMaxId--第二种方法