Reputation: 366
I have a table with 1000+ records in SQL Server 2008 R2. There is a SharePoint 2010 interface where users can update the rows of this table in real time, and there is one field (CLASS) that should be calculated upon update of one or two columns (COUNTRY_REASON or/and VENDOR_REASON).
I am thinking about creating an AFTER UPDATE trigger upon update, although I wonder if it won't get in a loop of some kind?
Is there a better way to perform this update than using a trigger? What is your opinion/advice?
CREATE TRIGGER [dbo].[TRG_NOSSCE_UPDATE_CLASS]
ON [TO_BDB].[dbo].[to_bdb_nossce_comm_act]
after UPDATE
AS
BEGIN
UPDATE [TO_BDB].[dbo].[to_bdb_nossce_comm_act]
SET class = CASE
WHEN ( country_reason = '' OR country_reason IS NULL )
AND ( vendor_reason = '' OR vendor_reason IS NULL )
THEN 'NOT CLASSIFIED'
WHEN country_reason = 'Vendor_Issue'
AND ( vendor_reason = '' OR vendor_reason IS NULL )
THEN 'SITE ISSUE'
WHEN country_reason = 'Vendor_Issue'
OR country_reason = ''
OR country_reason IS NULL
THEN (
CASE
WHEN vendor_reason IN ( 'Site Product Quality', 'Site QA Release' )
THEN 'SITE QUALITY'
WHEN vendor_reason = 'Customer Order Management'
THEN 'CPO SCM'
WHEN vendor_reason LIKE 'Other%' THEN 'OTHER REASON'
ELSE 'SITE SCM'
END
)
ELSE (
CASE
WHEN country_reason IN ( 'Local Product Quality',
'Local QA Release'
)
THEN 'CPO QUALITY'
WHEN country_reason IN ( 'Customs Clearance',
'Transport Damage',
'Transport Issue' )
THEN 'TRANSIT'
WHEN country_reason IN (
'Artwork', 'Complaince Checks',
'Registration',
'Safety Label Changes' )
THEN 'CPO LCM'
WHEN country_reason = 'Vendor_Issue'
THEN ''
WHEN country_reason LIKE 'Other%'
THEN 'OTHER REASON'
ELSE 'CPO SCM'
END
)
END
WHERE class = ''
OR class IS NULL
AND Len(comment) > 0
END
go
Upvotes: 1
Views: 2895
Reputation: 952
You use "INSTEAD OF INSERT,UPDATE"
and avoid the update with a single insert
Articles:
Try this ! add other columns where i have commented
create TRIGGER TRG_NOSSCE_UPDATE_CLASS
ON [dbo].[to_bdb_nossce_comm_act]
instead OF UPDATE
AS
BEGIN
UPDATE a
SET a.class = CASE
WHEN ( a.country_reason = ''
OR a.country_reason IS NULL )
AND ( a.vendor_reason = ''
OR a.vendor_reason IS NULL ) THEN 'NOT CLASSIFIED'
WHEN a.country_reason = 'Vendor_Issue'
AND ( a.vendor_reason = ''
OR a.vendor_reason IS NULL ) THEN 'SITE ISSUE'
WHEN a.country_reason = 'Vendor_Issue'
OR a.country_reason = ''
OR a.country_reason IS NULL THEN ( CASE
WHEN a.vendor_reason IN ( 'Site Product Quality', 'Site QA Release' ) THEN 'SITE QUALITY'
WHEN a.vendor_reason = 'Customer Order Management' THEN 'CPO SCM'
WHEN a.vendor_reason LIKE 'Other%' THEN 'OTHER REASON'
ELSE 'SITE SCM'
END )
ELSE ( CASE
WHEN a.country_reason IN ( 'Local Product Quality', 'Local QA Release' ) THEN 'CPO QUALITY'
WHEN a.country_reason IN ( 'Customs Clearance', 'Transport Damage', 'Transport Issue' ) THEN 'TRANSIT'
WHEN a.country_reason IN ( 'Artwork', 'Complaince Checks', 'Registration', 'Safety Label Changes' ) THEN 'CPO LCM'
WHEN a.country_reason = 'Vendor_Issue' THEN ''
WHEN a.country_reason LIKE 'Other%' THEN 'OTHER REASON'
ELSE 'CPO SCM'
END )
END
--,
--a.column1=b.column1,
--a.othercolumns=b.othercolumns
FROM [dbo].[to_bdb_nossce_comm_act] a,
inserted b
WHERE a.class = ''
OR a.class IS NULL
AND Len(a.comment) > 0
END
go
Imagining you use a cursor in your SP and have @class,@countryreason and @vendorreason as cursor variables, we can do as below
cursor loop....start
SET @class = CASE
WHEN ( @country_reason = ''
OR @country_reason IS NULL )
AND ( vendor_reason = ''
OR @vendor_reason IS NULL ) THEN 'NOT CLASSIFIED'
WHEN @country_reason = 'Vendor_Issue'
AND ( @vendor_reason = ''
OR @vendor_reason IS NULL ) THEN 'SITE ISSUE'
WHEN @country_reason = 'Vendor_Issue'
OR @country_reason = ''
OR @country_reason IS NULL THEN ( CASE
WHEN @vendor_reason IN ( 'Site Product Quality', 'Site QA Release' ) THEN 'SITE QUALITY'
WHEN @vendor_reason = 'Customer Order Management' THEN 'CPO SCM'
WHEN @vendor_reason LIKE 'Other%' THEN 'OTHER REASON'
ELSE 'SITE SCM'
END )
ELSE ( CASE
WHEN @country_reason IN ( 'Local Product Quality', 'Local QA Release' ) THEN 'CPO QUALITY'
WHEN @country_reason IN ( 'Customs Clearance', 'Transport Damage', 'Transport Issue' ) THEN 'TRANSIT'
WHEN @country_reason IN ( 'Artwork', 'Complaince Checks', 'Registration', 'Safety Label Changes' ) THEN 'CPO LCM'
WHEN @country_reason = 'Vendor_Issue' THEN ''
WHEN @country_reason LIKE 'Other%' THEN 'OTHER REASON'
ELSE 'CPO SCM'
END )
END
update table set other columns....,class=@class,.... where...
close cursor loop
SP can be more clear only if u share a piece or glance of it.
Upvotes: 1