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
 Creating a useable trigger.

Author  Topic 

Borgen313
Starting Member

2 Posts

Posted - 2012-10-11 : 07:40:13
Hi. I am pretty new to this sql server 2012 programming and have a difficult task in school wich i can't seem to figure out. So I now ask for your help.
The task is as follows:
Create a AFTER INSERT trigger for the table Sokande that verifies the new row(s) postalcode (postNr) and city is correct. This is done by finding the postalcode and making sure the city is correct. If the postalcode isn't found or the city doesn't match, the new row(s) should be deleted from the Sokande table by the trigger.

So thats the task I've been working on for two days. I hope you understand and I hope any of you can help me in any way possible.

Thanks, Borgen313.

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-10-11 : 07:46:24
Usually people on the forum are reluctant to post answers to school-related work for fear that they may be doing a disservice rather than helping. If you post what you have tried so far, I am sure many people on the forum would be very glad to help.

Take a look at this page, write something that you think is right, even if it doesn't do everything you want to and post the code: http://msdn.microsoft.com/en-us/library/ms189799.aspx
Go to Top of Page

Borgen313
Starting Member

2 Posts

Posted - 2012-10-11 : 08:01:00
Okey good to know. But no need to worry about that, everyone doing the task right now is just about to give up and this is our last hope.

This is what we have tried so far:

CREATE TRIGGER dbo.check_delete_postNr_ort ON dbo.Sokande

AFTER INSERT
AS
BEGIN
DECLARE @isokarNr numeric
DECLARE @ipostNr char
DECLARE @iort varchar
DECLARE @ppostNr char
DECLARE @port varchar

SELECT @isokarNr = sokarNr, @ipostNr = postNr, @iort = ort
FROM INSERTED

SELECT @ppostNr = postNr, @port = ort
FROM Postnummer
WHERE postNr = @ipostNr
OR
ort = @iort

IF @ipostNr <> @ppostNr OR @iort <> @port
DELETE FROM Sokande
WHERE sokarNr = @isokarNr
ELSE

RETURN
END
Go to Top of Page
   

- Advertisement -