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 2005 Forums
 Transact-SQL (2005)
 If condition

Author  Topic 

rsegecin
Yak Posting Veteran

82 Posts

Posted - 2007-03-21 : 13:53:40
I knew in SQL server and now I'm using SQl Server 2005. Does somebody knows what is wrong with that procedure?

create procedure teste

@PageOrder varchar(10),
@PageWay bit

As
Begin

if (@PageWay = 1)
begin
Select * order by @PageOrder asc from friends
end
else
begin
Select * order by @PageOrder desc from friends
end

End
Go

nr
SQLTeam MVY

12543 Posts

Posted - 2007-03-21 : 13:59:43
Select * order by @PageOrder asc from friends
s.b
Select * from friends order by xxxxx asc

can't use a variable in the order by clause

Select * from friends order by case when @PageOrder = 'mycol' then mycol end asc, case when @PageOrder = 'mycol2' then mycol2 end asc, case when @PageOrder = 'mycol3' then mycol3 end asc

or maybe
exec ('Select * from friends order by ' + @PageOrder + ' asc')
but the use will need permission on the table.
And someone will tell you to watch out for injection.

==========================================
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.
Go to Top of Page
   

- Advertisement -