November 2, 2010 at 7:04 am
Dear Experts,
I have a difficulty task in SQL Procedure creations,
I have to Import the csv file in sql server DB,
Example:
One csv file like CSV1.csv
With First row as Column Name
Like
P_ID,P_price,P_discount,P_tax
1 2.00$ 5% vat1.0
But I want to Write a Procedure to Import the CSV file
With Runtime only creating the Table.
That is the Table Name= CSV1
and Column_Names are:
P_ID,P_price,P_discount,P_tax
and values are :
1 2.00$ 5% vat1.0
Please help me To Import the CSV file in TSQl
But the IMPORT data Wizard we are able but I need to do this in TSQL
THanks in Advance
November 2, 2010 at 7:34 am
Well, you didn't give us any sample data (in comma-delimited format) to play with, so all I can do for you is to point you to this: Click here for the latest Delimited Split Function.
I'd recommend using BULK INSERT to load the file into a table with one large column.
Assuming:
CREATE TABLE #Source(CSVData varchar(7000));
BULK INSERT #Source FROM c:\temp\test.csv;
SELECT ds.item
FROM #Source s
CROSS APPLY dbo.DelimitedSplit8k(s.CSVData, ',') ds;
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
November 2, 2010 at 8:14 am
CREATE TABLE #header (Data varchar(max))
CREATE TABLE #result (dummycolumn int)
BULK INSERT #header FROM 'C:\TEMP\CSV1.csv' WITH (DATAFILETYPE = 'char',ROWTERMINATOR = '',LASTROW = 1)
DECLARE @sql varchar(max)
SELECT @sql = COALESCE(@sql+',['+SUBSTRING(Data, N, CHARINDEX(',', Data + ',', N) - N)+'] varchar(255) NULL',
SUBSTRING(Data, N, CHARINDEX(',', Data + ',', N) - N)+' varchar(255) NULL')
FROM #header
JOIN dbo.Tally
ON N < LEN(Data) + 2 AND SUBSTRING(',' + Data, N, 1) = ','
ORDER BY ROW_NUMBER() OVER (ORDER BY N)
EXEC('ALTER TABLE #result ADD '+@sql)
ALTER TABLE #result DROP COLUMN dummycolumn
BULK INSERT #result FROM 'C:\TEMP\CSV1.csv' WITH (DATAFILETYPE = 'char',FIELDTERMINATOR = ',', ROWTERMINATOR = '',FIRSTROW = 2)
SELECT * FROM #result
Far away is close at hand in the images of elsewhere.
Anon.
November 2, 2010 at 8:57 am
Thanks
But I got this Below error
Msg 208, Level 16, State 1, Line 7
Invalid object name 'dbo.Tally'.
I cant understand why u using the above table name..
November 2, 2010 at 9:18 am
Saravanan_tvr (11/2/2010)
ThanksBut I got this Below error
Msg 208, Level 16, State 1, Line 7
Invalid object name 'dbo.Tally'.
I cant understand why u using the above table name..
You might want to check out this article: The "Numbers" or "Tally" Table: What it is and how it replaces a loop.[/url]
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
November 2, 2010 at 11:21 am
Ooops!
Thanks Wayne, forgot about that in my haste. :blush:
Far away is close at hand in the images of elsewhere.
Anon.
November 2, 2010 at 11:30 am
David Burrows (11/2/2010)
Ooops!Thanks Wayne, forgot about that in my haste. :blush:
Actually, with the OP having close to 200 points, I would have assumed (s)he would have known what one was also. But, this is also one reason that I code an inline tally table instead (with remarks to the article, and to build a permanent one for better performance) when answering posts.
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
November 3, 2010 at 12:48 am
Thanks Now working fine, But In CSV file format, if one field like Product
descriptions having Multiple Comma Inside, but our SQL splitting them in to multiple column,
but We want to only one column with that Product descriptions, Is it possible, or we have to go any other file format..?
November 3, 2010 at 7:23 am
Column delimiters need to be unique in the file. If one of the columns allows commas, change to something else (like the |).
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
November 3, 2010 at 4:03 pm
Saravanan_tvr (11/3/2010)
Thanks Now working fine, But In CSV file format, if one field like Productdescriptions having Multiple Comma Inside, but our SQL splitting them in to multiple column,
but We want to only one column with that Product descriptions, Is it possible, or we have to go any other file format..?
Now we're only guessing. Please attach the first 10 rows of the file as a text file so we can help you properly.
--Jeff Moden
Change is inevitable... Change for the better is not.
November 3, 2010 at 10:56 pm
Thanks for all Replies,
We have changed into CSV format to Txt format with Pipline as Column Delimeter.. Now Bulk insert working fine..
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply