I want to export a .txt file from a sql script. I don't want any headings. I also don't want any commas (,) dividing my fields and I want them to keep the " which surrounds each value in my table. My code runs but includes headings, removes " from each field and inserts a ,.
My code
set echo off
set verify off
set termout on
set heading off
set pages 50000
set feedback off
set newpage none
set linesize 1000
set serveroutput off
spool New_Members.txt
SELECT 'Unique_ID, Name, Alt_Name, Entity_Type, Party_Type, Reference_#, Addr1, Addr2, Addr3, Town, County, Postcode,
Country,ALT_ADDRESS1,ALT_ADDRESS2,ALT_ADDRESS3,ALT_TOWN,ALT_COUNTY,ALT_POST_CODE,ALT_COUNTRY,NATIONALITY,
DOB,INDIVIDUAL_ID,INDIVIDUAL_ID_TYPE,COUNTRY_OF_REGISTRATION,COMPANY_ID,COMPANY_ID_TYPE,SOURCE_COUNTRY,SOURCE_SYSTEM,TRANSACTION_TYPE'
from dual;
/
select Unique_ID||','||Name||','||Alt_Name||','|| Entity_Type||','||Party_Type||','||Reference_#||','||Addr1||','||Addr2||','||
Addr3||','||Town||','||County||','||Postcode||','||Country||','||ALT_ADDRESS1||','||ALT_ADDRESS2||','||
ALT_ADDRESS3||','||ALT_TOWN||','||ALT_COUNTY||','||ALT_POST_CODE||','||ALT_COUNTRY||','||NATIONALITY||','||
DOB||','||INDIVIDUAL_ID||','||INDIVIDUAL_ID_TYPE||','||COUNTRY_OF_REGISTRATION||','||COMPANY_ID||','||COMPANY_ID_TYPE||','||
SOURCE_COUNTRY||','||SOURCE_SYSTEM||','||TRANSACTION_TYPE
from dbo.Temp_Weekly_Export_File;
/
spool off;
exit
You already have set header off, so you won't see any Oracle-generated column headings. Setting pagesize to zero would also suppress the headers. You may find it helpful to also set trimout on and set trimspool on.
What you seem to be seeing is the CSV header that you are explicitly generating with your first query. If you don't want to see that header line, just remove it from your script.
If you keep that header, remove the extra whitespace and particularly the line breaks, as they will cause the header to be treated as multiple rows when import to Excel etc. If you were trying to stop that line being too long in your script, you could concatenate multiple shorter strings instead:
select 'Unique_ID,Name,Alt_Name,Entity_Type,Party_Type,Reference_#,'
|| 'Addr1,Addr2,Addr3,Town,County,Postcode,Country,ALT_ADDRESS1,'
|| 'ALT_ADDRESS2,ALT_ADDRESS3,ALT_TOWN,ALT_COUNTY,ALT_POST_CODE,'
|| 'ALT_COUNTRY,NATIONALITY,DOB,INDIVIDUAL_ID,INDIVIDUAL_ID_TYPE,'
|| 'COUNTRY_OF_REGISTRATION,COMPANY_ID,COMPANY_ID_TYPE,'
|| 'SOURCE_COUNTRY,SOURCE_SYSTEM,TRANSACTION_TYPE'
from dual;
You are running both of your queries twice, because each of them is terminated with a semicolon (;) which submits that statement; and is then followed by a slash (/), which re-executes the current command buffer. You only need one or the other, so I'd remove both the slashes - but whichever you remove, be consistent, and check if there are coding guidelines in your organisation which prefer one over the other.
As noted in comments, your question is a bit confused, as you're explicitly adding commas and there are no double-quotes to remove. You may actually want to add double-quotes though, if any of the column values can contain the comma delimiter - which would confuse Excel (or whatever will use this file). You can concatenate double-quotes around specific fields as required, e.g.:
select Unique_ID||','||Name||','||Alt_Name||','|| Entity_Type||','||
Party_Type||','||Reference_#||',"'||Addr1||'","'||Addr2||'",'||
...
which would enclose the first two address column values in double-quotes in the output.
Apologies for a silly and newbie question, but I have no IT background whatsoever, and I don't seem to be able to find an answer in google for that.
I am currently trying to understand (very) simple batch script which happens to be:
for /F "tokens=1,2,3 delims=," %%i in (users.csv) DO dsadd user "cn=%%j
%%i %%k,ou=2013,ou=students,dc=[domain],dc=org"
The bits that is unclear for me are %%i, %%j, %%k. I can see that they represent the columns from the csv file, respectively 1,2,3, and that the output is in the order 2,1,3.
Now, my question is - are letters i, j, k for variables fixed? I can see that this works when I replace them with a, b, c respectively, so I guess not. So is it an agreed convention?
I guess that %%i in this bit delims=," %%i in (users.csv) determines what letter should correspond to the first token, and then the following tokens are assigned alphabetically to j and k?
The following attributes apply to the for command:
The for command replaces %variable or %%variable with each text string in the specified set until the command processes all of the files.
For variable names are case-sensitive, global, and no more than 52 total can be active at any one time.
To avoid confusion with the batch parameters %0 through %9, you can use any character for variable except the numerals 0 through 9. For simple batch files, a single character such as %%f works.
You can use multiple values for variable in complex batch files to distinguish different replaceable variables.
For - http://technet.microsoft.com/en-us/library/bb490909.aspx
The first letter defined will be the first token with the remaining tokens being the next letters alphabetically.
Type the command for /? for more details.
Here is my simplified scenario:
I have a table in SQL Server 2005 with single column of type varchar(500). Data in the column is always 350 characters in length.
When I run a select on it in SSMS query editor, copy & paste the result set in to a text file, the line length in the file is 350, which matches the actual data length.
But when I use sqlcmd with the -o parameter, the resulting file has line length 500, which matches the max length of varchar(500).
So question is, without using any string functions in select, is there a way to let sqlcmd know not to treat it like char(500) ?
You can use the sqlcmd formatting option -W to remove trailing spaces from the output file.
Read more at this MSDN article.
-W only works with default size of 256 for variable size columns. If you want more than that you got to use -y modifier which will tell you its mutually exclusive with -W. Basically you are out of luck and as in my case file grows from 0.5M to 172M. You have to use other ways to strip white space post file generation. Some PowerShell command or something.
How would I insert a variable previously established in a batch file into a text file. I have the inserting text into a text file down, i just cant figure out the insertion of a variable.
What I am doing
SET name = "Casey"
ECHO "Hey" + name > file.txt
The result
"Hey" + name
What I want
"Hey Casey"
You should do it like this:
SET name=Casey
ECHO "Hey %name%" > file.txt
Note that there is no spaces before and after the = in
name=Casey
Too bad syntax, you need to forget other programming languages, this is Batch.
First you can't use spaces when assing values to variables, this is the way to do it:
SET "name=Casey"
Also you can do this:
SET "name= Casey"
Second Batch don't have ANY concatenate operator for strings, forget + and &, & is for concatenating commands.
So this is the correct syntax:
SET "name=Casey"
(ECHO Hey %name%)> "file.txt"
Try to use () agrupation operators when Echo a numeric ending string like "MyName 2", to avoid problems in large scripts with Batch redirections.
I have a batch script in which I have set a variable- "cpu-count" and its default value is set to 1. But when I call the batch script, the cpu-count is passed as an argument and its value can vary from 1-n. For example, I have to invoke the file as ' myscript.bat cpucount-4 ' or ' myscript.bat cpucount-7 '. I have to get this value of '4' or '7' in my script. Does anyone know how to read this value from the argument passed to batch script?
You have several options to do that.
Getting from character 10 to end of string:
set value=%1
set value=%value:~9%
Removing from begining of value until dash:
set value=%1
set value=%value:*-=%
Separating value in two parts at dash, get second part:
for /F "tokens=2 delims=-" %%a in ("%1") do set value=%%a
I hope it helps...