sql: Compare Tables

?





1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

---使用 UNION、INTERSECT 或 EXCEPT 运算符合并的所有查询必须在其目标列表中有相同数目的表达式

select * from BookInfoList

--存在不同的

select BookInfoID,BookInfoBarCode from
BookInfoList where
BookInfoStatus=1 except
select BookInventoryInfoID,BookInventoryBarCode from
InventoryBookList where
BookInventoryPlanId=1

 

--存在相同的

select BookInfoID,BookInfoBarCode from
BookInfoList where
exists (select
BookInventoryInfoID,BookInventoryBarCode from
InventoryBookList  where
InventoryBookList.BookInventoryInfoID=BookInfoList.BookInfoID and 
BookInventoryPlanId=1)

---存在不同的

select
BookInfoID,BookInfoBarCode from
BookInfoList where
not exists (select
BookInventoryInfoID,BookInventoryBarCode from
InventoryBookList  where
InventoryBookList.BookInventoryInfoID=BookInfoList.BookInfoID and 
BookInventoryPlanId=1)

select
BookInventoryInfoID,BookInventoryBarCode from
InventoryBookList  where 
exists (select
BookInfoID,BookInfoBarCode from
BookInfoList where
InventoryBookList.BookInventoryInfoID=BookInfoList.BookInfoID)

--書盤點到的書藉

select
BookInventoryInfoID,BookInventoryBarCode from
InventoryBookList  where
BookInventoryPlanId=1

--查找没盘点没有盘点到的书藉,还要考虑在借的书藉

select
* from View_BookInfoList where
BookInfoID not
in (select BookInventoryInfoID from
InventoryBookList  where
BookInventoryPlanId=1)

select
* from View_BookInfoList where
BookInfoID not
in (select BookInventoryInfoID from
InventoryBookList  where
BookInventoryPlanId=2)

-- TEMPLATE - SQL Server T-SQL compare two tables

SELECT
Label=‘Found IN BookInfoList, NOT IN InventoryBookList‘,* FROM

(SELECT
BookInfoID,BookInfoBarCode FROM
BookInfoList

 EXCEPT

 SELECT 
BookInventoryInfoID,BookInventoryBarCode FROM
InventoryBookList where
BookInventoryPlanId=1) x

UNION ALL

SELECT
Label=‘Found IN InventoryBookList, NOT IN BookInfoList‘,* FROM

(SELECT 
BookInventoryInfoID,BookInventoryBarCode FROM
InventoryBookList  where
BookInventoryPlanId=1

 EXCEPT

 SELECT
BookInfoID,BookInfoBarCode FROM
BookInfoList) y

GO

-- SQL Server T-SQL compare  tables for 2005 & 2008

SELECT
Label=‘Found IN BookInfoList, NOT IN InventoryBookList‘,* FROM

(SELECT
BookInfoID,BookInfoBarCode FROM
BookInfoList

 EXCEPT

 SELECT 
BookInventoryInfoID,BookInventoryBarCode FROM
InventoryBookList  where
BookInventoryPlanId=2) x

UNION ALL

SELECT
Label=‘Found IN InventoryBookList, NOT IN BookInfoList‘,* FROM

(SELECT 
BookInventoryInfoID,BookInventoryBarCode FROM
InventoryBookList  where
BookInventoryPlanId=2

 EXCEPT

 SELECT
BookInfoID,BookInfoBarCode FROM
BookInfoList) y

GO

--

-- SQL find rows present in both tables

SELECT
BookInfoID,BookInfoBarCode FROM
BookInfoList

INTERSECT

SELECT
BookInventoryInfoID,BookInventoryBarCode FROM
InventoryBookList  where
BookInventoryPlanId=1

---

SELECT
BookInfoID,BookInfoBarCode FROM
BookInfoList

WHERE NOT EXISTS (SELECT
BookInventoryInfoID,BookInventoryBarCode FROM
InventoryBookList  

                  WHERE
InventoryBookList.BookInventoryInfoID=BookInfoList.BookInfoID and 
InventoryBookList.BookInventoryPlanId=1)

--

-- Alternate  query - same results

SELECT
BookInfoID,BookInfoBarCode FROM
BookInfoList

  LEFT
OUTER JOIN
InventoryBookList

    ON
InventoryBookList.BookInventoryInfoID=BookInfoList.BookInfoID

WHERE InventoryBookList.BookInventoryPlanId=1

GO

