If exists (select * From DBO. sysobjects where id = object_id (n' [DBO]. [p_movefile] ') and objectproperty (ID, n' isprocedure') = 1)
Drop procedure [DBO]. [p_movefile]
Go
/* -- Append data to a text file
Append data to a text file
If the file does not exist, the file will be created
-- Producer build 2004.08 (reference please keep this information )--*/
/* -- Call example
Exec p_movefile 'C:/aa.txt ', 'test write'
--*/
Create proc p_movefile
@ Filename varchar (1000), -- Name of the text file to be operated
@ Text varchar (8000) -- content to be written
As
Declare @ err int, @ SRC varchar (255), @ DESC varchar (255)
Declare @ OBJ int
Exec @ err = sp_oacreate 'scripting. FileSystemObject ', @ OBJ out
If @ err <> 0 goto lberr
Exec @ err = sp_oamethod @ OBJ, 'opentextfile', @ OBJ out, @ filename, 8, 1
If @ err <> 0 goto lberr
Exec @ err = sp_oamethod @ OBJ, 'writeline ', null, @ text
If @ err <> 0 goto lberr
Exec @ err = sp_oadestroy @ OBJ
Return
Lberr:
Exec sp_oageterrorinfo 0, @ SRC out, @ DESC out
Select cast (@ err as varbinary (4) as error code
, @ SRC as error source, @ DESC as error description
Go