Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 15th, 2004, 09:15 AM
Authorized User
 
Join Date: Dec 2004
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to create trigger in StoreProcedure

hi to all
I am bit new in store procedure and triggers and i am stuck in a problem
I am calling Store Procedure for inserting data into table1 and wana call trigger within the store procedure to insert part of data of table1 into table2

Can any expert help me....

 
Old December 16th, 2004, 12:47 AM
Friend of Wrox
 
Join Date: Dec 2004
Posts: 307
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Vadivel Send a message via Yahoo to Vadivel
Default

Shailu,

You need not manually call a trigger. Go through the following steps for better understanding.

1. Create a INSERT trigger on Table1 which would insert the desired data into Table2
2. From your stored procedure just insert records into Table1
3. Automatically the insert trigger on Table1 would fire and take care of inserting the values into Table2 also.

Hope this helps! If you are stuck somewhere or if you need any examples do let me know.

MVP ASP/ASP.NET
http://vadivel.thinkingms.com
 
Old December 16th, 2004, 01:39 AM
Authorized User
 
Join Date: Dec 2004
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thanks for u r response.
But the problem is I don't have to always insert data into table2 when I am inserting data in to table1 except certain situations.

So I require a StoreProcedure and a Trigger for such situation.



 
Old December 16th, 2004, 04:55 AM
Friend of Wrox
 
Join Date: Dec 2004
Posts: 307
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Vadivel Send a message via Yahoo to Vadivel
Default

Shailu,

You cannot fire a trigger yourself. Based on Insert/Update/Delete statement for a table .. it would fire automatically. That said, we can disable/enable a trigger based on our need.

So what i suggest is, within your SP when you don't want the trigger to fire (for some condition) just disable it.

For ex:
Alter table Table1 Disable Trigger testTrigger
go

Don't forget to re-enable the Trigger once your done :) Hope this helps!


Best Regards
Vadivel

MVP ASP/ASP.NET
http://vadivel.thinkingms.com
 
Old December 20th, 2004, 01:43 PM
Registered User
 
Join Date: Mar 2004
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I suggest, not using a trigger in this case (if there are only a relatively few tables involved). Use the stored procedure to populate table 2 (by getting the last inserted ID from table 1 -- @@IDENTITY variable), you can also specify what last identity you want from what table by using the following syntax:

 IDENT_CURRENT('databasename.user.objectname')

Goodluck.

 
Old December 22nd, 2004, 12:23 AM
Friend of Wrox
 
Join Date: Dec 2004
Posts: 307
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Vadivel Send a message via Yahoo to Vadivel
Default

For fetching the identity value its always better to use Scope_identity. It returns the most recently inserted IDENTITY in the current scope. Its available only from Sql Server 2000.

On the other hand Ident_current would give you the most recent identity value for that table regardless of who inserted it.

Best Regards
Vadivel

MVP ASP/ASP.NET
http://vadivel.thinkingms.com





Similar Threads
Thread Thread Starter Forum Replies Last Post
Create trigger on update bvpsekhar MySQL 23 May 4th, 2007 01:01 AM
need help w/create trigger by an insert operation jnt SQL Server 2000 2 December 14th, 2006 07:31 PM
create trigger on a view?? timbal25 Oracle 1 July 26th, 2005 08:14 PM
How to create a trigger for this constraint. method SQL Server 2000 6 June 30th, 2005 02:57 AM
Create Insert Trigger byron SQL Server 2000 6 November 19th, 2003 11:30 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.