Solved

Insert from a comma separated list (CSV)

Posted on 2014-11-14
7
76 Views
Last Modified: 2014-11-14
Hi

I have a table with 2 columns UserID & BookingID.

I'll know the bookingID and a comma separated list of UserID's.

How would I insert the CSV list with a common BookingID, witout a cursor.

Andy
0
Comment
Question by:Andy Green
  • 3
  • 3
7 Comments
 
LVL 45

Expert Comment

by:Vitor Montalvão
ID: 40442287
With BULK INSERT command. Here's an example:
BULK INSERT TableName
   FROM Filename -- Provide a string with full path to the filename
   WITH 
      (
         FIELDTERMINATOR =' |',
         ROWTERMINATOR =' |\n'
      );

Open in new window

0
 
LVL 3

Author Comment

by:Andy Green
ID: 40442299
Sorry my mistake - its not a file its a text string passed into a stored proc.

Andy
0
 
LVL 45

Expert Comment

by:Vitor Montalvão
ID: 40442336
Can you give an example of how are those records?
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 3

Author Comment

by:Andy Green
ID: 40442366
Table
BookingID UserID
1                      ab
1                      cd
1                      ef
2                      ab
2                      xy

This is how the table looks the userID are in fact GUIDS

Passed into the proc for the first 3 in the table:

@BookingID as int = 1
@UserID as nvarchar(200) = 'ab,cd,ef'

Andy
0
 
LVL 45

Accepted Solution

by:
Vitor Montalvão earned 500 total points
ID: 40442399
Ok, I understood now.
You need to use Common Table Expression (CTE) for do the recursive split:
DECLARE @BookingID as int = 1
DECLARE @UserID as nvarchar(200) = 'ab,cd,ef'

;WITH SplitString(StartPos,EndPos)
    AS(
        SELECT 0, CHARINDEX(',',@UserID)
        UNION ALL
        SELECT EndPos+1, CHARINDEX(',',@UserID,EndPos+1)
        FROM SplitString
        WHERE EndPos > 0
    )
SELECT @BookingID Booking, SUBSTRING(@UserID,StartPos,COALESCE(NULLIF(EndPos,0),LEN(@UserID)+1)-StartPos) UserID
FROM SplitString

Open in new window

0
 
LVL 3

Author Closing Comment

by:Andy Green
ID: 40442525
Very cool. Thank you.

Andy
0
 
LVL 9

Expert Comment

by:Valliappan AN
ID: 40442718
You can also use XML and Cross apply as follows:

DECLARE @BookingID int = 1
DECLARE @UserID nvarchar(200) = 'ab,cd,ef'


SELECT @BookingID As BookingID,  
     Split.a.value('.', 'VARCHAR(100)') AS UserID
 FROM  (SELECT
         CAST ('<U>' + REPLACE(@UserID, ',', '</U><U>') + '</U>' AS XML) AS String  
     ) AS A CROSS APPLY String.nodes ('/U') AS Split(a);

HTH
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Introduced in Microsoft SQL Server 2005, the Copy Database Wizard (http://msdn.microsoft.com/en-us/library/ms188664.aspx) is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now