Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
bougeac
Starting Member
7 Posts |
Posted - 2009-12-02 : 18:55:51
|
Hi, I have a frustrating problem...
I wrote a simple update statement earlier today that messed up a load of rows in one of my tables. The following is the basic statement (as i remember it)
UPDATE geographicalWeighting SET ItemValue1 = 'CASH' WHERE ItemValue1 = 'OTHERS' AND ItemValue2 NOT LIKE 'CASH%' AND FUNDDATE = (SELECT MAX(FACTDATE) FROM FACTDATETABLE)
My "FACTDATETABLE" had a maximum date value of 30/11/2009 and the following basic query retrieves 4 entries for this date with "ItemValue" set to "Others' :
SELECT * from geographicalWeighting where ItemValue1 = 'OTHERS' and ItemValue2 NOT LIKE 'CASH%' AND FUNDDATE = (SELECT MAX(FACTDATE) FROM FACTDATETABLE)
HOWEVER, when i ran the update code EVERY row in the GeographicalWeighting table had its ItemValue1 set to "CASH" irrespective of wether its value was 'OTHERS' or not.
The result is not what i expected and almost looks like only the UPDATE geographicalWeighting SET ItemValue1 = 'CASH'
part of the update was actually applied, the subsequent delimiting being totally ignored.
Am i doing something stupid here??
Hope someone out there has an idea.
Cheers, Chris
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-12-03 : 03:48:15
|
Can you post table structure with sample data?
Madhivanan
Failing to plan is Planning to fail |
 |
|
bougeac
Starting Member
7 Posts |
Posted - 2009-12-03 : 06:18:33
|
Hi, following on from my previous post i have worked out a solution.
By changing the following code :
UPDATE geographicalWeighting SET ItemValue1 = 'CASH' WHERE ItemValue1 = 'OTHERS' AND ItemValue2 NOT LIKE 'CASH%' AND FUNDDATE = (SELECT MAX(FACTDATE) FROM FACTDATETABLE)
to :
declare @processDate as dateTime
set @processDate = (SELECT MAX(FACTDATE) FROM FACTDATETABLE)
UPDATE geographicalWeighting SET ItemValue1 = 'CASH' WHERE ItemValue1 = 'OTHERS' AND ItemValue2 NOT LIKE 'CASH%' AND FUNDDATE = @processDate
my update behaves as expected!
For some reason the join on the "select max" was causing the issue, i dont understand this...
Cheers,
Chris
|
 |
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2009-12-03 : 07:49:45
|
The following also works as expected for me, so I can't replicate your problem...
--structure declare @geographicalWeighting table (ItemValue1 varchar(9), ItemValue2 varchar(9), FUNDDATE datetime) declare @FACTDATETABLE table (FACTDATE datetime) --/
--data insert @geographicalWeighting select 'OTHERS', 'CASH', '20091203' union all select 'OTHERS', 'A', '20091203' union all select 'OTHERS', 'B', '20091203' union all select 'OTHERS', 'B', '20091202'
insert @FACTDATETABLE select '20091203' union all select '20091202' --/
--calculation UPDATE @geographicalWeighting SET ItemValue1 = 'CASH' WHERE ItemValue1 = 'OTHERS' AND ItemValue2 NOT LIKE 'CASH%' AND FUNDDATE = (SELECT MAX(FACTDATE) FROM @FACTDATETABLE) --/
--results select * from @geographicalWeighting --/
Ryan Randall - Yak of all trades Solutions are easy. Understanding the problem, now, that's the hard part. |
 |
|
bougeac
Starting Member
7 Posts |
Posted - 2009-12-03 : 11:15:20
|
Hey Ryan, thanks for putting in the time to do that!
VERY puzzling... I have been writing sql stuff for years and have never experienced this behaviour before.
Although i worked out a way around it, id still like to know why my delimiting didnt seem to "kick in" when using the "select max date etc".
cheers,
chris |
 |
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2009-12-03 : 11:56:11
|
Hi Bougeac
quote:
The following is the basic statement (as i remember it)
UPDATE geographicalWeighting SET ItemValue1 = 'CASH' WHERE ItemValue1 = 'OTHERS' AND ItemValue2 NOT LIKE 'CASH%' AND FUNDDATE = (SELECT MAX(FACTDATE) FROM FACTDATETABLE)
You said 'as I remember it'.
Is it maybe just that you made a typo when you actually ran the update statement the first time.
I take it that you can't reproduce the problem with the code you posted?
Charlie =============================================================== Msg 3903, Level 16, State 1, Line 1736 The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
 |
|
bougeac
Starting Member
7 Posts |
Posted - 2009-12-03 : 19:38:07
|
Hi Charlie, YES i can reproduce the error every time!!
If i change my modified code back to the original, i get over 6000 rows being updated with "Cash" even though there are only 4 records that should actually be updated based on the delimiting criteria. This is very puzzling and doesnt make sense... |
 |
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2009-12-04 : 04:04:56
|
OK.
well there *must* be something weird with the data I guess.
Could you:
1) Post the table structure
2) Post the exact query (is it the one on the post or is it slightly different)
3) Give some sample data if possible.
If it can be reproduced on your setup, stands to reason one of us can replicate it as well.
What is your setup (vanilla sql server 2000?)
Regards,
Charlie =============================================================== Msg 3903, Level 16, State 1, Line 1736 The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
 |
