简体   繁体   中英

Attempting to output a SQL developer export to Excel by running a batch file

I'm new to running tasks through batch files. I have a batch and sql script that outputs to csv, which works. But I want the output to be in Excel. I've edited these scripts so that it can output in Excel. The proces does create an Excel file. However when I attempt to open the Excel file, I get an error that the file has not been found. What am I doing wrong?

This is the SQL script. Let's say this is stored in file.sql :

set verify off
set define off
set termout off
set heading off
set pages 50000
set feedback off
set newpage none
set trimspool on
set linesize 1000
set decimal 


whenever sqlerror exit


spool C:\pathname\filename.xlsx append

select
'' 
||COLUMN_1||
';'||COLUMN_2||
';'||COLUMN_3||
''
FROM blahblah.tablename;

spool off
exit

This is the batch script:

echo off

if exist C:\pathname\filename.xlsx del C:\pathname\filename.xlsx

:: creating a header

echo COLUMN_1;COLUMN_2;COLUMN_3;

sqlplus blahblah/password@databasename @C:\pathname\file.sql

I've basically just changed the .csv file extension in the scripts to a .xlsx extension but clearly this isn't enough, because I can't open the file.

Also, how do I just select all (select *) through this proces? I've tried changing that too, but I just ended up with no data at all (not even a csv file)

In Excel VBA:

 Sub ImportData()
 Dim Conn as New Connection
 Conn.Connectionstring = ' get details from here https://www.connectionstrings.com/sql-server/   depending on version of sql you are using
 conn.open
 dim rs as new recordset
 rs.open "Select * from BlahBlahTablename",conn
 if not rs.eof then ' if we have data
     range("a2").copyfromrecordset rs
 end if
 Dim f as field
 dim r as range
 for each f in rs.fields
   r = f.name
   set r = r.offset(0,1)
  next f
 rs.close
 set rs = nothing
 conn.close
 End Sub

This assumes a reference to ado in excel

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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