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
Solved

Passing variable number of parameters to SP and UDF

Posted on 2006-06-20
13
546 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
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Powershell GUI pull downs populated by MS SQL table 2 36
backup job space check 4 43
SQL Log size 3 17
Help With SQL Query 9 30
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
I have a large data set and a SSIS package. How can I load this file in multi threading?
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.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

840 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