在一个 table 中插入 IF 记录 EXISTS 而在另一个中不插入 EXISTS
Insert IF record EXISTS in one table and not EXISTS in other
IF EXISTS 问题
我有两个表
create table #Table1
(
DateID date, Shop int, MAC int, Stock int, Transit int
)
INSERT INTO #Table1 values ('01.01.2014', 1, 2, 2,3)
INSERT INTO #Table1 values ('01.04.2014', 1, 2, 2,3)`
create table #Table2
(
DateID date, Shop int, MAC int, OnHand int
)
INSERT INTO #Table2 values ('01.01.2014', 1, 2, 2)
INSERT INTO #Table2 values ('01.01.2014', 2, 3, 1)
INSERT INTO #Table2 values ('01.01.2014', 3, 1, 4)
INSERT INTO #Table2 values ('01.02.2014', 1, 2, 5)
然后我需要插入 Table 1 如果它不存在于 Table 1 DateID, Shop , MAC 但存在于 Table 2
我有这个说法
DECLARE @Date date = '01.02.2014'
IF EXISTS (SELECT a.DateID, a.Shop, a.MAC
FROM #Table1 a
INNER JOIN #Table2 b ON a.Shop = b.Shop
AND a.MAC = b.MAC
AND a.DateID = b.DateID
WHERE a.DateID = @Date)
INSERT INTO #Table1 (DateID, Shop, MAC, Stock)
select
@Date, a.Shop, a.MAC , sum (OnHand)
from
#Table2 a
where
DateID <= @Date
group by
a.Shop, a.MAC
order by
1
other IF
else
它正在工作,但因为它有
IF EXISTS
如果我使用
,它总是正确的
IF NOT EXISTS
它总是假的,这部分永远不会执行。
请帮忙!
您可以在 sql 语句而不是 if 语句中执行此操作:
insert into #Table1
(DateID, Shop, MAC, Stock)
select @Date, a.Shop, a.MAC , sum(OnHand)
from #Table2 a
left join #Table1 b
ON a.Shop = b.Shop
AND a.MAC = b.MAC
AND a.DateID = b.DateID
where b.Shop is null
group by a.Shop, a.MAC
order by 1
如果你需要一个 IF stmt - 做这样的事情:
set @RowsToInsert = 0
select @RowsToInsert = count(*)
from #Table2 a
left join #Table1 b
ON a.Shop = b.Shop AND a.MAC = b.MAC AND a.DateID = b.DateID
where b.Shop is null
IF @RowsToInsert > 0 ... you will need to put insert logic here
IF @RowsToInsert = 0 ... no rows are missing, put update or other logic here
我通过创建 NONCLUSTERED INDEX
并设置 IGNORE_DUP_KEY = ON
来修复它
create table #Table1 (
DateID date, Shop int, MAC int, Stock int, Transit int )
CREATE UNIQUE NONCLUSTERED INDEX IX_RecvID ON #F_STOCK (DateID,Shop ,MAC) WITH (IGNORE_DUP_KEY = ON)
如果您想使用 EXISTS
那么这可能有效:
INSERT INTO #Table1 (DateID, Shop, MAC, Stock)
SELECT @Date,
A.Shop,
A.MAC ,
SUM (OnHand)
FROM #Table2 A
WHERE NOT EXISTS
(
SELECT 1
FROM #Table1 B
WHERE A.Shop = B.Shop AND
A.MAC = B.MAC AND
A.DateID = B.DateID
)
AND
A.DateID <= @Date
GROUP BY
A.Shop,
A.MAC
IF EXISTS 问题
我有两个表
create table #Table1
(
DateID date, Shop int, MAC int, Stock int, Transit int
)
INSERT INTO #Table1 values ('01.01.2014', 1, 2, 2,3)
INSERT INTO #Table1 values ('01.04.2014', 1, 2, 2,3)`
create table #Table2
(
DateID date, Shop int, MAC int, OnHand int
)
INSERT INTO #Table2 values ('01.01.2014', 1, 2, 2)
INSERT INTO #Table2 values ('01.01.2014', 2, 3, 1)
INSERT INTO #Table2 values ('01.01.2014', 3, 1, 4)
INSERT INTO #Table2 values ('01.02.2014', 1, 2, 5)
然后我需要插入 Table 1 如果它不存在于 Table 1 DateID, Shop , MAC 但存在于 Table 2
我有这个说法
DECLARE @Date date = '01.02.2014'
IF EXISTS (SELECT a.DateID, a.Shop, a.MAC
FROM #Table1 a
INNER JOIN #Table2 b ON a.Shop = b.Shop
AND a.MAC = b.MAC
AND a.DateID = b.DateID
WHERE a.DateID = @Date)
INSERT INTO #Table1 (DateID, Shop, MAC, Stock)
select
@Date, a.Shop, a.MAC , sum (OnHand)
from
#Table2 a
where
DateID <= @Date
group by
a.Shop, a.MAC
order by
1
other IF
else
它正在工作,但因为它有
IF EXISTS
如果我使用
,它总是正确的IF NOT EXISTS
它总是假的,这部分永远不会执行。
请帮忙!
您可以在 sql 语句而不是 if 语句中执行此操作:
insert into #Table1
(DateID, Shop, MAC, Stock)
select @Date, a.Shop, a.MAC , sum(OnHand)
from #Table2 a
left join #Table1 b
ON a.Shop = b.Shop
AND a.MAC = b.MAC
AND a.DateID = b.DateID
where b.Shop is null
group by a.Shop, a.MAC
order by 1
如果你需要一个 IF stmt - 做这样的事情:
set @RowsToInsert = 0
select @RowsToInsert = count(*)
from #Table2 a
left join #Table1 b
ON a.Shop = b.Shop AND a.MAC = b.MAC AND a.DateID = b.DateID
where b.Shop is null
IF @RowsToInsert > 0 ... you will need to put insert logic here
IF @RowsToInsert = 0 ... no rows are missing, put update or other logic here
我通过创建 NONCLUSTERED INDEX
并设置 IGNORE_DUP_KEY = ON
create table #Table1 (
DateID date, Shop int, MAC int, Stock int, Transit int )
CREATE UNIQUE NONCLUSTERED INDEX IX_RecvID ON #F_STOCK (DateID,Shop ,MAC) WITH (IGNORE_DUP_KEY = ON)
如果您想使用 EXISTS
那么这可能有效:
INSERT INTO #Table1 (DateID, Shop, MAC, Stock)
SELECT @Date,
A.Shop,
A.MAC ,
SUM (OnHand)
FROM #Table2 A
WHERE NOT EXISTS
(
SELECT 1
FROM #Table1 B
WHERE A.Shop = B.Shop AND
A.MAC = B.MAC AND
A.DateID = B.DateID
)
AND
A.DateID <= @Date
GROUP BY
A.Shop,
A.MAC