都是主管惹的祸......
/*
以字段名Field,表名Table作为参数写一个函数,通过这两个参数取得Table中的Field在最大的基础上加1
NNDXX, 本来可以用自增长的方式, 可是主管的爱好实在不敢恭维,偏要说chat型,还指明要8位, 哎:(
大家帮我看看,下面的代码,运行不了, 两种方法都不行.....
*/
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
--第二种方法
/* 错误提示
服务器: 消息 137,级别 15,状态 2,过程 bf_GetIncreaseId,行 47
必须声明变量 '@cTableName'。
*/
declare @GetMaxId int
select max(@cFieldName) from @cTableName
if (@GetMaxId = 0 )
set @GetMaxId = 1
else
set @GetMaxId = @GetMaxId + 1
Return @GetMaxId
end