Question

I'm sure this is a common problem, but I just can't seem to find exactly the help I'm looking for. Apologies!

I'm trying to perform a bulk insert from a CSV file—the table I need to insert the data into has a few fields that cannot be null—like a row unique identifier field that is normally generated with newid(), and a few other fields such as the ID number of the user making the changes, etc...

Poking around online, I think I understand how to do a basic bulk import if the CSV file contains the same number of fields as the table, OR if it's okay to leave missing fields null or default, but I haven't been able to find somewhere that shows me how to assign some values from the CSV files, and to also predefine the other required table values. It would be extremely helpful if someone could show me how to do an import from a CSV where I can assign missing fields from variables within my stored procedure!

Any tips? Here is the code I've been trying to use to get this done.

BULK
INSERT tbl1_
FROM '\\...\importtest.txt'
WITH
(
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)
GO
--Check the content of the table.
SELECT *
FROM tbl1_
GO
--Drop the table to clean up database.
SELECT *
FROM tbl1_
GO

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top