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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Trigger advice

Author  Topic 

chris_cs
Posting Yak Master

223 Posts

Posted - 2010-03-03 : 11:38:47
Hey guys,

I now have a trigger which is working afetr some help from this forum. The trigger is shown below:
UPDATE ct
SET [text] = CASE WHEN ct.longflag = 1 THEN CAST(ct.[text] AS VARCHAR(MAX)) ELSE ct.shorttext END
+ '; ' + REPLACE(i.shortnarrative, '*Fixed fee for ', '') + '; '
FROM casetext ct
INNER JOIN inserted i ON ct.caseid = i.caseid
WHERE ct.texttype = '_B'
AND ct.longflag = 1
AND SUBSTRING(i.shortnarrative, 1, 1) = '*'

UPDATE ct
SET ct.shorttext = CASE WHEN LEN(ct.shorttext) + LEN(i.shortnarrative) < 252
THEN ct.shorttext + ' ' + + REPLACE(i.shortnarrative, '*Fixed fee for ', '') + '; '
ELSE NULL
END,
ct.longflag = CASE WHEN LEN(ct.shorttext) + LEN(i.shortnarrative) < 252
THEN ct.longflag
ELSE 1
END,
ct.[text] = CASE WHEN LEN(ct.shorttext) + LEN(i.shortnarrative) < 252
THEN ct.[text]
ELSE ct.shorttext + '; ' + REPLACE(i.shortnarrative, '*Fixed fee for ', '') + '; '
END
FROM casetext ct
INNER JOIN inserted i ON ct.caseid = i.caseid
WHERE ct.texttype = '_B'
AND ct.longflag <> 1
AND SUBSTRING(i.shortnarrative, 1, 1) = '*'

INSERT INTO casetext(caseid, texttype, textno, class, [language], modifieddate, longflag, shorttext, [text])
SELECT i.caseid, '_B', 0, NULL, NULL, NULL, 0, REPLACE(i.shortnarrative, '*Fixed fee for ', '') + '; ', NULL
FROM inserted i
WHERE NOT EXISTS
(
SELECT *
FROM casetext ct
WHERE ct.caseid = i.caseid
AND ct.texttype = '_B'
)
AND SUBSTRING(i.shortnarrative, 1, 1) = '*'


Now I seem to have this working I need to make some further changes to meet a business requirement. I need to prevent certain rows being acted upon if a specific row was entered at the same time (1 or more rows can be entered into the parent table at any one time). One of the columns in the parent table is WIPCODE, so imagine the following was entered:

WIPCODE
P001
P002
P009
P055

What I need to do is ignore the rows which have a WIPCODE of P001 and P002 if a row with a WIPCODE of P009 is inserted by the user at the same time. I've tried using something like the following but it doesn't seem to do what I want.

if (select count(*) from inserted where wipcode = 'P001') = 1
begin
delete from inserted where wipcode in ('P009SC', 'P092SC', 'P9A1SC', 'P9A2SC', 'P9P9SC')
end


Do you have any ideas as to how I could achieve this? I hope this all makes sense and any help would be much appreciated.

Chris

Sachin.Nand

2937 Posts

Posted - 2010-03-03 : 12:25:47
Is it some kind of Bulk Insert?

PBUH
Go to Top of Page

chris_cs
Posting Yak Master

223 Posts

Posted - 2010-03-04 : 03:57:06
It isn't no. The trigger is on a table that has charges entered into it by our users. They could enter one or more charges into the system at any one time.
Go to Top of Page
   

- Advertisement -