You are here: Query Syntax help « MsSQL Server « IT news, forums, messages
Query Syntax help

Posted by hharry on 09/17/05 05:06

Hello All,

I have the following table:

CREATE TABLE [dbo].[TBL_NAME] (
[NAME] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[STANDARD_NAME] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS
NULL
) ON [PRIMARY]
GO

With values:

insert into tbl_name
values('DAN', 'DANIEL')
insert into tbl_name
values('DANNY', 'DANIEL')
insert into tbl_name
values('DANYY', 'DANIEL')

Question is:

I need want to construct a query which returns all names for a standard
name plus the standard name itself.
e.g.
if name = 'DAN' then return 'DAN', 'DANNY', 'DANYY', 'DANIEL'
ff name = 'DANIEL', then return 'DAN', 'DANNY', 'DANYY', 'DANIEL'

i have the following sql:

declare @name varchar(50)
select @name = 'DANIEL'
select standard_name from tbl_name where name = @name
union
select name from tbl_name where standard_name = (select standard_name
from tbl_name where name = @name)
union
select name from tbl_name where standard_name = @name
union
select standard_name from tbl_name where standard_name = @name

----

declare @name varchar(50)
select @name = 'DANNY'
select standard_name from tbl_name where name = @name
union
select name from tbl_name where standard_name = (select standard_name
from tbl_name where name = @name)
union
select name from tbl_name where standard_name = @name
union
select standard_name from tbl_name where standard_name = @name

----

Both appear to work fine..can anyone see a fault or suggest a cleaner
way to achieve the above ?

Suggestions/pointers appreciated
Thanks in advance

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация