2016-12-09 83 views
2

我正在尋找關於如何執行某些操作的建議,我相當肯定我應該能夠使用SSIS進行操作。SSIS:在成功寫入時執行其他操作

我有兩個數據庫,我正在更新表A的表B,但是當我這樣做時,我希望永遠成功寫入表B,並在原始數據庫中向日志表中寫入一些附加信息。 (見圖) enter image description here

我不確定我會用什麼控件來完成這個任務,但懷疑它可以在SSIS中完成。

+0

你的意思是在逐行插入操作?......我認爲一個觸發應該是這樣的任務而不是SSIS替代的最佳人選...... – Hackerman

+0

您還可以通過定期做t-sql:'begin tran - >寫入tableB->寫入日誌表 - > commit'。您也可以爲「遠程」表創建一個**同義詞**,並將它寫入短直接引用,就好像它在當前數據庫中一樣。 –

+0

@Hackerman - 實際上有兩個單獨的操作,我將構建一個將涉及插入記錄,而另一個將嚴格更新現有記錄。 表中已經存在觸發器,所以增加更多的觸發器可能是個問題。如果我們能夠在不操縱數據庫之外的其他數據庫2內容的情況下完成此操作,那我最好。 –

回答

0

經過Ivan Starostin和董事會其他人的建議,我選擇放棄使用SSIS來完成此過程,並使用存儲過程從網頁中的代碼調用來執行此功能。我正在包含下面的插入過程SP的示例,因爲我正在處理其餘部分,但想要關閉此問題。感謝Ivan和所有的投入。 SSIS根本不是這種情況的最佳工具。我發現使用SP更清潔。

USE [CInTracDB] 
GO 

/******** Object: StoredProcedure [dbo].[usp_InsertNewAssets] Script Date: 12/15/2016 3:00:00 PM *****/ 
SET ANSI_NULLS ON 
GO 

SET QUOTED_IDENTIFIER ON 
GO 

-- ============================================= 
-- Author:  <Ken Carter> 
-- Create date: <Dec 15, 2016> 
-- Description: <Input Fields from Updates table, Insert all NEW records into CMDB.dbo.cmdb_assets, then copy record from 
--    Updates to CInTracDB.dbo.Update_log, then Delete Record from Updates, 
--    Return only sucesss/fail signal. 
-- ============================================= 

CREATE PROCEDURE [dbo].[usp_InsertNewAssets](
@CAssetID as bigint, 
@SubmitBy as varchar(50), 
@SubmitDT as datetime, 
@ApproveBy as varchar(50), 
@Approved as char(10), 
@ApprovedDT as datetime, 
@Imported as char(10), 
@ImportDT as datetime, 
@AssetID as bigint, 
@AssetType as int, 
@AssetName as varchar(128), 
@AssetShortDesc as varchar(50), 
@AssetLongDesc as varchar(200), 
@AssetAddNotes as varchar(254), 
@AssetManuf as varchar(50), 
@AssetModel as varchar(50), 
@AssetTag as varchar(20), 
@AssetAcqDate as datetime, 
@AssetDTAssetID as varchar(20), 
@AssetLocGrp as varchar(10), 
@AssetLoc1 as varchar(8), 
@AssetLoc2 as varchar(8), 
@AssetLoc3 as varchar(8), 
@AssetParent as bigint, 
@AssetStatus as int, 
@AssetPropType as int, 
@AssetPrimUser as varchar(20), 
@AssetEntered as datetime, 
@AssetEnteredBy as varchar(50), 
@AssetOwner as varchar(50), 
@AssetCompany as varchar(10), 
@AssetPriIPAddr as varchar(15), 
@AssetPriMACAddr as varchar(17), 
@AssetPriOS as varchar(20), 
@AssetPriOSSP as varchar(20), 
@AssetNotes as varchar(500), 
@AssetAdminGrp as varchar(100), 
@AssetSerialNum as varchar(75), 
@AssetOperType as int, 
@AssetOperStatus as int 
) 
AS 
BEGIN 
SET NOCOUNT ON; 

