[thelist] Comma delimited string in SQL

Joshua Olson joshua at waetech.com
Mon Oct 13 09:41:26 CDT 2003


From: "Stevenson N" <ngilas2003 at yahoo.com>
Sent: Monday, October 13, 2003 10:26 AM


> Hi all,
> I have comma delimited string i.e. 1,2,4,5. I want to loop through each
> data in the string as i insert the data in a table.
>
> How can i achieve this? Please assist!

The solution will depend greatly on your database and scripting language, as
well as the scenario.

Just a little fyi, if the numbers in the string are values from records in a
table somewhere in the database, then you could format your SQL thusly:

INSERT INTO mytable
  (foo, bar)
SELECT
  'foo', 'bar'
FROM thetablethelistcamefrom
WHERE id IN (1,2,4,5)

Rather than looping you are telling the database to insert multiple records.

This technique won't work if the numbers in the list don't appear in a table
somewhere.

<tip type="posing question" author="Joshua Olson">
Always provide as much information as possible when asking a programming
question.  Database type, server type, language, scenario, etc.  The more
information available up front will reduce the number of emails back asking
for more information.
</tip>

<><><><><><><><><><>
Joshua Olson
Web Application Engineer
WAE Tech Inc.
http://www.waetech.com
706.210.0168



More information about the thelist mailing list