Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Creating an Instead of Insert trigger that blocks certain values

$
0
0

Hi guys, I'd like to create an INSTEAD OF trigger for the Person.Address table in AdventureWorks so that anytime someone inserts a value that does not begin with a number in AddressLine1 column, it returns a message "Invalid, value must contain numbers".

Can someone please write the code for this? thanks.

This is what I managed to write but it's still accepting addresses without numbers in AddressLine1 column, what did I do wrong?

CREATE TRIGGER trg_PersonAddress_AfterInsert
ON Person.Address
INSTEAD OF INSERT
AS
BEGIN
IF EXISTS (SELECT AddressLine1 FROM Person.Address
WHERE AddressLine1 LIKE '[0-9]%')
INSERT INTO Person.[Address] (AddressID,AddressLine1,AddressLine2,City,StateProvinceID,PostalCode,SpatialLocation,rowguid,ModifiedDate)
SELECT AddressID,AddressLine1,AddressLine2,City,StateProvinceID,PostalCode,SpatialLocation,rowguid,ModifiedDate
FROM inserted
ELSE
PRINT 'Invalid must contain numbers'
END;





Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>