Solved

Passing variable number of parameters to SP and UDF

Posted on 2006-06-20
13
543 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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
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.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

707 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

13 Experts available now in Live!

Get 1:1 Help Now