declare
@copyFromId bigint =234
,@copyToId bigint =229
as
\-----------New Working ------------
\--first temp table start--
select
FareId, FromZoneId,ToZoneId,Price,CompanyRate
into #temp1
from
Fare_ZoneWisePricing
where
FareId =@copyFromId
\--End first temp table --
\--start second temp table--
select
FareId, FromZoneId,ToZoneId,Price,CompanyRate
into #temp2
from
Fare_ZoneWisePricing
where FareId=@copyToId
\--End second temp table--
\---------------Main Operation start--------------------
insert into Fare_ZoneWisePricing(fareid,fromzoneid,tozoneid,price,companyrate)
SELECT
@copyToId
,FromZoneId
,ToZoneId
,Price
,CompanyRate
FROM
\#temp1 a
WHERE not exists (
SELECT
FromZoneId
,ToZoneId
,Price
,CompanyRate
FROM
\#temp2 b
where
a.FromZoneId =b.FromZoneId and a.ToZoneId =b.ToZoneId
GROUP BY
FromZoneId
,ToZoneId
,Price
,CompanyRate
)
\------------------End Main Operation---------------------
drop table #temp1
drop table #temp2
declare
@copyFromId bigint =234
,@copyToId bigint =229
as
-----------New Working ------------
--first temp table start--
select
FareId, FromZoneId,ToZoneId,Price,CompanyRate
into #temp1
from
Fare_ZoneWisePricing
where
FareId =@copyFromId
--End first temp table --
--start second temp table--
select
FareId, FromZoneId,ToZoneId,Price,CompanyRate
into #temp2
from
Fare_ZoneWisePricing
where FareId=@copyToId
--End second temp table--
---------------Main Operation start--------------------
insert into Fare_ZoneWisePricing(fareid,fromzoneid,tozoneid,price,companyrate)
SELECT
@copyToId
,FromZoneId
,ToZoneId
,Price
,CompanyRate
FROM
#temp1 a
WHERE not exists (
SELECT
FromZoneId
,ToZoneId
,Price
,CompanyRate
FROM
#temp2 b
where
a.FromZoneId =b.FromZoneId and a.ToZoneId =b.ToZoneId
GROUP BY
FromZoneId
,ToZoneId
,Price
,CompanyRate
)
------------------End Main Operation---------------------
drop table #temp1
drop table #temp2
1条答案
按热度按时间6ie5vjzr1#
不需要
EXISTS (SELECT x, y, z FROM table WHERE correlation GROUP BY x, y, z)
,EXISTS()只需要有任何一行匹配相关性,因此您只需要EXISTS (SELECT * FROM table WHERE correlation)
。将所有内容都插入临时表中(只是内联查询)也可能没有什么好处 (事实上,会有很大的开销)。
这意味着您的脚本可以简化为...
下一个优化是确保
(FareId, FromZoneId, ToZoneId)
上有索引。