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 Veteran
SSC Veteran (234 reputation)SSC Veteran (234 reputation)SSC Veteran (234 reputation)SSC Veteran (234 reputation)SSC Veteran (234 reputation)SSC Veteran (234 reputation)SSC Veteran (234 reputation)SSC Veteran (234 reputation)

Group: General Forum Members
Points: 234 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
SSCommitted
SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)

Group: General Forum Members
Points: 1526 Visits: 1274
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
SSCertifiable
SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)

Group: General Forum Members
Points: 5967 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 Veteran
SSC Veteran (234 reputation)SSC Veteran (234 reputation)SSC Veteran (234 reputation)SSC Veteran (234 reputation)SSC Veteran (234 reputation)SSC Veteran (234 reputation)SSC Veteran (234 reputation)SSC Veteran (234 reputation)

Group: General Forum Members
Points: 234 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 Rookie
SSC Rookie (44 reputation)SSC Rookie (44 reputation)SSC Rookie (44 reputation)SSC Rookie (44 reputation)SSC Rookie (44 reputation)SSC Rookie (44 reputation)SSC Rookie (44 reputation)SSC Rookie (44 reputation)

Group: General Forum Members
Points: 44 Visits: 193
Dynamic sql will allow you do exactly what you are wanting.
Mitesh Oswal
Mitesh Oswal
SSC Eights!
SSC Eights! (918 reputation)SSC Eights! (918 reputation)SSC Eights! (918 reputation)SSC Eights! (918 reputation)SSC Eights! (918 reputation)SSC Eights! (918 reputation)SSC Eights! (918 reputation)SSC Eights! (918 reputation)

Group: General Forum Members
Points: 918 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
SSCommitted
SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)

Group: General Forum Members
Points: 1510 Visits: 1599
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 Rookie
SSC Rookie (44 reputation)SSC Rookie (44 reputation)SSC Rookie (44 reputation)SSC Rookie (44 reputation)SSC Rookie (44 reputation)SSC Rookie (44 reputation)SSC Rookie (44 reputation)SSC Rookie (44 reputation)

Group: General Forum Members
Points: 44 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
SSCommitted
SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)

Group: General Forum Members
Points: 1510 Visits: 1599
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.
SSCommitted
SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)

Group: General Forum Members
Points: 1875 Visits: 2859
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