Drop Table

Support Forum for database administrators and web based access to important newsgroups related to databases
Register on Database Support Forum Edit your profileCalendarFind other Database Support forum membersFrequently Asked QuestionsSearch this forum -> 
For Database admins: Free Database-related Magazines Now Free shipping to Texas


Post New Thread










Thread
Author

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?


Report this thread to moderator Post Follow-up to this message
Old Post
Igor
03-29-06 12:29 PM


Re: Stored procedure with unknown numer of paramters, how?
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/d... />
ql01c5.asp

--
Tom

----------------------------------------------------
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Columnist, SQL Server Professional
Toronto, ON   Canada
www.pinpub.com
.
"Igor" <jerosimic@gmail.com> wrote in message
news:1143628023.313215.26320@z34g2000cwc.googlegroups.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?


Report this thread to moderator Post Follow-up to this message
Old Post
Tom Moreau
03-29-06 12:29 PM


Re: Stored procedure with unknown numer of paramters, how?
OK, thanks i'll try that.


Report this thread to moderator Post Follow-up to this message
Old Post
Igor
03-29-06 12:29 PM


Re: Stored procedure with unknown numer of paramters, how?
Igor (jerosimic@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-...ist-of-integers for
one alternative. Read the rest of the article to see some more.


--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx

Report this thread to moderator Post Follow-up to this message
Old Post
Erland Sommarskog
03-30-06 01:26 AM


Re: Stored procedure with unknown numer of paramters, how?
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;


Report this thread to moderator Post Follow-up to this message
Old Post
--CELKO--
03-30-06 02:30 PM


Re: Stored procedure with unknown numer of paramters, how?
> 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--" <jcelko212@earthlink.net> wrote in message
news:1143728606.490777.142380@i39g2000cwa.googlegroups.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;
>



Report this thread to moderator Post Follow-up to this message
Old Post
Tony Rogerson
03-31-06 08:30 AM


Re: Stored procedure with unknown numer of paramters, how?
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.


Report this thread to moderator Post Follow-up to this message
Old Post
Doug
04-06-06 06:30 PM


Re: Stored procedure with unknown numer of paramters, how?
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...


Report this thread to moderator Post Follow-up to this message
Old Post
Igor
04-07-06 12:35 PM


Sponsored Links





Last Thread Next Thread
Post New Thread

Microsoft SQL Server forum archive

Show a Printable Version Email This Page to Someone! Receive updates to this thread
Microsoft SQL Server
Access database support
PostgreSQL Replication
SQL Server ODBC
FoxPro Support
PostgreSQL pgAdmin
SQL Server Clustering
MySQL ODBC
Web Applications with dBASE
SQL Server CE
MySQL++
Sybase Database Support
MS SQL Full Text Search
PostgreSQL Administration
SQL Anywhere support
DB2 UDB Database
Paradox Database Support
Filemaker Database
Berkley DB
SQL 2000/2000i database
ASE Database
Forum Jump:
All times are GMT. The time now is 07:54 PM.

 
Mobile devices forum | Database support forum archive




Copyrights DropTable.com Database Support Forum 2004 - 2006