/**
 * 取得表的主键方法
 *
 * author tanxin 2011-11-25
 */
create function [dbo].[haf_sys_id_create]
(
  @id char(40)
)
returns char(40)
as
begin
declare @strId char(40)
set @strId = newid()
set @strId = 'HIT'+ substring(@strId, 1, 8) 
                     + substring(@strId, 10, 4)
                     + substring(@strId, 15, 4)
                     + substring(@strId, 20, 4)
                     + substring(@strId, 25, 12)
   return @strId
end消息 443,级别 16,状态 1,过程 haf_sys_id_create,第 14 行
在函数内的 'newid' 中对带副作用的或依赖于时间的运算符的使用无效。
怎么解决呢?

解决方案 »

  1.   

    newid具有不确定性
    函数的要求返回的结果是确定的
      

  2.   

    select  @strId = newid() from 表
      

  3.   

    newid()是不确定函数, sql server不支持在函数中使用不确定函数.请改用存储过程.
      

  4.   

    既然人家不让用,为啥偏要用呢!
    /**
     * 取得表的主键方法
     *
     * author tanxin 2011-11-25
     */
    create procedure [dbo].[haf_sys_id_create]
    (
      @strID char(40) output
    )
    as
    begin
    set @strId = newid()
    set @strId = 'HIT'+ substring(@strId, 1, 8) 
                         + substring(@strId, 10, 4)
                         + substring(@strId, 15, 4)
                         + substring(@strId, 20, 4)
                         + substring(@strId, 25, 12)
    end
    go
    declare @strID char(40)
    exec [haf_sys_id_create] @strID output
    select @strID
    /*
    ----------------------------------------
    HITEE63CF373CD64F4BAD519D8A6D41B340     (1 行受影响)*/
    go
    drop procedure [dbo].[haf_sys_id_create]
      

  5.   

    函数内不允许使用newid等不确定函数
    楼主要是想在insert into 里使用函数获取id,改存储过程也不行,那就别用newid了