【转】SQL Server T-SQL写文本文件

原文:http://www.nigelrivett.net/SQLTsql/WriteTextFile.html

The are several methods of creating text files from sql server.

  • osql
  • bcp
  • redirection commands
  • sp_MakeWebTask
  • Com object calls

Remember that in all cases the path of the output file will be relative to the server - not to the client on which the application is running.

osql.
This example will just output master..sysobjects to the file c:\osqloutput.txt.

declare @cmd varchar(1000)
select @cmd = ‘osql -U -P -S -Q"select * from master..sysobjects" -o"c:\osqloutput.txt" -w500‘
exec master..xp_cmdshell @cmd

bcp
bcp very fast and easy to use for just dumping a table out - can be used against a view too.

master..xp_cmdshell ‘bcp master..sysobjects out c:\file.bcp -S -U -P -c ‘

redirection commands
You will need to create the data to be output as in dynamic sql statements
The first command here creates or overwrites the file - the rest append to the file.

exec master..xp_cmdshell ‘echo hello > c:\file.txt‘
exec master..xp_cmdshell ‘echo appended data >> c:\file.txt‘
exec master..xp_cmdshell ‘echo more data >> c:\file.txt‘

will generate a file containing
hello
appended data
more data

sp_MakeWebTask
This is for creating html code from a query

Com object calls
Using sp_oa... system stored procedures and creating com objects or existing applications you can probably do whatevr you wish - but you should probably ask whether sql server is the right place to control this.

时间: 2024-09-27 11:00:43

【转】SQL Server T-SQL写文本文件的相关文章

[Oracle][ODBC SQL Server Driver][SQL Server]对象名 'RECOVER.HS_TRANSACTION_LOG' 无效(转)

原帖由 qingyun 于 2010-6-21 15:44 发表 在写pl/sql的时候,有个很重要的注意点:比如:begin  update  某个sqlserver的表@dblink名字 .....;  update 某个oracle的表...;end; 这段pl/sql执行会报错:错误信息是:-----------------------------------------------------------------执行失败:ORA-02054: 事务处理 2.12.27634 有问题

SQL SERVER 查看SQL语句IO,时间,索引消耗

1.查看SQL语句IO消耗 set statistics io on     select * from dbo.jx_order where order_time>'2011-04-12 12:49:57.580' set statistics io off 2.查看SQL语句时间消耗 set statistics time on      select * from dbo.jx_order where order_time>'2011-04-12 12:49:57.580' set st

连接SQL Server执行SQL语句

public static DataTable GetData() { string Connect = ConfigurationManager.AppSettings["ConnectionString"].ToString(); SqlConnection sc = new SqlConnection(Connect); sc.Open(); SqlCommand cm = new SqlCommand("select * from Department");

SQL Server 2008——SQL命令INSERT

T-SQL的INSERT命令的语法: INSERT [INTO]     {table_name|view_name}     [{(column_name,column_name,-)}]     {VALUES (expression,expression,-)} 方括号内是可选的. 列名的列表必须用圆括号包围住,逗号分隔. 关键字VALUES是必须的,数据值用圆括号包围,逗号分隔. 最好是尽量避免在名称中包含空格: 如果表或视图的名称与保留字相同或包含空格,则必须用方括号或双引号将名称包围

(转)[SQL Server] 动态sql给变量赋值(或返回值给变量)

本文转载自:http://blog.csdn.net/xiaoxu0123/article/details/5684680 [SQL Server] 动态sql给变量赋值(或返回值给变量) declare @i_counts int, @i_times int; set @str_sql = 'select @tmp_counts = counts, @tmp_times=times ' + ' from ' +@str_dbname+ '.dbo.t_msisdn with(nolock) '

SQL Server 2008 /SQL Server 2008 R2 配置数据库邮件

原文:SQL Server 2008 /SQL Server 2008 R2 配置数据库邮件 从2005开始,就引入了"数据库邮件"功能.并且取代SQLMail.原有SQLMail可以继续使用. SQLMail要求有应用程序编程接口(Extended Messaging Application Programming Interface,MAPI),安装新的Office时可能导致SQLMail失败. 数据库邮件更加安全.可靠,并且不需要MAPI.具有群集感知能力.可以自动重传发送失败的

SQLSERVER 免费对比数据库结构和数据的工具支持:SQL Server 2012, SQL Server 2008 and SQL Server 2005

New xSQL Schema Compare - version 5 Compare the schemas of two SQL Server databases, review differences, generate synchronization script and execute it - xSQL Schema Compare makes database change management easy. xSQL Schema Compare supports SQL Serv

SQL SERVER 插件SQL Prompt

打开SQL,提示“SQL Prompt has been disabled due to an error with the registration of a required DLL - TextMgrP.dll. To resolve the error, repair SQL Server Management Studio or Visual Studio from the Windows Control Panel.” 解决办法: 重新注册TextMgrP.dll 此动态库 开始->

Sql Server中sql语句自动换行

怎么让sql server中的sql语句自动换行呢? 如下图: 工具--选项--所有语言 Sql Server中sql语句自动换行

Sql Server利器sql prompt

看完园子里VAllen介绍sql prompt博文Red Gate系列之三 SQL Server 开发利器 SQL Prompt 5.3.4.1 Edition T-SQL智能感知分析器 完全破解+使用教程后,结合自己的使用做些补充备忘. 解决问题 解决Sql Server Management Studio 中无法格式化Sql语句问题.原来切换到PL/Sql Developer中格式化Sql语句的日子终于结束了. 每次打select * from xxx,噼里啪啦一通打的过快导致字母顺序错乱.