|
bougeac
Starting Member
7 Posts |
Posted - 2009-12-07 : 05:35:08
|
Hi guys,
ok, here is the table structure :
GeographicWeighting :
FundDataDate datetime FundId int 4 ItemName01 nvarchar 50 ItemName02 nvarchar 50 ItemValue01 float 8 ItemValue02 float 8
Sample data :
30/11/2009 5 N America Bonds - Govt 45 0 30/11/2009 30/11/2009 5 N America Bonds - I/Linked 7 0 30/11/2009 30/11/2009 5 N America F/R Notes 5 0 30/11/2009 30/11/2009 5 UK Bonds - Corp 3 0 30/11/2009 30/11/2009 6 Europe Bonds 16 0 30/11/2009 30/11/2009 6 Others Equities 8 0 30/11/2009 30/11/2009 6 Others Equities 8 0 30/11/2009 30/11/2009 6 Japan Equities 4 0 30/11/2009 30/11/2009 6 N America Bonds 10 0 30/11/2009 30/11/2009 6 N America Bonds - I/Linked 5 0 30/11/2009 30/11/2009 6 N America Equities 16 0 30/11/2009
The following is the code (that if applied to the above data would cause EVERY entry to be stamped with CASH...)
( the max date value being 30/11/2009)
UPDATE xxxweb02.fundfactsheetdev.dbo.GeographicalWeighting SET itemname01 = 'Cash' WHERE ( itemname01 = 'Others' AND itemname02 NOT LIKE 'Cash%' AND fundDataDate = (SELECT MAX(FundFactSheetDate) FROM xxxweb02.fundfactsheetdev.dbo.FundFactSheetDates) )
The following code causes only 2 records from the above data example to be updated :
declare @processDate as datetime
set @processdate = (SELECT MAX(FundFactSheetDate) FROM xxxweb02.fundfactsheetdev.dbo.FundFactSheetDates)
UPDATE xxx.fundfactsheetdev.dbo.GeographicalWeighting SET itemname01 = 'Cash' FROM xxx.fundfactsheetdev.dbo.GeographicalWeighting WHERE itemname01 = 'Others' AND funddatadate = @processDate
I MUST have been doing something stupid, but just cant see it...
Chris
|
 |