DECLARE @AssetFACL as bit = 0 
DECLARE @AssetStatVer as bit = 0 

BEGIN 
    INSERT INTO CMDB.dbo.cmdb_assets (AssetType, AssetName, AssetShortDesc, AssetLongDesc, AssetAddNotes, AssetManuf, 
     AssetModel, AssetTag, AssetAcqDate, AssetDTAssetID, AssetLocGrp, AssetLoc1, AssetLoc2, AssetLoc3, AssetParent, 
     AssetStatus, AssetPropType, AssetPrimUser, AssetEntered, AssetEnteredBy, AssetOwner, AssetCompany, AssetPriIPAddr, 
     AssetPriMACAddr, AssetPriOS, AssetPriOSSP, AssetNotes, AssetAdminGrp, AssetSerialNum, AssetOperType, 
     AssetOperStatus, AssetFACL, AssetStatVer) 

     SELECT AssetType, AssetName, AssetShortDesc, AssetLongDesc, AssetAddNotes, AssetManuf, AssetModel, AssetTag, 
     AssetAcqDate, AssetDTAssetID, AssetLocGrp, AssetLoc1, AssetLoc2, AssetLoc3, AssetParent, AssetStatus, AssetPropType, 
     AssetPrimUser, AssetEntered, AssetEnteredBy, AssetOwner, AssetCompany, AssetPriIPAddr, AssetPriMACAddr, AssetPriOS, 
     AssetPriOSSP, AssetNotes, AssetAdminGrp, AssetSerialNum, AssetOperType, AssetOperStatus 
     FROM Updates 
     WHERE AssetID > 0 
END 

BEGIN 
    Insert INTO Update_log (CAssetID, SubmitBy, SubmitDT, ApproveBy, Approved, ApprovedDT, Imported, ImportDT, AssetID, 
     AssetType, AssetName, AssetShortDesc, AssetLongDesc, AssetAddNotes, AssetManuf, AssetModel, AssetTag, AssetAcqDate, 
     AssetDTAssetID, AssetLocGrp, AssetLoc1, AssetLoc2, AssetLoc3, AssetParent, AssetStatus, AssetPropType, 
     AssetPrimUser, AssetEntered, AssetEnteredBy, AssetOwner, AssetCompany, AssetPriIPAddr, AssetPriMACAddr, AssetPriOS, 
     AssetPriOSSP, AssetNotes, AssetAdminGrp, AssetSerialNum, AssetOperType, AssetOperStatus) 

     SELECT AssetType, AssetName, AssetShortDesc, AssetLongDesc, AssetAddNotes, AssetManuf, AssetModel, AssetTag, 
     AssetAcqDate, AssetDTAssetID, AssetLocGrp, AssetLoc1, AssetLoc2, AssetLoc3, AssetParent, AssetStatus, AssetPropType, 
     AssetPrimUser, AssetEntered, AssetEnteredBy, AssetOwner, AssetCompany, AssetPriIPAddr, AssetPriMACAddr, AssetPriOS, 
     AssetPriOSSP, AssetNotes, AssetAdminGrp, AssetSerialNum, AssetOperType, AssetOperStatus 
     FROM Updates 
     WHERE AssetID > 0 
END 

BEGIN 
    DELETE FROM Updates 
    WHERE (AssetID > 0) 
END 
END 
GO 
0

使用OLEDB轉換對錶B執行UPDATE,然後按照寫入日誌表的目標進行操作。

+0

@Ivan_Starostin我在這裏傾向於您的建議方向,並考慮放棄使用SSIS。我正在考慮編寫兩個存儲過程,並簡單地編寫代碼來解決頁面背後的代碼。我會給我最大的靈活性。我相信。 –

+0

如果兩個數據庫位於同一臺服務器上,使用純SQL絕對是最好的選擇。這裏使用SSIS的唯一原因是如果兩者位於不能連接的不同服務器上。 –

相關問題