【发布时间】:2015-09-28 09:03:57
【问题描述】:
我的存储过程有 3 个参数,我正在传递 (@nameP, @idP, @dateP)
并将数据插入#myTemp 表中
然后我用
select *
into dbo.realTable
from #myTemp
然后我想根据dateP 和idP 过滤掉任何已经存在的数据(在dbo.FinalTable 中):
insert into dbo.FinalTable
select * from dbo.realTable
where not exists (select * from dbo.FinalTable
where idP = @idP
and dateP = @dateP)
drop table dbo.realTable
当我执行我的程序时,数据被附加到表中。问题是如果我输入相同的idP 并为相同的dateP 再次执行它,它不应该插入任何东西,但它会插入。我认为问题可能出在insert into 部分。
编辑:
如果我从 where 子句中删除 and dateP = @dateP,这将非常有效)
ps:谢谢大家的回答,即使在我的情况下我只需要做我上面写的,我从你的回答中学到了
【问题讨论】:
-
查看
MERGE命令。另一点可能是SELECT DISTINCT。广告请提供示例数据和预期输出, -
当我运行你的代码时它不会插入第二次。
-
@name 没有在任何地方提及。那是怎么用的?
-
@name 实际上是我正在连接的链接数据库
标签: sql sql-server stored-procedures sql-server-2012