Solved

how to pass parameter for order by class from asp.net website to store procedure

Posted on 2009-07-09
3
157 Views
Last Modified: 2012-05-07
I am trying to create a stored proc which will take a optional parameter
and if it passed from my website then its value should add in the order by clause.

I tried to write the proc like below but it does not work. I got an compile error.

any one could help me out here
create PROCEDURE [dbo].[sp_GetRecord]
	@ClipSerialNumberID int	,
	@SortColumnName varchar(100) = null
	
AS
BEGIN
	if(@SortColumnName IS null)
	BEGIN 
		SELECT [OID],[Name],[ClipSerialNumberID]
		FROM [Record]
		where ClipSerialNumberID  = @ClipSerialNumberID  
	END
	ELSE
	BEGIN
		SELECT [OID],[Name],[ClipSerialNumberID]
		FROM [Record]
		where ClipSerialNumberID  = @ClipSerialNumberID 
		ORDER BY @SortColumnName
	END	
	
END

Open in new window

0
Comment
Question by:ziorrinfotech
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 125 total points
ID: 24815926
create PROCEDURE [dbo].[sp_GetRecord]
      @ClipSerialNumberID int      ,
      @SortColumnName varchar(100) = null
     
AS
BEGIN
      if(@SortColumnName IS null)
      BEGIN
            SELECT [OID],[Name],[ClipSerialNumberID]
            FROM [Record]
            where ClipSerialNumberID  = @ClipSerialNumberID  
      END
      ELSE
      BEGIN
            DECLARE @nSQl NVARCHAR(1000)
            SELECT @nSQl = '
            SELECT [OID],[Name],[ClipSerialNumberID]
            FROM [Record]
            where ClipSerialNumberID  = @ClipSerialNumberID
            ORDER BY ' + @SortColumnName
        EXEC sp_ExecuteSQL @nSQl, N'@ClipSerialNumberID int ' ,@ClipSerialNumberID  
      END      
     
END

0
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 24815966
As aneeshattingal has pointed out, you would have to execute the query with dynamic SQL.  

http:#a24815926 is the correct solution
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 24815980
or else you have to use a Large case statement

ORDER BY case (@SortColumnName ) WHEN 'ID' THEN ID
                                                               WHEN 'Name' THEN NAME,
                                                               ELSE ClipSerialNumberID END
0

Featured Post

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question