Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Insert into using a multiple result select

Author  Topic 

ronstone
Starting Member

32 Posts

Posted - 2002-02-26 : 11:19:24
It seems I can only do this with cursors. Is there a better way to do this?

Say I have a column called "Numbers" (varchar).

And I want to do an INSERT into the column Numbers the results of a SELECT statement that return multiple rows (of numbers, say 1,2,3,4,5,6,7,8,9,10).

When I run INSERT, only 10 shows up. Not 1,2,3,4 etc. I want to be able to insert the 10 rows of numbers into the varchar Numbers column with a <CR> after each number.

Any sugguestions?

Thanks,
Ron


robvolk
Most Valuable Yak

15732 Posts

Posted - 2002-02-26 : 11:33:02
Garth wrote a really good article on combining rows into CSV strings:

http://www.sqlteam.com/item.asp?ItemID=2368

You can modify it to use a carriage return if you like. It uses a varchar variable to build the string, once it is built you can INSERT it into the table:

INSERT INTO myTable (CSV_column) VALUES (@csvstring)

Go to Top of Page

ronstone
Starting Member

32 Posts

Posted - 2002-02-26 : 11:52:34

Thank Robvolk for the quick response, can't wait to try out the COALESCE function!



Go to Top of Page
   

- Advertisement -