如何通过SQL脚本(用于日志)保存文本文件中的数据 [英] How can I save data in the Text File by SQL scripts (for logs)

查看:143
本文介绍了如何通过SQL脚本(用于日志)保存文本文件中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hey Guys。



我想使用SQL脚本在文本文件中保存一个字符串。



示例:我有一个字符串'abcdefghijklmnopqrstuvwxyz',我希望使用SQL脚本将该字符串保存在特定位置的文本文件中。



另外我想检查一下,如果有一个文件已经存在(@建议位置)同名。



你能帮我解决一下这个问题。

Hey Guys.

I want to save a string in the text file by using the SQL Scripts.

Example : I have a string 'abcdefghijklmnopqrstuvwxyz', I want to save this string the text file in the particular location by using the SQL scripts.

Also i want to check that if there is a file already exist (@ advised location) with the same name.

Can you please help me to solve this query.

推荐答案

这是你可以使用的代码

1)创建和写入txt文件

2)检查是否文件存在



Here is code you can use
1) To create and write txt file
2) To check whether file exists

DECLARE @Text VARCHAR(MAX)
,@filelocation VARCHAR(MAX)
,@cmd VARCHAR(100)
    SET @Text = 'abcdefghijklmnopqrstuvwxyz'
    SET @filelocation = 'D:\out_vid.txt'
--    SET @cmd = 'bcp ' + @querytext + ' queryout ' + @filelocation + ' -T -c -U'
--    EXEC master..XP_CMDSHELL @cmd
SET @cmd ='echo ' +  @Text + ' > E:\out_vid.txt'
EXECUTE Master.dbo.xp_CmdShell  @cmd

DECLARE @isExists INT
exec master.dbo.xp_fileexist 'E:\out_vid.txt', 
@isExists OUTPUT
SELECT case @isExists 
when 1 then 'Yes' 
else 'No' 
end as isExists


这篇关于如何通过SQL脚本(用于日志)保存文本文件中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