Question : How to add Order by in a SQL stored procedure

I have the following stored procedure.  I am trying to add an ORDER BY [RQH_iID] DESC which works for that variable but the other variables it does not work.  Any ideas as to how I can get this to work?  Basically what I am looking for is for every variable the information comes back sort by order [RQH_iID].

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
USE [utc]
GO
/****** Object:  StoredProcedure [dbo].[sp_utRequestQuoteHeaderListFiltered]    Script Date: 08/23/2010 15:12:49 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER  PROCEDURE [dbo].[sp_utRequestQuoteHeaderListFiltered]
	@UserID int,
	@RQH_iStatus int,
	@OnlyHW bit,
	@OnlyQR bit
AS
DECLARE @sql        nvarchar(4000)

SELECT @sql = '
	SELECT 	[RQH_iID],
	RQH.[UserID],
	[RQH_dDate],
	[RQH_sTicketNumber],
	[STO_iID],
	[RQH_sLocContactName],
	[RQH_sLocContactPhone],
	[RQH_sLocShippingAdress],
	[RQH_sLocShippingAdress1],
	[RQH_sLocShippingAdress2],
	[RQH_sLocShippingCity],
	[RQH_sLocShippingState],
	[RQH_sLocShippingZip],
	[RQH_sTrackingOut1],
	[RQH_sTrackingOut2],
	[RQH_sTrackingOut3],
	[RQH_sTrackingIn1],
	[RQH_sTrackingIn2],
	[RQH_sTrackingIn3],
	[RQH_sNotes],
	[RQH_lHotSwap],
	[RQH_iStatus],
	[RQH_dCreatedOn],
	[RQH_lCancelled],
	[RQH_dClosedOn]
FROM utRequestQuoteHeader  RQH WHERE 1=1 ORDER BY [RQH_iID] DESC'  

/* User Id */
IF @UserID <> 0                                          
	SELECT @sql = @sql + ' AND RQH.UserId = '+ convert(varchar(20),@UserId)
/* Status */
IF @RQH_iStatus <> -1                                      
	SELECT @sql = @sql + ' AND RQH_iStatus = '+ convert(varchar(20),@RQH_iStatus)

/* Hot Swap */
IF @OnlyHW = 1
	SELECT @sql = @sql + ' AND RQH_lHotSwap = 1'

/* Hot Swap */
IF @OnlyQR = 1
	SELECT @sql = @sql + ' AND (RQH_lHotSwap = 0 or RQH_lHotSwap is Null)'


	
EXEC(@sql)

Answer : How to add Order by in a SQL stored procedure

you have to add the order by AFTER the where clauses...
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
USE [utc]
GO
/****** Object:  StoredProcedure [dbo].[sp_utRequestQuoteHeaderListFiltered]    Script Date: 08/23/2010 15:12:49 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER  PROCEDURE [dbo].[sp_utRequestQuoteHeaderListFiltered]
        @UserID int,
        @RQH_iStatus int,
        @OnlyHW bit,
        @OnlyQR bit
AS
DECLARE @sql        nvarchar(4000)
SET NOCOUNT ON
SET @sql = '
        SELECT  [RQH_iID],
        RQH.[UserID],
        [RQH_dDate],
        [RQH_sTicketNumber],
        [STO_iID],
        [RQH_sLocContactName],
        [RQH_sLocContactPhone],
        [RQH_sLocShippingAdress],
        [RQH_sLocShippingAdress1],
        [RQH_sLocShippingAdress2],
        [RQH_sLocShippingCity],
        [RQH_sLocShippingState],
        [RQH_sLocShippingZip],
        [RQH_sTrackingOut1],
        [RQH_sTrackingOut2],
        [RQH_sTrackingOut3],
        [RQH_sTrackingIn1],
        [RQH_sTrackingIn2],
        [RQH_sTrackingIn3],
        [RQH_sNotes],
        [RQH_lHotSwap],
        [RQH_iStatus],
        [RQH_dCreatedOn],
        [RQH_lCancelled],
        [RQH_dClosedOn]
FROM utRequestQuoteHeader  RQH WHERE 1=1 '  

/* User Id */
IF @UserID <> 0                                          
        SET @sql = @sql + ' AND RQH.UserId = '+ convert(varchar(20),@UserId)
/* Status */
IF @RQH_iStatus <> -1                                      
        SET @sql = @sql + ' AND RQH_iStatus = '+ convert(varchar(20),@RQH_iStatus)

/* Hot Swap */
IF @OnlyHW = 1
        SET @sql = @sql + ' AND RQH_lHotSwap = 1'

/* Hot Swap */
IF @OnlyQR = 1
        SET @sql = @sql + ' AND (RQH_lHotSwap = 0 or RQH_lHotSwap is Null)'


SET @sql = @sql + ' ORDER BY [RQH_iID] DESC '
        
EXEC(@sql)
Random Solutions  
 
programming4us programming4us