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.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 COALESCE ... ?

Author  Topic 

mahesh_bote
Constraint Violating Yak Guru

298 Posts

Posted - 2006-11-22 : 03:25:34
can anybody tell me how to use COALESCE function in inner query?

here is my sql statement...

DECLARE @EmpID VARCHAR(100)
SELECT
....
,(SELECT @EmpID = COALESCE(CONVERT(VARCHAR(100), @EmpID) + ', ', '') + CAST(Emp_ID AS VARCHAR(15)) FROM USER_MSTR WHERE User_ID IN ( SELECT User_ID FROM USER_DTL WHERE CO_ID = Com.CO_ID GROUP BY User_ID) AS Emp_ID
...
FROM ...

when i run this sql it showing me this error

"Incorrect syntax near '='."

whats wrong with syntax?

thanks in advance,

Mahesh

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2006-11-22 : 03:29:38
Remove @EmpID = to begin with.


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2006-11-22 : 03:36:47
No, that will not work.
You have use a function for this. There are a lot of examples of this here at SQLTeam.

See http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=53293 and http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=56058


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

mahesh_bote
Constraint Violating Yak Guru

298 Posts

Posted - 2006-11-22 : 04:19:59
thanks Peso,

is it possible to do without using any function, or can't we did it in single sql statement? I have already one function which convert Rows into Column(Comma separated values). When i learn about COALESCE function, i tried this with single sql, its working fine. Now i wants to implement it in my one of the inner query.

Mahesh
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2006-11-22 : 04:48:02
Not the way you want it, as a subquery.
Think about what will happen for netx row. @EmpID will already be initialized with a value...


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

mahesh_bote
Constraint Violating Yak Guru

298 Posts

Posted - 2006-11-22 : 04:51:02
thanks Peso,

i didn't considered the case. thank u very much. so it means i have to use function for this.

well, i will.

Mahesh
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2006-11-22 : 10:59:10
I contenst that the sql has to be this convulted



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page
   

- Advertisement -