標籤:general insert result update 資料來源
Execute Sql Task組件是一個非常有用的Control Flow Task,可以直接執行SQL語句,例如,可以執行資料更新命令(update,delete,insert),也可以執行select查詢語句,返回結果集,結果集可以是一行,也可以是多行。
一,General 選項卡
1,返回結果集(Result Set)
None:表示不返回結果,在執行Update,delete或insert命令時,使用該選項;
Single row:返回單行結果,可以在Result Set 選項卡中,將結果集返回到1個或多個變數中
Full result set:返回多行結果,結果集儲存在object對象中
650) this.width=650;" src="http://images.cnitblog.com/blog2015/628084/201504/141101223545001.jpg" style="margin:0px;padding:0px;border:0px;" />
2,組件執行的SQL命令(SQL Statement)
ConnectionType:串連到資料來源的連結類型,如果是OLEDB連結,選中OLE DB;
Connection:連結字串
SQLSourceType:SQL資料來源的類型,共有三個選項:Direct Input,File Connection 和 Variable。Direct Input表示:直接輸入SQL命令;FileConnection表示:將SQL命令儲存在.sql檔案中;Variable表示:SQL命令儲存在SSIS的變數中,如果Execute SQL Task執行的SQL命令是動態變化的,使用Variable選項較好;
SQLStatement:要執行的SQL語句,根據SQLSourceType的不同,有三種不同的值:SQL語句,.sql檔案路徑,或SSIS變數;
3,樣本:SQLSourceType=Variable
650) this.width=650;" src="http://images.cnitblog.com/blog2015/628084/201504/141141571511291.jpg" style="margin:0px;padding:0px;border:0px;" />
650) this.width=650;" src="http://images.cnitblog.com/blog2015/628084/201504/141142075737740.jpg" style="margin:0px;padding:0px;border:0px;" />
二,Expressions選項卡
SSIS Package的屬性,既可以在General Tab中手動設定,也可以在Expressions Tab中配置,將配置屬性的值儲存到變數中,可以動態控制包的執行,
樣本,將SqlStatementSource的值儲存在變數中,結果和SqlSourceType=Variable是一樣的,只不過實現方式不同,並且SSIS在執行過程中,使用Expressions的屬性覆蓋General中配置的屬性值。
650) this.width=650;" src="http://images.cnitblog.com/blog2015/628084/201504/141143185739516.jpg" style="margin:0px;padding:0px;border:0px;" />
三,Result Set選項卡
如果在General選項卡中,將Result Set設定為Singel Row,可以將結果值儲存在變數中
650) this.width=650;" src="http://images.cnitblog.com/blog2015/628084/201504/141148205105305.jpg" style="margin:0px;padding:0px;border:0px;" />
四,Parameter Mapping 選項卡
如果sql命令在執行的過程中需要傳遞參數,可以在Parameter Mapping 選項卡中進行配置。如果使用的OLE DB 連結,需要使用 ?代表第一個參數,並且在Parameter Mapping 中將parameter name設定為參數的序號,第一個? 的序號是0,即序號從0依次遞增
650) this.width=650;" src="http://images.cnitblog.com/blog2015/628084/201504/141306123704660.jpg" style="margin:0px;padding:0px;border:0px;" />
650) this.width=650;" src="http://images.cnitblog.com/blog2015/628084/201504/141305057924367.jpg" style="margin:0px;padding:0px;border:0px;" />
五,返回受影響資料的行數
ExecValueVariable 屬性是Task的標準屬性。有些Task在執行完成後,會返回輸出結果,為了擷取Task的輸出結果,我們可以定義一個變數,儲存輸出結果。Task的ExecValueVariable屬性就是用來指定儲存Task輸出結果的變數名。預設屬性值是none,表示task的輸出結果不會被儲存。
Execute SQL Task 返回被更新的資料的行數,我們可以為ExecValueVariable屬性指定一個變數,用來接收Task的輸出值(Execution value),在下遊組件中可以引用該變數,擷取 Execute SQL Task 更新的資料行數。
Returns the number of rows affected by the SQL statement(s).The ExecValue is using the @@ROWCOUNT to assign the value of the variable and absent a @@ROWCOUNT the value returned is -1.
樣本 Execute Sql Task的ExecValueVariable 用法
1,設計Package的Control Flow
650) this.width=650;" src="http://images2015.cnblogs.com/blog/628084/201610/628084-20161023140311295-1883773331.png" style="margin:0px;padding:0px;border:0px;" />
650) this.width=650;" src="http://images2015.cnblogs.com/blog/628084/201610/628084-20161023140316185-654677094.png" style="margin:0px;padding:0px;border:0px;" />
Execute Sql Task的屬性:ExecValueVariable的值是變數varCount,該Task執行的SQL語句如下:
insert into dbo.delay_testVALUES(1),(2),(3)insert into dbo.delay_testVALUES(2),(3)
650) this.width=650;" src="http://images2015.cnblogs.com/blog/628084/201610/628084-20161023140339029-185898184.png" style="margin:0px;padding:0px;border:0px;" />
Task:insert Data執行的SQL語句是,傳入的參數是User::varCount
insert into dbo.dt_testvalues(?)
650) this.width=650;" src="http://images2015.cnblogs.com/blog/628084/201610/628084-20161023140403232-1414428968.png" style="margin:0px;padding:0px;border:0px;" />
2,查看結果,第一個Task返回的結果是varCount是2,這個結果實際上是@@RowCount,SSIS在執行語句之後,將@@RowCount賦值到Execute SQL Task 屬性ExecValueVariable指定的變數中。
SSIS Execute SQL Task 用法