IF statement in Data Flow

时间:2016-05-17 11:17:28

标签: sql-server tsql ssis

In OLE DB Source I use below code (SQL command from variable - I get table name from variable) to handle nonexisting tables (I have to iterate through all tables list and get data from existing tables):

IF (EXISTS (SELECT * 
    FROM INFORMATION_SCHEMA.TABLES
    WHERE TABLE_NAME = 'KLIENT_PRZEKROCZENIA'))

BEGIN 
select max(REPO_DATE) REPO_DATE_SQLServer from KLIENT_PRZEKROCZENIA END 
ELSE 
select NULL  REPO_DATE_SQLServer

It's work in MS, but in SSIS I get:

TITLE: Microsoft Visual Studio

Error at max(REPO_DATE) [SQL Server [122]]: SSIS Error Code DTS_E_OLEDBERROR. An OLE DB error has occurred. Error code: 0x80004005. An OLE DB record is available. Source: "Microsoft SQL Server Native Client 10.0" Hresult: 0x80004005 Description: "Invalid object name 'KLIENT_PRZEKROCZENIA'.".

Error at max(REPO_DATE) [SQL Server [122]]: Unable to retrieve column information from the data source. Make sure your target table in the database is available.

------------------------------ ADDITIONAL INFORMATION:

Exception from HRESULT: 0xC020204A (Microsoft.SqlServer.DTSPipelineWrap)

This result is merged with another OLE DB Source (this source have all tables) in the same Data Flow:

Flow

How to handle missing tables?

3 个答案:

答案 0 :(得分:2)

我认为以下内容应该有效。

IF NOT EXISTS (SELECT * 
               FROM INFORMATION_SCHEMA.TABLES
               WHERE TABLE_NAME = 'KLIENT_PRZEKROCZENIA')
   SELECT CAST(NULL AS DATE) AS REPO_DATE_SQLServer
ELSE
   EXEC ('SELECT MAX(REPO_DATE) REPO_DATE_SQLServer from KLIENT_PRZEKROCZENIA')

答案 1 :(得分:0)

这应该告诉SQL Server数据库中是否存在用户表:

if (exists (select * from sysobjects where type = 'U' and name = 'KLIENT_PRZEKROCZENIA'))

答案 2 :(得分:0)

if exists (
select 1 from INFORMATION_SCHEMA.TABLES where table_name = 'Your table name')
Begin 
    print 'its here'
end
else
Begin
    print 'its not here'
end