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

Stored Procedure with variable parameters

Hi,

I have a View which contains (of course) several columns. Now I want to make
a Stored Procedure that does a Select based on the parameters.

For exemple: One time I want to select all the reocrds with ColumnA = 'Value
A', an other time all the reords with ColumnB = 'An other value', a third
time all records with "ColumnA = 'Value C' AND ColumnB = 'this value'" etc
etc.

So my paramters must be kind of flexible.

What is the best solution for this?
- Adding a paramter to my stored procedure for every column, evaluate them
and when not empty build a SQL-query dynamically and EXEC it?
- Building the WHERE-clause in my application (VB.NET 2005), and giving this
as 1 paramter to my stored procedure, and build the sql query and EXEC it?
- Some nice SQL Server/VB.NET solution that handles this kind of cases in a
smooth way? :-)

Are there different kinds of performances with the different possible
solutions?

Thanks a lot in advance,

Pieter
Nov 21 '05 #1
3 1369
Have a look at

http://www.sommarskog.se/dyn-search.html

--
Roji. P. Thomas
Net Asset Management
http://toponewithties.blogspot.com
"Pieter" <pi**********@hotmail.com> wrote in message
news:ui**************@tk2msftngp13.phx.gbl...
Hi,

I have a View which contains (of course) several columns. Now I want to
make a Stored Procedure that does a Select based on the parameters.

For exemple: One time I want to select all the reocrds with ColumnA =
'Value A', an other time all the reords with ColumnB = 'An other value', a
third time all records with "ColumnA = 'Value C' AND ColumnB = 'this
value'" etc etc.

So my paramters must be kind of flexible.

What is the best solution for this?
- Adding a paramter to my stored procedure for every column, evaluate them
and when not empty build a SQL-query dynamically and EXEC it?
- Building the WHERE-clause in my application (VB.NET 2005), and giving
this as 1 paramter to my stored procedure, and build the sql query and
EXEC it?
- Some nice SQL Server/VB.NET solution that handles this kind of cases in
a smooth way? :-)

Are there different kinds of performances with the different possible
solutions?

Thanks a lot in advance,

Pieter

Nov 21 '05 #2
Hi
I think you need something like that written by Erland
For more info , I suggest you to visit on his website
http://www.sommarskog.se/

CREATE PROCEDURE search_orders_1 --
1
@orderid int = NULL, --
2
@fromdate datetime = NULL, --
3
@todate datetime = NULL, --
4
@minprice money = NULL, --
5
@maxprice money = NULL, --
6
@custid nchar(5) = NULL, --
7
@custname nvarchar(40) = NULL, --
8
@city nvarchar(15) = NULL, --
9
@region nvarchar(15) = NULL, --
10
@country nvarchar(15) = NULL, --
11
@prodid int = NULL, --
12
@prodname nvarchar(40) = NULL, --
13
@debug bit = 0 AS --
14
--
15
DECLARE @sql nvarchar(4000), --
16
@paramlist nvarchar(4000) --
17
--
18
SELECT @sql = --
19
'SELECT o.OrderID, o.OrderDate, od.UnitPrice, od.Quantity, --
20
c.CustomerID, c.CompanyName, c.Address, c.City, c.Region, --
21
c.PostalCode, c.Country, c.Phone, p.ProductID, --
22
p.ProductName, p.UnitsInStock, p.UnitsOnOrder --
23
FROM Orders o --
24
JOIN [Order Details] od ON o.OrderID = od.OrderID --
25
JOIN Customers c ON o.CustomerID = c.CustomerID --
26
JOIN Products p ON p.ProductID = od.ProductID --
27
WHERE 1 = 1' --
28
--
29
IF @orderid IS NOT NULL --
30
SELECT @sql = @sql + ' AND o.OrderID = @xorderid' + --
31
' AND od.OrderID = @xorderid' --
32
--
33
IF @fromdate IS NOT NULL --
34
SELECT @sql = @sql + ' AND o.OrderDate >= @xfromdate' --
35
--
36
IF @todate IS NOT NULL --
37
SELECT @sql = @sql + ' AND o.OrderDate <= @xtodate' --
38
--
39
IF @minprice IS NOT NULL --
40
SELECT @sql = @sql + ' AND od.UnitPrice >= @xminprice' --
41
--
42
IF @maxprice IS NOT NULL --
43
SELECT @sql = @sql + ' AND od.UnitPrice <= @xmaxprice' --
44
--
45
IF @custid IS NOT NULL --
46
SELECT @sql = @sql + ' AND o.CustomerID = @xcustid' + --
47
' AND c.CustomerID = @xcustid' --
48
--
49
IF @custname IS NOT NULL --
50
SELECT @sql = @sql + ' AND c.CompanyName LIKE @xcustname + ''%''' --
51
--
52
IF @city IS NOT NULL --
53
SELECT @sql = @sql + ' AND c.City = @xcity' --
54
--
55
IF @region IS NOT NULL --
56
SELECT @sql = @sql + ' AND c.Region = @xregion' --
57
--
58
IF @country IS NOT NULL --
59
SELECT @sql = @sql + ' AND c.Country = @xcountry' --
60
--
61
IF @prodid IS NOT NULL --
62
SELECT @sql = @sql + ' AND od.ProductID = @xprodid' + --
63
' AND p.ProductID = @xprodid' --
64
--
65
IF @prodname IS NOT NULL --
66
SELECT @sql = @sql + ' AND p.ProductName LIKE @xprodname + ''%''' --
67
--
68
SELECT @sql = @sql + ' ORDER BY o.OrderID' --
69
--
70
IF @debug = 1 --
71
PRINT @sql --
72
--
73
SELECT @paramlist = '@xorderid int, --
74
@xfromdate datetime, --
75
@xtodate datetime, --
76
@xminprice money, --
77
@xmaxprice money, --
78
@xcustid nchar(5), --
79
@xcustname nvarchar(40), --
80
@xcity nvarchar(15), --
81
@xregion nvarchar(15), --
82
@xcountry nvarchar(15), --
83
@xprodid int, --
84
@xprodname nvarchar(40)' --
85
--
86
EXEC sp_executesql @sql, @paramlist, --
87
@orderid, @fromdate, @todate, @minprice, @maxprice, --
88
@custid, @custname, @city, @region, @country, --
89
@prodid, @prodname --
90

