Question : Syntax for Passing parameters from .net form  to use for  ORDER BY values

vb.net 2008
sql server express 2005

I have a stored procedure that I use and pass a parameter for its value from a form.
USE [DescMatchGisXref]
GO
/****** Object:  StoredProcedure [dbo].[get_View_SAP_Data_Grid1]    Script Date: 07/20/2010 00:59:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:            <Author,,Name>
-- Create date: <Create Date,,>
-- Description:      <Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[get_View_SAP_Data_Grid1] @param3 nvarchar(100)  
AS      
SELECT     dbo.SapData.MFRNUM, dbo.SapData.MFRNAME, dbo.SapData.WWGDESC, dbo.SapData.ITEM, dbo.SapData.XREF, dbo.SapData.SPIN,
dbo.SapData.REDBOOKNUM, dbo.SapData.WWGMFRNAME, dbo.SapData.WWGMFRNUM, WwgExtra.dbo.SapAltData.ALT1,
WwgExtra.dbo.SapAltData.ALT1SPIN, dbo.SapData.RICHTEXT, dbo.SapData.COMMENTS, WwgExtra.dbo.SapAltData.ALT1DESC,
WwgExtra.dbo.SapAltData.ALT1DESC2, WwgExtra.dbo.WwgExtra.GREEN, WwgExtra.dbo.WwgExtra.SEGMENT,
WwgExtra.dbo.WwgExtra.PRIVATE_LABEL
FROM dbo.SapData INNER JOIN
WwgExtra.dbo.WwgExtra ON dbo.SapData.ITEM = WwgExtra.dbo.WwgExtra.ITEM INNER JOIN
WwgExtra.dbo.SapAltData ON WwgExtra.dbo.WwgExtra.ITEM = WwgExtra.dbo.SapAltData.ITEM
WHERE SapData.MFRNUM LIKE '' + @param3 + '%'

This stored procedure is filling a datagridview.

What I need:
I want to us this stored procedure but I want to sort my grid by 2 columns so I need a unique  stored procedure to do this.

So I need to pass 2 values from my form
combobox1  will have column names
combobox2 will have column names

I want to use the values from these comboboxes and insert them into my stored procedure for the ORDER BY  

so I'm guessing something like
-----------------------------------------------------------------
ALTER PROCEDURE [dbo].[get_View_SAP_Data_Grid1] @param3 nvarchar(100)   <------------- HERE FOR NEW PARAMETERS ? 4 AND 5  
......
...
WHERE SapData.MFRNUM LIKE '' + @param3 + '%' order by '' + @param4 + '' , '' + @param5 + ''

Thanks
fordraiders



 


 

Answer : Syntax for Passing parameters from .net form  to use for  ORDER BY values

Order by isn't something you can do with a parameter without doing dynamic SQL.  That is perfectly acceptable though.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
ALTER PROCEDURE [dbo].[get_View_SAP_Data_Grid1] @param3 nvarchar(100)  ,@param4 nvarchar(100) ,@param5 nvarchar(100) 
AS      
set nocount on
declare @sql nvarchar(max)
set @SQN = N'
SELECT     dbo.SapData.MFRNUM, dbo.SapData.MFRNAME, dbo.SapData.WWGDESC, dbo.SapData.ITEM, dbo.SapData.XREF, dbo.SapData.SPIN,
dbo.SapData.REDBOOKNUM, dbo.SapData.WWGMFRNAME, dbo.SapData.WWGMFRNUM, WwgExtra.dbo.SapAltData.ALT1,
WwgExtra.dbo.SapAltData.ALT1SPIN, dbo.SapData.RICHTEXT, dbo.SapData.COMMENTS, WwgExtra.dbo.SapAltData.ALT1DESC,
WwgExtra.dbo.SapAltData.ALT1DESC2, WwgExtra.dbo.WwgExtra.GREEN, WwgExtra.dbo.WwgExtra.SEGMENT,
WwgExtra.dbo.WwgExtra.PRIVATE_LABEL
FROM dbo.SapData INNER JOIN
WwgExtra.dbo.WwgExtra ON dbo.SapData.ITEM = WwgExtra.dbo.WwgExtra.ITEM INNER JOIN
WwgExtra.dbo.SapAltData ON WwgExtra.dbo.WwgExtra.ITEM = WwgExtra.dbo.SapAltData.ITEM
WHERE SapData.MFRNUM LIKE  @param3 + ''%''
order by ' + @param4 + ' , ' + @param5 

exec sp_Executesql @SQL,N'@param3 nvarchar(100)',@param3=@param3
Random Solutions  
 
programming4us programming4us