简体   繁体   English

如何将mySQL #temp表导出到脚本中的.csv文件?

[英]How can I export a mySQL #temp table to a .csv file within script?

I'd like to write a script that allows me to export .csv files from 15-20 temporary tables I created, using a script instead of having to copy and paste in a separate .csv file and then save them down. 我想编写一个脚本,允许我从我创建的15-20个临时表中导出.csv文件,使用脚本而不必复制并粘贴到单独的.csv文件中,然后将其保存下来。

:!!sqlcmd -S server -d database-E -Q "SET NOCOUNT ON 
SELECT * FROM TABLE" -o "C:\Users\name\Documents\folder\filename.csv"
-W -w 1024 -s ","

I've tried this, which works (not formatting correctly) but it doesn't seem to be work at all for a temp table; 我试过这个,它有效(没有正确格式化)但它似乎根本不适用于临时表; the .csv file contains this. .csv文件包含此内容。

Msg 208  Level 16    State 1     Server SERVERNAME
Invalid object name '#TEMPTABLE'.       

I cannot obtain "elevated privileges" to be able to use BCP export, because I cannot write a stored procedure, create a new database, or access the command line. 我无法获得“提升权限”以便能够使用BCP导出,因为我无法编写存储过程,创建新数据库或访问命令行。 Is there a workaround for this? 这有解决方法吗?

Temp tables are ephemeral; 临时表是短暂的; they do not persist across sessions. 他们不会在会议期间坚持下去。 Instead of creating temp tables, create actual tables, either in the database that you're working with, or in tempdb , then export the data from tempdb 而不是创建临时表,在您正在使用的数据库中或在tempdb创建实际表,然后从tempdb导出数据

An example: 一个例子:

sqlcmd -S server -d database -E -Q "If Exists (select * FROM tempdb.sys.tables WHERE name = 'Tmp_DataExport1') drop TABLE tempdb..Tmp_DataExport1;"

sqlcmd -S server -d database -E -Q "SELECT TOP 5 * INTO tempdb..Tmp_DataExport1 FROM T_SourceTable"

sqlcmd -S server -d database -E -Q "SELECT * FROM tempdb..Tmp_DataExport1" -o "c:\\temp\\filename.csv" -W -w 1024 -s ","

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM