菜鸟问题,SQL自增主键,无法用函数生成
我的主键是10位字符型的,我想用函数生成一个自增主键,却不行
create table IntKey ( KeyValue Int);
insert into IntKey values(0);
--主键生成函数,主键10位字符
create function GetKey returns char(10)
as
begin
declare @KeyValue Int
update IntKey set @KeyValue = KeyValue = KeyValue + 1
return 'OP' + right('00000000' + convert(varchar(8),@KeyValue),8)
end
在SQL 2005内运行出错:
消息 443,级别 16,状态 15,过程 GetKey,第 7 行
在函数内的 'UPDATE' 中对带副作用的或依赖于时间的运算符的使用无效。