MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 5th, 2012, 09:29 AM
BitZoid's Avatar
BitZoid BitZoid is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 98 BitZoid User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 2 h 20 m 38 sec
Reputation Power: 1
Creating Trigger, New To SQL Server Managment

I'm creating a trigger, that prevents deletion of a row in the Installation Table by catching a Delete request and instead of "ing" it, to update the column Uninstall and Uninstall Date, with uninstall bit value of 1 and the current date. However, I cannot get the trigger to select only the row being deleted. It actually updates all the rows with these values, when I need it to only effect the row being deleted.

The Columns in the table are
InstallKey - Uninstall - UninstallDate

Trigger:

Code:
ALTER TRIGGER [dbo].[tr_Uninstall] ON [dbo].[Installation]
INSTEAD OF DELETE AS DECLARE @Uninstall bit
DECLARE @UninstallDate datetime
DECLARE @InstallKey NCHAR(10)
SELECT @Uninstall = Uninstall FROM Inserted
SELECT @UninstallDate = UninstallDate FROM Inserted
SELECT @InstallKey = InstallKey FROM Inserted
WHERE InstallKey = @InstallKey
BEGIN
UPDATE Installation
SET Uninstall = 1, UninstallDate = GETDATE()
 Print 'The installation has been marked as Uninstalled' END

Reply With Quote
  #2  
Old November 5th, 2012, 09:50 AM
BitZoid's Avatar
BitZoid BitZoid is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 98 BitZoid User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 2 h 20 m 38 sec
Reputation Power: 1
Nevermind, Im new but I figured it out:

Code:
ALTER TRIGGER [dbo].[tr_Uninstall] ON [dbo].[Installation]
INSTEAD OF DELETE AS
DECLARE @Uninstall bit
DECLARE @UninstallDate datetime
DECLARE @InstallKey NCHAR(10)
SELECT @Uninstall = Uninstall FROM Deleted
SELECT @UninstallDate = UninstallDate FROM Deleted
SELECT @InstallKey = InstallKey FROM Deleted
BEGIN
UPDATE Installation SET Uninstall = 1, UninstallDate = GETDATE()
WHERE @InstallKey = InstallKey
Print 'The installation has been marked as Uninstalled' END

Reply With Quote
  #3  
Old January 10th, 2013, 03:10 AM
speedtrap speedtrap is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2011
Posts: 7 speedtrap User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 30 m 2 sec
Reputation Power: 0
Quote:
Originally Posted by BitZoid
Nevermind, Im new but I figured it out:

Code:
ALTER TRIGGER [dbo].[tr_Uninstall] ON [dbo].[Installation]
INSTEAD OF DELETE AS
DECLARE @Uninstall bit
DECLARE @UninstallDate datetime
DECLARE @InstallKey NCHAR(10)
SELECT @Uninstall = Uninstall FROM Deleted
SELECT @UninstallDate = UninstallDate FROM Deleted
SELECT @InstallKey = InstallKey FROM Deleted
BEGIN
UPDATE Installation SET Uninstall = 1, UninstallDate = GETDATE()
WHERE @InstallKey = InstallKey
Print 'The installation has been marked as Uninstalled' END


one thing you should keep bare in mind, that triggers on SQL Server works differently from Oracle. Triggers on SQL Server works as set-based while Oracle's can act as set-based or row-based. But often, DBA creating triggers in Oracle as row-based.
So,...as an "short" alternative for your trigger, you can create as below:
Code:
alter trigger tid_uninstall on installation
instead of delete as
begin
	update installation
		set uninstall=1, UninstallDate=getdate()
		from installation join deleted on installation.InstallKey=deleted.InstallKey;
	print 'DONE.';
end;

Reply With Quote
  #4  
Old January 10th, 2013, 06:11 AM
r937's Avatar
r937 r937 is online now
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,357 r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 2 Days 4 h 42 m 44 sec
Reputation Power: 4140
Quote:
Originally Posted by speedtrap
one thing you should keep bare in mind...

[offtopic]

bear in mind = remember

keep bare in mind = think about porn



[/offtopic]
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book

Reply With Quote
  #5  
Old January 10th, 2013, 07:24 PM
speedtrap speedtrap is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2011
Posts: 7 speedtrap User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 30 m 2 sec
Reputation Power: 0
Quote:
Originally Posted by r937

[offtopic]

bear in mind = remember

keep bare in mind = think about porn



[/offtopic]


wkwkwkwkwkkw....my mistake. I just realized I made typo in this thread. as correction: "...keep bear in mind..." Thanks r937.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > Creating Trigger, New To SQL Server Managment

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap