473,418 Members | 2,044 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,418 software developers and data experts.

Query parameters - Tricky Stored Procedure Question

Hi there, I would like to create a simple search form to allow users to
search for a job number based on a number of parameters. I think I
understand how to use parameteres associated with Stored Procedures
with a data reader to add various parameters.

However, if I have a stored procedure such as

CREATE usp_SelectfromJobNumbers (@par1 datatype, @par2 datatype, @par3
datatype)

AS

SELECT FROM Jobs WHERE
PAR1 = @par1 AND
PAR2 = @par2 AND
PAR3 = @par3

I cannot just pass the one parameter to the procedure. The question
is then: how is it possible to create a stored procedure which can
accept any number of parameters an then smartly modify the query in the
store procedure with CASE statements or similar?

I don't really want to do this as in-line code as it easily becomes
unmaintainable and it is hard to add additional conditions. I was
thinking of passing a string array into the Stored Procedure and having
the stored procedure parse through the array, determine the parameters
and do the grunt of the work. The problem is that I haven't got a lot
of experience with these advanced stored procedures using CASE
statements and the like...
Any ideas would be greatly appreciated.
Regards,

Ric

Aug 4 '06 #1
5 2337
Hello ric_deez,

This is one case in which an inline ad-hoc query would be preferable I do
believe. Done right it is extremely maintainable. TSQL, while up to the
task, is not designed for such a query and would indeed be the less maintainable
solution.

Put a layer of abstraction between the UI and the query. This abstraction
layer would take as inputs the name of the field to search as well as the
value to search for. It would then give you a hashtable or some similar
construct which you could then loop over to construct your query.

Dont forget that ad-hoc queries can still take advantage of the SqlParameter
object (eg. SELECT field1 FROM table1 WHERE (field1 = @field1) -- tCommand.Parameters.AddWithValue("@field1",
value1) )

Enjoy,
-Boo
Hi there, I would like to create a simple search form to allow users
to search for a job number based on a number of parameters. I think I
understand how to use parameteres associated with Stored Procedures
with a data reader to add various parameters.

However, if I have a stored procedure such as

CREATE usp_SelectfromJobNumbers (@par1 datatype, @par2 datatype, @par3
datatype)

AS

SELECT FROM Jobs WHERE
PAR1 = @par1 AND
PAR2 = @par2 AND
PAR3 = @par3
I cannot just pass the one parameter to the procedure. The question
is then: how is it possible to create a stored procedure which can
accept any number of parameters an then smartly modify the query in
the store procedure with CASE statements or similar?

I don't really want to do this as in-line code as it easily becomes
unmaintainable and it is hard to add additional conditions. I was
thinking of passing a string array into the Stored Procedure and
having the stored procedure parse through the array, determine the
parameters and do the grunt of the work. The problem is that I
haven't got a lot of experience with these advanced stored procedures
using CASE statements and the like...

Any ideas would be greatly appreciated.

Regards,

Ric

Aug 4 '06 #2
Hi there Boo,

Can you please explain how to use the sqlParameter with ad-hoc queries?
>>Dont forget that ad-hoc queries can still take advantage of the SqlParameter
SELECT field1 FROM table1 WHERE (field1 = @field1) -- tCommand.Parameters.AddWithValue("@field1",
value1) )
Thanks!

Ric

GhostInAK wrote:
Hello ric_deez,

This is one case in which an inline ad-hoc query would be preferable I do
believe. Done right it is extremely maintainable. TSQL, while up to the
task, is not designed for such a query and would indeed be the less maintainable
solution.

Put a layer of abstraction between the UI and the query. This abstraction
layer would take as inputs the name of the field to search as well as the
value to search for. It would then give you a hashtable or some similar
construct which you could then loop over to construct your query.

Dont forget that ad-hoc queries can still take advantage of the SqlParameter
object (eg. SELECT field1 FROM table1 WHERE (field1 = @field1) -- tCommand.Parameters.AddWithValue("@field1",
value1) )

Enjoy,
-Boo
Hi there, I would like to create a simple search form to allow users
to search for a job number based on a number of parameters. I think I
understand how to use parameteres associated with Stored Procedures
with a data reader to add various parameters.

However, if I have a stored procedure such as

CREATE usp_SelectfromJobNumbers (@par1 datatype, @par2 datatype, @par3
datatype)

AS

