SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Trigger - Access to filepath?


Trigger - Access to filepath?

Author
Message
scampbell301
scampbell301
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
Points: 4 Visits: 6
I need to create a trigger event (on create) that will, based on values in the new row, open a matching text file, read in it's contents and append them to a field in that row.

For example, upon adding the row: Smith, John, 12345 the trigger would look for a file "12345.txt" on d:\data\files read in it's contents and append them back into the same row.

Is it possible for a trigger to do this or am I barking up the wrong tree? Thanks.

Thanks.
Colleen M. Morrow
Colleen M. Morrow
SSChasing Mays
SSChasing Mays (643 reputation)SSChasing Mays (643 reputation)SSChasing Mays (643 reputation)SSChasing Mays (643 reputation)SSChasing Mays (643 reputation)SSChasing Mays (643 reputation)SSChasing Mays (643 reputation)SSChasing Mays (643 reputation)

Group: General Forum Members
Points: 643 Visits: 1086
USE AdventureWorks;
GO

CREATE TABLE myTable(
FileName nvarchar(60),
FileType nvarchar(60),
Document nvarchar(max));
GO

ALTER TRIGGER myTrig
ON myTable
INSTEAD OF INSERT
AS
DECLARE @filename nvarchar(60);
DECLARE @filepath nvarchar(60);
DECLARE @sqlstmt nvarchar(4000);
DECLARE @document nvarchar(max)

SELECT @filename = Filename FROM inserted ;
SET @filepath = 'D:\' + @filename +'.txt';

set @sqlstmt = 'SELECT @docout = BulkColumn FROM OPENROWSET(BULK ''' + @filepath + ''', SINGLE_CLOB) AS Document;'
select @sqlstmt

exec sp_executesql @sqlstmt, N'@docout nvarchar(max) OUTPUT', @docout=@document output

INSERT INTO myTable select i.filename, i.filetype, @document from inserted i

GO





Colleen M. Morrow
Cleveland DBA
scampbell301
scampbell301
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
Points: 4 Visits: 6
Well damn. I guess I'll take that as a "Yes." :-)

Much more than I asked for and much appreciated.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search