I’m trying to create a table in a database using SQL. The data is currently in a text file that looks like this, but has thousands of lines like that.
My trouble lies primarily in the parameters of the “CREATE TABLE” function. Is there a way to say that I want all of the columns? Right now the code looks like:
CREATE TABLE dbo.CFTC_Fin_Data
(
)
BULK INSERT dbo.CFTC_Fin_Data
FROM 'H:\user\Desktop\CFTCData1.txt'
WITH
(
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
);
Thanks for any help. I’m pretty new to SQL, so I apologize if similar questions have been answered already, but I couldn’t understand any of the answers that I read.
Well, you have to look at your file and see what kind of info you have in each field.
With the line in your files, it seems that
Field1 is a string (3 char, maybe more in other lines) ? =>
NVARCHAR(100)Field2 is a string (xx char) =>
NVARCHAR(255)Field3 is a DATE => DATE
Field4 seems to be all kind of things =>
NVARCHAR(100)So something like
But you need to examinate all your file to find the real right values and data types
EDIT :
In fact, your really need to do it with code ?
Cause with import-export wizard (in SQL Server Management Studio), you can import a data file, and the wizard can create the table for you if you need to.
Try :
Right-click on your database name,
=> Tasks
=> Import Data
and follow the wizard.
You can also save the SSIS package if you need to.
Think it will be much more appropriate for you !