OK, hopefully all this helps. I took what you posted in the comment and turned it into a stored procedure. The scenario is that you have a flat file that has a consistent format which you'll load to the same table each time (dbo.custCompInfo_Tab). That table will be merged into the final destination (dbo.Daily_Sync) matching on CompanyName. I added a few data columns to illustrate the merge further.
SETUP:
CREATE TABLE dbo.Daily_Sync
(CompanyName VARCHAR(10)
, UserId INT
, col1 INT
, col2 INT
, col3 INT
)
CREATE TABLE dbo.custCompInfo_Tab
(CompanyName VARCHAR(10)
, col1 INT
, col2 INT
, col3 INT
)
I have two data files to load, TrialBalance.txt and TrialBalance2.txt. They contain the following data:
TrialBalance.txt
abc,1,2,3
def,4,5,6
qwe,7,8,9
asd,10,11,12
zxc,13,14,15
TrialBalance2.txt
abc,1,2,3
def,20,21,22
qwe,7,8,9
xcv,10,11,12
xbv,13,14,15
I created a stored procedure that truncates the staging table, loads the table with the data from the file path passed in and then merges it into the destination.
CREATE PROCEDURE dbo.loadDailyData
@FullFilePath NVARCHAR(MAX)
AS
BEGIN
DECLARE @sql NVARCHAR(MAX)
TRUNCATE TABLE dbo.custCompInfo_Tab
SET @sql = N'BULK INSERT dbo.custCompInfo_Tab FROM ''' + @FullFilePath
+ ''' WITH ( FIELDTERMINATOR ='','',ROWTERMINATOR = ''\n'',FIRSTROW = 1 )'
SELECT @sql
EXEC sp_executesql @sql
MERGE INTO dbo.Daily_Sync AS TGT
USING
(SELECT CompanyName
, USER_ID() usrid
, col1
, col2
, col3
FROM dbo.custCompInfo_Tab
) AS SRC
ON TGT.Companyname = SRC.CompanyName
WHEN MATCHED
THEN UPDATE
SET TGT.Companyname = SRC.companyname
, TGT.col1 = SRC.col1
, TGT.col2 = SRC.col2
, TGT.col3 = SRC.col3
WHEN NOT MATCHED
THEN INSERT (companyname
, UserId
, col1
, col2
, col3
)
VALUES (SRC.CompanyName
, SRC.usrid
, SRC.col1
, SRC.col2
, SRC.col3
);
END
There is dynamic sql here that is used to build the string and other than the rowcounts, the BULK INSERT string is all that is returned.
Finally, we can see the tables before and after:
SELECT *
FROM dbo.custCompInfo_Tab
SELECT *
FROM dbo.Daily_Sync
EXEC dbo.loadDailyData @FullFilePath = 'D:\xmlData\TrialBalance.txt'
SELECT *
FROM dbo.custCompInfo_Tab
SELECT *
FROM dbo.Daily_Sync
EXEC dbo.loadDailyData @FullFilePath = 'D:\xmlData\TrialBalance2.txt'
SELECT *
FROM dbo.custCompInfo_Tab
SELECT *
FROM dbo.Daily_Sync
RESULTS:
