Microsoft SQL Server
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Iron Speed
Go Back   ASP Free ForumsDatabaseMicrosoft SQL Server

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 ASP Free Forums Sponsor:
Ajax Application Generator Generate database and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!
  #1  
Old May 7th, 2008, 04:25 AM
ctmas ctmas is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 74 ctmas User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 4 h 6 m 16 sec
Reputation Power: 5
Thumbs up SQL Server 2000 - Triggers

Hi,


I need to write a trigger for insert, update and delete. But how do I differentiate the action in the trigger..

I need something like this..



if inserted
begin
insert into user_hist from inserted.......
end
if updated
begin
insert into user_hist from updated....
end
if deleted
begin
insert into user_hist from deleted....
end

Reply With Quote
  #2  
Old May 8th, 2008, 05:13 AM
iht4 iht4 is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 83 iht4 User rank is Sergeant (500 - 2000 Reputation Level)iht4 User rank is Sergeant (500 - 2000 Reputation Level)iht4 User rank is Sergeant (500 - 2000 Reputation Level)iht4 User rank is Sergeant (500 - 2000 Reputation Level)iht4 User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 2 Days 20 h 58 m 11 sec
Reputation Power: 12
I don't think it is possible to have different logic for each type in the one trigger. You can however have more than one trigger on a table each of which can be ( but does not have to be) for a different transaction type.

Loads of info in sql help.

Reply With Quote
  #3  
Old May 10th, 2008, 01:22 AM
visakh16 visakh16 is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Dec 2007
Location: Trivandrum,Kerala
Posts: 32 visakh16 User rank is Sergeant (500 - 2000 Reputation Level)visakh16 User rank is Sergeant (500 - 2000 Reputation Level)visakh16 User rank is Sergeant (500 - 2000 Reputation Level)visakh16 User rank is Sergeant (500 - 2000 Reputation Level)visakh16 User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 10 h 4 m
Reputation Power: 11
Quote:
Originally Posted by ctmas
Hi,


I need to write a trigger for insert, update and delete. But how do I differentiate the action in the trigger..

I need something like this..



if inserted
begin
insert into user_hist from inserted.......
end
if updated
begin
insert into user_hist from updated....
end
if deleted
begin
insert into user_hist from deleted....
end

You can differentiate the actions like this

IF SELECT COUNT(*) FROM INSERTED >0
AND SELECT COUNT(*) FROM DELETED >0 THEN

--Your update logic here as during update action both inserted and deleted tables get populated with your new values and old values respectively

ELSE IF SELECT COUNT(*) FROM INSERTED >0 AND SELECT COUNT(*) FROM DELETED =0

--Your Insert logic here as during insert action only INSERTED table will get populated with values you're inserting

ELSE IF IF SELECT COUNT(*) FROM DELETED >0 AND SELECT COUNT(*) FROM INSERTED =0

--Your DELETE logic as during delete action DELETED table alone gets populated

Reply With Quote
Reply

Viewing: ASP Free ForumsDatabaseMicrosoft SQL Server > SQL Server 2000 - Triggers


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

 Free IT White Papers!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five "checkpoints" for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

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





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway