得到id列最小未用值的 函数执行不成功,怎么修改
自己定义了一个得到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