| Author |
Topic |
|
barflyz
Starting Member
47 Posts |
Posted - 2010-04-23 : 14:18:54
|
Hi, I am fairly new to advanced SQL in SQL Server.I have the following in a table.ID Products1 a1 b1 c2 a2 b3 r3 y3 rI want to list the ID in a row (1 row per id) followed by a new column which would concatenete the values in the products column.hopeful result from example aboveid products1 a,b,c2 a,b3 a,b,r,y,rThere is an unknown max of products per ID so it has to be dynamic.If you can post is an descriptive simple manner that would help!thanks so much  |
|
|
pk_bohra
Master Smack Fu Yak Hacker
1182 Posts |
|
|
ms65g
Constraint Violating Yak Guru
497 Posts |
Posted - 2010-04-23 : 14:25:48
|
| Using UDF:CREATE FUNCTION dbo.Concat (@id INT) RETURNS VARCHAR(200) ASBEGINDECLARE @Concat VARHCAR(200);SELECT @Concat = ISNULL(@Concat+',', '') + productFROM table_nameWHERE id = @idRETURN @ConcatENDSELECT id, dbo.Concat(id)FROM table_nameGROUP BY id |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-04-23 : 14:32:40
|
| If its SQL 2005, better to go for XML method it performs better than UDF------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
barflyz
Starting Member
47 Posts |
Posted - 2010-04-23 : 14:58:29
|
| sorry for these questions but what does UDF stand for?Also, does (@id INT) I am referring to id from the table? what does (@id INT) mean in business terms? how does it know when to stop looping? thanksUsing UDF:CREATE FUNCTION dbo.Concat (@id INT) RETURNS VARCHAR(200)ASBEGINDECLARE @Concat VARHCAR(200);SELECT @Concat = ISNULL(@Concat+',', '') + productFROM table_nameWHERE id = @idRETURN @ConcatENDSELECT id, dbo.Concat(id)FROM table_nameGROUP BY id |
 |
|
|
ms65g
Constraint Violating Yak Guru
497 Posts |
Posted - 2010-04-23 : 15:17:39
|
| @id is a input parameter that use in body of user-defined function.First execute CREATE statement then use SELECT statement. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-04-23 : 15:18:57
|
quote: Originally posted by barflyz sorry for these questions but what does UDF stand for?Also, does (@id INT) I am referring to id from the table? what does (@id INT) mean in business terms? how does it know when to stop looping? thanksUsing UDF:CREATE FUNCTION dbo.Concat (@id INT) RETURNS VARCHAR(200)ASBEGINDECLARE @Concat VARHCAR(200);SELECT @Concat = ISNULL(@Concat+',', '') + productFROM table_nameWHERE id = @idRETURN @ConcatENDSELECT id, dbo.Concat(id)FROM table_nameGROUP BY id
see below to understand different UDF types with exampleshttp://www.sqlteam.com/article/user-defined-functions------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
barflyz
Starting Member
47 Posts |
Posted - 2010-04-28 : 09:00:21
|
| How do I pull this function and insert the data into a column into a table?thanks |
 |
|
|
karthik_padbanaban
Constraint Violating Yak Guru
263 Posts |
Posted - 2010-04-28 : 09:08:53
|
| declare @tbl table (id int,part varchar(50)) insert into @tbl values(1,'a')insert into @tbl values(1,'b')insert into @tbl values(1,'c')insert into @tbl values(2,'a')insert into @tbl values(2,'b')insert into @tbl values(3,'r')insert into @tbl values(3,'y')insert into @tbl values(3,'r') SELECT t.id,STUFF((SELECT ','+ part FROM @tbl WHERE id=t.id FOR XML PATH('')),1,1,'') AS AFROM (SELECT DISTINCT id FROM @tbl)tKarthikhttp://karthik4identity.blogspot.com/ |
 |
|
|
barflyz
Starting Member
47 Posts |
Posted - 2010-04-28 : 09:37:57
|
| HI, I am trying to use the example above like shown below. I want to concatenate many codes (varchar (200)) for each pid (numeric 19,0) but the code below gives me the error as - Error: Arithmetic overflow error converting varchar to data type numeric. (State:22003, Native Code: 1FB3)CREATE FUNCTION dbo.Concat (@pid numeric(19,0)) RETURNS numeric(19,0)ASBEGINDECLARE @Concat varchar(200);SELECT @Concat = ISNULL(@Concat+',', '') + codeFROM problemWHERE pid = @pidRETURN @ConcatENDSELECT pid, dbo.Concat(pid)FROM problemGROUP BY pid |
 |
|
|
ms65g
Constraint Violating Yak Guru
497 Posts |
Posted - 2010-04-28 : 09:48:08
|
quote: Originally posted by barflyz HI, I am trying to use the example above like shown below. I want to concatenate many codes (varchar (200)) for each pid (numeric 19,0) but the code below gives me the error as - Error: Arithmetic overflow error converting varchar to data type numeric. (State:22003, Native Code: 1FB3)CREATE FUNCTION dbo.Concat (@pid numeric(19,0)) RETURNS numeric(19,0)ASBEGINDECLARE @Concat varchar(200);SELECT @Concat = ISNULL(@Concat+',', '') + codeFROM problemWHERE pid = @pidRETURN @ConcatENDSELECT pid, dbo.Concat(pid)FROM problemGROUP BY pid
the red line should be:RETURNS VARCHAR(200) |
 |
|
|
barflyz
Starting Member
47 Posts |
Posted - 2010-04-28 : 09:48:26
|
| I may have figured it out. Will post if notthanks to all the experts!you are the best! |
 |
|
|
trouble2
Constraint Violating Yak Guru
267 Posts |
Posted - 2011-11-08 : 07:16:27
|
| HI I made this concat function:ALTER FUNCTION [dbo].[ConcatCategories](@ArticleID VARCHAR(10))RETURNS VARCHAR(8000)ASBEGIN DECLARE @Output VARCHAR(8000) SET @Output = '' SELECT @Output = CASE @Output WHEN '' THEN CAST(CategoryID AS VARCHAR) ELSE @Output + ', ' + CAST(CategoryID AS VARCHAR) END FROM ArticleCategories WHERE ArticleID = @ArticleID ORDER BY CategoryID RETURN @OutputENDHowever, when I get a list of articles, it is kind of a heavy function for SQL server.So is there a better waY?The list sp would be something like:select articleid, article, ..., Categories = dbo.ConcatCategories(ArticleID)from articleswhere ....etc.The secret to creativity is knowing how to hide your sources. (Einstein) |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-11-08 : 07:30:07
|
quote: Originally posted by trouble2 HI I made this concat function:ALTER FUNCTION [dbo].[ConcatCategories](@ArticleID VARCHAR(10))RETURNS VARCHAR(8000)ASBEGIN DECLARE @Output VARCHAR(8000) SET @Output = '' SELECT @Output = CASE @Output WHEN '' THEN CAST(CategoryID AS VARCHAR) ELSE @Output + ', ' + CAST(CategoryID AS VARCHAR) END FROM ArticleCategories WHERE ArticleID = @ArticleID ORDER BY CategoryID RETURN @OutputENDHowever, when I get a list of articles, it is kind of a heavy function for SQL server.So is there a better waY?The list sp would be something like:select articleid, article, ..., Categories = dbo.ConcatCategories(ArticleID)from articleswhere ....etc.The secret to creativity is knowing how to hide your sources. (Einstein)
please dont reopen old threadspost your question as a new threadAnyways, fr you if you're using sql 2005 or above use STUFF method given in first link------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|