Last active
August 29, 2015 14:23
-
-
Save clintmjohnson/08cd7e9a773aca287e27 to your computer and use it in GitHub Desktop.
SQL Server Merge From File Such as CSV Using OPENROWSET
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT * | |
FROM OPENROWSET(BULK 'c:\temp\Customers.txt', | |
FORMATFILE = 'c:\temp\CustomersFmt.xml') AS SRC; | |
SELECT * | |
FROM OPENROWSET(BULK 'c:\temp\Customers.txt', | |
FORMATFILE = 'c:\temp\CustomersFmt.xml') AS SRC; | |
Similarly, the OPENROWSET function can be used directly in the USING clause of the MERGE statement, like so: | |
MERGE INTO Sales.MyCustomers AS TGT | |
USING OPENROWSET(BULK 'c:\temp\Customers.txt', | |
FORMATFILE = 'c:\temp\CustomersFmt.xml') AS SRC | |
ON SRC.custid = TGT.custid | |
WHEN MATCHED THEN UPDATE | |
SET TGT.companyname = SRC.companyname, | |
TGT.country = SRC.country, | |
TGT.phone = SRC.phone | |
WHEN NOT MATCHED THEN INSERT | |
VALUES( SRC.custid, SRC.companyname, SRC.country, SRC.phone ); | |
MERGE INTO Sales.MyCustomers AS TGT | |
USING OPENROWSET(BULK 'c:\temp\Customers.txt', | |
FORMATFILE = 'c:\temp\CustomersFmt.xml') AS SRC | |
ON SRC.custid = TGT.custid | |
WHEN MATCHED THEN UPDATE | |
SET TGT.companyname = SRC.companyname, | |
TGT.country = SRC.country, | |
TGT.phone = SRC.phone | |
WHEN NOT MATCHED THEN INSERT | |
VALUES( SRC.custid, SRC.companyname, SRC.country, SRC.phone ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Not a Proper SQL Comments Only