Sql server 检查联合查询中是否存在SQL表

Sql server 检查联合查询中是否存在SQL表,sql-server,sql-server-2008,if-statement,Sql Server,Sql Server 2008,If Statement,我该怎么做?我正在尝试在我的表上运行下面的查询。但是首先我想检查数据库是否存在。我试图运行以下语法,但它给了我一个编译错误: '关键字'IF'附近的语法不正确。' if object_id('table1') Is not null Select empName, empStoreNum, empSales, 'East' As SalesDistrict FROM store1 UNION ALL if object_id('table2') is not null Select empNam

我该怎么做?我正在尝试在我的表上运行下面的查询。但是首先我想检查数据库是否存在。我试图运行以下语法,但它给了我一个编译错误: '关键字'IF'附近的语法不正确。'

if object_id('table1') Is not null
Select empName, empStoreNum, empSales, 'East' As SalesDistrict
FROM store1
UNION ALL
if object_id('table2') is not null
Select empName, empStoreNum, empSales, 'East' As SalesDistrict
FROM store2
UNION ALL
if object_id('table3') is not null
Select empName, empStoreNum, empSales, 'East' As SalesDistrict
FROM store3

你不能。查询中的表必须存在。我可以想出两种方法来解决你的问题。一个使用临时表,另一个使用动态SQL。第一种方法如下所示:

declare @t table (empName varchar(255), empStoreNum int, empSales money);
if object_id('table1') Is not null
    insert into @t(empName, empStoreNum, empSales)
        Select empName, empStoreNum, empSales, 'East' As SalesDistrict
        FROM store1;

if object_id('table2') is not null
    insert into @t(empName, empStoreNum, empSales)
        Select empName, empStoreNum, empSales, 'East' As SalesDistrict
        FROM store2;

if object_id('table3') is not null
    insert into @t(empName, empStoreNum, empSales)
        Select empName, empStoreNum, empSales, 'East' As SalesDistrict
        FROM store3;

select *
from @t;

你不能。查询中的表必须存在。我可以想出两种方法来解决你的问题。一个使用临时表,另一个使用动态SQL。第一种方法如下所示:

declare @t table (empName varchar(255), empStoreNum int, empSales money);
if object_id('table1') Is not null
    insert into @t(empName, empStoreNum, empSales)
        Select empName, empStoreNum, empSales, 'East' As SalesDistrict
        FROM store1;

if object_id('table2') is not null
    insert into @t(empName, empStoreNum, empSales)
        Select empName, empStoreNum, empSales, 'East' As SalesDistrict
        FROM store2;

if object_id('table3') is not null
    insert into @t(empName, empStoreNum, empSales)
        Select empName, empStoreNum, empSales, 'East' As SalesDistrict
        FROM store3;

select *
from @t;

在我的示例中,我正在执行一条sql语句。它检查数据库“AdventureWorksDW2012” 若数据库存在,它将检查表“DimDate”。如果该表存在,则执行一个示例选择语句

我使用这个示例是因为您没有提供数据库名称

Use master
Go

if Exists(
Select * from sys.databases where name = 'AdventureWorksDW2012')
begin
    Declare @sql  varchar(max) = 
    'use [AdventureWorksDW2012]

    if Exists(
    Select 1 from sys.tables where name = ''DimDate''
    )
    begin
        Select 1
    End
    '

    Exec (@sql)
End
结果

--When AdventureWorksDW2012 exists and it contains DimDate


希望这有帮助。

在我的示例中,我正在执行一条sql语句。它检查数据库“AdventureWorksDW2012” 若数据库存在,它将检查表“DimDate”。如果该表存在,则执行一个示例选择语句

我使用这个示例是因为您没有提供数据库名称

Use master
Go

if Exists(
Select * from sys.databases where name = 'AdventureWorksDW2012')
begin
    Declare @sql  varchar(max) = 
    'use [AdventureWorksDW2012]

    if Exists(
    Select 1 from sys.tables where name = ''DimDate''
    )
    begin
        Select 1
    End
    '

    Exec (@sql)
End
结果

--When AdventureWorksDW2012 exists and it contains DimDate


希望这能有所帮助。

3在临时表中插入一个Select,然后我会尝试使用以下步骤:从store_proc('store3')@Igos中选择empName、empStoreNum、empSales、'East'作为SalesDistrict。我不遵循您的示例?想法是使用函数从动态sql返回数据。通过这种方式,您可以从任何表中获取数据:从GetStore('store3')中选择empName、empStoreNum、empSales、SalesDistrict 3插入到临时表中,然后一次选择我将尝试使用以下过程:从store_proc('store3')中选择empName、empStoreNum、empSales、'East'作为SalesDistrict@Igos我不遵循您的示例?想法是使用函数从动态sql返回数据。通过这种方式,您可以从任何表中获取数据:从GetStore('store3')ohh中选择empName、empStoreNum、empSales、SalesDistrict。。迟到了几分之一秒:)哦。。迟到几秒钟:)