SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Creating a useable trigger.
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Borgen313
Starting Member

2 Posts

Posted - 10/11/2012 :  07:40:13  Show Profile  Reply with Quote
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.


Edited by - Borgen313 on 10/11/2012 07:45:33

sunitabeck
Flowing Fount of Yak Knowledge

5155 Posts

Posted - 10/11/2012 :  07:46:24  Show Profile  Reply with Quote
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 - 10/11/2012 :  08:01:00  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000