Append data to a text file using SQL

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.