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

Stored procedure with unknown numer of paramters, how?

I have one table with categories

tblCategories

cat_id | cat_name

-----------------

1 | cat 1

2 | cat 2

3 | cat 3

4 | cat 4

5 | cat 5

6 | cat 6

and one table with projects which relates to tblCategories

tblProjects

proj_id | proj_name | cat_id

----------------------------

1 | proj 1 | 2

2 | proj 2 | 2

3 | proj 3 | 3

4 | proj 4 | 2

How would you create stored procedure for searching some string in
filed proj_name but within multiple categories

for example

CREATE PROCEDURE [spSearch]

(

@SEARCH_STRING nvarchar(200),

@CAT_ID int

)

AS

BEGIN

SELECT proj_id

FROM tblProjects

WHERE (proj_name LIKE '%' + @SEARCH_STRING + '%') AND (cat_id =
@CAT_ID)

END

But that one works only with one categorie and i need to search for one
or more categories at once, does anyone have a solution? Is there
something like ellipsis (...) in C++ for MSSQL?

Mar 29 '06 #1
7 1379
Feed the list of categories to the proc in the form of an XML doc. You can
then use OPENXML (SQL 2000) and join onto it. In SQL 2005, you can use
XQuery to do the same.

http://msdn.microsoft.com/library/de...ml/sql01c5.asp

--
Tom

----------------------------------------------------
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Columnist, SQL Server Professional
Toronto, ON Canada
www.pinpub.com
..
"Igor" <je*******@gmail.com> wrote in message
news:11*********************@z34g2000cwc.googlegro ups.com...
I have one table with categories

tblCategories

cat_id | cat_name

-----------------

1 | cat 1

2 | cat 2

3 | cat 3

4 | cat 4

5 | cat 5

6 | cat 6

and one table with projects which relates to tblCategories

tblProjects

proj_id | proj_name | cat_id

----------------------------

1 | proj 1 | 2

2 | proj 2 | 2

3 | proj 3 | 3

4 | proj 4 | 2

How would you create stored procedure for searching some string in
filed proj_name but within multiple categories

for example

CREATE PROCEDURE [spSearch]

(

@SEARCH_STRING nvarchar(200),

@CAT_ID int

)

AS

BEGIN

SELECT proj_id

FROM tblProjects

WHERE (proj_name LIKE '%' + @SEARCH_STRING + '%') AND (cat_id =
@CAT_ID)

END

But that one works only with one categorie and i need to search for one
or more categories at once, does anyone have a solution? Is there
something like ellipsis (...) in C++ for MSSQL?

Mar 29 '06 #2
OK, thanks i'll try that.

Mar 29 '06 #3
Igor (je*******@gmail.com) writes:
But that one works only with one categorie and i need to search for one
or more categories at once, does anyone have a solution? Is there
something like ellipsis (...) in C++ for MSSQL?


No. There are a couple of methods to pass lists of data to SQL Server. Tom
mention XML. For a single list, that may be a little heavy-handed. Check out
http://www.sommarskog.se/arrays-in-s...st-of-integers for
one alternative. Read the rest of the article to see some more.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Mar 29 '06 #4
Plese stop puttign those silly prefixes on data element name -- it
makes you look like an OO programmer who never read ISO-11179 standard
or took a course in data modeling. The name "tblProjects implies that
you are working on furniture :) Likewise, a procesure named "search"
does not tell us what is being searched. An attriburte cannot be both
a category and an identifier -- did you mean project categories?

Did you know that a T-SQL Procddure can handle over 1000 parameters?
You can probabl;y get by with a mere 50 of them.

CREATE PROCEDURE SearchProjects
(@my_project CHAR(15), @cat01 INTEGER, @cat02 INTEGER, ., @cat99
INTEGER)
AS
BEGIN
SELECT proj_id
FROM Projects
WHERE proj_name LIKE '%' + @my_project + '%'
AND proj_cat IN
(@cat01, @cat02, ., @cat99);
<<error handling here>>;
END;

Mar 30 '06 #5
> Did you know that a T-SQL Procddure can handle over 1000 parameters?
You can probabl;y get by with a mere 50 of them.
Get real, that is the worst way to do it.

Pass in a set of data or XML and do it efficiently and think of scalability.

--
Tony Rogerson
SQL Server MVP
http://sqlserverfaq.com - free video tutorials
"--CELKO--" <jc*******@earthlink.net> wrote in message
news:11**********************@i39g2000cwa.googlegr oups.com... Plese stop puttign those silly prefixes on data element name -- it
makes you look like an OO programmer who never read ISO-11179 standard
or took a course in data modeling. The name "tblProjects implies that
you are working on furniture :) Likewise, a procesure named "search"
does not tell us what is being searched. An attriburte cannot be both
a category and an identifier -- did you mean project categories?

Did you know that a T-SQL Procddure can handle over 1000 parameters?
You can probabl;y get by with a mere 50 of them.

CREATE PROCEDURE SearchProjects
(@my_project CHAR(15), @cat01 INTEGER, @cat02 INTEGER, ., @cat99
INTEGER)
AS
BEGIN
SELECT proj_id
FROM Projects
WHERE proj_name LIKE '%' + @my_project + '%'
AND proj_cat IN
(@cat01, @cat02, ., @cat99);
<<error handling here>>;
END;

Mar 31 '06 #6
celkos answer = GAG

GAG is an advanced sql topic that can be best summarized as the sound
the cat makes when regurgitating fur balls.

an elegant way where you have only one data element, but multiple
occurances is to pass the name of a temp table containing the data.
another way is to pass a delimited list - ugly code for sql to parse it
out, but multiple example are out there for leveraging and it is bug
free long term.

Apr 6 '06 #7
I used what Erland Sommarskog suggested, it works fine.
Thanks all for your help.
---
"Plese stop puttign those silly prefixes on data element name -- it
makes you look like an OO programmer who never read ISO-11179 standard
or took a course in data modeling."
actually i didn't read that standard and didn't go on course in data
modeling :)

"Likewise, a procesure named "search" does not tell us what is being
searched."
i was just trying to simplify procedure names and table fields so my
question would not get to complicated...

Apr 7 '06 #8

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

Similar topics

7
by: Együd Csaba | last post by:
Hi, I've a problem with some of my stored procs. My config is: RH7.1, Postgres 7.3.2 I had converted a few fields of a few tables from one type to another and after this I made all the...
1
by: Rhino | last post by:
I am trying to get a sense of requirements and best practices for Java stored procedures in DB2 V7.2 for Windows. 1. Is it required or recommended that any of the following be closed before...
0
by: HumanJHawkins | last post by:
"HumanJHawkins" <JHawkins@HumanitiesSoftware.Com> wrote in message news:i9nac.8030$Dv2.2242@newsread2.news.pas.earthlink.net... > (The SQL Group suggested I post this here... Sorry for the...
3
by: nandan | last post by:
Hi, Has any one ever compared the performance of calling a DataTable's Select method with a stored procedure doing the same thing? My point is: dataRows = DataTable.Select(filter) is better or...
1
by: Jim Heavey | last post by:
Hello, I have an update stored procedure which is in a package. In my code I create all of the paramters and then call the stored procedure with "ExecuteNonQuery()" method of the command. The...
3
by: Pieter | last post by:
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...
1
by: Lars Erik Thorsplass | last post by:
I have recently ventured into the exciting world of stored procedures, but I have now become lost. Background: Am currently working on access control in a web application. My goal is to...
4
by: Jack | last post by:
Hi, I am trying to run an example code from a book. However I am getting the following error message: Number: -2147217900 Description: Syntax error or access violation Source: Microsoft OLE...
1
by: flickimp | last post by:
Hi I have a stored procedure on SQL Server 2000 called: .Capacity_Planning.dbo.sp_flickimp_test I have an Excel Sheet with the 2 paramters for the Stored procedure in cells A1 and B1. How...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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?
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
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
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,...
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.