Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Passing variable number of parameters to SP and UDF

Posted on 2006-06-20
13
Medium Priority
?
560 Views
Last Modified: 2012-06-21
Hi Experts,

How can we make a SP or UDF to accept variable number of paramters, same like we pass to SP_ExecuteSql and Coalesce()?

Thanks,
Imran
0
Comment
Question by:imrancs
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
13 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 16941632
you can put optional parameters:

create proc <procname>
( @param1 int
, @param2 int = NULL
, @param3 int = NULL
)


0
 
LVL 4

Assisted Solution

by:batchakamal
batchakamal earned 800 total points
ID: 16941644
Either,
pass the values as a delimited text, then do the separation inside the stored procedure using temporary table.

OR

the best way is to use XML. Send the parameters as a XML Document, then using OPENXML you can manipulate it.
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 16941683
Continuing Angels Comments


create proc <procname>
( @param1 int
, @param2 int = NULL
, @param3 int = NULL
)


call like

ProcName @param1 = 10, @param3 =10  
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 10

Author Comment

by:imrancs
ID: 16941815
Thanks guys for your feedback, but this is not what I am looking for. Is there any other solution other than the above mentioned?

Imran
0
 
LVL 10

Author Comment

by:imrancs
ID: 16941837
How the SP_ExecuteSql and Coalesce() are implemented?

Imran
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 16941890
>How the SP_ExecuteSql and Coalesce() are implemented?
well; those are internal procedures (developed in c).

I don't see what is wrong about the optional parameters, as some system stored procedure work the same way..
0
 
LVL 10

Author Comment

by:imrancs
ID: 16942068
angelIII, there is nothing wrong with optional parameters except you need to know Max number of parameters and may be the data type too.

I was just looking for the way if could be done simply in t-sql.

Imran
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 1200 total points
ID: 16942239
>except you need to know Max number of parameters and may be the data type too.
I know what you want to get at, a PARAMARRAY parameter like in VB, giving to the procedure code an array of the values passed.
But, helas, no such thing in SQL. the closest thing is the XML parameter
0
 
LVL 10

Author Comment

by:imrancs
ID: 16942271
Ok, Thanks angelIII and aneeshattingal for your help.

Imran
0
 
LVL 10

Author Comment

by:imrancs
ID: 16942319
Can you please give me some example to how to use XML for the above?

Imran
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 16942396
0
 
LVL 10

Author Comment

by:imrancs
ID: 16942461
Thanks again angelIII.

Imran
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 16942473
you are welcome
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Ready to get certified? Check out some courses that help you prepare for third-party exams.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question