得到id列最小未用值的 函数执行不成功,怎么修改

asdmusic8 2003-10-26 09:41:23
自己定义了一个得到id列最小未用值的 函数
传入 字段名,数据表名称,
但是报
服务器: 消息 443,级别 16,状态 2,过程 getMaxNumber,行 11
在函数内不正确地使用了 'EXECUTE'。

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[getMaxNumber]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[getMaxNumber]
GO

SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO

create function getMaxNumber(
@iFILENAME VARCHAR(100),@iTABLENAME VARCHAR(100)
)

returns int
as
begin

declare @rtn int
exec('

select @rtn=isnull(max(cast('+@iFILENAME+' as int)+1,1) from [DBO].['+@iTABLENAME+']
select isnull(min(ids),@rtn) from (
select ( select count('+@iFILENAME+')+1 from [DBO]['+ @iTABLENAME+'] where id<a.id) as ids,*
from [DBO]['+ @iTABLENAME+'] as a) as u
where ids<>'+@iFILENAME+'

');
return @rtn
end

GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO

...全文
33 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
asdmusic8 2003-10-26
  • 打赏
  • 举报
回复
我原来可以把 这个函数写在 字段的默认值中,那过程该怎么写在 字段的默认值中
pengdali 2003-10-26
  • 打赏
  • 举报
回复
函数里不可以用exec这是sqlserver的约束!


你改成过程。

34,875

社区成员

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

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