RE: Select statement

From: Alejandro Mesa (AlejandroMesa_at_discussions.microsoft.com)
Date: 10/26/04


Date: Tue, 26 Oct 2004 09:43:04 -0700

You can create a user function that return the list. The main cons is that
the longest value is 8000 characters.

Example:

use pubs
go

create function dbo.ufn_authors_list (
@title_id [tid]
)
returns varchar(8000)
as
begin
declare @s varchar(8000)

set @s = ''

select
        @s = @s + case when @s > '' then ',' else '' end + au_lname + ' ' +
au_fname
from
        dbo.titleauthor as ta
        inner join
        dbo.authors as a
        on a.au_id = ta.au_id and ta.title_id = @title_id
        
return nullif(@s, '')
end
go

select
        t.title_id,
        t.title,
        dbo.ufn_authors_list(t.title_id) as authors_list
from
        dbo.titles as t
go

drop function dbo.ufn_authors_list
go

AMB

"Steven Richardson" wrote:

> Anyone know how two join muliple records into 1.
>
> I have 3 tables like the Pubs DB.
>
> Authors
> Titles
> TitleAuthors
>
> This allows for many to many relationships.
>
> I need to be able to query this database so I can import titles & authors to
> a system that does not deal with many to many relations ships & so want the
> query to return the authors in one colum like below.
>
>
> title_id fulltitle author1
> title_id fulltitle author1, author2, author3
> title_id fulltitle author2, author3
> etc..
> etc..
>
> I would imagine there would be someway of doing this in a function or a
> cursor.
>
> Many Thanks
> Steve
>
>
>



Relevant Pages

  • Re: Multiple Records into 1
    ... > I have 3 tables like the Pubs DB. ... > I need to be able to query this database so I can import titles & authors ... > a system that does not deal with many to many relations ships & so want ... > query to return the authors in one colum like below. ...
    (microsoft.public.sqlserver.programming)
  • Finding max(sum(sales)) in group by query
    ... Following is the query to list the id and sum from titles table for each ... USE pubs ... How can I modify the query to return the row with maximum of sum. ...
    (microsoft.public.sqlserver.programming)
  • Re: Join multple records
    ... > I have 3 tables like the Pubs DB. ... > I need to be able to query this database so I can import titles & authors ... > a system that does not deal with many to many relations ships & so want ... > query to return the authors in one colum like below. ...
    (microsoft.public.sqlserver.programming)
  • "call to undefined function" mysql_error when adding new rows to table
    ... I have recently set up a mysql database of local pubs, ... to add new listings (using PHP to generate an INSERT query). ... when I try to add a new pub I get an error message "call to undefined ... I don't know which version of PHP Supanames has installed.) ...
    (alt.php)
  • "call to undefined function" mysql_error when adding new rows to table
    ... I have recently set up a mysql database of local pubs, ... to add new listings (using PHP to generate an INSERT query). ... when I try to add a new pub I get an error message "call to undefined ...
    (comp.lang.php)