SELECT FROM Jobs WHERE
PAR1 = @par1 AND
PAR2 = @par2 AND
PAR3 = @par3
I cannot just pass the one parameter to the procedure. The question
is then: how is it possible to create a stored procedure which can
accept any number of parameters an then smartly modify the query in
the store procedure with CASE statements or similar?

I don't really want to do this as in-line code as it easily becomes
unmaintainable and it is hard to add additional conditions. I was
thinking of passing a string array into the Stored Procedure and
having the stored procedure parse through the array, determine the
parameters and do the grunt of the work. The problem is that I
haven't got a lot of experience with these advanced stored procedures
using CASE statements and the like...

Any ideas would be greatly appreciated.

Regards,

Ric
Aug 4 '06 #3
Hello ric_deez,

I thought I had. However, if by explain you mean write you the entire code..
*sigh* fine.. but only because I don't hate you, yet. Thias is off the
top of my head...

Dim tConnection As SqlConnection = New SqlConnection(connection_string_here)
Dim tCommand As SqlCommand = New SqlCommand
Dim tAdapter As SqlDataAdapter = New SqlDataAdapter(tCommand)
Dim tTable as DataTable = New DataTable

With tCommand
.Connection = tConnection
.CommandType = CommandType.Text
.ComandText = "SELECT field1 FROM table1 WHERE (field1 = @value1)"
' Note: Here you would pass in your constructed ad-hoc query
.Parameters.AddWithValue("@value1", value1) ' See, the parameter
name corresponds to the parameter name in the query.. spiffy huh..
End With

tConnection.Open
tAdapter.Fill(tTable)
tConnection.Close
' And now you got a shiney new DataTable to show your friends

tAdapter = Nothing
tCommand = Nothing
tConnection = Nothing

-Boo
Hi there Boo,

Can you please explain how to use the sqlParameter with ad-hoc
queries?
>>Dont forget that ad-hoc queries can still take advantage of the
SqlParameter
SELECT field1 FROM table1 WHERE (field1 = @field1) --
tCommand.Parameters.AddWithValue("@field1",
value1) )
Thanks!

Ric

GhostInAK wrote:
>Hello ric_deez,

This is one case in which an inline ad-hoc query would be preferable
I do
believe. Done right it is extremely maintainable. TSQL, while up to
the
task, is not designed for such a query and would indeed be the less
maintainable
solution.
Put a layer of abstraction between the UI and the query. This
abstraction layer would take as inputs the name of the field to
search as well as the value to search for. It would then give you a
hashtable or some similar construct which you could then loop over to
construct your query.

Dont forget that ad-hoc queries can still take advantage of the
SqlParameter
object (eg. SELECT field1 FROM table1 WHERE (field1 = @field1) --
tCommand.Parameters.AddWithValue("@field1",
value1) )
Enjoy,
-Boo
>>Hi there, I would like to create a simple search form to allow users
to search for a job number based on a number of parameters. I think
I understand how to use parameteres associated with Stored
Procedures with a data reader to add various parameters.

However, if I have a stored procedure such as

CREATE usp_SelectfromJobNumbers (@par1 datatype, @par2 datatype,
@par3 datatype)

AS

SELECT FROM Jobs WHERE
PAR1 = @par1 AND
PAR2 = @par2 AND
PAR3 = @par3
I cannot just pass the one parameter to the procedure. The
question
is then: how is it possible to create a stored procedure which can
accept any number of parameters an then smartly modify the query in
the store procedure with CASE statements or similar?
I don't really want to do this as in-line code as it easily becomes
unmaintainable and it is hard to add additional conditions. I was
thinking of passing a string array into the Stored Procedure and
having the stored procedure parse through the array, determine the
parameters and do the grunt of the work. The problem is that I
haven't got a lot of experience with these advanced stored
procedures using CASE statements and the like...

Any ideas would be greatly appreciated.

Regards,

Ric

Aug 4 '06 #4
Hi Boo,

Thanks for your help on this. I didn't actually mean for you to write
the entire code but I do appreciate you taking the time to do so as it
has answered my question. I was wondering if you had to specify the
CommandType as CommandType.Text, which you have now confirmed.

This is pretty nifty, thanks!!!

Ric
GhostInAK wrote:
Hello ric_deez,

I thought I had. However, if by explain you mean write you the entire code..
*sigh* fine.. but only because I don't hate you, yet. Thias is off the
top of my head...

