2012年3月27日星期二
BCP within a Trigger Freezes
Has anybody run into an issue where a call to BCP within a trigger freezes Query Analyzer? When I try to stop the execution of the update statement, the program then crashes.
I have no problem running BCP from within Query Analyzer itself.
Here is the code I am using
----
CREATE TRIGGER tr_ack ON [dbo].[Track_tb]
FOR UPDATE
AS
if update(AckStatus)
insert into ins_hold
select e.dc + e.po, e.emp,
case AckStatus
when 2 then 'Acknowledged'
when 6 then 'Overdue'
end
from eack as e, document_tb as d, inserted
where (inserted.DocumentKey = d.DocumentKey) and
(d.DocumentName = e.DocNum)
exec master..xp_cmdshell 'bcp ins_hold out c:\temp\testfile.txt -c -T -STestServer'
drop table ins_hold
------
I have also tried this with global temp tables rather than a permanent one, doing a select query and using queryout from BCP and receive the same type of crash.
I can do a select * from ins_hold within the trigger and the results display correctly. I can do bcp out from within Query Analyzer using the data from the ins_hold table within the trigger and it works correctly.
The update statement I'm using within Query Analyzer to test is:
----
update Track_tb
Set AckStatus = 2
where DocumentKey = (
select DocumentKey
from Document_tb
where DocumentName = '0018830')
---
Thanks for any help you can provide, I'm greatly appreciative.
-Gregwhen issuing the command:
exec master..xp_cmdshell 'bcp ins_hold out c:\temp\testfile.txt -c -T -STestServer'
you need to use a three part naming convention for the table <DB NAME>.<OWNER NAME>.<TABLE NAME>.
What version of SQL Server are you using?|||Using SQL 2000 for this (sorry I didn't include that originally)
I did try doing what you suggested before and ran into the same problem.
Thanks for the reply.|||Okay, go back to using a three part name, BCP will not work otherwise.
xp_cmdshell runs under the SQL Server Agent account. HAve you created that account on your SQL server?
I just tried issueing a BCP command via xp_cmdshell (Cut & past of your code) and everything worked.
have you tried xp_cmdshell'dir c:\temp\*.*'? I tried it on my server and get a nasty access denied message.
This might be a bugger to track down so hang in there!|||Hey Paul,
Went back to the 3 part naming convention. The Server Agent account is set up on the system. I also tried doing the xp_cmdshell 'dir c:\temp' from within the trigger and it returned the results without any hitches.
When you say you tried the BCP command of my code, do you mean that you tried that within a trigger or just from Query Analyzer? It seems to work fine for me within Query Analyzer, just gets hitched up when it tries to run from within a trigger.
I'm going to run it again today and put all the traces on to see if I can come up with something in there. I tried this before, but didn't see anything indicative of why it may be freezing up.
For the time being, I've been using a simple VBScript program on a 2 hour schedule to accomplish the same task, but I'd much rather have it running within the trigger so the results are instantaneous rather than having a potential 2 hour wait.
Thanks again for the help,|||Okay, did some more testing.
I also tried to do an out from an ISQL command.
It worked fine from Query Analyzer, but when I tried to have it fire from within the trigger, it again hung and eventually froze.
That command was:
----
declare @.str
set @.str = 'isql -Q"select * from ins_hold" -E -oc:\temp\testfile.txt'
exec master..xp_cmdshell @.str
----
So I was wondering if perhaps it isn't configured for write access through sa, so I then did a:
----
exec master..xp_cmdshell 'dir c:\temp > c:\temp\testfile.txt'
----
within the trigger and it executed correctly and didn't freeze.
Doing more research for the time being to see if I can find anything on the 'net related to this (haven't had any luck so far).
Thanks,
Greg|||No, I did not try the BCP via a trigger. I do think you hit apon the problem though.
What happens if you add th "-o" parameter to your BCP command? My WAG is that if any results are returned form xp_cmdshell the trigger will hang. The "-o" will redirect output from BCP to a file eliminating any data returned from xp_cmdshell. Worth a shot!|||Okay, tried using the -o option to see if I can get something returned from BCP, but nothing gets generated (in either the output or log file). When I run from within Query Analyzer, it posts the return text, so it's again limited to being an issue with the Trigger itself.
So back to the drawing board (or Internet if you prefer).
Regardless of whether I get this worked out or not, thanks Paul--you've been a tremendous help thus far.
-Greg|||Okay, I couldn't find anything that said you couldn't do this so I tried setting up a table and trigger to simulate the problem...
Code:
------------------------------------------
exec master..xp_cmdshell 'dir f:\MSSQL2k'
go
create table TestTable(f1 int)
go
create trigger Tigger on dbo.TestTable
for INSERT, UPDATE, DELETE
AS
exec master..xp_cmdshell 'bcp dba.dbo.ProcedureUsage out f:\MSSQL2k\temp.txt -c -T -S hgw2db17 -o f:\MSSQL2k\temp.out', no_output
go
insert into TestTable values(1)
go
exec master..xp_cmdshell 'dir f:\MSSQL2k'
go
------------------------------------------
Results:
------------------------------------------
output
----------------------------
Volume in drive F is New Volume
Volume Serial Number is 183C-AD6B
NULL
Directory of f:\MSSQL2k
NULL
08/21/2002 11:54a <DIR> .
08/21/2002 11:54a <DIR> ..
02/22/2002 03:23p <DIR> MSSQL
08/21/2002 12:02p 108 temp.out
08/21/2002 12:02p 4,319 temp.txt
2 File(s) 4,427 bytes
3 Dir(s) 12,744,605,696 bytes free
NULL
output
----------------------------
Volume in drive F is New Volume
Volume Serial Number is 183C-AD6B
NULL
Directory of f:\MSSQL2k
NULL
08/21/2002 11:54a <DIR> .
08/21/2002 11:54a <DIR> ..
02/22/2002 03:23p <DIR> MSSQL
08/21/2002 12:02p 108 temp.out
08/21/2002 12:02p 4,319 temp.txt
2 File(s) 4,427 bytes
3 Dir(s) 12,744,605,696 bytes free
NULL
------------------------------------------
Any chance you could try the above code on your server? 'F:\MSSQL2K' is the "root" for my SQL Server install. Also, I remembered that you can add the "no_output" parameter to xp_cmdshell, maybe that would help.|||Okay, ran through the code which you had there (couple of changes to reflect my setup obviously). Worked perfectly--within the trigger and everything.
So that being said, I did the same thing with the table I had created in the previous trigger and it worked fine as well.
So now I'm wondering where in my old code I'm getting hitched up at since it is not the BCP actually freezing up (though I was able to do selects from within the trigger previously and display the data I was attempting to write out via BCP).
I also tried having two triggers:
One on track_Tb off of updates which inserts the values into ins_hold
One on ins_hold off of inserts which bcp's itself out to a data file.
Sames results with those.
Both processes seem to work correctly independent of one another correctly, but freeze up whenever you attempt to use them together.
Thanks again,
Greg|||Glad to hear you got everything to work. Sometimes the bloody forest blocks the view of the trees! My advice, press on to your final solution!|||I don't know if this TRIGGER is used in production and is being fired by an event within a client application. If it is you may want to rethink executing BCP from within a TRIGGER. The event that fired the TRIGGER will hold locks on the records until the TRIGGER has completed. Which means that locks are held until the BCP has finished. This could and most likely will degrade performance and cause potential deadlocks.|||Originally posted by achorozy
I don't know if this TRIGGER is used in production and is being fired by an event within a client application. If it is you may want to rethink executing BCP from within a TRIGGER. The event that fired the TRIGGER will hold locks on the records until the TRIGGER has completed. Which means that locks are held until the BCP has finished. This could and most likely will degrade performance and cause potential deadlocks.
It is in fact going to be used in a production environment, with the table being updated from a client application. However, the table being updated is Track_tb, and I am inserting data into ins_hold and running BCP off of the ins_hold table. Will this table be locked as well?
Also, when the insert is performed on the ins_hold table from within the trigger, will the trigger hold this lock until the end of the execution of it? If so, that would explain why the system seems to hang up when I try to do a BCP with that data.
Thanks for the advice and help,
Greg|||What about using DTS and a scheduled job to create the text file every 5 minutes or so? You could even go a bit further and use the trigger to record the last time a change was made to the table and the job could check to see if the DTS was even needed!
OR
Set up a DTS package to export the data to a test file. Set up a job to launch the DTS package. Create a trigger on the ins_hold table to launch the job two or so minutes in the future. This will allow you to get in and out of your trigger quickly, keep your text file current, and prevent excessive data io when frequent updates are performed. I would probably add a table to record the last time the job ran so I could guarantee a text file write every ten minutes or so during peek times.|||To answer your question - yes. When an SQL statement (INSERT/UPDATE/DELETE) causes a trigger to fire and that trigger performs a database operation that in turn fires another trigger, etc., etc.. This become one big, long transaction and all locks are held.
You can have the trigger insert data into ins_hold table, but you'll need to create a scheduled job that will peform the BCP on a time interval. You can use DTS as Paul has stated or you can modify the current code that does the BCP into a stored procedure and schedule it with SQL Server Agent. Either way is fine.|||Hey guys,
Thanks for all the help. I hadn't even thought of the locks on the table when I was trying to do this. I already have a VBScript program working which essentially does what I was trying to do, so I will continue to utilize this and tack it on to NT Services.
Thanks again,
Greg|||Wow, Success!
Thanks to your guys help and pointing me in the direction of locking, I got the sucker working perfectly now.
If I just put WITH (NOLOCK) in the query from BCP, it will do a dirty read (which is fine because this table exists only for this trigger) and BCP executes without any problem.
So what it looks like now
--
exec master..xp_cmdshell 'bcp "select * from ins_hold WITH (NOLOCK)" queryout c:\temp\testfile.txt -c -T -STestServer'
--
Works like a charm...
Can't thank you guys enough.
-Gregsql
2012年3月6日星期二
BCP error - missing data
reporting events I've noticed that the data in the table is missing
records. I've also noticed that the missing records (records in the
table but not in the BCP out files) seem to occur in contiguous
blocks. Since the complete set of records exists in the table, I
assume this points to an issue in the way the TableUpdate script/
Triggers interact with the system. But i tried to take out the bcp
procedure, do test on trigger, then no data missing, So, I think the
problem is still on bcp part. Could you help me with that?
CREATE TABLE [EventUpdate] (
[id] [int] NOT NULL ,
[eventid] [int] NOT NULL ,
[sequenceid] [int] NOT NULL ,
[UpdatePass] [int] NULL
) ON [PRIMARY]
GO
create trigger trgEventUpdate on EventLog For Insert,Update as
insert into EventUpdate (id,eventid,sequenceid) select ins.id,
ins.eventid,ins.sequenceid from inserted ins
bcp script:
bcp "select a.* from w..eventlog a, w..eventupdate b where
a.eventid=b.eventid and a.sequenceid=b.sequenceid and b.eventid<>-1
and b.sequenceid<>-1 and b.updatepass=1" queryout 30sec-%TFN_NOW%.wrk -
U <userwithaccess-P <password-S doserver -f EventLog.fmt
Thanks in advance for your reply!(danceli@.gmail.com) writes:
Quote:
Originally Posted by
After loading the BCP files that are created during the trigger/
reporting events I've noticed that the data in the table is missing
records. I've also noticed that the missing records (records in the
table but not in the BCP out files) seem to occur in contiguous
blocks. Since the complete set of records exists in the table, I
assume this points to an issue in the way the TableUpdate script/
Triggers interact with the system. But i tried to take out the bcp
procedure, do test on trigger, then no data missing, So, I think the
problem is still on bcp part. Could you help me with that?
Please, could you take that again, and more slowly this time? Keep in
mind that people reading this newsgroup does not know about your system.
You are missing data but where? You appear to extract data with BCP, do
you load it anywhere else?
One thing I can note:
Quote:
Originally Posted by
create trigger trgEventUpdate on EventLog For Insert,Update as
insert into EventUpdate (id,eventid,sequenceid) select ins.id,
ins.eventid,ins.sequenceid from inserted ins
>
>bcp "select a.* from w..eventlog a, w..eventupdate b where
>a.eventid=b.eventid and a.sequenceid=b.sequenceid and b.eventid<>-1
>and b.sequenceid<>-1 and b.updatepass=1" queryout 30sec-%TFN_NOW%.wrk -
>U <userwithaccess-P <password-S doserver -f EventLog.fmt
The BCP query has a condition on the column UpdatePass which is not
set in the trigger. So from what you have posted, I would not expect
anything at all to go in the BCP file.
--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx|||1.
Quote:
Originally Posted by
You are missing data but where? You appear to extract data withBCP, do
you load it anywhere else?
On table Eventlog, there are missing data.
2.
Quote:
Originally Posted by
Quote:
Originally Posted by
create trigger trgEventUpdate on EventLog For Insert,Update as
insert into EventUpdate (id,eventid,sequenceid) select ins.id,
ins.eventid,ins.sequenceid from inserted ins
>
Quote:
Originally Posted by
bcp"select a.* from w..eventlog a, w..eventupdate b where
a.eventid=b.eventid and a.sequenceid=b.sequenceid and b.eventid<>-1
and b.sequenceid<>-1 and b.updatepass=1" queryout 30sec-%TFN_NOW%.wrk -
U <userwithaccess-P <password-S doserver -f EventLog.fmt
>
TheBCPquery has a condition on the column UpdatePass which is not
set in the trigger. So from what you have posted, I would not expect
anything at all to go in theBCPfile.
sorry, i didn't clearly explain it. i did set the UpdataPass:
CREATE TABLE [EventLogUpdate] (
[id] [int] NOT NULL ,
[eventid] [int] NOT NULL ,
[sequenceid] [int] NOT NULL ,
[UpdatePass] [int] NULL
) ON [PRIMARY]
GO
create trigger trgEventUpdate on EventLog For Insert,Update as
insert into EventLogUpdate (id,eventid,sequenceid) select ins.id,
ins.eventid,ins.sequenceid from inserted ins
GO|||(danceli@.gmail.com) writes:
Quote:
Originally Posted by
On table Eventlog, there are missing data.
But it was the EventLog table you had the trigger on? If data is
missing in EventLog what has the trigger or the BCP stuff to do with it?
Quote:
Originally Posted by
sorry, i didn't clearly explain it. i did set the UpdataPass:
>
CREATE TABLE [EventLogUpdate] (
[id] [int] NOT NULL ,
[eventid] [int] NOT NULL ,
[sequenceid] [int] NOT NULL ,
[UpdatePass] [int] NULL
) ON [PRIMARY]
GO
create trigger trgEventUpdate on EventLog For Insert,Update as
insert into EventLogUpdate (id,eventid,sequenceid) select ins.id,
ins.eventid,ins.sequenceid from inserted ins
GO
I still can't see any update of UpdatePass?
It's your call. If you don't want to explain what you are doing, that
is alright. But unfortunately it is difficult to answer your questions
in this case.
--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx
BCP error - missing data
reporting events I've noticed that the data in the table is missing
records. I've also noticed that the missing records (records in the
table but not in the BCP out files) seem to occur in contiguous
blocks. Since the complete set of records exists in the table, I
assume this points to an issue in the way the TableUpdate script/
Triggers interact with the system. But i tried to take out the bcp
procedure, do test on trigger, then no data missing, So, I think the
problem is still on bcp part. Could you help me with that?
CREATE TABLE [EventUpdate] (
[id] [int] NOT NULL ,
[eventid] [int] NOT NULL ,
[sequenceid] [int] NOT NULL ,
[UpdatePass] [int] NULL
) ON [PRIMARY]
GO
create trigger trgEventUpdate on EventLog For Insert,Update as
insert into EventUpdate (id,eventid,sequenceid) select ins.id,
ins.eventid,ins.sequenceid from inserted ins
bcp script:
bcp "select a.* from w..eventlog a, w..eventupdate b where
a.eventid=b.eventid and a.sequenceid=b.sequenceid and b.eventid<>-1
and b.sequenceid<>-1 and b.updatepass=1" queryout 30sec-%TFN_NOW%.wrk -
U <userwithaccess-P <password-S doserver -f EventLog.fmt
Thanks in advance for your reply!Danceli,
Are your triggers set-based?
-- Bill
<danceli@.gmail.comwrote in message
news:1170212352.130762.295120@.m58g2000cwm.googlegr oups.com...
Quote:
Originally Posted by
After loading the BCP files that are created during the trigger/
reporting events I've noticed that the data in the table is missing
records. I've also noticed that the missing records (records in the
table but not in the BCP out files) seem to occur in contiguous
blocks. Since the complete set of records exists in the table, I
assume this points to an issue in the way the TableUpdate script/
Triggers interact with the system. But i tried to take out the bcp
procedure, do test on trigger, then no data missing, So, I think the
problem is still on bcp part. Could you help me with that?
>
CREATE TABLE [EventUpdate] (
[id] [int] NOT NULL ,
[eventid] [int] NOT NULL ,
[sequenceid] [int] NOT NULL ,
[UpdatePass] [int] NULL
) ON [PRIMARY]
GO
create trigger trgEventUpdate on EventLog For Insert,Update as
insert into EventUpdate (id,eventid,sequenceid) select ins.id,
ins.eventid,ins.sequenceid from inserted ins
>
>
>
bcp script:
>
bcp "select a.* from w..eventlog a, w..eventupdate b where
a.eventid=b.eventid and a.sequenceid=b.sequenceid and b.eventid<>-1
and b.sequenceid<>-1 and b.updatepass=1" queryout 30sec-%TFN_NOW%.wrk -
U <userwithaccess-P <password-S doserver -f EventLog.fmt
>
Thanks in advance for your reply!
>
BCP Error
Although if I run the bcp command separately I do not get this error and the command runs successfully. I am using the Exec Master..xp_cmdshell command.
Please advice.
Thanks,
PankajError = [Microsoft][ODBC SQL Server Driver]Unable to open BCP host data-file"
this error occurs when you give the output path as a directory which does not exist. i.e. you give the path as C:\enigma\enigma.txt even though the directory enigma does not exist on your PC. Create the directory and reissue the command. The BCP command only creates a data file ... not the directory
2012年2月23日星期四
bcp and trigger: missing data in bcp out file
inserted, updated to the table. And also, I made batch file using bcp
to extract the newly updated / inserted records.
But I got missing data in bcp out file like this:
Missing 1200 records, blocked at:
/*
777946 296188 2007-01-29 21:25:45.063
778145 296494 2007-01-29 21:25:47.063
*/
1. trigger.sql
CREATE TABLE [FERUpdate] (
[id] [int] NOT NULL ,
[fid] [int] NOT NULL ,
[sid] [int] NOT NULL ,
[UpdatePass] [int] NULL
) ON [PRIMARY]
GO
create trigger trgFERUpdate on FER For Insert,Update as
insert into FERUpdate(id,fid,sid) select ins.id, ins.fid,ins.sid from
inserted ins
2. bcp.bat
--
isql -U <user-P <pw-S server -Q "update AA..FERUpdate set
UpdatePass=1 where UpdatePass is null"
bcp "select a.* from AA..FER a, AA..FERUpdate b where a.fid=b.fid and
a.sid=b.sid and b.fid<>-1 and b.sid<>-1 and b.updatepass=1" queryout
%TFN_NOW%.wrk -U <user-P <pw-S server -f FER.fmt
isql -U <user-P <pw-S server -Q "delete from AA..FERUpdate where
UpdatePass=1"
--
--
I have been struggling with this for these two days. Your any helps
are appreciated, Please help me out!! Thanks!!!(danceli@.gmail.com) writes:
Quote:
Originally Posted by
I have made trigger on table 'FER' that would be fired if data is
inserted, updated to the table. And also, I made batch file using bcp
to extract the newly updated / inserted records.
>
But I got missing data in bcp out file like this:
>
Missing 1200 records, blocked at:
/*
777946 296188 2007-01-29 21:25:45.063
>
778145 296494 2007-01-29 21:25:47.063
*/
What numbers are these?
Quote:
Originally Posted by
2. bcp.bat
--
isql -U <user-P <pw-S server -Q "update AA..FERUpdate set
UpdatePass=1 where UpdatePass is null"
>
bcp "select a.* from AA..FER a, AA..FERUpdate b where a.fid=b.fid and
a.sid=b.sid and b.fid<>-1 and b.sid<>-1 and b.updatepass=1" queryout
%TFN_NOW%.wrk -U <user-P <pw-S server -f FER.fmt
>
isql -U <user-P <pw-S server -Q "delete from AA..FERUpdate where
UpdatePass=1"
--
How often do you run this?
What is the meaning if the <-1 things?
And how do you conclude that the data is missing? There is no
ORDER BY clause in your SELECT, so the missing rows may be elsewhere
in the file.
--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx|||Either you query is wrong or you did the DELETE operation before the
bcp out command.
2012年2月16日星期四
Batch update and trigger
I have a trigger on a table tracking changes to certain fields.
However, when I do a batch update on that field, it looks like the
trigger only gets fired once. However, I do want to track changes on
every record that the update statement touches. So, is there anyway to
make it work or is there any work around? Do I have to create a cursor
and update one record a time?
Thanks a lot,
blueyep, triggers in SQL Server are fired once per statement, not once per
row. when you describe how do you need to track changes, we might be
able to help|||what I want to do is very simple. I want to track the change to a field
in one table. if the value changes, I will insert a row in the change
log table. Basically if old field value <> new field value, insert a
row in the change log.
However, in another script, I sometimes update the field for many rows
if they meet the criteria. Therefore, I have something like UPDATE
table1 SET field1= 'Y' WHERE field2>field3. When this statement is
executed, the trigger only fired once and therefore, only one row gets
inserted into the changelog table.
I just wonder if there is anyway I can make the trigger fired for each
row without abandoning the batch update and use cursor to do update for
each individual row
Thanks a lot.|||if you don't modify the PK, that's easy:
create table seq(s_id int identity, i int)
insert into seq(i) values(1)
insert into seq(i) values(2)
insert into seq(i) values(3)
insert into seq(i) values(4)
go
create table seq_audit(s_id int, old_i int, new_i int)
go
create trigger seq_upd
on seq
for update
as
insert into seq_audit
select inserted.s_id, deleted.i, inserted.i from inserted, deleted
where inserted.s_id = deleted.s_id
go
select * from seq
s_id i
-- --
1 1
2 2
3 3
4 4
go
update seq set i=i+1 where s_id > 1
go
select * from seq_audit
s_id old_i new_i
-- -- --
2 2 3
3 3 4
4 4 5
go
drop table seq
drop table seq_audit
2012年2月11日星期六
Basic Trigger Firing Question
I seem to be having a problem where the trigger is firing only for some of the inserted records but not others, seemingly randomly (although I know there must be some logical explanation for how it's choosing which record to fire under).
Thanks =) MattHere's how I ended up getting multirow inserts to work properly using an INSTEAD OF trigger rather than a AFTER trigger.
-- INSTEAD OF Trigger That Fires Single-Row AFTER Triggers for a Table with IDENTITY
CREATE TRIGGER trg_T1IOIS ON T1 INSTEAD OF INSERT
AS
SELECT IDENTITY(int, 1, 1) AS key_col, data_col
INTO #t1
FROM inserted
DECLARE @.key AS int
SELECT @.key = MIN(key_col) FROM #t1
WHILE @.key IS NOT NULL
BEGIN
INSERT INTO T1
SELECT data_col FROM #t1 WHERE key_col = @.key
SELECT @.key = MIN(key_col)
FROM #t1
WHERE key_col > @.key
END
GO|||Each Trigger written should be able to deal with either a Single Insert/Delete/update or Bulk Insert/Delete/update
I tend to use the following Format :-
BEGIN
IF @.@.RowCount =1
BEGIN
/*DO SINGLE ROW OPERATION*/
END
ELSE
BEGIN
/*DO MULTI ROW OPERATION*/
END
END
Generally I alter the TSQL to cope with both eventualities
and I believe it is good practice to apply this to ALL Triggers unless of course there are special cicumstances.
Hope this Helps
GW
basic trigger ?
I want to have a trigger than when record is inserting into table A, the
trigger inserts a record into table B
I want the identity value from Table A to be one of the values inserted into
table B.
How do I get the indentity value from tableA in my trigger so that I can
insert into table b
thanksAussie Rules (someone@.somewhere.com) writes:
> I want to have a trigger than when record is inserting into table A, the
> trigger inserts a record into table B
> I want the identity value from Table A to be one of the values inserted
> into table B.
> How do I get the indentity value from tableA in my trigger so that I can
> insert into table b
CREATE TRIGGER A_tri ON A FOR INSERT AS
INSERT B (some_col, some_other_col, ...)
SELECT i.identity_col, i.other_col, ...
FROM inserted i
JOIN ...
inserted is a virttual table holds the inserted rows, so you find the
identity value right there.
Be aware of that a trigger fires once per *statement*.
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp|||Sirs
You might want to consider using 'After Insert' instead of 'For
Insert'. This way you know that the record has been successfully inserted
into Table A, Before you insert into Table B.
Mark
"Erland Sommarskog" <esquel@.sommarskog.se> wrote in message
news:Xns96C24758FE7FYazorman@.127.0.0.1...
> Aussie Rules (someone@.somewhere.com) writes:
the
> CREATE TRIGGER A_tri ON A FOR INSERT AS
> INSERT B (some_col, some_other_col, ...)
> SELECT i.identity_col, i.other_col, ...
> FROM inserted i
> JOIN ...
> inserted is a virttual table holds the inserted rows, so you find the
> identity value right there.
> Be aware of that a trigger fires once per *statement*.
>
> --
> Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
> Books Online for SQL Server SP3 at
> http://www.microsoft.com/sql/techin.../2000/books.asp
>|||I expect you meant Instead Of rather than For.
For Insert triggers are classified as After Insert triggers, and only fire
after the row has been successfully inserted. Instead Of triggers fire in
place of the triggering action. If you want to perform an insert, then the
body of the Instead Of Insert trigger must issue an insert into the
underlying table.
"Mark Moss" <markmoss@.adelphia.net> wrote in message
news:O4hAxQPrFHA.1172@.TK2MSFTNGP11.phx.gbl...
> Sirs
> You might want to consider using 'After Insert' instead of 'For
> Insert'. This way you know that the record has been successfully inserted
> into Table A, Before you insert into Table B.
> Mark
> "Erland Sommarskog" <esquel@.sommarskog.se> wrote in message
> news:Xns96C24758FE7FYazorman@.127.0.0.1...
> the
inserted
can
>