"Pieter" <pi**********@hotmail.com> wrote in message
news:ui**************@tk2msftngp13.phx.gbl...
Hi,

I have a View which contains (of course) several columns. Now I want to
make a Stored Procedure that does a Select based on the parameters.

For exemple: One time I want to select all the reocrds with ColumnA =
'Value A', an other time all the reords with ColumnB = 'An other value', a
third time all records with "ColumnA = 'Value C' AND ColumnB = 'this
value'" etc etc.

So my paramters must be kind of flexible.

What is the best solution for this?
- Adding a paramter to my stored procedure for every column, evaluate them
and when not empty build a SQL-query dynamically and EXEC it?
- Building the WHERE-clause in my application (VB.NET 2005), and giving
this as 1 paramter to my stored procedure, and build the sql query and
EXEC it?
- Some nice SQL Server/VB.NET solution that handles this kind of cases in
a smooth way? :-)

Are there different kinds of performances with the different possible
solutions?

Thanks a lot in advance,

Pieter

Nov 21 '05 #3
Thanks guys, really great articles! I guess this will be the solution with
the best performance, flexible, and safe...

Thanks!

Pieter

"Uri Dimant" <ur**@iscar.co.il> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Hi
I think you need something like that written by Erland
For more info , I suggest you to visit on his website
http://www.sommarskog.se/

CREATE PROCEDURE search_orders_1 --
1
@orderid int =
-- 2
@fromdate datetime =
-- 3
@todate datetime =
-- 4
@minprice money =
-- 5
@maxprice money =
-- 6
@custid nchar(5) =
-- 7
@custname nvarchar(40) =
-- 8
@city nvarchar(15) =
-- 9
@region nvarchar(15) =
-- 10
@country nvarchar(15) =
-- 11
@prodid int =
-- 12
@prodname nvarchar(40) =
-- 13
@debug bit = 0
-- 14
--
15
DECLARE @sql
-- 16
@paramlist
-- 17
--
18
SELECT @sql
-- 19
'SELECT o.OrderID, o.OrderDate, od.UnitPrice,
ity, -- 20
c.CustomerID, c.CompanyName, c.Address, c.City,
.Region, -- 21
c.PostalCode, c.Country, c.Phone,
-- 22
p.ProductName, p.UnitsInStock,
r -- 23
FROM Orders
-- 24
JOIN [Order Details] od ON o.OrderID =
-- 25
JOIN Customers c ON o.CustomerID =
-- 26
JOIN Products p ON p.ProductID =
-- 27
WHERE 1 =
-- 28
--
29
IF @orderid IS NOT
-- 30
SELECT @sql = @sql + ' AND o.OrderID = @xorderid'
-- 31
' AND od.OrderID =
-- 32
--
33
IF @fromdate IS NOT
-- 34
SELECT @sql = @sql + ' AND o.OrderDate >=
-- 35
--
36
IF @todate IS NOT
-- 37
SELECT @sql = @sql + ' AND o.OrderDate <=
-- 38
--
39
IF @minprice IS NOT
-- 40
SELECT @sql = @sql + ' AND od.UnitPrice >=
-- 41
--
42
IF @maxprice IS NOT
-- 43
SELECT @sql = @sql + ' AND od.UnitPrice <=
-- 44
--
45
IF @custid IS NOT
-- 46
SELECT @sql = @sql + ' AND o.CustomerID = @xcustid'
-- 47
' AND c.CustomerID =
-- 48
--
49
IF @custname IS NOT
-- 50
SELECT @sql = @sql + ' AND c.CompanyName LIKE @xcustname +
%''' -- 51
--
52
IF @city IS NOT
-- 53
SELECT @sql = @sql + ' AND c.City =
-- 54
--
55
IF @region IS NOT
-- 56
SELECT @sql = @sql + ' AND c.Region =
-- 57
--
58
IF @country IS NOT
-- 59
SELECT @sql = @sql + ' AND c.Country =
-- 60
--
61
IF @prodid IS NOT
-- 62
SELECT @sql = @sql + ' AND od.ProductID = @xprodid'
-- 63
' AND p.ProductID =
-- 64
--
65
IF @prodname IS NOT
-- 66
SELECT @sql = @sql + ' AND p.ProductName LIKE @xprodname +
%''' -- 67
--
68
SELECT @sql = @sql + ' ORDER BY
-- 69
--
70
IF @debug =
-- 71
PRINT
-- 72
--
73
SELECT @paramlist = '@xorderid
-- 74
@xfromdate
-- 75
@xtodate
-- 76
@xminprice
-- 77
@xmaxprice
-- 78
@xcustid
-- 79
@xcustname
-- 80
@xcity
-- 81
@xregion
-- 82
@xcountry
-- 83
@xprodid
-- 84
@xprodname
-- 85
--
86
EXEC sp_executesql @sql,
-- 87
@orderid, @fromdate, @todate, @minprice,
@maxprice, -- 88
@custid, @custname, @city, @region,
ry, -- 89
@prodid,
-- 90

"Pieter" <pi**********@hotmail.com> wrote in message
news:ui**************@tk2msftngp13.phx.gbl...
Hi,

I have a View which contains (of course) several columns. Now I want to
make a Stored Procedure that does a Select based on the parameters.

For exemple: One time I want to select all the reocrds with ColumnA =
'Value A', an other time all the reords with ColumnB = 'An other value',
a third time all records with "ColumnA = 'Value C' AND ColumnB = 'this
value'" etc etc.

So my paramters must be kind of flexible.

What is the best solution for this?
- Adding a paramter to my stored procedure for every column, evaluate
them and when not empty build a SQL-query dynamically and EXEC it?
- Building the WHERE-clause in my application (VB.NET 2005), and giving
this as 1 paramter to my stored procedure, and build the sql query and
EXEC it?
- Some nice SQL Server/VB.NET solution that handles this kind of cases in
a smooth way? :-)

Are there different kinds of performances with the different possible
solutions?

Thanks a lot in advance,

Pieter


Nov 21 '05 #4

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

Similar topics

6
by: Shaun Stuart | last post by:
I've got a webpage that calls some stored procedures with input variables. The procedures return recordsets and also some output variables. We're trying to get the values of the output variables....
3
by: dinesh prasad | last post by:
I'm trying to use a servlet to process a form, then send that data to an SQL server stored procedure. I'm using the WebLogic 8 App. server. I am able to retrieve database information, so I know my...
0
by: Nashat Wanly | last post by:
HOW TO: Call a Parameterized Stored Procedure by Using ADO.NET and Visual C# .NET View products that this article applies to. This article was previously published under Q310070 For a Microsoft...
2
by: Dino L. | last post by:
How can I run stored procedure (MSSQL) ?
12
by: Bill Nguyen | last post by:
What's the VB syntax to run the CR report using the following SP? I use CrystalreportViewer and ReportDocument. Thanks Bill Here's the SP in SQLserver 2K: CREATE proc mysp_ReportSubmission...
9
by: anilcool | last post by:
Hi all, Another novice question. I want to insert multiple records into my DB2 database using stored procedure but I do not know how many I would like to insert at any given time. Each record...
7
by: Siv | last post by:
Hi, I have a stored procedure that I want to execute and then wait in a loop showing a timer whilst it completes and then carry on once I get notification that it has completed. The main reason...
9
by: fniles | last post by:
I am using VB.NET 2003 and SQL2000 database. I have a stored procedure called "INSERT_INTO_MYTABLE" that accepts 1 parameter (varchar(10)) and returns the identity column value from that table....
2
by: jed | last post by:
I have created this example in sqlexpress ALTER PROCEDURE . @annualtax FLOAT AS BEGIN SELECT begin1,end1,deductedamount,pecentageextra FROM tax
12
by: Dooza | last post by:
I have a stored procedure that takes a number of inputs, does a bulk insert, and then outputs a recordset. When I run the stored procedure in Server Management Studio I also get a return value from...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.