473,775 Members | 2,570 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

column update function

ben
This is probably a common problem with a standard design pattern, but
I'm having trouble finding the solution.

I have a table with a lot of columns, for this example I'll just use
three but in reality its more like 20.

Create Table myTable (int col_one primary key, int col_two,
varchar(20) col_three) etc...

I want to write a sproc that allows updating of this column. Say I
have a sproc

create sproc myUpdate int @col_one, int @col_two, varchar(20)
col_three

as

update myTable col_two = @col_two, col_three = @col_three
where col_one = @col_one

then if I only want to update col_two I have to pass in the current
value of col_three so that it remains the same, which seems pretty
inefficient. so I could change it to:

as

update myTable col_two = coalasce(@col_t wo, col_two)
, col_three = coalasce(@col_t hree, col_three)
where col_one = @col_one

and then if I wanted to leave col_three the way it is then I could
just do

exec myUpdate 1, 2, NULL

the only problem here is that what if the value of col_three is
currently 3, and I want to set it to NULL? Under the current method,
setting someting to NULL is impossible

finally, I'd like to use parameter naming in my exec calls. that way
I can just say someting like

exec myUpdate 1, col_three=3

this would update col_three to 3 and leave the rest of the fields
untouched. you can see how handy this would be if you just want to
change a few of the fields in a table with a large number of columns.

I'm sure this has been done before, can somebody point me in the right
direction?

Thanks,

Ben
Jul 20 '05 #1
1 3698
ben (sa********@yah oo.com) writes:
update myTable col_two = coalasce(@col_t wo, col_two)
, col_three = coalasce(@col_t hree, col_three)
where col_one = @col_one

and then if I wanted to leave col_three the way it is then I could
just do

exec myUpdate 1, 2, NULL

the only problem here is that what if the value of col_three is
currently 3, and I want to set it to NULL? Under the current method,
setting someting to NULL is impossible

finally, I'd like to use parameter naming in my exec calls. that way
I can just say someting like

exec myUpdate 1, col_three=3

this would update col_three to 3 and leave the rest of the fields
untouched. you can see how handy this would be if you just want to
change a few of the fields in a table with a large number of columns.


T-SQL is not a language that lends itself to this sort of thing. There
is no way to tell whether a parameter was passed explicitly or not. You
can of course test for NULL, but it may have been an explicit NULL.

One alternative would be to have extra marker variables to tell whether
a parameter applies or not. It quickly gets bulky. It can be reduced to
a single parameter which is a bitmask, but that is cryptic and error-prone.

What we do in our update procedures is to pass all column values. But
then we typically have read all to the GUI and now we are sending them
back. If some operation updates only affects a few columns, that is
typically an individual UPDATE statement in a different procedure.
--
Erland Sommarskog, SQL Server MVP, so****@algonet. se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
9012
by: Lisa Tang | last post by:
Hi, I have a table t1 with a long raw column, and I need to create a view v1 with a long raw column being a function ff1 of the long raw column in t1. And I need to update t1 with reverse function rf1 whenever there is update, insert or delete to v1. My problem is it is not allowed to refer long raw from :new or :old, so I cannot get the long raw value when v1 is inserted, updated or deleted. Could anyone give me some advice or...
8
11599
by: pb648174 | last post by:
I have a single update statement that updates the same column multiple times in the same update statement. Basically i have a column that looks like .1.2.3.4. which are id references that need to be updated when a group of items is copied. I can successfully do this with cursors, but am experimenting with a way to do it with a single update statement. I have verified that each row being returned to the Update statement (in an...
12
3684
by: Philip Sherman | last post by:
I'm trying to copy production statistics to a test database and can't set the column statistics for a VARGRAPHIC column to match what RUNSTATS generated on production. The reason code and some testing I did indicates that the length of the low2key value is too long. It almost looks like the potential length of data to be stored is being calculated on the length of the hex string; without consideration that two bytes of the string...
2
3570
by: Tim Newton | last post by:
Hi I have a vb app that uses an access database to store information. This app has been distributed to several users. I would like to increase the size of a field in an access table using my vb app so the users dont have to input all their data again, what is the best way to do it. I normally use oracle and would call something like an "alter table modfiy column col1 varchar2(100)". I'm guessing this cannot be done in access and a...
9
2695
by: Max Weebler | last post by:
Hi, I have a datagrid built that has an alternating item style that sets the backcolor and ForeColor of its rows. I have 4 template columns. One of them has a LinkButton embedded in it to perform updates. All the styles that are set are being followed by all templated columns with the exception for the update column. The update column does set the BackColor appropriately but when setting its ForeColor the LinkButton's color is blue and...
6
10680
by: Aaron Smith | last post by:
Ok. I have a dataset that has multiple tables in it. In one of the child tables, I have a column that I added to the DataSet (Not in the DataSource). This column does not need to be stored in the data on the datasource. It simply gets the first name and last name of an instructor and displays it in the grid. I have two major problems.... One, it doesn't display in the column until the row is saved, (Even after calling a refresh on the...
5
5567
by: SQLMan_25 | last post by:
Hi All, I am trying to create a user defined function that fetches the price of an item. I have written a scalar function that takes itemid and returns its price. Simple version of tables would be like this: Item( itemid int,--pk price decimal(18,4)
7
4232
by: jknaty | last post by:
I'm trying to create a function that splits up a column by spaces, and I thought creating a function that finds the spaces with CHARINDEX and then SUBSTRING on those values would an approach. I get an error saying that the I have an Invalid column 'Course_Number'. Not sure why but I am very new to User Defined Functions. Here is what I have so far: CREATE FUNCTION CourseEvalBreakdown (
1
2600
by: Odd B Andersen | last post by:
I am trying to create a function that adds 1 to the value in a hexadecimalcolumn. This function I want to use inside an SQL PL procedure. I have a table with a column with char(16) for bit data. A 'select hex(col1) from tab1' may return x'FFF59E344C59684CA17BB105677343BE'. I have tried to create a function that takes the hexadecimal value (the in parameter is also char(16) for bit data) and adds 1 to the input value. I have managed to add...
1
3142
by: rverghese | last post by:
I have a BEFORE UPDATE trigger on a table that calls the tsvector_update_trigger() built in function to update a tsvector field. Since this trigger is called for every update to the table, the trigger function will be called even when the tsvector related field is not updated. My question is, does the index on that tsvector column get updated each time, even though the column is being updated with the same value? If it is I could replace...
0
10268
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10107
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10048
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9916
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8939
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7464
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5360
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5486
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4017
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.