dbTalk Databases Forums  

Re: Updating Master Table Using Transaction Table Where Null Value Means Leave Field Alone

microsoft.public.sqlserver.mseq microsoft.public.sqlserver.mseq


Discuss Re: Updating Master Table Using Transaction Table Where Null Value Means Leave Field Alone in the microsoft.public.sqlserver.mseq forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Vishal Parkar
 
Posts: n/a

Default Re: Updating Master Table Using Transaction Table Where Null Value Means Leave Field Alone - 07-11-2003 , 07:17 PM






Quote:
What is the best way of dealing with the null values in the
TransactionsTable?
The best way of dealing with null values is not to have them.

Try :
--following trigger will give you an idea how you can implement your
requirements using trigger.
alter trigger tr_transtype on TransactionsTable
for insert,update
as
if @@rowcount=0
return

if exists(select 1 from deleted)
begin
if update(value1)
update b
set iname=a.iname,
value1 = b.value1 + isnull(a.value1,0)
from mastertable b, inserted a where a.contractid = b.contractid
--updating existing row into mastertable if contractid is new.

if update(value2)
update b
set iname=a.iname,
value2 = b.value2 + isnull(a.value2,0)
from mastertable b, inserted a where a.contractid = b.contractid
--updating existing row into mastertable if contractid is new.

if update(value3)
update b
set iname=a.iname,
value2 = b.value3 + isnull(a.value3,0)
from mastertable b, inserted a where a.contractid = b.contractid
--updating existing row into mastertable if contractid is new.

return
end

if exists(select 1 from inserted)
begin
insert into [MasterTable] (contractid,iname,value1,value2,value3)
select contractid,iname,value1,value2,value3 from inserted
where contractid not in(select contractid from [dbo].[MasterTable] )
--inserting new row into mastertable if contractid is new.

end

--
-Vishal
unemotionalhumanoid <unemotionalhumanoid (AT) yahoo (DOT) com> wrote

Quote:
I have two tables a master table and transaction table, the master table
is
updated by records in the transaction table. A simplification of the
tables
is below:

CREATE TABLE [dbo].[MasterTable] (
[ContractID] [int] NOT NULL ,
[IName] [varchar] (50) COLLATE Latin1_General_CI_AS NULL ,
[Value1] [int] NULL ,
[Value2] [int] NULL ,
[Value3] [int] NULL ,
[Deleted] [bit]
) ON [PRIMARY]
GO

ALTER TABLE [dbo].[MasterTable] WITH NOCHECK ADD
CONSTRAINT [PK_MasterTable] PRIMARY KEY CLUSTERED
(
[ContractID]
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[TransactionsTable] (
[ContractID] [int] NOT NULL ,
[TransactionType] [char] (1) COLLATE Latin1_General_CI_AS NOT NULL ,
[IName] [varchar] (50) COLLATE Latin1_General_CI_AS NULL ,
[Value1] [int] NULL ,
[Value2] [int] NULL ,
[Value3] [int] NULL
) ON [PRIMARY]
GO

The TransactionType is either 'U' or 'D' meaning Update or Delete. I plan
to
use a trigger on the TransactionsTable to update the MasterTable, when a
transaction record is added to the TransactionsTable the record with the
same ContractID in the MasterTable will be updated if a record doesn't
exist
it will be created. The Value* fields will contain a positive or negative
integer and will be added to the value in the corresponding Value* field
in
the MasterTable. If the Value* field in the TransactionsTable contains a
null then the corresponding Value* field in the MasterTable will not be
updated, it will keep the same value. An Example:

MasterTable Containing Records:

1,'test',5,6,7,0

After record below has been added to TransactionsTable:

1,'U','test name', 5,null,-7,0

The record in the MasterTable should be:

1,'test name',10,6,0,0

What is the best way of dealing with the null values in the
TransactionsTable? In my actual tables I have 20 fields that need to be
updated like this.

Thank you in advance






Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.