You can create another table for temporary storing the results from INSERTED
before calling bcp
.
create trigger monitorTrigger on test
AFTER insert
as
declare @sql varchar(8000)
--delete it every time
TRUNCATE TABLE test2.dbo.tempInserted
--populate it from inserted
INSERT INTO test2.dbo.tempInserted
SELECT * FROM INSERTED
--use it in bcp
SELECT @sql = 'bcp "select * from test2.dbo.tempInserted" queryout I:Filemytest.txt -c -t -T -S YAMUNASQLEXPRESS'
exec xp_cmdshell @sql
EDIT:
Apparently this will not work, because table tempInserted
is locked at the time bcp
is called.
Here is a workaround idea, maybe not the most elegant solution but should work (if you are not on express edition). You can use trigger just to store the inserted data into this table and you can create a job that runs periodically (every 5 mins let's say) and read from that table, copy to file and delete.
So trigger would be just:
create trigger monitorTrigger on test
AFTER insert
as
BEGIN
INSERT INTO test2.dbo.tempInserted
SELECT * FROM INSERTED
END
and Stored Procedure to copy to file - that you can run from the job:
CREATE PROC transferToFile
AS
BEGIN
declare @sql varchar(8000)
SELECT @sql = 'bcp "select * from test2.dbo.tempInserted" queryout I:Filemytest.txt -c -t -T -S YAMUNASQLEXPRESS'
exec xp_cmdshell @sql
--delete at the end
TRUNCATE TABLE test2.dbo.tempInserted
END
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…