Wrox Programmer Forums
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 10th, 2008, 01:55 AM
Registered User
 
Join Date: Feb 2008
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to krshekhar Send a message via Yahoo to krshekhar
Default Query

I have a table temp like this

    temp_id temp_name
      1 shekhar
      1 ranjan
      1 ram
      2 manoj
      ............
      ....... ...
Now I want to create a store procedure which return values like this

       temp1_id temp1_name
          1 shekhar, ranjan, ram
          2 manoj,.....
         .............

please help me
Thanks in advance

SHEKHAR
 
Old September 10th, 2008, 02:50 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Look here:

http://www.sqlteam.com/article/using...limited-string
 
Old September 10th, 2008, 05:54 PM
Friend of Wrox
 
Join Date: Oct 2006
Posts: 475
Thanks: 0
Thanked 9 Times in 9 Posts
Default

Heh... one of my favorite articles on the subject is in the following link. It's a pretty good "How To" and has some information on things to avoid.

http://www.sqlservercentral.com/arti...st+Data/61572/

--Jeff Moden
 
Old September 10th, 2008, 06:53 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Well, gee...no wonder you like it. <grin/>

My standard answer to this question--which I get really tired of repeating--is to do the concatenation in whatever language/report generator you are using that *invokes* the SQL.

Almost surely more efficient than doing it in the SQL query, since SQL wasn't designed for this.

But sometimes I just get lazy and let people dig their own graves.
 
Old September 10th, 2008, 07:10 PM
Friend of Wrox
 
Join Date: Oct 2006
Posts: 475
Thanks: 0
Thanked 9 Times in 9 Posts
Default

Heh... I'm not partial or anything, huh?

I agree with you on letting the client do the concatenation instead of the SQL Server, but not for the same reason as you... server is just one box with many clients making demands on it... let the clients do some of the work.

I've not tested the GUI method (swore off GUI's 5 yers ago) against the server method for performance. As you can see in the article, a properly written concatenation on the server side is pretty darned fast. It would be an interesting test.

--Jeff Moden
 
Old September 10th, 2008, 11:47 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

I suspect that the full answer may lie in how much data ends up being duplicated when the concatenation is done in the client. But I don't think that whether the client is GUI-based or perhaps just a command line or maybe a printed report.

Anyway, for this simple example, where the records are nothing but an id and a string, I'm betting on the client side. After all, the code is *SO* dirt simple. Pseudo code:

reccords = execute("select id, name from table order by id, name")
id = records("id")
list = ""
do until no more records
    if records("id") != id
        output list
        list = ""
        id = records("id")
    end
    list += records("name")
    next record
loop
output list

Can't get much simpler (or faster) than that.
 
Old September 11th, 2008, 07:31 PM
Friend of Wrox
 
Join Date: Oct 2006
Posts: 475
Thanks: 0
Thanked 9 Times in 9 Posts
Default

Yo9u say it can't get much faster... does that mean you have, say, a million row test that can be programmatically duplicated? Not busting chops here... I'm just a performance freak and am always interested in the method with the best performance. I'm also a genuine data troll and haven't even loaded anything like visual studio so I normally end up writing the test table code and a T-SQL solution and let someone else do the testing. It also keeps people from thinking I put my thumb on the scale.

--Jeff Moden
 
Old September 11th, 2008, 11:00 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Oh, sorry...I'm assuming that the SQL query to *GET* this data has already been optimized as far as it can. So the only thing I'm doing here is, effectively, the final "Coalesce" (or equivalent).

Heck, if I had a million rows to process, I would *NEVER* do it in anything but SQL. This is strictly for the typical query that returns a handful of records (might be a handful selected from millions, of course) and is then displaying the delimited list (in a web page, in a report, whatever).

I can't think of any excuse for ever needing to create a set of records where one field contained a delimited list for any purpose *except* display. It obviously makes no sense to do such a query for the purpose of saving the results in some table or passing the results to some other query or or or. At least I can't think of any valid reason to do so. Have you actually encountered such a case?
 
Old September 11th, 2008, 11:38 PM
Friend of Wrox
 
Join Date: Oct 2006
Posts: 475
Thanks: 0
Thanked 9 Times in 9 Posts
Default

Heh... ok. I get it.

--Jeff Moden





Similar Threads
Thread Thread Starter Forum Replies Last Post
Output Query to txt file from SQL Query everest SQL Server 2005 4 November 22nd, 2007 01:49 AM
how to make a query from an existing query raport SQL Language 3 November 13th, 2006 08:59 PM
I solved insert query.now see this Update Query. [email protected] VB.NET 2002/2003 Basics 2 September 21st, 2006 12:48 AM
Syntax error in query. Incomplete query clause. dispickle ADO.NET 3 April 16th, 2004 01:04 PM
Error on Make-Table Query In Union Query rylemer Access 1 August 20th, 2003 07:42 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.