Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
chapo
Starting Member
39 Posts |
Posted - 2008-03-15 : 08:57:42
|
| I have the following function that I was able to put together with the help of the following article [url][/url]http://sqlblog.com/blogs/adam_machanic/archive/2006/07/12/rowset-string-concatenation-which-method-is-best.aspx but I'm having some problems with it any help would be greatly appreciated.USE database1GO CREATE FUNCTION dbo.Concatdwg_Seq (@prt_Mark CHAR(2))RETURNS VARCHAR(8000)ASBEGIN DECLARE @Output VARCHAR(8000) SET @Output = '' SELECT @Output = CASE @Output WHEN '' THEN dwg_Seq ELSE @Output + ', ' + dwg_Seq END FROM dbo.Un_Combined WHERE prt_Mark = @prt_Mark ORDER BY dwg_Seq RETURN @OutputENDGOprt_Mark | dwg_Seq 12 | 12a,23b 25c | 1b,5e,8d,100asI get the following errorsMsg 325, Level 15, State 1, Line 2Incorrect syntax near 'FUNCTION'. You may need to set the compatibility level of the current database to a higher value to enable this feature. See help for the stored procedure sp_dbcmptlevel.Msg 137, Level 15, State 2, Line 14Must declare the scalar variable "@prt_Mark".Msg 178, Level 15, State 1, Line 17A RETURN statement with a return value cannot be used in this context.Msg 156, Level 15, State 1, Line 1Incorrect syntax near the keyword 'To'. |
|
|
ranganath
Posting Yak Master
209 Posts |
Posted - 2008-03-15 : 09:56:44
|
| HI,No Need to write Function this will be usefuldeCLARE @StrConcat tABLE (col1 nvarchar(10),col2 nvarchar(10))insert into @StrConcatselect 'db1','host1' union all select 'db1','host2' union all select 'db1','host3' union all select 'db2','host1' union all select 'db2','host2' union all select 'db3','host2' union all select 'db3','host3'Declare @col1 varchar(100)Set @col1 = 'Db1'select col1, stuff( ( select ', '+ col2 from @StrConcat t1 where t2.col1 = t1.col1 for xml path('')),1,1,'')from @StrConcat t2 where col1 = @Col1group by col1 order by col1 |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2008-03-15 : 11:23:43
|
quote: I'm having some problems with it
what is the problem ? KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
dataguru1971
Master Smack Fu Yak Hacker
1464 Posts |
Posted - 2008-03-15 : 11:54:30
|
[code]BEGINDECLARE @Output VARCHAR(8000)SET @Output = ''SELECT @Output = CASE @Output WHEN '' THEN dwg_Seq ELSE @Output + ', ' + dwg_Seq ENDFROM dbo.Un_CombinedWHERE prt_Mark = @prt_MarkORDER BY dwg_SeqRETURN @OutputEND[/code]based on that the first Case evaluation will always be true. you SET @Concat = '' then evaluate if it = '' it will always be true.It seems your intent is to concat the values returned in the rows into a string. A Case statement won't do that..it will return rows based on the evaluation.The variable declaration error is fruit of the poisoned tree by way of the Create Function error returned. What version of SQL are you trying to create this on? and what version is "database1" ?What is sample source data, and what is desired output result? Poor planning on your part does not constitute an emergency on my part. |
 |
|
|
chapo
Starting Member
39 Posts |
Posted - 2008-03-15 : 12:28:41
|
| Thanks for the quick responses everyone.dataguru1971I'm using SQL 2005I'm no sure if this is the information you are looking when it comes to my database version 9.00.1399.06 Here is some sample dataI have multiple tables link together base on wo_No and prt_Mark fields and I get the following results.wo_No | prt_Mark | dwg_Seq |324037 | d400 | 1S2 |324037 | d400 | A1 |324037 | d400 | 1 |219001 | 56 | 2B |219001 | 56 | 2C |219001 | 56 | 2C |What I would like is to combine the unique dwg_Seq where they have the same wo_No and prt_Mark. 324037 | d400 | 1S2,A1,1 |219001 | 56 | 2B,2C |If there is anything else that you required let me know. |
 |
|
|
dataguru1971
Master Smack Fu Yak Hacker
1464 Posts |
Posted - 2008-03-15 : 22:45:44
|
Actually, I see where you were going, and my comment was wrong for that particular scenario (as to the case order)as I said with your GO statements, you can't put those in the "middle" of the function or errors will throw.you were clearly following the syntax from here:http://sqljunkies.com/WebLog/amachanic/archive/2004/11/10/5065.aspx?Pending=truebut were slightly off..your variable needs to be at least 3 to work since the field value being passed can be 3 characters. Also, add a group by to ensure that unique results are returned.The following works for me to get your desired results:CREATE TABLE foo (wo_No int not null ,prt_Mark nvarchar(4) not null ,dwg_Seq nvarchar(3) not null)Insert Into foo (wo_No,prt_mark,dwg_Seq)Select 324037, 'd400', '1S2' UNION ALLSelect 324037, 'd400', 'A1' UNION ALLSelect 324037, 'd400', '1' UNION ALLSelect 219001, '56', '2B' UNION ALLSelect 219001, '56', '2C' UNION ALLSelect 219001, '56', '2C' goCREATE FUNCTION dbo.Concatdwg_Seq (@prt_Mark CHAR(4))RETURNS VARCHAR(8000)ASBEGINDECLARE @Output VARCHAR(8000)SET @Output = ''SELECT @Output = CASE @Output WHEN '' THEN dwg_Seq ELSE @Output + ', ' + dwg_Seq ENDFROM fooWHERE prt_Mark = @prt_MarkGroup by dwg_SeqORDER BY dwg_SeqRETURN @OutputENDGOSelect wo_no,prt_Mark, dbo.Concatdwg_Seq (prt_mark)FROM fooGROUP by wo_no,prt_Mark, dbo.Concatdwg_Seq (prt_mark)ORDER By wo_noGODrop Table fooDrop Function dbo.Concatdwg_Seq However, I am not sure why you got the errors, but I got this to work.wo_no prt_Mark (No column name)219001 56 2B, 2C324037 d400 1, 1S2, A1 Poor planning on your part does not constitute an emergency on my part. |
 |
|
|
|
|
|
|
|