select
* FROM InventoryBookList WHERE
InventoryBookList.BookInventoryPlanId=1

--

select
* FROM BookInfoList

left join InventoryBookList on
InventoryBookList.BookInventoryInfoID=BookInfoList.BookInfoID

WHERE InventoryBookList.BookInventoryPlanId=1

--存在相同的

select
* FROM InventoryBookList

left join BookInfoList on
InventoryBookList.BookInventoryInfoID=BookInfoList.BookInfoID

WHERE InventoryBookList.BookInventoryPlanId=1

---圖書註銷,報廢

IF EXISTS (SELECT
* FROM sysobjects WHERE
[name] = ‘proc_Select_BookCancellationSearch‘)

DROP PROCEDURE proc_Select_BookCancellationSearch

GO

CREATE
PROCEDURE proc_Select_BookCancellationSearch

(

 @BookInfoCancellStar Datetime,

 @BookInfoCancellEnd Datetime,

 @search nvarchar(100)

)

as

declare
@sql nvarchar(4000),@where
nvarchar(4000)

set @sql=‘select * from View_BookCancellationList where  BookCancelInfoDate>=‘‘‘+ cast(@BookInfoCancellStar as
varchar)+‘‘‘ and BookCancelInfoDate<=‘‘‘+cast(@BookInfoCancellEnd as
varchar)+‘‘‘‘

set @where=‘‘

if @Search<>‘‘

begin

 set
@where[email protected]where+‘ and (BookInfoISBN like ‘‘%‘[email protected] +‘%‘‘ or BookInfoBarCode like ‘‘%‘[email protected] +‘%‘‘ or BookCancelInfoDescription like ‘‘%‘[email protected] +‘%‘‘ or BookInfoName like ‘‘%‘[email protected] +‘%‘‘  or BookInfoRemarks  like ‘‘%‘[email protected] +‘%‘‘ or BookKindName like ‘‘%‘[email protected] +‘%‘‘  or AuthorName like ‘‘%‘[email protected] +‘%‘‘)‘

end

set @[email protected][email protected]where+‘ order by BookCancelInfoDate desc‘

print @sql

exec(@sql)

GO

?





1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

---在借和注销的书籍

IF EXISTS (SELECT
TABLE_NAME FROM
INFORMATION_SCHEMA.VIEWS

      WHERE
TABLE_NAME = ‘View_BookLendingCancellation‘)

DROP VIEW View_BookLendingCancellation

GO

CREATE VIEW View_BookLendingCancellation

AS

select BookLendingInfoID,BookLendingInfoBarCode from
BookLendingList where
BookLendingReturn is
null  --在借的書

union

select BookCancelInfoID,BookCancelBarCode from
BookCancellationList                                   --註銷的書

GO

select
* from View_BookLendingCancellation

--计算在馆的书

select
* from View_BookInfoList where
not exists (select
BookLendingInfoID,BookLendingInfoBarCode  from
View_BookLendingCancellation  where
View_BookLendingCancellation.BookLendingInfoID=View_BookInfoList.BookInfoID)

---在借和注销,盘点的书籍

select
BookLendingInfoID,BookLendingInfoBarCode from
BookLendingList where
BookLendingReturn is
null  --在借的書

union

select
BookInventoryInfoID,BookInventoryBarCode from
InventoryBookList  where
BookInventoryPlanId=1   --盤點的書

union

select
BookCancelInfoID,BookCancelBarCode from
BookCancellationList                                   --註銷的書

GO

--计算盘点问题

declare
@BookInventoryPlanId int

set @BookInventoryPlanId=1

drop table
#a

select
BookLendingInfoID,BookLendingInfoBarCode  into
#a  from
BookLendingList where
BookLendingReturn is
null

insert
into #a(BookLendingInfoID,BookLendingInfoBarCode) select
BookInventoryInfoID,BookInventoryBarCode from
InventoryBookList  where
[email protected]

insert
into #a(BookLendingInfoID,BookLendingInfoBarCode) select
BookCancelInfoID,BookCancelBarCode from
BookCancellationList   

--select * from #a

select
* from View_BookInfoList where
not exists (select
* from #a where #a.BookLendingInfoID=View_BookInfoList.BookInfoID)

select
BookLendingInfoID,BookLendingInfoBarCode from
BookLendingList where
BookLendingReturn is
null union select BookInventoryInfoID,BookInventoryBarCode from
InventoryBookList  where
[email protected]  union
select BookCancelInfoID,BookCancelBarCode from
BookCancellationList

sql: Compare Tables,布布扣,bubuko.com

时间: 2024-12-08 04:43:22

sql: Compare Tables的相关文章

Red Gate系列之一 SQL Compare 10.4.8.87 Edition 数据库比较工具 完全破解+使用教程

Red Gate系列之一 SQL Compare 10.4.8.87 Edition 数据库比较工具 完全破解+使用教程 Red Gate系列之二 SQL Source Control 3.0.13.4214 Edition 数据库版本控制器 完全破解+使用教程 Red Gate系列之三 SQL Server 开发利器 SQL Prompt 5.3.4.1 Edition T-SQL智能感知分析器 完全破解+使用教程 Red Gate系列之四 SQL Data Compare 10.2.0.88

使用Red Gate Sql Compare 数据库同步工具进行SQL Server的两个数据库的结构比较、同步

将测试版的项目同步(部署)到正式版的时候,两个数据库的结构比较与同步时,如果修改数据库的时候没有记录好修改了那些表,很难将两个数据库进行同步 RedGate Sql Compare使用简介说明: 1.比对并同步数据库中的所有对象 包括存储过程.关系.表.视图和用户定义的函数 2.报告所有差异 3.生成直接在目标数据库上运行的迁移脚本 Red gate sql compare 官网:http://www.red-gate.com/products/sql-development/sql-compa

SQL Compare 10.4.8.87 Edition 数据库比较工具 完全破解+使用教程

一.使用教程 SQL Compare是编程人员常用的比较两个数据库之间差异的工具.可以用来比较数据库里面的对象.比如存储过程,数据库中的数据,表的结构. 下面我就简单介绍一下如何使用 1.选择要比较的数据库,点"Compare Now"进行比较 2.对比两个数据库之间的差异,选择要同步的东西.红框出是对比窗口,可以看到两个数据库之间的差异 3.同步数据库中的对象,点"Synchronization Wizard" 4.开始同步,因为一直按"下一步"

SQL -&gt; Multi Tables

主键:唯一.非空,经常用于JOIN的场景.一般设为auto_increment,用于唯一标示记录. 外键:主键对应到另外一张表的字段映射,外键的存在就是为了连接多个表. 内连接:JOIN就是INNER JOIN,一张动图胜过千言万语. (外)左连接:合并所有记录,左表的记录不动,右表(字段)匹配了就合并,(字段)没匹配的置空. CROSS JOIN:无条件合并所有记录,对于联合查询很有帮助,因为没有过滤,记录是原始的. UNION:合并结果集(多sql语句),去重复记录 WITH:其实就是子查询

sql compare options

Add object existence checks Use DROP and CREATE instead of ALTER Ignore clollations Ignore database and server name in synonyms Ignore filegroups,partition schemes and partition functions Ignore fill factor and index padding Ignore user properties Ig

Red Gate - SQL Source Control实现对SQL SERVER 的源代码控制

原文地址:http://bbs.csdn.net/topics/350165431 SQL Server 一直没有一款很好的源码控制器,之前自己曾尝试自己写一个,将所有的 脚本 自动生成到某一目录下,然后将该目录提交到SVN中,进行管理,但是由于 有些存储过程中的 引号太多,没能把脚本完整的导出,最后只好搁浅 了. 昨天在逛国外论坛时 ,遇到了一款很强大的工具. 只有 20多天的使用期限,我在国外没找到 序列号,在国内发现没有研究这个工具的Programer. 所以分享给大家,一起来研究下. 我

SQL 增、删、改、查 封装函数

<?php//连接数据库 function connects($host,$username,$password,$databaseName){ $conn=mysql_connect($host,$username,$password) or die('连接数据库失败'); mysql_select_db($databaseName); mysql_query('set names utf8'); return $conn; }//查询 function Select_tb($tables,$

比较sql server两个数据库

http://opendbdiff.codeplex.com/ http://www.red-gate.com/ 有SQL Compare和SQL Prompt 开源的? http://www.codeproject.com/Articles/205011/SQL-Server-Database-Comparison-Tool

第57课:Spark SQL on Hive配置及实战

1,首先需要安装hive,参考http://lqding.blog.51cto.com/9123978/1750967 2,在spark的配置目录下添加配置文件,让Spark可以访问hive的metastore. [email protected]:/usr/local/spark/spark-1.6.0-bin-hadoop2.6/conf# vi hive-site.xml <configuration> <property>   <name>hive.metast