Tuesday, March 20, 2012

Could you write a special stored procedure for me in SQL 2005?

Could you write a special stored procedure for me in SQL 2005?

When I execute the SQL "select TagName from Th_TagTable whereIDTopic=@.IDTopic" , it return several rows such as
TagName= Sport
TagName= New
TagName= Health

I hope there is a stored procedure which can join all TagName into a single string and return,
it means when I pass @.IDTopic to the stored procedure, it return "Sport,New,Health"

How can write the stored procedure?

Hi,

You can use the below t-sql stateents to get the desired list of tagnames. COALESCE is the key of the below script


DECLARE @.IdTopic int
SET @.IdTopic = 1

DECLARE @.str AS nvarchar(255)

SELECT
@.str = COALESCE(@.str + ', ', '') + tagname
FROM Th_TagTable (NoLock)
WHERE IdTopic = @.IdTopic

SELECT @.str

You can better create a user-defined function instead of a stored procedure to get similar values.

Check the article and sample athttp://kodyaz.com/articles/article.aspx?articleid=29 which gets the list of a book's authors.

Eralper

http://www.kodyaz.com

|||Thanks!

No comments:

Post a Comment