Solved

Passing variable number of parameters to SP and UDF

Posted on 2006-06-20
13
544 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
13 Comments
 
LVL 142

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 200 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
 
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 142

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
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 
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 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 300 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 142

Expert Comment

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

Author Comment

by:imrancs
ID: 16942461
Thanks again angelIII.

Imran
0
 
LVL 142

Expert Comment

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

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

867 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now