惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Hugging Face - Blog
Hugging Face - Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
人人都是产品经理
人人都是产品经理
Microsoft Azure Blog
Microsoft Azure Blog
Engineering at Meta
Engineering at Meta
B
Blog RSS Feed
大猫的无限游戏
大猫的无限游戏
博客园_首页
雷峰网
雷峰网
V
Visual Studio Blog
爱范儿
爱范儿
A
About on SuperTechFans
量子位
N
Netflix TechBlog - Medium
Microsoft Security Blog
Microsoft Security Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
MongoDB | Blog
MongoDB | Blog
U
Unit 42
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
月光博客
月光博客
S
SegmentFault 最新的问题
J
Java Code Geeks
H
Hackread – Cybersecurity News, Data Breaches, AI and More

博客园 - nerozhang

NHibernate小结(一) 数据库字符串处理 数据库中的字符串操作 通过查询语句链接到远程数据库 得到数据库表的结构 NHibernate Quick Start Guide 唐诗赏析--唐诗的起源,发展 PetShop 4.0的缓存处理 AggregateCacheDependency、CacheDependency、SqlCacheDependency Asp.net 2.0和PetShop4 的缓存示例 自定义 HttpModule 示例 在global.asax中实现URL重写(应用) Community Starter Kit - 结构 Community Starter Kit - 功能介绍 Community Starter Kit -简介 Petshop 4.0学习-MasterPage.master文件 PetShop 4.0学习-数据访问层之数据库访问设计 PetShop 4.0学习--登录以及注册功能的分析 PetShop 4.0学习--体系架构 PetShop 4.0学习--业务功能介绍
从两张表中取出不一致的数据
nerozhang · 2008-03-07 · via 博客园 - nerozhang

DECLARE @verify TABLE (h883id char(9not null)
DECLARE @outmemo TABLE (h883id char(9not null)
DECLARE @t1 TABLE (h883id char(9not null)

declare @strCorpID char(4)
declare @StartDate DateTime
declare @EndDate DateTime
declare @count int
set @strCorpID='C275'
--set @StartDate='1900-04-01'
--
set @EndDate = '2008-02-03'

select @startdate=startdate,@enddate=enddate from verifymain
where corpid=@strcorpid
--print @startdate
--
print @enddate

insert into @outmemo 
select distinct h883id from (
--成品报关出区:
select om.h883id,'成品' as goodstype,--ps.oriproductid,
ps.productpartno,ps.hscode,ps.chinesename,ps.spec,ps.type,
sum(oub.quantity) sumquantity ,ps.unit,
sum(oub.net) sumnet,sum(oub.gross) sumgross,sum(oub.price) sumamount,oub.currency,'报关出区' as billtype
from outmemo om join outunitebefore oub 
on om.outmemoid=oub.outmemoid and om.approve='1' and om.cancel='0'
    
join productstocks ps 
on ps.productstocksid=oub.productstocksid 
where om.corpid=@strCorpid and convert(char(10),om.approvetime,120)>=@StartDate 
and convert(char(10),om.approvetime,120)<=@EndDate 
group by om.h883id,ps.oriproductid,ps.productpartno,ps.hscode,
ps.chinesename,ps.spec,ps.type,ps.unit,oub.currency)t1

insert into @verify
select distinct h883id from (
select h883id from verify where corpid=@strcorpid and billtype='outmemo'
union all 
select h883id from verifydetail where corpid=@strcorpid and billtype='outmemo'
)t2

--获得@verify表中的记录数
select @count=count(*from @verify
print 'verify:'+cast(@count as char(10))+''

--获得@outmemo表中的记录数
select @count=count(*from @outmemo
print 'outmemo:'+cast(@count as char(10))+''

--取得两张表不一致的记录
--
首先取得两张表中共同的数据
insert into @t1
select v.h883id from @verify v join @outmemo o
on o.h883id=v.h883id

--得到两张表中不一致的记录
select h883id,'verify' as billtype from @verify
where h883id not in(select h883id from @t1)
union all
select h883id,'outmemo' as billtype from @outmemo
where h883id not in(select h883id from @t1)

使用了表变量,首先把需要比较的记录放到表变量中。然后根据表变量中的值查找出不一致的记录。