Solved

Passing variable number of parameters to SP and UDF

Posted on 2006-06-20
13
547 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 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 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 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 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 Webinar: AWS Backup & DR

Join our upcoming webinar with experts from AWS, CloudBerry Lab, and the Town of Edgartown IT to discuss best practices for simplifying online backup management and cutting costs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
point in time restore in SQL server 26 45
T-SQL: Stored Procedure Syntax 3 34
SQL 2014 missing dll from Bin? 3 34
Need age at date of document 5 20
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how the fundamental information of how to create a table.

733 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