Hi All,
I am trying to create a trigger that auto-increments a PK field from another sequence table. Since I cannot change the structure of the existing table, I need to pull and increment the field and update the record for insert as a char type field. When I insert the record in the application, the auto-increment field remains blank. My table struture for the sequence file is as follows:
seq_id numeric 9(18,0)
table_name char 15
column_name char 15
last_num numeric 9(18,0)
CREATE TRIGGER SHOP_PERSON_SEQUENCE
ON [root].[ae_h_emp_e]
FOR INSERT
AS
DECLARE @SHOPPERSON VARCHAR(15)
SET @SHOPPERSON = CAST(@SHOPPERSON AS VARCHAR(15))
BEGIN
IF (SELECT SHOP_PERSON FROM INSERTED) IS NULL
SET @SHOPPERSON=(SELECT LAST_NUM FROM SEQ_NUMBER WHERE COLUMN_NAME = 'shop_person')
UPDATE SEQ_NUMBER
SET SEQ_NUMBER.LAST_NUM = SEQ_NUMBER.LAST_NUM + 1 WHERE COLUMN_NAME = 'shop_person'
UPDATE [root].[ae_h_emp_e]
SET SHOP_PERSON = @SHOPPERSON
WHERE SHOP_PERSON IS NULL
END
Homer: But every time I learn something new, it pushes out something old! Remember that time I took a home wine-making course and forgot how to drive?
Marge Simpson: That's because you were drunk!
Homer: And how.
I am trying to create a trigger that auto-increments a PK field from another sequence table. Since I cannot change the structure of the existing table, I need to pull and increment the field and update the record for insert as a char type field. When I insert the record in the application, the auto-increment field remains blank. My table struture for the sequence file is as follows:
seq_id numeric 9(18,0)
table_name char 15
column_name char 15
last_num numeric 9(18,0)
CREATE TRIGGER SHOP_PERSON_SEQUENCE
ON [root].[ae_h_emp_e]
FOR INSERT
AS
DECLARE @SHOPPERSON VARCHAR(15)
SET @SHOPPERSON = CAST(@SHOPPERSON AS VARCHAR(15))
BEGIN
IF (SELECT SHOP_PERSON FROM INSERTED) IS NULL
SET @SHOPPERSON=(SELECT LAST_NUM FROM SEQ_NUMBER WHERE COLUMN_NAME = 'shop_person')
UPDATE SEQ_NUMBER
SET SEQ_NUMBER.LAST_NUM = SEQ_NUMBER.LAST_NUM + 1 WHERE COLUMN_NAME = 'shop_person'
UPDATE [root].[ae_h_emp_e]
SET SHOP_PERSON = @SHOPPERSON
WHERE SHOP_PERSON IS NULL
END
Homer: But every time I learn something new, it pushes out something old! Remember that time I took a home wine-making course and forgot how to drive?
Marge Simpson: That's because you were drunk!
Homer: And how.