仅在表中不存在记录时,才需要在表中插入记录。
无论如何,我想返回id列(如果找到记录)或最后使用的id列(如果记录是新记录)。
if exists (select id from DataElement where bytepos=0 and bitpos=0 and byteorder=0 )
select id from DataElement where bytepos=0 and bitpos=0 and byteorder=0
else
begin
insert into DataElement values ('SID','',0,0,0,8,129);
select scope_identity() as id
end
这个脚本可以,但是我想避免两次执行SELECT操作。
如果我只回来
select id
我收到错误“无效列'id'”。
如果有一种方法可以存储第一个选择的返回记录并返回它,如果有的话?
使用变量:
DECLARE @id int
SELECT TOP 1 @id = id from DataElement where bytepos=0 and bitpos=0 and byteorder=0
IF @id IS NULL
begin
insert into DataElement values ('SID','',0,0,0,8,129);
select @id = scope_identity()
end
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句