Solved

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

Posted on 2009-07-09
3
154 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
  • 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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

813 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now