日期:2014-05-17 浏览次数:20630 次
--TA表 表中数据有160万条记录
create table TA
(BH VARCHAR(10) NULL,
HW VARCHAR(10) NULL,
SL INT
)
--TB表 表中数据有10万条记录
CREATE TABLE TB
(RQ VARCHAR(10 NULL),
BH VARCHAR(10) NULL,
HW VARCHAR(10) NULL,
SL INT
)
SELECT BH,HW,SL FROM TA WHERE
not exists (select 1 from TB where TB.BH+TB.HW=TA.BH+TA.BH);
SELECT BH,HW,SL FROM TA WHERE
not exists (select 1 from TB where TB.BH=TB.HW and TA.BH=TA.BH);
select BH,HW,SL from TA
where not exists (select 1 from TB where TB.BH+TB.WH=TA.BH+TA.WH)
--if object_id('TA') is not null
-- drop table TA
--Go
--create table TA
--(
--BH VARCHAR(10) NULL,
--HW VARCHAR(10) NULL,
--SL INT
--)
--if object_id('TB') is not null
-- drop table TB
--Go
--CREATE TABLE TB
--(
--RQ VARCHAR(10) NULL,
--BH VARCHAR(10) NULL,
--HW VARCHAR(10) NULL,
--SL INT
--)
IF OBJECT_ID('tempdb..#tmp_TA') IS NOT NULL
DROP TABLE #tmp_TA
SELECT *,BH+HW AS IDEN INTO #tmp_TA FROM TA
IF OBJECT_ID('tempdb..#tmp_TB') IS NOT NULL
DROP TABLE #tmp_TB
SELECT *,BH+HW AS IDEN INTO #tmp_TB FROM TB
CREATE NONCLUSTERED INDEX #tmp_TA_IDEN ON #tmp_TA(IDEN)
CREATE NONCLUSTERED INDEX #tmp_TB_IDEN ON #tmp_TB(IDEN)
SELECT BH,HW,SL FROM #tmp_TA AS a WHERE NOT EXISTS(SELECT 1 FROM #tmp_TB AS x WHERE a.IDEN=x.IDEN)