Dim tConnection As SqlConnection = New SqlConnection(connection_string_here)
Dim tCommand As SqlCommand = New SqlCommand
Dim tAdapter As SqlDataAdapter = New SqlDataAdapter(tCommand)
Dim tTable as DataTable = New DataTable

With tCommand
.Connection = tConnection
.CommandType = CommandType.Text
.ComandText = "SELECT field1 FROM table1 WHERE (field1 = @value1)"
' Note: Here you would pass in your constructed ad-hoc query
.Parameters.AddWithValue("@value1", value1) ' See, the parameter
name corresponds to the parameter name in the query.. spiffy huh..
End With

tConnection.Open
tAdapter.Fill(tTable)
tConnection.Close
' And now you got a shiney new DataTable to show your friends

tAdapter = Nothing
tCommand = Nothing
tConnection = Nothing

-Boo
Hi there Boo,

Can you please explain how to use the sqlParameter with ad-hoc
queries?
>Dont forget that ad-hoc queries can still take advantage of the
SqlParameter
SELECT field1 FROM table1 WHERE (field1 = @field1) --
tCommand.Parameters.AddWithValue("@field1",
value1) )
Thanks!

Ric

GhostInAK wrote:
Hello ric_deez,

This is one case in which an inline ad-hoc query would be preferable
I do
believe. Done right it is extremely maintainable. TSQL, while up to
the
task, is not designed for such a query and would indeed be the less
maintainable
solution.
Put a layer of abstraction between the UI and the query. This
abstraction layer would take as inputs the name of the field to
search as well as the value to search for. It would then give you a
hashtable or some similar construct which you could then loop over to
construct your query.

Dont forget that ad-hoc queries can still take advantage of the
SqlParameter
object (eg. SELECT field1 FROM table1 WHERE (field1 = @field1) --
tCommand.Parameters.AddWithValue("@field1",
value1) )
Enjoy,
-Boo
Hi there, I would like to create a simple search form to allow users
to search for a job number based on a number of parameters. I think
I understand how to use parameteres associated with Stored
Procedures with a data reader to add various parameters.

However, if I have a stored procedure such as

CREATE usp_SelectfromJobNumbers (@par1 datatype, @par2 datatype,
@par3 datatype)

AS

SELECT FROM Jobs WHERE
PAR1 = @par1 AND
PAR2 = @par2 AND
PAR3 = @par3
I cannot just pass the one parameter to the procedure. The
question
is then: how is it possible to create a stored procedure which can
accept any number of parameters an then smartly modify the query in
the store procedure with CASE statements or similar?
I don't really want to do this as in-line code as it easily becomes
unmaintainable and it is hard to add additional conditions. I was
thinking of passing a string array into the Stored Procedure and
having the stored procedure parse through the array, determine the
parameters and do the grunt of the work. The problem is that I
haven't got a lot of experience with these advanced stored
procedures using CASE statements and the like...

Any ideas would be greatly appreciated.

Regards,

Ric
Aug 5 '06 #5
Hello ric_deez,

The default value for SqlCommand.CommandType is indeed CommandType.Text,
however, I prefer to set this property as I think it makes the code more
readable.

-Boo
Hi Boo,

Thanks for your help on this. I didn't actually mean for you to write
the entire code but I do appreciate you taking the time to do so as it
has answered my question. I was wondering if you had to specify the
CommandType as CommandType.Text, which you have now confirmed.

This is pretty nifty, thanks!!!

Ric

GhostInAK wrote:
>Hello ric_deez,

I thought I had. However, if by explain you mean write you the
entire code..
*sigh* fine.. but only because I don't hate you, yet. Thias is off
the
top of my head...
Dim tConnection As SqlConnection = New
SqlConnection(connection_string_here)
Dim tCommand As SqlCommand = New SqlCommand
Dim tAdapter As SqlDataAdapter = New SqlDataAdapter(tCommand)
Dim tTable as DataTable = New DataTable
With tCommand
.Connection = tConnection
.CommandType = CommandType.Text
.ComandText = "SELECT field1 FROM table1 WHERE (field1 = @value1)"
' Note: Here you would pass in your constructed ad-hoc query
.Parameters.AddWithValue("@value1", value1) ' See, the
parameter
name corresponds to the parameter name in the query.. spiffy huh..
End With
tConnection.Open
tAdapter.Fill(tTable)
tConnection.Close
' And now you got a shiney new DataTable to show your friends
tAdapter = Nothing
tCommand = Nothing
tConnection = Nothing
-Boo
>>Hi there Boo,

