Click here to Skip to main content
15,921,452 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi all,

I have a table Test having three columns as Name, age and salary.

I want query as

select * from Test order by Name
select * from Test order by age
select * from Test order by salary

I want to use it using stored procedure means I want to pass one values from

order by Name ,order by age,order by salary in a parameter in SP.

Such that if I pass order by Name then I my query in SP should be
select * from Test order by Name.

I created SP as but getting error.
Please help

create proc Tesstify
@string varchar(max)
@abcc varchar(max)
set @abcc=@string
select * from abc @abcc

Mohd Wasif
walterhevedeich 30-May-11 2:11am    
What's the error?
Sandeep Mewara 30-May-11 2:13am    
You have asked so many question and yet you fail to add the error here which will help others in helping you out.
Mohd Wasif 30-May-11 2:25am    
Question is solved

If you need help with basics, you can see this link[^].
Share this answer
Mohd Wasif 30-May-11 2:25am    
Please try Dynamic SQL

DECLARE @tmp AS TABLE ( Name VARCHAR(10), Age INT, Salary INT)
INSERT INTO @tmp(Name,Age,Salary) (SELECT 'A',1,10)
INSERT INTO @tmp(Name,Age,Salary) (SELECT 'C',10,11)
INSERT INTO @tmp(Name,Age,Salary) (SELECT 'B',100,12)
DECLARE @YourParameter VARCHAR(10)
SET @YourParameter =1
SET @DynamicSQL = 'SELECT * FROM @tmp ORDER BY ' + @YourParameter

Execute Dynamic SQL commands in SQL Server
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900