日期:2014-05-18 浏览次数:20458 次
CREATE TABLE ORDERS ( OrderID VARCHAR(50), MemberID VARCHAR(100), CREATEON DATETIME ) INSERT INTO Orders SELECT 'FP1','1','2012-05-12' UNION SELECT 'FP2','1','2012-05-13' UNION SELECT 'FP3','1','2012-05-14' UNION SELECT 'FP4','2' ,'2012-05-12' CREATE TABLE OrderShipping ( OrderID VARCHAR(50) , Address1 VARCHAR(50), Address2 VARCHAR(50), CountryName VARCHAR(50), ProviNce VARCHAR(50) ) INSERT INTO OrderShipping SELECT 'FP1', '金华1','金华','中国1','浙江' UNION SELECT 'FP2', '金华1','金华','中国1','浙江' UNION SELECT 'FP3', '金华2','金华','中国2','浙江' UNION SELECT 'FP4', '深圳','深圳','中国2','广东' if OBJECT_ID('pro_test')is not null drop proc pro_test go create proc pro_test(@menberid int) as select px=ROW_NUMBER()over(order by getdate()), t.OrderID,n.Address1,n.Address2,n.CountryName,n.ProviNce into #test from(select OrderID,MemberID,count(OrderID)over(partition by MemberID) as Total from ORDERS)t inner join OrderShipping n on t.OrderID=n.OrderID where t.Total>=2 and t.MemberID=@menberid declare @px int select @px=max(px) from #test if (select ProviNce from #test where px=@px) <>(select ProviNce from #test where px=@px-1) print '地址不一致为异常' else print '地址一致为正常' go exec pro_test 1 /* 地址一致为正常 */
------解决方案--------------------
CREATE TABLE ORDERS ( OrderID VARCHAR(50), MemberID VARCHAR(100), CREATEON DATETIME ) INSERT INTO Orders SELECT 'FP1','1','2012-05-12' UNION SELECT 'FP2','1','2012-05-13' UNION SELECT 'FP3','1','2012-05-14' UNION SELECT 'FP4','2' ,'2012-05-12' CREATE TABLE OrderShipping ( OrderID VARCHAR(50) , Address1 VARCHAR(50), Address2 VARCHAR(50), CountryName VARCHAR(50), ProviNce VARCHAR(50) ) INSERT INTO OrderShipping SELECT 'FP1', '金华1','金华','中国1','浙江' UNION SELECT 'FP2', '金华1','金华','中国1','浙江' UNION SELECT 'FP3', '金华2','金华','中国2','浙江' UNION SELECT 'FP4', '深圳','深圳','中国2','广东' if OBJECT_ID('pro_test')is not null drop proc pro_test go create proc pro_test(@menberid int) as select px=ROW_NUMBER()over(order by getdate()), t.OrderID,n.Address1,n.Address2,n.CountryName,n.ProviNce into #test from(select OrderID,MemberID,count(OrderID)over(partition by MemberID) as Total from ORDERS)t inner join OrderShipping n on t.OrderID=n.OrderID where t.Total>=2 and t.MemberID=@menberid declare @px int select @px=max(px) from #test if (select CountryName+ProviNce+Address1+Address2 from #test where px=@px) <>(select CountryName+ProviNce+Address1+Address2 from #test where px=@px-1) print '地址不一致为异常' else print '地址一致为正常' go exec pro_test 1 /* 地址不一致为异常 */ --我只是比较了最后一个Province,没比较全部地址,这下应该对了吧
------解决方案--------------------