473,396 Members | 2,018 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

SP that updates a table according to optional parameters?

Hi,

Here's my scenario: I want to update a row using an SP. Sometimes I want to update the whole row, sometimes just a couple of fields in the row. I don't want to write an SP for every scenario, I want to write one that knows only to update the fields for which I have passed in parameters containing data.

I have got so far with the SP:

CREATE PROCEDURE [dbo].[usp_updateprice]
@suppid varchar(3),
@partnum varchar(30),
@description varchar(60),
@xrefnum varchar(30),
@nato_code varchar(22),
@reconind varchar(1),
@discode varchar(2),
@np_discode varchar(2),
@minordq int,
@current_price money,
@price_annex_f money,
@price_am money,
@price_painted money,
@surcharge money,
@flagtext varchar(245)
AS
BEGIN
UPDATE prices SET suppid = @suppid,
partnum = @partnum,
description = @description,
xrefnum = @xrefnum,
nato_code = @nato_code,
reconind = @reconind,
discode = @discode,
np_discode = @np_discode,
minordq = @minordq,
current_price = @current_price,
price_annex_f = @price_annex_f,
price_am = @price_am,
price_painted = @price_painted,
surcharge = @surcharge,
flagtext = @flagtext
WHERE suppid = @suppid AND partnum = @partnum
END

Anyone have any ideas as to how this could be accomplished? I am calling the SP with VB.net

Many thanks in advance.
Apr 7 '09 #1
5 3117
No worries, I have found the answer.
Apr 7 '09 #2
Hi can u plz post the answer even I want to know
Apr 21 '09 #3
OK, this SP takes some parameters and updates a record with them. If any of the parameters that are passed to the procedure are NULL then the original values are left unchanged in the target field. The SP passes back the rowcount of rows updated which is useful to know.


GO
CREATE PROCEDURE [dbo].[usp_updateprice]
-- Incoming Parameters
@suppid varchar(3),
@partnum varchar(30),
@description varchar(60),
@current_price money
AS
BEGIN
UPDATE prices SET suppid = isnull(@suppid, suppid),
partnum = isnull(@partnum, partnum),
description = isnull(@description, description),
current_price = isnull(@current_price,current_price)
WHERE suppid = @suppid AND partnum = @partnum
END

return @@rowcount
Apr 21 '09 #4
Hi thnks but can we write the above Update query as Dynamic Sql?
Apr 22 '09 #5
ck9663
2,878 Expert 2GB
yes, build your t-sql string and use EXEC


-- CK
Apr 24 '09 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Vijay Balki | last post by:
This might be a dumb question to ask. Can someone answer me with patience.. When I have a typed or un-typed dataset, the only way I can update is like below? Dim con As New...
5
by: Patient Guy | last post by:
In my reading of the Strict and Transitional DTD for HTML 4.0, the table row (TR) elements are contained within table section elements: THEAD, TFOOT, and TBODY. The table section elements are...
16
by: ad | last post by:
Does C#2.0 support optional parameters like VB.NET: Function MyFunction(Optional ByVal isCenter As Boolean = False)
14
by: cody | last post by:
I got a similar idea a couple of months ago, but now this one will require no change to the clr, is relatively easy to implement and would be a great addition to C# 3.0 :) so here we go.. To...
12
by: pamelafluente | last post by:
Hi guys, In the past I have used several time optional parameters in my function. But Now I am more inclined to think that they are more dangerous than useful, and probably better to be...
1
Merlin1857
by: Merlin1857 | last post by:
How to search multiple fields using ASP A major issue for me when I first started writing in VB Script was constructing the ability to search a table using multiple field input from a form and...
17
by: DeZZar | last post by:
Hi all, I need to regularly backup my database as an Excel file and have been using the File Export option. Problem is I need anyone using the database to be able to do this easily - nopt...
6
by: provor | last post by:
Hello, I have the following code that I am using when a user presses a button to import an excel file into a table. The code is hard coded to point to the correct table. This works great for this...
7
by: jamesclose | last post by:
My problem is this (apologies if this is a little long ... hang in there): I can define a function in VB.NET with optional parameters that wraps a SQL procedure: Sub Test(Optional ByVal Arg1...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.