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
 General SQL Server Forums
 New to SQL Server Programming
 order by procedure

Author  Topic 

eugz
Posting Yak Master

210 Posts

Posted - 2009-09-11 : 14:47:30
Hi All.
How to sort that procedure by First Name? After execution I got error message:
Incorrect syntax near the keyword 'Union'.

Thanks.
quote:
procedure [dbo].[sp_GetEmployee]
(
@BlankAll varchar(1)
)
as
Declare @SQLStr as NVarChar(4000)
Set @SQLStr = 'select user_id
,RTrim(First_Name) + '' '' + RTrim(Last_Name) Employee
from dbo.v_UserName
Order by First_Name
'
(@InActive as Varchar(10))
If @BlankAll = 0 OR @BlankAll = 2 Set @SQLStr = @SQLStr + ' Union Select 0,'''''
Select -1,''ALL'',''ALL'','''','''+@StandardType+''','''',0,0'
Set @SQLStr = @SQLStr
print @SQLStr
exec sp_executeSQL @SQLStr

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2009-09-11 : 14:59:48
the ORDER by has to be the final clause in the statement (after any UNIONs).

Looks like your statement after the UNION has a different number of columns then the statement prior to the UNION. That will also error. It also looks like you don't even need dynamic sql for this. Can you just put a where clause in your second statement like: where @blankAll in (0,2)

Be One with the Optimizer
TG
Go to Top of Page
   

- Advertisement -