用户定义函数不能用于执行一组修改全局数据库状态的操作,只能对函数内的临时表进行INSERT,DELETE和UPDATE操作垃圾数据库,什么破规定啊

解决方案 »

  1.   

    可以这样:
    create table IntKey(KeyChar char(10))
    go
    create function GetKey()
    returns char(10)
    as
    begin
    declare @KeyValue int
    declare @KeyReturn varchar(20)
    set @KeyValue = cast(isnull((select max(KeyChar) from IntKey),0) as int) + 1
    set @KeyReturn = '000000000' + ltrim(str(@KeyValue))
    return right(@KeyReturn,10)
    endgodeclare @i int
    set @i = 0
    while @i < 100
    begin
    insert into IntKey(KeyChar)
    select dbo.GetKey()
    set @i = @i + 1
    end
    select * from IntKey
    go
    drop function GetKey
    drop table IntKey
      

  2.   

    谢谢ping3000,可是这个函数怎么用啊?我要的是在插入数据时,可以这样写代码:insert into ... select GetKey(),... 
    然后主键数值是自增的
      

  3.   

    可以这样用:
    create table IntKey(KeyChar char(10),字段 nvarchar(20))
    insert into IntKey(KeyChar,字段)
    select dbo.GetKey(),'字段值'
    这样KeyChar就是自增的了
      

  4.   

    我知道了你的做法了。可是select max()效率较低;
    更重要的是每个表都要单独处理了
      

  5.   

    也可以建个索引用,select top 1 KeyChar from IntKey order by KeyChar desc
      

  6.   

    都不是好办法,SQL Server真是个垃圾数据库不过非常谢谢ping3000