I've struggled with other StackOverflow responses. I would like to save the output of a query to a local text file - it doesn't really matter where the text file is located as long as it is on my local machine.
Code I am using:
\COPY (
select month,count(*) as distinct_Count_month
from
(
select UNIQUE_MEM_ID,to_char(transaction_date, 'YYYY-MM') as month
FROM yi_fourmpanel.card_panel WHERE COBRAND_ID = '10006164'
group by UNIQUE_MEM_ID,to_char(transaction_date, 'YYYY-MM')
) a
group by month) TO 'mycsv.csv' WITH CSV HEADER;
Error with this code is:
<!-- language: none -->
An error occurred when executing the SQL command:
\COPY (
ERROR: syntax error at or near "\"
Position: 1
\COPY (
^
Execution time: 0.08s
(Statement 1 of 2 finished)
An error occurred when executing the SQL command:
select month,count(*) as distinct_Count_month
from
(
select UNIQUE_MEM_ID,to_char(transaction_date, 'YYYY-MM') as month
FROM yi_fourmpanel.card_panel...
ERROR: syntax error at or near ")"
Position: 260
group by month) TO 'mycsv.csv' WITH CSV HEADER
^
Execution time: 0.08s
(Statement 2 of 2 finished)
2 statements failed.
Script execution finished
Total script execution time: 0.16s
1.For server COPY remove \ and run in psql following:
COPY (
WITH data(val1, val2) AS ( VALUES
('v1', 'v2')
)
SELECT *
FROM data
) TO 'yourServerPath/output.csv' CSV HEADER;
cat yourServerPath/output.csv :
val1,val2
v1,v2
2.For client COPY:
psql -h host -U user -d database -c "\copy \
( \
WITH data(val1, val2) AS ( VALUES \
(1, 2) \
) \
SELECT * FROM data) TO 'yourClientPath/output.csv' CSV HEADER;"
cat yourClientPath/output.csv:
val1,val2
1,2
UPDATED
As for example provided, on your client machine in terminal you need to run following script with absolute path to your mycsv.csv :
psql -h host -U username -d db -c "\COPY ( \
select month,count(*) as distinct_Count_month \
from \
( \
select UNIQUE_MEM_ID,to_char(transaction_date, 'YYYY-MM') as month \
FROM yi_fourmpanel.card_panel WHERE COBRAND_ID = '10006164' \
group by UNIQUE_MEM_ID,to_char(transaction_date, 'YYYY-MM') \
) a \
group by month) TO 'path/mycsv.csv' WITH CSV HEADER;"
Related
I have this query, and in the table/column ttransactionlog_1/occurdatetime, it returns the time value with a .000 at the end, can someone tell me how to remove that from each row? here is a line from the outputfile showing the .000 at the end of the time
0,112213,2021-03-11 14:00:00.000,Santiago,Melody,AdminClock.
Here is the query:
sqlcmd -S clock\punch -d test -U xxx -P xxxxx -Q "Select Distinct TTransactionLog_1.DecisionTimeInterval, TTransactionLog_1.UserID, TTransactionLog_1.OccurDateTime, TTransactionLog_1.lastname, TTransactionLog_1.firstname, TSystemLog1.Name
From TTransactionLog_1 Inner join TSystemLog1 On TTransactionLog_1.NodeID=TSystemLog1.NodeID where TSystemLog1.NodeID = 3 and TTransactionLog_1.OccurDateTime > = dateadd(hh, -1, getdate())
" -s "," -h-1 -W -o "C:\ATR\adminreport.csv"
Thanks in advance!
Does just using Convert on the OccurDateTime value work for you?
Example
declare #OccurDateTime datetime='20210311 14:00:00.000'
select Convert(varchar(19),#OccurDateTime,121)
I'm keen to export a SQL Server query result to an XML file.
I seem to get carriage returns in the resulting file.
I'm wondering what approach I should take to remove the carriage returns from the XML results file?
What I have tried is:
DOS command:
sqlcmd -S HSL-PC0242 -U sa -P PasswordX -i "D:\SQL\auditlog_query1.sql" -C -o "D:\SQL\auditlog_query1_out.xml"
D:\SQL\auditlog_query1.sql contains:
SELECT
A.*
FROM
H2PenguinDev.[dbo].[AuditLog] A
JOIN H2PenguinDev.dbo.ImportProviderProcesses IPP ON IPP.ImportType = 'Z'
AND A.OperatorID = IPP.OperatorID
AND A.AuditTypeID in ( '400','424','425' )
WHERE
A.[PostTime] >= IPP.StartTime
AND A.[PostTime] <= dateadd(second, 90, IPP.StartTime)
FOR XML PATH('Record'), ROOT('AuditLog')
This seems to work.
2Gb output limit tho .. which is fine for this case.
Can open resulting XML in excel ..
and/or use notepad XML plugin and pretty print option to view ..
Note the requirement for ## temp tables rather than single # temp table name.
SELECT A.MyXML
INTO ##AuditLogTempTable
FROM
(SELECT CONVERT(nvarchar(max),
(
SELECT
A.*
FROM
[dbo].[AuditLog] A
JOIN ImportProviderProcesses IPP ON IPP.ImportType = 'Z'
AND A.OperatorID = IPP.OperatorID
AND A.AuditTypeID in ( '400','424','425' )
WHERE
A.[PostTime] >= IPP.StartTime
AND A.[PostTime] <= dateadd(second, 90, IPP.StartTime)
FOR XML PATH('Record'), ROOT('AuditLog')
)
, 0
) AS MyXML
) A
EXEC xp_cmdshell 'bcp "SELECT MyXML FROM ##AuditLogTempTable" queryout "D:\bcptest1.xml" -T -c -t,'
Iam writing a script in unix where where iam trying to implement the following
1) Connect to a database
2) run a select query and fetch the results in a file for validation
Now i have written the following
#!/bin/bash
file="./database.properties"
if [ -f "$file" ]
then
echo "$file found."
. $file
echo "User Id = " ${userName}
echo "user password = " ${password}
echo "schema = " ${schema}
sqlplus -S ${userName}/${password}#${schema}
set feedback off trimspool on
spool workflow_details.txt;
SELECT WORKFLOW_NAME, START_TIME, END_TIME, (END_TIME-START_TIME)*24*60 as TIME_TAKEN
FROM schema1.table1
WHERE WORKFLOW_NAME IN ('argument1,argument2,argument3,argument4')
AND WORKFLOW_RUN_ID IN (SELECT MAX(WORKFLOW_RUN_ID) FROM schema2.table3
WHERE WORKFLOW_NAME IN ('argument1'));
spool off;
exit;
else
echo "$file not found."
fi
The requirement is the value iam using in In clause i.e( argument1,argument2....etc.) is present in a file and the script should be modified such that the arguments will be fetched and placed in In clause through comma separation. The number of arguments is dynamic . How to modify the code.
In short I need to fetch the arguments for IN clause at run time from a file having the argument details . The file will look like having a single column consisting of arguments.
As mentioned in my comments you need to use Collection to fulfill your requirement. See below demo and explanation inline.
In PLSQL
-- Declare a Nested table of type Number. You can declare it of type of your argument1,argument2..
Create or replace type var is table of number;
/
DECLARE
v_var var := var ();
v_num number;
BEGIN
--Fetching rows to collection
SELECT * BULK COLLECT INTO
v_var
FROM (
SELECT 1 FROM dual
UNION ALL
SELECT 2 FROM dual
);
--Printing values of collection
FOR rec IN 1..v_var.count LOOP
dbms_output.put_line(v_var(rec) );
END LOOP;
--Using in Where clause.
Select count(1)
into v_num
from dual where 1 Member of v_var; --<-- this is how you pass the collection of number in `IN` clause.
dbms_output.put_line(v_num );
END;
In your case: UNIX script
#!/bin/bash
#read from file and prepare the "in clause" --<--Put a loop to read through the file
in_clause=argument1,argument2 #--Prepare your `in_clause`
file="./database.properties"
if [ -f "$file" ]
then
echo "$file found."
. $file
echo "User Id = " ${userName}
echo "user password = " ${password}
echo "schema = " ${schema}
sqlplus -S ${userName}/${password}#${schema}
set feedback off trimspool on
spool workflow_details.txt;
SELECT workflow_name,
start_time,
end_time,
( end_time - start_time ) * 24 * 60 AS time_taken
FROM schema1.table1
WHERE workflow_name IN ($in_clause ) #<--Use in clause
AND workflow_run_id IN (SELECT MAX(workflow_run_id) FROM schema2.table3 WHERE workflow_name IN ( 'argument1' )
);
spool off;
exit;
else
echo "$file not found."
fi
PS: Not tested
I have a function like so -
CREATE
OR REPLACE FUNCTION ind (bucket text) RETURNS table (
middle character varying (100),
last character varying (100)
) AS $body$ BEGIN return query
select
fname as first,
lname as last
from all_records
; END;
$body$ LANGUAGE PLPGSQL;
How do I output the results of select ind ('Mob') into a tsv file?
I want the output to look like this -
first last
MARY KATHERINE
You can use the COPY command
example:
COPY (select * from ind('Mob')) TO '/tmp/ind.tsv' CSV HEADER DELIMITER E'\t';
the file '/tmp/ind.tsv' will contain you data
Postgres doesn't allow copy with header for tsv for some reason.
If you're using a linux based system you can do it with a script like this:
#create file with tab delimited column list (use \t between each column name)
echo -e "user_id\temail" > user_output.tsv
#now you can append the results of your query to that file by copying to STDOUT
psql -h your_host_name -d your_database_name -c "\copy (SELECT user_id, email FROM my_user_table) to STDOUT;" >> user_output.tsv
Alternatively, if your script is long and you don't want to pass it in with -c command you can use the same approach from a .sql file, use "--quiet" to avoid notices being passed into your file
psql --quiet -h your_host_name -d your_database_name -f your_sql_file.sql >> user_output.tsv
I`m working with Vertica. I try to export data from SELECT query into csv. I tried making it with sql query:
SELECT * FROM table_name INTO OUTFILE '/tmp/fileName.csv' FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n';
I got an error:
[Vertica][VJDBC](4856) ERROR: Syntax error at or near "INTO"
Is there a way to export a query result to a csv file? I prefer not to use vsql, but if there no other way, I will use it. I tried the following:
vsql -c "select * from table_name;" > /tmp/export_data.txt
Here is how you do it:
vsql -U dbadmin -F ',' -A -P footer=off -o dumpfile.txt -c "select ... from ... where ...;"
Reference: Exporting Data Using vsql
Accordingly to https://my.vertica.com/docs/7.1.x/HTML/Content/Authoring/ConnectingToHPVertica/vsql/ExportingDataUsingVsql.htm
=> SELECT * FROM my_table;
a | b | c
---+-------+---
a | one | 1
b | two | 2
c | three | 3
d | four | 4
e | five | 5
(5 rows)
=> \a
Output format is unaligned.
=> \t
Showing only tuples.
=> \pset fieldsep ','
Field separator is ",".
=> \o dumpfile.txt
=> select * from my_table;
=> \o
=> \! cat dumpfile.txt
a,one,1
b,two,2
c,three,3
d,four,4
e,five,5
By following way you can write to CSV file as comma separated and no footer.
vsql -h $HOST -U $USER -d $DATABASE -w $PASSWORD -f $SQL_PATH/SQL_FILE -A -o $FILE -F ',' -P footer=off -q