I have a text file include the content like the following,and also have SQL table have the same structure, does anyone know how to loop insert the content to SQL from txt file?and everyday we need to import once,we don’t want to import the same record to the table,Pers.no will have the unique value.Many thanks.
Most simple approach is the following:
1/ create datatable of same structure like the target SQL table
2/ read the txt file into the datatable
3/ use “Insert” activity to insert datatable into SQL table
Thanks, yes, I use the same way to update the data to SQL table. I don’t want to update the duplicate data to the same table, I only need to increase part, not all the data.
In cases where I receive data which overlap with dataset already inserted in SQL table I usually use an insert trigger to filter out the duplicate data on database level. It’s easier than dealing with duplicates in application.
Sample below
Cheers
CREATE TRIGGER [dbo].[IO_Trig_INS_P002_PriceList] ON [dbo].[P002_PriceList]
INSTEAD OF INSERT
AS
BEGIN
SET NOCOUNT ON
– Check for duplicate PriceList. If there is no duplicate, do an INSERT.
IF (NOT EXISTS (SELECT 1 FROM P002_PriceList PL, inserted I
WHERE PL.date = I.date
AND PL.warehouse = I.warehouse
AND PL.product = I.product))
INSERT INTO P002_PriceList
SELECT date, warehouse, product, price, file_name
FROM inserted
ELSE
–If there is a duplicate, change to UPDATE so that there will not
–be a duplicate key violation error.
UPDATE P002_PriceList
SET price = I.price, file_name = I.file_name
FROM P002_PriceList PL, inserted I
WHERE PL.date = I.date
AND PL.warehouse = I.warehouse
AND PL.product = I.product
END