473,396 Members | 1,892 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

SP - Convert CSV to a Link table

inspireuk
I have a CSV file containing linked IDs eg 1,2,3

I have a table with two columns ID and LinkedID

I would like to pass the CSV string in and have the table updated so that each relationship is added as a new record

ID,LinkedID
1,2
1,3
2,1
2,3
3,1
3,2

Any ideas?

Thanks

Josh
Aug 2 '07 #1
6 1884
azimmer
200 Expert 100+
I have a CSV file containing linked IDs eg 1,2,3

I have a table with two columns ID and LinkedID

I would like to pass the CSV string in and have the table updated so that each relationship is added as a new record

ID,LinkedID
1,2
1,3
2,1
2,3
3,1
3,2

Any ideas?

Thanks

Josh
Why not import the CSV file into a table directly? (Enterprise Manager -> Tools -> Data Transformation Services -> Import Data ...)
Aug 3 '07 #2
because the list is dynamically generated.

Josh
Aug 3 '07 #3
azimmer
200 Expert 100+
You mean that you need a procedure that takes a CSV string (of the from "ID,LinkedID") as an input parameter and then adds the the record to the table? (If so I was confused of your mentioning a "CSV file".)

The following procedure does just that:
Expand|Select|Wrap|Line Numbers
  1. create procedure csvinsert (@csvstring as varchar(200))
  2. as
  3. declare @commapos as int
  4.  
  5. set @commapos = charindex(',',@csvstring)
  6. insert into IDTABLE VALUES(left(@csvstring,@commapos-1),right(@csvstring,len(@csvstring)-@commapos))
  7.  
Aug 4 '07 #4
sorry shouldn't have used the phase 'csv file'. Let me try to explain again what i'm trying to do.

the sp would have a string input of '1,2,3'

from this i need to generate a list of all the possible combinations
1,2
1,3
2,1
2,3
3,1
3,2

which then need to be inserted into a two column table.

the string input could be any length eg '1,2,3,4,5' and so the generated list would be different.

i hope this makes sense.

thanks for your help

Josh
Aug 4 '07 #5
azimmer
200 Expert 100+
sorry shouldn't have used the phase 'csv file'. Let me try to explain again what i'm trying to do.

the sp would have a string input of '1,2,3'

from this i need to generate a list of all the possible combinations
1,2
1,3
2,1
2,3
3,1
3,2

which then need to be inserted into a two column table.

the string input could be any length eg '1,2,3,4,5' and so the generated list would be different.

i hope this makes sense.

thanks for your help

Josh
Next attempt :):
Expand|Select|Wrap|Line Numbers
  1. create procedure create_linked_ids(@csvstring as varchar(200))
  2. as
  3. declare @pos as int, @lastpos as int
  4. declare @temptbl TABLE (tmpid int)
  5.  
  6. set @lastpos = 1
  7. set @pos = charindex(',',@csvstring)
  8. while (@pos > 0)
  9. begin
  10.     insert into @temptbl values (substring(@csvstring,@lastpos,@pos-@lastpos))
  11.     set @lastpos=@pos+1
  12.     set @pos=charindex(',',@csvstring,@lastpos)
  13. end
  14. insert into @temptbl values (substring(@csvstring,@lastpos,len(@csvstring)-@lastpos+1))
  15. select a.tmpid 'ID',b.tmpid 'LinkedID' from @temptbl a, @temptbl b where a.tmpid<>b.tmpid
  16.  
  17. go
  18.  
Use it like this:
Expand|Select|Wrap|Line Numbers
  1. exec create_linked_ids '1,2,3'
If the result has to go into a table then insert the 'INTO <tablename>' caluse into the last line of the procedure (before 'FROM').

Hope it helps
Aug 6 '07 #6
that works great! thanks.
Aug 6 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Carl Gilbert | last post by:
Hi I have some ASP code that I want to run from CD within a VB.NET windows application with a web browser control. However, to get the ASP pages to run without a server is proving quite...
3
by: ET | last post by:
I don't know whats the problem, but after I added functions to first verify, then relink linked tables if not found, now I can't convert that database to MDE format. I can split the database, but...
29
by: Jan | last post by:
Hi: I have an Access database that's been running (in one form or another) for a couple of different clients for a few years. Now a new client has requested that it be implemented with a SQL...
2
by: egoldthwait | last post by:
I need to convert a 17mb access 2000 db to Oracle and house it in a Citrix farm. The issue: we have never converted an Access Db to Oracle but can probably use Oracle's Workbench to assist with...
1
by: Jorge | last post by:
I want to convert this xml to pdf using cocoon <?xml version="1.0" encoding="ISO-8859-1" ?> <document> <header> <title>Pagina</title> </header> <body> <h1>Link dei siti pił visitati da...
5
by: Bob Homes | last post by:
In VB6, foreground and background colors of controls had to be assigned a single number. If you knew the RGB values for the color, you still had to convert them into the single number accepatable...
1
by: Aaron_nesbitt | last post by:
Hi, Is there any way to convert a sting (Pulled from a data tabel) into VB.NET code? The original case statment looked like this: Select Case e.Link.ItemName Case "frmNEWFORM" Dim f As...
26
by: mark | last post by:
The idea of this is very simle. The site is 800px wide and sits in the middle of the browser window, on either side of the site I want a different background image aligned against it. If I were...
4
by: donpro | last post by:
Hi, I've created a table where the header columns link to an AJAX function which calls a PHP file and returns content - the purpose is to sort the table on the heading. The code snippet is:...
11
by: lenygold via DBMonster.com | last post by:
I am tryieng to convert our time consuming recursive queries too very efficient queries based on nested set model. The only problem is to convert an adjacency list model into a nested set model,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.