SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


stored procedure with optional parameters


stored procedure with optional parameters

Author
Message
Naveen Kumar-807681
Naveen Kumar-807681
SSC-Addicted
SSC-Addicted (414 reputation)SSC-Addicted (414 reputation)SSC-Addicted (414 reputation)SSC-Addicted (414 reputation)SSC-Addicted (414 reputation)SSC-Addicted (414 reputation)SSC-Addicted (414 reputation)SSC-Addicted (414 reputation)

Group: General Forum Members
Points: 414 Visits: 78
Hi Everybody,

I have a stored procedure with does multiple tasks like displaying the data in multiple grids, updating the records, adding the records etc. All these are done in a single procedure. I want to create a stored procedure with accepts optional parameters. The user may not send all the parameters if he is calling the stored procedure for data display. He will pass all the parameters when ever he wants to add records.

Thanks & Regards,
Naveen kumar

Who is wise? He that learns from everyone. Who is powerful? He that governs his passions. Who is rich? He that is content. Who is that? Nobody.Smile
Mahesh Bote
Mahesh Bote
SSCrazy
SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)

Group: General Forum Members
Points: 2634 Visits: 1286
Parameters in SProcs are not optional. You have to mentioend them while calling the SProc. You can instead mention 'DEFAULT' keyword while passing the parameters. Like:

EXEC dbo.sp_Sample Default, Default



Mahesh


MH-09-AM-8694
MarkusB
MarkusB
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10401 Visits: 4208
Mahesh Bote (2/2/2009)
Parameters in SProcs are not optional. You have to mentioend them while calling the SProc. You can instead mention 'DEFAULT' keyword while passing the parameters. Like:

EXEC dbo.sp_Sample Default, Default



Mahesh

If you use default values for your parameters you don't have to mention them. A good example is sp_send_dbmail which has about 20 parameters, but most of the time I supply only 2 or 3.

Markus Bohse
Naveen Kumar-807681
Naveen Kumar-807681
SSC-Addicted
SSC-Addicted (414 reputation)SSC-Addicted (414 reputation)SSC-Addicted (414 reputation)SSC-Addicted (414 reputation)SSC-Addicted (414 reputation)SSC-Addicted (414 reputation)SSC-Addicted (414 reputation)SSC-Addicted (414 reputation)

Group: General Forum Members
Points: 414 Visits: 78
Hi,

I got the solution. create sp like

create PROCEDURE COMPLIANCE_TASK_DETAILS(@USR_ID INT, @OPERATION VARCHAR(10), @GET_ID INT = null,
@GET_TASK_NAME VARCHAR(300) = null, @GET_STATUS VARCHAR(100) = null,
@ASSIGNED_TO INT = null, @GET_COMMENTS VARCHAR(500) = null
)
AS
BEGIN
statements........
end

Who is wise? He that learns from everyone. Who is powerful? He that governs his passions. Who is rich? He that is content. Who is that? Nobody.Smile
mtaylor7210 63514
mtaylor7210 63514
SSC Journeyman
SSC Journeyman (92 reputation)SSC Journeyman (92 reputation)SSC Journeyman (92 reputation)SSC Journeyman (92 reputation)SSC Journeyman (92 reputation)SSC Journeyman (92 reputation)SSC Journeyman (92 reputation)SSC Journeyman (92 reputation)

Group: General Forum Members
Points: 92 Visits: 193
Dynamic sql will allow you do exactly what you are wanting.
Mitesh Oswal
Mitesh Oswal
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1168 Visits: 653


CREATE PROC usp_procdetails
@Param1 INT = 1
@Param2 VARCHAR(100) = 'Default'--This can work by adding value also.

AS

BEGIN
--Add your code depends upon default.
END

GO

EXEC usp_procdetails

GO



Regards,
Mitesh OSwal
+918698619998
Steve-3_5_7_9
Steve-3_5_7_9
SSCrazy
SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)

Group: General Forum Members
Points: 2894 Visits: 1609
mtaylor7210 63514 (4/9/2010)
Dynamic sql will allow you do exactly what you are wanting.


... but also opens you up to SQL Injection attacks.



mtaylor7210 63514
mtaylor7210 63514
SSC Journeyman
SSC Journeyman (92 reputation)SSC Journeyman (92 reputation)SSC Journeyman (92 reputation)SSC Journeyman (92 reputation)SSC Journeyman (92 reputation)SSC Journeyman (92 reputation)SSC Journeyman (92 reputation)SSC Journeyman (92 reputation)

Group: General Forum Members
Points: 92 Visits: 193
True but anytime you are taking parameters you open yourself up to injection.
Steve-3_5_7_9
Steve-3_5_7_9
SSCrazy
SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)

Group: General Forum Members
Points: 2894 Visits: 1609
The security vulnerability footprint is much higher when using dynamic sql over a properly written parametrized stored procedure.

I apologize to the original poster for getting off the "thread" topic.



nigel.
nigel.
Hall of Fame
Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)

Group: General Forum Members
Points: 3215 Visits: 2907
mtaylor7210 63514 (4/13/2010)
True but anytime you are taking parameters you open yourself up to injection.


No, I believe you are mistaken.

Please explain why you think using parameterized stored procedures opens you up to SQL injection?

Unless, of course, you use those parameters to create dynamic SQL.

--
Nigel
Useful Links:
How to post data/code on a forum to get the best help
The "Numbers" or "Tally" Table - Jeff Moden

Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search