Can you please explain how to use the sqlParameter with ad-hoc
queries?

Dont forget that ad-hoc queries can still take advantage of the
SqlParameter
SELECT field1 FROM table1 WHERE (field1 = @field1) --
tCommand.Parameters.AddWithValue("@field1",
value1) )
Thanks!

Ric

GhostInAK wrote:

Hello ric_deez,

This is one case in which an inline ad-hoc query would be
preferable
I do
believe. Done right it is extremely maintainable. TSQL, while up
to
the
task, is not designed for such a query and would indeed be the less
maintainable
solution.
Put a layer of abstraction between the UI and the query. This
abstraction layer would take as inputs the name of the field to
search as well as the value to search for. It would then give you
a
hashtable or some similar construct which you could then loop over
to
construct your query.
Dont forget that ad-hoc queries can still take advantage of the
SqlParameter
object (eg. SELECT field1 FROM table1 WHERE (field1 = @field1)
--
tCommand.Parameters.AddWithValue("@field1",
value1) )
Enjoy,
-Boo
Hi there, I would like to create a simple search form to allow
users to search for a job number based on a number of parameters.
I think I understand how to use parameteres associated with Stored
Procedures with a data reader to add various parameters.
>
However, if I have a stored procedure such as
>
CREATE usp_SelectfromJobNumbers (@par1 datatype, @par2 datatype,
@par3 datatype)
>
AS
>
SELECT FROM Jobs WHERE
PAR1 = @par1 AND
PAR2 = @par2 AND
PAR3 = @par3
I cannot just pass the one parameter to the procedure. The
question
is then: how is it possible to create a stored procedure which
can
accept any number of parameters an then smartly modify the query
in
the store procedure with CASE statements or similar?
I don't really want to do this as in-line code as it easily
becomes
unmaintainable and it is hard to add additional conditions. I was
thinking of passing a string array into the Stored Procedure and
having the stored procedure parse through the array, determine the
parameters and do the grunt of the work. The problem is that I
haven't got a lot of experience with these advanced stored
procedures using CASE statements and the like...
Any ideas would be greatly appreciated.
>
Regards,
>
Ric
>

Aug 5 '06 #6

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

Similar topics

5
by: Bruno Alexandre | last post by:
Hi guys, withou using SP, I want to be able to add a Parameter to the SQL Query and retrive the Recordset so I can use the Paging property under the recorset object.... how can I do this? I'm...
11
by: Eugenio | last post by:
Excuse me in advance fo my little English. I've got this stored procedure **************************************************************************** ********** declare @Azienda as...
4
by: Angel Cat | last post by:
I have 2 tables joined together by the IDs, People and the pets they own PEOPLE ID NAME 1 JohnSMith 2 JaneDoe PETS ID PET
10
by: Thomas R. Hummel | last post by:
I have a stored procedure that suddenly started performing horribly. The query plan didn't look right to me, so I copy/pasted the code and ran it (it's a single SELECT statement). That ran pretty...
0
by: totierne | last post by:
comp.databases.ms-access, I want to know how to use Oracle views with session variables in Access. The parameterised views in access, are migrated to views with per session variables. The...
6
by: lesperancer | last post by:
SELECT distinct b.t_orno, b.t_pono FROM tblMonthlyBooking AS b, tblFilterDate, tblFilterDate AS tblFilterDate_1 WHERE (((b.t_yearMonth) Between . And .)); tblMonthlyBooking is a sql server...
5
by: Nesa | last post by:
I have a stored procedure that wraps a moderately complex query over 5, 6 related tables. The performance of the procedure is unacceptably slow as it takes on average 5-10 min to complete. To...
9
by: Frawls | last post by:
Hi I Am am having problems with a stored Procedure that i wrote. Basically whats happening is that the Stored procedure Runs fine when i EXECUTE it in SQL Query analyzer. But when i debug...
6
by: jsacrey | last post by:
Hey everybody, got a secnario for ya that I need a bit of help with. Access 97 using linked tables from an SQL Server 2000 machine. I've created a simple query using two tables joined by one...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.