|
bougeac
Starting Member
7 Posts |
Posted - 2009-12-07 : 05:37:10
|
Charlie,
Forgot to mention that Yes, im running this query against a sqlserver 2000 setup.
Cheers,
Chris |
 |
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2009-12-07 : 08:15:09
|
Erm -- are you sure about the sample data? It doesn't seem to fit the table you posted. (in particular a date has a hard time being converted to float) (last column of sample data)
also there is NO record in the table with an itemname01 = 'Others'
Charlie =============================================================== Msg 3903, Level 16, State 1, Line 1736 The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
 |
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2009-12-07 : 08:23:20
|
never mind -- I see the tabs now.
Is this a good representation?
/* Hi guys,
ok, here is the table structure :
GeographicWeighting : FundDataDate datetime FundId int 4 ItemName01 nvarchar 50 ItemName02 nvarchar 50 ItemValue01 float 8 ItemValue02 float 8 */ IF OBJECT_ID('tempDb..#GeographicWeighting') IS NOT NULL DROP TABLE #GeographicWeighting CREATE TABLE #GeographicWeighting ( [fundDataDate] DATETIME , [FundId] INT -- No widths on INTS 4 , [ItemName01] NVARCHAR(50) , [ItemName02] NVARCHAR(50) , [ItemValue01] FLOAT(8) , [ItemValue02] FLOAT(8) )
/* Sample data :
30/11/2009 5 N America Bonds - Govt 45 0 30/11/2009 30/11/2009 5 N America Bonds - I/Linked 7 0 30/11/2009 30/11/2009 5 N America F/R Notes 5 0 30/11/2009 30/11/2009 5 UK Bonds - Corp 3 0 30/11/2009 30/11/2009 6 Europe Bonds 16 0 30/11/2009 30/11/2009 6 Others Equities 8 0 30/11/2009 30/11/2009 6 Others Equities 8 0 30/11/2009 30/11/2009 6 Japan Equities 4 0 30/11/2009 30/11/2009 6 N America Bonds 10 0 30/11/2009 30/11/2009 6 N America Bonds - I/Linked 5 0 30/11/2009 30/11/2009 6 N America Equities 16 0 30/11/2009 */ INSERT #GeographicWeighting ( [fundDataDate] , [FundId] , [ItemName01] , [ItemName02] , [ItemValue01] , [ItemValue02] )
SELECT '20091130', 5, 'N America', 'Bonds - Govt', 45, 0 UNION ALL SELECT '20091130', 5, 'N America', 'Bonds - I/Linked', 7, 0 UNION ALL SELECT '20091130', 5, 'N America', 'F/R Notes', 5, 0 UNION ALL SELECT '20091130', 5, 'UK Bonds', '- Corp', 3, 0 UNION ALL SELECT '20091130', 6, 'Europe', 'Bonds', 16, 0 UNION ALL SELECT '20091130', 6, 'Others', 'Equities', 8, 0 UNION ALL SELECT '20091130', 6, 'Others', 'Equities', 8, 0 UNION ALL SELECT '20091130', 6, 'Japan', 'Equities', 4, 0 UNION ALL SELECT '20091130', 6, 'N America', 'Bonds', 10, 0 UNION ALL SELECT '20091130', 6, 'N America', 'Bonds - I/Linked', 5, 0 UNION ALL SELECT '20091130', 6, 'N America', 'Equities', 16, 0 /* The following is the code (that if applied to the above data would cause EVERY entry to be stamped with CASH...)
( the max date value being 30/11/2009)
UPDATE xxxweb02.fundfactsheetdev.dbo.GeographicalWeighting SET itemname01 = 'Cash' WHERE ( itemname01 = 'Others' AND itemname02 NOT LIKE 'Cash%' AND fundDataDate = (SELECT MAX(FundFactSheetDate) FROM xxxweb02.fundfactsheetdev.dbo.FundFactSheetDates) ) */ UPDATE #GeographicWeighting SET itemname01 = 'Cash' WHERE ( itemname01 = 'Others' AND itemname02 NOT LIKE 'Cash%' AND fundDataDate = '20091130' )
/* The following code causes only 2 records from the above data example to be updated :
declare @processDate as datetime
set @processdate = (SELECT MAX(FundFactSheetDate) FROM xxxweb02.fundfactsheetdev.dbo.FundFactSheetDates)
UPDATE xxx.fundfactsheetdev.dbo.GeographicalWeighting SET itemname01 = 'Cash' FROM xxx.fundfactsheetdev.dbo.GeographicalWeighting WHERE itemname01 = 'Others' AND funddatadate = @processDate
I MUST have been doing something stupid, but just cant see it...
Chris */
SELECT * FROM #GeographicWeighting
I only get 2 rows updated from the UPDATE statement. My results look like
fundDataDate FundId ItemName01 ItemName02 ItemValue01 ItemValue02 2009-11-30 00:00:00.000 5 N America Bonds - Govt 45 0 2009-11-30 00:00:00.000 5 N America Bonds - I/Linked 7 0 2009-11-30 00:00:00.000 5 N America F/R Notes 5 0 2009-11-30 00:00:00.000 5 UK Bonds - Corp 3 0 2009-11-30 00:00:00.000 6 Europe Bonds 16 0 2009-11-30 00:00:00.000 6 Cash Equities 8 0 2009-11-30 00:00:00.000 6 Cash Equities 8 0 2009-11-30 00:00:00.000 6 Japan Equities 4 0 2009-11-30 00:00:00.000 6 N America Bonds 10 0 2009-11-30 00:00:00.000 6 N America Bonds - I/Linked 5 0 2009-11-30 00:00:00.000 6 N America Equities 16 0
Charlie =============================================================== Msg 3903, Level 16, State 1, Line 1736 The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
 |
|
bougeac
Starting Member
7 Posts |
Posted - 2009-12-08 : 05:40:59
|
Hi Charlie, which update statement did you use ?
Im definately (unfortunately) correct regarding the results on my database - with that data structure/data and the update statement using the "and fundate = (select max(date etc", i get a mass update of my records. I had to restore the database back from a previous save after i ran the update last week, a pain in the backside!!
On the basis that you did run an identical update, do you have any thoughts on why the delimiting (on my db) didnt seem to get used ?
Could there be something screwed at a lower level ? Should i perhaps of looked at the execution plan of the sql to see if there was something sinister lurking in there. Could the primary key structure on the table possibly have a detrimental affect on the update (cant see it myself)...
I would like to get to the bottom of this as its gonna make me nervous in the future whenever i have a similair update to perform...
Cheers,
Chris
|
 |
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2009-12-08 : 05:52:54
|
Can you copy my chunk of code and run it? It only references a temp table.
I was using the update statement:
UPDATE #GeographicWeighting SET itemname01 = 'Cash' WHERE ( itemname01 = 'Others' AND itemname02 NOT LIKE 'Cash%' AND fundDataDate = '20091130' )
Obviously I don't have the base tables that you have but because of the
itemname01 = 'Others'
line I can see no way that the update would have behaved the way that you describe.
Can you replicate the problem in any way that is portable. If you can then we have something to test with.
Can you even replicate the problem again by restoring a previous backup to a temp location and then running the update statement?
Charlie =============================================================== Msg 3903, Level 16, State 1, Line 1736 The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
 |
|
|
|
|
|
|