create function ... @主鍵
return int
as
    ...
    select identity(int,1,1) as id,基他字段
    into #新表
    from 你的表    declare @i int
    select @i=id 
    from #新表
    where 主鍵=@主鍵    drop table #新表
    return(@i)    ...

解决方案 »

  1.   


    --如果是按主键从小到大来确定记录的位置,可以用这样的函数create function f_rownum(@主键 varchar(10))
    returns int
    as
    begin
    return(select sum(1) from 表 where 主键<=@主键)
    end
    go--调用
    select dbo.f_rownum('asdfa')
      

  2.   

    函数没有,可以通过以下语句select 记录号=count(*)+1 from table where fieldname<'主键值'
      

  3.   

    因為不知記錄是否按主鍵排序(主鍵列不一定是聚集索引列)tabTest表中unit為主鍵字段create function dbo.FetchRow (@A varchar(10))
    returns int
    as
    begin   
     
        declare  @table table (id int identity(1,1),unit varchar(10))
       insert into @table 
      select unit
        from tabTest    declare @i int
        select @i=id 
        from @table
        where unit=@A   
        return(@i)end