473,400 Members | 2,145 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,400 software developers and data experts.

Parameter.Add() problem

Hi,
I was looking at this method within the SqlCommand class. It is the
SqlCommand.Parameter.Add(string, object) method.

My question is, how do I use this parameter within a stored procedure?

What i am really trying to do is dynamically change the ORDER BY method
within the SPROC. For example (i know this won't work):
CREATE PROCEDURE dbo.ClassFindAll
(
@sortBy VARCHAR(50)
)
AS
SELECT *
FROM Classes
ORDER BY @sortBy

Can anyone help?

Thank you,

Tim.
Nov 16 '05 #1
1 3358
Timothy,

SQL won't allow you to do this. If you need it to be truly
configurable, then you would have to call sp_exec, creating the SQL string,
and then making the call. However if you choose to do that, then all of the
benefits of stored procedures fly out the window. You would be better off
just creating a SQL command and executing that dynamically.

However, if you know that you will only sort on certain combinations of
fields, then what I would do is create an enumeration representing the
different orders you want to perform, and then have a conditional statement
in the stored procedure that would execute based on the value you pass in.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Timothy V" <tr****@msn.com> wrote in message
news:uW**************@tk2msftngp13.phx.gbl...
Hi,
I was looking at this method within the SqlCommand class. It is the
SqlCommand.Parameter.Add(string, object) method.

My question is, how do I use this parameter within a stored procedure?

What i am really trying to do is dynamically change the ORDER BY method
within the SPROC. For example (i know this won't work):
CREATE PROCEDURE dbo.ClassFindAll
(
@sortBy VARCHAR(50)
)
AS
SELECT *
FROM Classes
ORDER BY @sortBy

Can anyone help?

Thank you,

Tim.

Nov 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Dan | last post by:
I've run into an interesting problem, and seemed to have stumped 3 newsgroups and 2 other forums. For some reason when I try to insert a record into a SQL table that has a Text column, the...
4
by: andy | last post by:
i am coding a dll which contains a function take a const wchar_t* as parameter, call it form a exe, the wchar_t is passed to dll function, but the function in dll will then call another function...
5
by: Lasse Edsvik | last post by:
Hello I have this: public SqlParameter GetParameter(string sqlparam) { SqlParameter param = cmd.Parameters.Add(sqlparam);
3
by: Jesper Jensen | last post by:
I have the following problem: I have created the following SQL for my app. With the below shown code (Example 1) I am able to retrieve the records I need into dataset dsFind. Now however I want...
1
by: Vaelek | last post by:
Hello, I'm somewhat new to DB programming and have a question. I am now taking over an application that a previous programmer had written and he had made use of the Parameter.Add function for DB2...
11
by: pamelafluente | last post by:
Hi I am executing some simple sample code: Using OleDbCommand As New OleDbCommand(Me.DBQuery.Text, Me.OleDbConnection) Dim OleDbParameter As OleDbParameter =...
7
by: ashtek | last post by:
Hi, I have a generic function that executes a stored procedure & returns a data table. Code: === public static DataTable ExecuteStoredProcedure(string strProc,SqlParameter paramArray) {...
1
by: John Bailo | last post by:
This is a my solution to getting an Output parameter from a SqlDataSource. I have seen a few scant articles but none of them take it all the way to a solution. Hopefully this will help some...
1
by: Elmo Watson | last post by:
The date format in the 'weekending' textbox is 5/5/2007 Here's what I've got: With cmd.Parameters ..Add(New SqlParameter("@SQLweekending", SqlDbType.DateTime, weekending.Text)) the error...
5
by: Trevisc | last post by:
Happy Thursday Everyone, I am trying to create a parameter that is one long varchar but that will be used in a SQL statement IN function: //string queryString = GetCurrentTitles(); //Below is...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.