0

I found a similar questions are regarding this but I couldn't get it to for for my issue..

Lets say I have the string compiled of many many lines:

1,Users,2015-01-20\n
1,System,2015-01-18\n
....
....

I need to put it into existing table:

╔══════════╦═══════════╦══════════╗
║ SomeNum  ║ SystemType║ Date     ║
╠══════════╬═══════════╬══════════╣

enter code here

So it would look like:

╔══════════╦═══════════╦══════════╗
║ SomeNum  ║ SystemType║ Date     ║
╠══════════╬═══════════╬══════════╣
║ 1        ║     Users ║2015-01-20║
║ 1        ║    System ║2015-01-18║

How do I do that? I must say I know little to nothing about SQL so forgive me for any stupid Q that will follow, thank you :)

O. San
  • 1,107
  • 3
  • 11
  • 25
  • 1
    Possible duplicate of [Split comma delimited string and insert to a table (int)](http://stackoverflow.com/questions/23438761/split-comma-delimited-string-and-insert-to-a-table-int) – Harsha W Mar 30 '17 at 06:07
  • Yes, I saw that Q and tried to use it, but there it's an example of taking a single line and adding it vertically to a column, I'm trying to get an entire input to a table delimited by comma and put it in a table horizontally. It might be very similar but I couldn't get it to work... – O. San Mar 30 '17 at 06:12
  • Which DBMS are you using? Postgres? Oracle? –  Mar 30 '17 at 06:19
  • Store that string in a file, then import the file with whatever tools you have for the database you use (which you didn't tell us) –  Mar 30 '17 at 06:20
  • Hi, I'm using SQL Server, the script for the SQL is actually called from C# program using using (var conn = new SqlConnection(_connectionString)) { conn.Execute( @"......'"); } – O. San Mar 30 '17 at 06:27
  • and the string comes from a resource in the project (Properties.Resources) so I pass Properties.Resources.InputInfo.ToString() – O. San Mar 30 '17 at 06:28
  • https://msdn.microsoft.com/en-us/library/7ek5da1a(v=vs.110).aspx and https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlbulkcopy(v=vs.110).aspx –  Mar 30 '17 at 06:37

2 Answers2

0

Please try this:

insert into table_name values ( '1' ,'Users' , '2015-01-20')
Buddy
  • 10,874
  • 5
  • 41
  • 58
Anup
  • 144
  • 1
  • 7
  • Hi, thank you for your help, but I'm trying to put thousands of lines from a single string.. I clarified it more in the Q in case it wasn't clear enough – O. San Mar 30 '17 at 06:17
0

You can user Bulk Insert functionality of sql server. first create text or csv file of the data

BULK INSERT table_name  
FROM @filepath
WITH   
  (  
     FIELDTERMINATOR =',',  
     ROWTERMINATOR ='\n'  
  );  

Where table_name is your table name and @filepath is your csvfile path

For more information on bulk insert click here