Solved

Optional Parameters for Update

Posted on 2003-11-06
13
1,769 Views
Last Modified: 2010-05-18
Hello all,

I am building an ASP application which will update a SQL Server database.

The app will, at various times, update several different fields within the same table.  For example, if I am updating a customers table, I may need to update only address one time, and only phone number another time, and still yet other times, I may need to update all fields in the table.  It seems like I shouldn't have to write separate stored procedures for each of these instances.  How can I write my stored procedure such that I can update a variable number of fields each time it is called.
0
Comment
Question by:knottydrd
[X]
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
  • 6
  • 3
  • 3
  • +1
13 Comments
 
LVL 58

Accepted Solution

by:
amit_g earned 250 total points
ID: 9696200
Build the update statement in ASP dynamically instead of writing the stored procedure.
0
 

Author Comment

by:knottydrd
ID: 9696263
I've done that in the past, but I've not been happy with the performance.  Is there a way to do it in SQL Server Stored Proc?
0
 
LVL 58

Expert Comment

by:amit_g
ID: 9696316
There is no performance gain by using stored procedure vs. executing the dynamic sql as the SQL server caches last few queries irrespctive of its origin. Stored procedure are also cached in the same way as any other dynamic query and so they have no advantage performance wise.

You can have optional parameter in stored procedures and do what you are asking but that only complicates the code and spreads business logic in 2 places instead of one. That is a maintanace overhead. Unless you or your company has a standard of writing all queries in stored procedure, there is no need for writing stored procedures for simple queries like one line select, update and inserts.

If you still feel that you need to write a stored procedure we can tell you how to use optional parameters.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:knottydrd
ID: 9696418
I'm working with an existing application in which all other db opps are done using stored procedures.
0
 
LVL 15

Expert Comment

by:namasi_navaretnam
ID: 9696453
Amit G is absolutely right!

Here is an example.

   sSQL = ""     
   sSQL = sSQL + " Update  MyTable "
   sSQL = sSQL + " Set My_id = " + CStr(aMyId) + ",  "
   sSQL = sSQL + "     Status = 'Test.' "
   sSQL = sSQL + "  Where (Status = 'A') "

   objConn1.BeginTrans  
   objConn1.Execute sSQL
   objConn1.CommitTrans

But the easiest way to to update all fields all the time if possible. It would be easier to maintain such code that way.

Even if you do that using strored proc, pass a value to all input parameters all time. But within proc update fields where the corresponding input variables are not null.

This way you have only one proc.

 
   
0
 
LVL 58

Expert Comment

by:amit_g
ID: 9696608
Ok. Here is how you can create a stored procedure with optional parameters ...

CREATE PROCEDURE MyStoredProcedure
       @first int = NULL
      ,@second int = NULL
      ,@third int = 0
AS

...
...
...

You can give a null value or a predetermined value to any parameter that you make an optional parameter. Later in the logic you can check if the parameter has the default value (say null) and that means it was not passed by the caller. To call these stored procedure from the client you can use ADO's Command object with or without Parameters. For example you could call the aboove stored procedure as

    cmd.CommandText = "MyStoredProcedure"
    cmd.CommandType = adCmdStoredProc
    cmd.Parameters.Append cmd.CreateParameter("@second", adInteger, adParamInput)
    cmd.Parameters("param1") = Whatever

    cmd.Execute

This way you have passed only second parameter. You can also call this in this way ...

    cmd.CommandText = "exec MyStoredProcedure @second = " & Whatever

    cmd.Execute
0
 

Author Comment

by:knottydrd
ID: 9696647
Thanks!  Let me review and try this out, I'll post as to how it goes.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 9699091
Also, when you get a chance please maintain yur old open questions:

1 09/02/2003 250 Email a "Fillable" PDF  Open JavaScript
2 08/07/2003 125 Unable to open registry key  Open Active Server Pages (ASP)
3 09/14/2003 250 NVidia Video Card only 16 colors  Open Windows ME
4 10/02/2003 500 Implicit conversion from data type varch...  Open Microsoft SQL Server

Thanks,
Anthony
0
 
LVL 15

Assisted Solution

by:namasi_navaretnam
namasi_navaretnam earned 250 total points
ID: 9700713
Within your stored proc you write an update statement using case. This way you have single proc to update you table.

Let say for example  @col1, col2 are input parameters. (col1 is varchar, col2 is integer)

update YourTable
set col1 = CASE WHEN (@col1 IS NULL or @col1 = '') THEN col1 ELSE @col1 END,
    col2 = CASE WHEN (@col2 IS NULL) THEN col2 ELSE @col2 END
where pk_id = @pk_id


My Example that works,

declare @job_desc varchar(100),
        @min_lvl integer

select @job_desc = 'TEST', @min_lvl = 10

update Jobs
set job_desc = CASE WHEN (@job_desc IS NULL or @job_desc = '') THEN Job_Desc ELSE @Job_DESC END,
    min_lvl = CASE WHEN (@min_lvl IS NULL) THEN min_lvl ELSE @min_lvl END
where job_id = 1


HTH

Namasi Navaretnam
0
 
LVL 15

Expert Comment

by:namasi_navaretnam
ID: 9954918
Would you need further help? If not, please close this issue. :)

Regards.
0
 

Author Comment

by:knottydrd
ID: 9958332
Ok.  I'll close it today.
0
 

Author Comment

by:knottydrd
ID: 9958789
I'm splitting the points because this project was scuttled and I never got a chance to fully implement these solutions.  I am picking the "right" answer based upon tests that I did at the time of this post.
0
 

Author Comment

by:knottydrd
ID: 9958818
Amit_g's original answer turned out to be the best solution.  It is best to make the dynamic SQL statement in your ASP code, but in this particular application, I was extending functionality of an established app.  This app made strict use of keeping queries in Stored Procedures and not ASP.  I felt that it was best to continue this with the extension that I was working on.

Thanks all for the comments.
0

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
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…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

739 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