> Actually i will combine the multiple numbers into one single record
>
[quoted text clipped - 9 lines]
> 4 test4 54808 54808
> 4 test4 74594 54808<br> 74594
Add a DateInserted column, and use it in sorting.
If you delete the row with Phone = 34454, do you want to have to also
update the row with 45445 so that FinalPhone = 68638<br>45445? Of
course not - that would be a maintenance nightmare. Move FinalPhone
from the database to the UI layer, or at least from the table to a view.
I created two tables and a trigger and I have all the functionality
that you wanted, without having to create a cursor. The key is to let
the database do the work and this way, any record that validly gets
inserted into the phone_numbers table automatically gets put into the
phone_output list.
If you expect to be deleting or modifying records from the
phone_numbers table then you will need to add update and delete
triggers. (Note: for delete, use the replace function)
If you need more help, just post back.
Cheers,
Jason Lepack
Try this:
-- Begin SQL
CREATE TABLE [dbo].[phone_numbers](
[num] [int] NOT NULL,
[phone_name] [varchar](50) NOT NULL,
[phone] [varchar](50) NOT NULL
) ON [PRIMARY]
GO
CREATE UNIQUE NONCLUSTERED INDEX [nat_key] ON [dbo].[phone_numbers]
(
[num] ASC,
[phone] ASC
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[phone_output](
[num] [int] NOT NULL,
[phone_name] [nvarchar](50) NOT NULL,
[final_phone] [varchar](max) NULL
) ON [PRIMARY]
GO
CREATE TRIGGER [dbo].[update_final_phone]
ON dbo.phone_numbers
AFTER INSERT
AS
BEGIN
insert into
phone_output(num, phone_name)
select distinct
i.num,
i.phone_name
from
inserted i
left outer join phone_output p
on i.num = p.num
where
p.num is null
update
phone_output
set
final_phone = coalesce(final_phone + '<br>','') + phone
from
inserted i
where
phone_output.num = i.num
END
GO
insert into phone_numbers (num,phone_name,phone) values
(1,'test1','12345')
insert into phone_numbers (num,phone_name,phone) values
(1,'test1','23455')
insert into phone_numbers (num,phone_name,phone) values
(2,'test2','68638')
insert into phone_numbers (num,phone_name,phone) values
(3,'test3','67684')
insert into phone_numbers (num,phone_name,phone) values
(4,'test4','54808')
insert into phone_numbers (num,phone_name,phone) values
(4,'test4','74594')
go
select * from phone_numbers
select * from phone_output
-- End SQL
> > What exactly do you want to do? Maybe there is a better way of doing
> > it that you haven't come across.
[quoted text clipped - 93 lines]
>
> - Show quoted text -
Jason Lepack - 30 May 2007 14:33 GMT
I created delete and update triggers... here's the complete ddl.
CREATE TABLE [dbo].[phone_numbers](
[num] [int] NOT NULL,
[phone_name] [varchar](50) NOT NULL,
[phone] [varchar](50) NOT NULL
) ON [PRIMARY]
GO
CREATE UNIQUE NONCLUSTERED INDEX [nat_key] ON [dbo].[phone_numbers]
(
[num] ASC,
[phone] ASC
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[phone_output](
[num] [int] NOT NULL,
[phone_name] [nvarchar](50) NOT NULL,
[final_phone] [varchar](max) NULL
) ON [PRIMARY]
GO
CREATE TRIGGER [dbo].[insert_final_phone]
ON dbo.phone_numbers
AFTER INSERT
AS
BEGIN
insert into
phone_output(num, phone_name)
select distinct
i.num,
i.phone_name
from
inserted i
left outer join phone_output p
on i.num = p.num
where
p.num is null
update
phone_output
set
final_phone = coalesce(final_phone + '<br>','') +
phone
from
inserted i
where
phone_output.num = i.num
END
GO
CREATE TRIGGER [dbo].[delete_final_phone]
ON dbo.phone_numbers
AFTER DELETE
AS
BEGIN
update
phone_output
set
final_phone = replace(final_phone, d.phone+'<br>', '')
from
deleted d
where
phone_output.num = d.num
update
phone_output
set
final_phone = replace(final_phone, d.phone, '')
from
deleted d
where
phone_output.num = d.num
delete phone_output
where final_phone = ''
END
GO
CREATE TRIGGER [dbo].[update_final_phone]
ON dbo.phone_numbers
AFTER UPDATE
AS
BEGIN
update
phone_output
set
final_phone = replace(final_phone, d.phone+'<br>', '')
from
deleted d
where
phone_output.num = d.num
update
phone_output
set
final_phone = replace(final_phone, d.phone, '')
from
deleted d
where
phone_output.num = d.num
delete phone_output
where final_phone = ''
insert into
phone_output(num, phone_name)
select distinct
i.num,
i.phone_name
from
inserted i
left outer join phone_output p
on i.num = p.num
where
p.num is null
update
phone_output
set
final_phone = coalesce(final_phone + '<br>','') +
phone
from
inserted i
where
phone_output.num = i.num
END
GO
insert into phone_numbers (num,phone_name,phone) values
(1,'test1','12345')
insert into phone_numbers (num,phone_name,phone) values
(1,'test1','23455')
insert into phone_numbers (num,phone_name,phone) values
(1,'test1','68638')
insert into phone_numbers (num,phone_name,phone) values
(3,'test3','67684')
insert into phone_numbers (num,phone_name,phone) values
(4,'test4','54808')
insert into phone_numbers (num,phone_name,phone) values
(4,'test4','74594')
go
delete phone_numbers where num = 1 and phone = '23455'
delete phone_numbers where num = 3
go
update phone_numbers set num = 2 where phone = '54808'
update phone_numbers set phone = '7686' where phone = '12345'
go
> I created two tables and a trigger and I have all the functionality
> that you wanted, without having to create a cursor. The key is to let
[quoted text clipped - 180 lines]
>
> - Show quoted text -
> Actually i will combine the multiple numbers into one single record
>
[quoted text clipped - 20 lines]
>
> this view would be sent to UI for display...
GROUP BY is not your guy, in this case. See this link for some ideas on
to produce concatenated lists:
http://www.projectdmx.com/tsql/rowconcatenate.aspx

Signature
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx