2013-05-14 47 views
2

我有一個SSIS,爲了工作必須有一些特定的表。因此,我創建了一個「Execute Sql Task」包,它首先驗證表是否存在並具有特定數量的列,如果不存在,則刪除現有表,然後重新創建所需的列。 底線是,我的SQL腳本看起來像這樣(在同一個「執行SQL任務」包):表刪除和重新創建在SSIS包

declare @nr integer; 
set @nr=(select COUNT (*) 
     from dbo.syscolumns 
     where id= (select id 
        from dbo.sysobjects 
        where id = object_id(N'[bogdan].[dbo].[my_table]') 
        and OBJECTPROPERTY(id, N'IsUserTable') = 1)); 

if (@nr<>10 and @nr<>0) /*to verify if my_table exists and has the required number of  colums - 10 */ 
    drop table [bogdan].[dbo].[my_table] 
go 

declare @nr1 integer; 
set @nr1=(select count(*) 
      from dbo.sysobjects 
      where id = object_id(N'[bogdan].[dbo].[My_table]') 
      and OBJECTPROPERTY(id, N'IsUserTable') = 1); 
if @nr1=0 
create table bogdan.dbo.my_table (....) on [PRIMARY] 
go 

/*end of script*/ 

的問題是,即使我已經下降(從上面的例子MY_TABLE)表,有仍然是sysobjects中名爲「my_table」的對象。 @ nr1變量的值爲1,而不是0,因此不會創建具有所需結構的表,因爲if子句不會被執行。

任何提示?

感謝,

+0

你確定你的表被丟棄?如果'@nr = 10'。如果'@nr不是零'而'@nr不是10',那麼你只會做出放棄。 – 2013-05-14 11:03:16

+0

是的,我確定。我從Sql Server中放棄了自己。如果@ nr = 10,它應該不做任何事情,因爲它存在並且它有正確的列。 – BogdanM 2013-05-14 11:09:17

+0

@BogdanM:你的查詢是完美的。它應該工作。如果可能請發佈您的原始查詢。這可能會幫助我們找到問題。 – Maximus 2013-05-14 12:27:13

回答

1

試試這個,你就會知道,如果你的表實際上是被丟棄:

declare @nr integer; 
set @nr=(select COUNT (*) 
     from dbo.syscolumns 
     where id= (select id 
        from dbo.sysobjects 
        where id = object_id(N'[bogdan].[dbo].[my_table]') 
        and OBJECTPROPERTY(id, N'IsUserTable') = 1)); 

if (@nr<>10 and @nr<>0) 
/*to verify if my_table exists and has the required number of  colums - 10 */ 
    begin 
    drop table [bogdan].[dbo].[my_table] 
    set @nr=0 
    end 

if @nr=0 
create table bogdan.dbo.my_table (....) on [PRIMARY] 
go 

拉吉

相關問題