都是主管惹的祸......

zhaodiqing 2003-11-14 10:38:56
/*
以字段名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
...全文
43 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhaodiqing 2003-11-14
  • 打赏
  • 举报
回复
大力:

存储过程执行通过, 但我怎么取到它的返回值??
zhaodiqing 2003-11-14
  • 打赏
  • 举报
回复
挖土: 不行
zhaodiqing 2003-11-14
  • 打赏
  • 举报
回复
大力: 调用也不成功?????
DigJim 2003-11-14
  • 打赏
  • 举报
回复
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

--第二种方法
zhaodiqing 2003-11-14
  • 打赏
  • 举报
回复
这是存储过程....

也可以取返回值??
txlicenhe 2003-11-14
  • 打赏
  • 举报
回复
1:
先别骂人,其实主管一定有他的道理,我就比较喜欢用日期加流水号的方式
2:
http://expert.csdn.net/Expert/topic/2364/2364046.xml?temp=.829693
[交流]动态SQL语句
pengdali 2003-11-14
  • 打赏
  • 举报
回复
调用:

declare @a int
exec @a=过程名 'xx','tt'

select @a
pengdali 2003-11-14
  • 打赏
  • 举报
回复
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

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