|
|
Question : How to check for null value in trigger?
|
|
I have a Table (Tbl_1) into which a record is inserted from time to time. Once a record is inserted into Tbl_1 then I will have to use the columns from the new record and populate other tables. The column values in Tbl_1 can be "NULL". I am planning to write a DB Trigger to take care of creating records in other tables. I am not sure how I can handle existence of "NULL" column values. If a column value is NULL then I will will have to insert "NULL" value. What is the conditional statement I can make use of to check for "NULL" value? Any help is very much appreciated.
Kind regards
|
Answer : How to check for null value in trigger?
|
|
samble, a trigger is effectively a proc attached to a table, you can easily create a conditional dynamic sql viz:
CREATE TABLE Customers (CustomerName VARCHAR(100) NULL, CustomerAge INT NULL, ....) GO CREATE TRIGGER cust_insert ON Customers FOR INSERT AS DECLARE @sql=varchar(255) SELECT @sql="insert TableA (col1) select "+CASE WHEN CustomerName = NULL THEN "NULL" ELSE CustomerName END SELECT @sql=@sql+"insert TableB (col1) select "+CASE WHEN CustomerAge = NULL THEN "NULL" ELSE CustomerAge END .. .. EXEC (@sql) GO
INSERT Customers SELECT "George W Bush", 99 SELECT * FROM Customers
|
|
|
|
|