Wrox Home  
Search P2P Archive for: Go

  Return to Index  

sql_language thread: Stuck on this


Message #1 by "Niall Hannon (ext. 772)" <Niall.Hannon@f...> on Wed, 27 Nov 2002 11:24:58 -0000
This will work in MSSQL.  Of course, change column/table names & datatypes 
as necessary.

set nocount on
declare @id varchar(25)
create table #temp
(id int, name nvarchar(200))
declare curs cursor 
for
select id from testing
open curs
fetch next from curs into @id
while @@fetch_status = 0
begin
    declare @sql nvarchar(200)
    set @sql = ''
    select @sql = @sql + ', ' + name from testing where id = @id
    insert into #temp
    select @id, substring(@sql,2,200)
    fetch next from curs into @id
end
close curs
deallocate curs
select distinct id, name from #temp
drop table #temp

Let us know.

Dan

> Hi,

Is there a way to do the following in SQL.

Need to return something like this in a result set

	record 1: "ID1", "Comp1",
	record 2: "ID2", "Comp2"
	record 3: "ID3", "Comp1 Comp2"   ===> This is a concatentaion of 2
records from table 2 - the problem is here!

TABLE 1:
	ID1
	ID2
	ID3

TABLE 2:
	ID1, Comp1
	ID2, Comp2
	ID3, Comp1
	ID3, Comp2

You can see the relationship between the 2 tables is 1 to many but I cant
thikn of a way to return a result set like above.

Any ideas?

Thanks
Niall


**************************************************************************
The information contained in this e-mail is confidential,
may be privileged and is intended only for the use of the
recipient named above. If you are not the intended
recipient or a representative of the intended recipient,
you have received this e-mail in error and must not copy,
use or disclose the contents of this email to anybody
else. If you have received this e-mail in error, please
notify the sender immediately by return e-mail and
permanently delete the copy you received. This email has
been swept for computer viruses. However, you should
carry out your own virus checks.


Registered in Ireland, No. 205721. http://www.FINEOS.com
**************************************************************************


  Return to Index