| Author |
Topic |
|
vin
Starting Member
26 Posts |
Posted - 2003-03-13 : 05:29:04
|
| hi folks,i am trying to concatenate the values into a string,by reading the ename column from emp table.The sp gets created, but not able to print the o/p.How can i do this ?(This is a example, to simulate the actual requirement)alter procedure emp_spasdeclare @a varchar(500)declare @b varchar(700)declare emp_cus cursor for select distinct(ename) from empopen emp_cuswhile @@FETCH_STATUS=0beginfetch next from emp_cus into @aselect @b=@b + ' ' +@aprint @bendclose emp_cusdeallocate emp_cusgoexec emp_spThis displays no output. |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2003-03-13 : 06:10:33
|
| alter procedure emp_sp as declare @b varchar(700)select @b = coalesce(@b + ' ','') + enamefrom(select ename = distinct(ename) from emp) as aselect @bgoexec emp_spyou might want insteadalter procedure emp_sp @b varchar(700) outputas select @b = coalesce(@b + ' ','') + enamefrom(select ename = distinct(ename) from emp) as agodeclare @s varchar(700)exec emp_sp @s outputselect @s(oops , instead of +)==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy.Edited by - nr on 03/13/2003 10:49:52 |
 |
|
|
vin
Starting Member
26 Posts |
Posted - 2003-03-13 : 09:33:05
|
| Hi,Just realted to the above example,Using the selected values of the ename column, i will concatinte into a string (Which contains value+datataype+ ',') which i should use to create a table dynamically.The problem i am facing is, the last column value should not have a ',' in it. For eg, john varchar(20), this ',' should not be there for last value, but for all other columns it should be there. How can get this done. Is there any keyword with cursor, which can be used to findout beforehand how many values are there in the cursor, so that i can just select the last record and trim the comma for that...or is there any logic to get this done.could somebody guide me about the same |
 |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2003-03-13 : 10:48:27
|
| Why do you want to use a cursor?for ename values john, jim declare @b varchar(700) select @b = coalesce(@b + ', ','') + ename + ' varchar(20)'from (select ename = distinct(ename) from emp) as a should give'john varchar(20), jim varchar(20)'in @b.==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy.Edited by - nr on 03/13/2003 10:49:00 |
 |
|
|
vin
Starting Member
26 Posts |
Posted - 2003-03-14 : 00:59:13
|
| Hey thanx nr,but i am getting this error---------------alter procedure emp_spasdeclare @b varchar(700) select @b = coalesce(@b + ' ','') + ename from (select ename=distinct(ename) from emp) as aselect @b go -------------------Server: Msg 156, Level 15, State 1, Procedure emp_sp, Line 6Incorrect syntax near the keyword 'distinct'.-----------------------------------------Edited by - vin on 03/14/2003 01:03:59 |
 |
|
|
xpandre
Posting Yak Master
212 Posts |
Posted - 2003-03-14 : 04:16:13
|
| alter procedure emp_sp as declare @b varchar(700) set @b=''select @b = coalesce(@b + ' ','') + ename from (select distinct(ename) from emp) as a select @b go |
 |
|
|
vin
Starting Member
26 Posts |
Posted - 2003-03-14 : 05:17:03
|
| hey thanx xpandre,it is working....could u plese tell me how this is working.or could u tell me which part of doc i shouldrefer to understand this.and let me tell u, that i am very new to T-sqlthanx |
 |
|
|
SqlStar
Posting Yak Master
121 Posts |
Posted - 2003-03-14 : 09:59:13
|
| HiWhenever u create a varchar variable, initially the variable should be have NULL value only. so that NULL value with any value merged, that return value also NULL. I think ur doubt clear.Am i right?IT Knowledge is power |
 |
|
|
Page47
Master Smack Fu Yak Hacker
2878 Posts |
|
|
|