SQL Server:检查表是否存在

时间:2015-11-27 08:43:28

标签: sql-server

我正在尝试检查通过openquery访问的表是否存在以下代码,但它不起作用:

    IF EXISTS (SELECT * FROM OPENQUERY(SADATABASE, ' select     *
                           from xx_201509_T                           ' 
            ) )

2 个答案:

答案 0 :(得分:0)

尝试这样的事情:

IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[xx_201509_T]') AND type in (N'U'))
***DO WHATEVER HERE***

答案 1 :(得分:-1)

SELECT OBJECT_ID('xx_201509_T', 'U') 

如果是NULL,则表格不存在

编辑:所以在你的openquery中使用它是这样的:

IF (SELECT objectID FROM OPENQUERY(SADATABASE, 'SELECT OBJECT_ID(''xx_201509_T'', ''U'') as objectID') )  IS NOT NULL 
BEGIN
 print 'xx_201509_T TABLE EXISTS'
END