Click here to monitor SSC
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-Enthusiastic
SSC-Enthusiastic (162 reputation)SSC-Enthusiastic (162 reputation)SSC-Enthusiastic (162 reputation)SSC-Enthusiastic (162 reputation)SSC-Enthusiastic (162 reputation)SSC-Enthusiastic (162 reputation)SSC-Enthusiastic (162 reputation)SSC-Enthusiastic (162 reputation)

Group: General Forum Members
Points: 162 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
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1104 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
SSCarpal Tunnel
SSCarpal Tunnel (4.5K reputation)SSCarpal Tunnel (4.5K reputation)SSCarpal Tunnel (4.5K reputation)SSCarpal Tunnel (4.5K reputation)SSCarpal Tunnel (4.5K reputation)SSCarpal Tunnel (4.5K reputation)SSCarpal Tunnel (4.5K reputation)SSCarpal Tunnel (4.5K reputation)

Group: General Forum Members
Points: 4457 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-Enthusiastic
SSC-Enthusiastic (162 reputation)SSC-Enthusiastic (162 reputation)SSC-Enthusiastic (162 reputation)SSC-Enthusiastic (162 reputation)SSC-Enthusiastic (162 reputation)SSC-Enthusiastic (162 reputation)SSC-Enthusiastic (162 reputation)SSC-Enthusiastic (162 reputation)

Group: General Forum Members
Points: 162 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
Forum Newbie
Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)

Group: General Forum Members
Points: 6 Visits: 193
Dynamic sql will allow you do exactly what you are wanting.
Mitesh Oswal
Mitesh Oswal
Right there with Babe
Right there with Babe (798 reputation)Right there with Babe (798 reputation)Right there with Babe (798 reputation)Right there with Babe (798 reputation)Right there with Babe (798 reputation)Right there with Babe (798 reputation)Right there with Babe (798 reputation)Right there with Babe (798 reputation)

Group: General Forum Members
Points: 798 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
SSC Eights!
SSC Eights! (982 reputation)SSC Eights! (982 reputation)SSC Eights! (982 reputation)SSC Eights! (982 reputation)SSC Eights! (982 reputation)SSC Eights! (982 reputation)SSC Eights! (982 reputation)SSC Eights! (982 reputation)

Group: General Forum Members
Points: 982 Visits: 1584
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
Forum Newbie
Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)

Group: General Forum Members
Points: 6 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
SSC Eights!
SSC Eights! (982 reputation)SSC Eights! (982 reputation)SSC Eights! (982 reputation)SSC Eights! (982 reputation)SSC Eights! (982 reputation)SSC Eights! (982 reputation)SSC Eights! (982 reputation)SSC Eights! (982 reputation)

Group: General Forum Members
Points: 982 Visits: 1584
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.
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1333 Visits: 2823
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