t-sql**:
exec master..xp_cmdshell 'bcp 庫名.dbo.表名out c:/temp.xls -c -q -s"servername" -u"sa" -p""'
引數:s 是sql伺服器名;u是使用者;p是密碼
說明:還可以匯出文字檔案等多種格式
declare @str varchar(600)
select @str=
'bcp "select convert(nvarchar(2),99 )+ cast(year(trxdatetime) as nvarchar)+cast(month(trxdatetime) as nvarchar)+cast(day(trxdatetime) as nvarchar)+convert(varchar(2),trxdatetime,8)+substring(convert(varchar(5),trxdatetime,14),4,4) + convert(nvarchar(2),1 ) + rtrim(ltrim(employeeno)) from sdt..employeetransaction where empgroup=convert(nvarchar(7),2010301) and prodgroup=convert(nvarchar(7),2010301) and logdate=convert(varchar(10),getdate()-1,23) order by employeeno,trxdatetime" queryout "e:/oldfactoryemployeeattendance/' +
cast(year(getdate()) as nvarchar)+cast(month(getdate()) as nvarchar)+cast(day(getdate()-1) as nvarchar)
+'.txt" -c -q -s"svr26" -u"sa" -p"123"'
--select @str
exec master..xp_cmdshell @str
--copy 檔案復到另乙個目錄
--remark: copy file to other server
declare @copyfile varchar(300)
set @copyfile='copy e:/oldfactoryemployeeattendance/'+ cast(year(getdate()) as nvarchar)+cast(month(getdate()) as nvarchar)+cast(day(getdate()-1) as nvarchar) +
'.txt //gproreport/test/'+ cast(year(getdate()) as nvarchar)+cast(month(getdate()) as nvarchar)+cast(day(getdate()-1) as nvarchar) + '.txt'
--select @copyfile
exec master..xp_cmdshell @copyfile
--注:系統定時執行可把這段sql code加到job
SQL多結果集匯出Excel
由於本專案工作中需要,有時會匯出一些資料給客戶,但又不是每次都需要,可能這次用了下次可能就不會使用,匯出資料,我們正在做的乙個專案中與四川地區有關,所以匯出資料就有如下需求 1 按各市匯出資料,把第個市名作為sheet名 這到沒什麼難就是21個地市州,查出來粘到exc el中 2 把各市各區縣志出乙...
SQL查詢結果匯出到Excel中
執行 object storedprocedure dbo exportfile script date 2016 05 10 23 21 13 set ansi nulls on goset quoted identifier on gosp configure show advanced opt...
MySQL查詢結果匯出到Excel
mysql uroot p use databa xx select user,host,password from user into outfile tmp data.xls select b.name,a.title,a.answer from plugin faq question a jo...