与SQL Server中的永久表列进行比较时,临时表列丢失

时间:2014-04-25 09:38:08

标签: sql sql-server

我需要比较临时表列和永久表列以获取两个表中不存在的列,但是临时表列不存在。我相信我已经使用下面的脚本创建了它。当我使用以下查询查询临时表列时,我找不到任何列。谁知道出了什么问题?

select 
    c.name 
from 
    tempdb.sys.columns c
inner join 
    tempdb.sys.tables t ON c.object_id = t.object_id
where 
    t.name ='#authors'

表格和数据

    CREATE TABLE [dbo].[Authors](
            [Client_Id] [nvarchar](50) NOT NULL,
            [Project_Id] [nvarchar](50) NOT NULL,
            [Person_Id] [int] NOT NULL,
            [Author_Number] [int] NOT NULL,
            [Family_Name] [nvarchar](50) NULL,
            [First_Name] [nvarchar](50) NULL,
        )

        INSERT INTO Authors (Client_Id, Project_Id, Person_Id, Author_Number, Family_Name, First_Name)
        VALUES ('Client1','TEST1',12345,1,'Giust','Fede')
        INSERT INTO Authors (Client_Id, Project_Id, Person_Id, Author_Number, Family_Name, First_Name)
        VALUES ('Client1','TEST1',12345,2,'Ma','Ke')
        INSERT INTO Authors (Client_Id, Project_Id, Person_Id, Author_Number, Family_Name, First_Name)
        VALUES ('Client2','TEST2',12346,1,'Jones','Peter')
        INSERT INTO Authors (Client_Id, Project_Id, Person_Id, Author_Number, Family_Name, First_Name)
        VALUES ('Client2','TEST2',12346,2,'Davies','Bob')
        INSERT INTO Authors (Client_Id, Project_Id, Person_Id, Author_Number, Family_Name, First_Name)
        VALUES ('Client3','TEST3',12346,3,'Richards','Craig')

SELECT * INTO #authors FROM authors

代码

select 
    c2.table_name,c2.COLUMN_NAME
from 
    [INFORMATION_SCHEMA].[COLUMNS] c2
where 
    table_name = 'Authors'
    and c2.COLUMN_NAME NOT in (select c.name 
                               from tempdb.sys.columns c
                               inner join tempdb.sys.tables t ON c.object_id = t.object_id
                               where t.name ='#authors')

1 个答案:

答案 0 :(得分:1)

tempdb中表的名称不仅仅是#authors。 SQL Server更改名称。就我而言,名称为#authors____________________________________________________________________________________________________________000000000059

您可以更改查询以使用like选择正确的表格:

select c.name from tempdb.sys.columns c
    inner join tempdb.sys.tables t
 ON c.object_id = t.object_id

where t.name like '#authors%'