我听说过使用声明式编程,但我不知道如何在sql server 2005中做这样的事情 (我输入的速度非常快,所以我不确定所有语法是否正确,但我认为你会明白我在寻找什么)
declare curs cursor for
select @Name, @Description, @Id FROM TableA
open curs
while(1=1)
begin
fetch next from curs into
@Name,
@Description,
@Id
if(@@fetch_status<>0) break
set @recordCount = (SELECT COUNT(*) As RecordCount FROM Class1 WHERE
Class1Id = @Id)
if(@recordCount > 0)
begin
if(@Name = 'BAD NAME') CONTINUE
UPDATE Class1 SET
Name = @Name
, Description = @Description
WHERE Class1Id = @Id
end
else
begin
INSERT INTO Class1 (Class1Id, Name, Description)
VALUES (@Id, @Name, @Description)
end
end
close curs
deallocate curs
答案 0 :(得分:2)
UPDATE Class1
SET Name = t.Name,
Description = t.Description
FROM Class1 c
JOIN TableA t ON c.id=t.id
WHERE t.name <> 'BAD NAME'
INSERT INTO Class1 (Class1Id, Name, Description)
select t.Id, t.Name, t.Description
FROM TableA t
LEFT JOIN Class1 c on t.id=c.id
where c.id IS NULL