Passing script to psql in Excel 2013 - psql

I can open the psql shell and access my nfldb data base from Excel:
Sub atest()
Dim wsh As Object
Set wsh = VBA.CreateObject("WScript.Shell")
Dim waitOnReturn As Boolean: waitOnReturn = True
Dim windowStyle As Integer: windowStyle = 1
wsh.Run "cmd /c psql nfldb nfldb"
And go to the shell and extract info from the table 'team' in database nfldb to the file 'teamNames' with:
nfldb=> \copy (SELECT * FROM team) TO 'C:\temp\teamNames.csv' With CSV
My question is how can I run the entire code from within Excel so that the nfldb database is opened AND the rows from the 'team' table are saved to the file 'teamNames'
How do I combine both codes?
Any help would be greatly appreciated!

If anyone else needs help with this, I resolved the issue myself after much trial and error.
Dim wsh As Object
Set wsh = VBA.CreateObject("WScript.Shell")
Dim waitOnReturn As Boolean: waitOnReturn = True
Dim windowStyle As Integer: windowStyle = 1
wsh.Run "cmd /c psql -U nfldb -f C:\Python27\writeTeams.sql"
Will open the nlfd database in a psql shell and run the script contained in the file 'writeTeams.sql'

Related

Export table to a csv file in DBeaver with command

I am using the following command to export the data
\copy (select * From table_name) To 'my_path' With CSV DELIMITER ',' HEADER;
But I get error syntax error at or near "\".
If I use
copy (select * From table_name) To 'my_path' With CSV DELIMITER ',' HEADER;
I get the error COPY TO instructs the PostgreSQL server process to write a file. You may want a client-side facility such as psql's \copy.
I really do not know which command I should run. Thanks in advance for your help:))

How to execute a system command inside a informix stored procedure?

I need to execute a os command inside a informix SPL. My informix instance runs on Solaris machine. I tried with following codes. But gives below mentioned error.
Please correct me.
CREATE PROCEDURE log_message (message VARCHAR(70))
DEFINE os_command VARCHAR(100);
-- Output message to log file
LET os_command = 'ls -ltrh /informix/nuwan/' || message || ' >> /informix/nuwan/test/data.log';
SYSTEM os_command;
END PROCEDURE;
Then I called the procedure as follows
CALL log_message('Test message...');
it give below error
668: The system command cannot be executed or it exited with a non-zero status.
2: No such file or directory
Use the SYSTEM statement to issue an operating-system command from within an SPL routine.
https://www.ibm.com/support/knowledgecenter/en/SSGU8G_14.1.0/com.ibm.sqls.doc/ids_sqs_1358.htm

SQLPlus: SPOOLing vs writing to log

I am using a shell script to 'spool' a query. Here is a toy version:
#!/bin/sh
sqlplus -s userid/pass#SID << EOF
set echo off
set term off
set trims on
set pages 0
set feedback off
set linesize 1000
set colsep "|"
SPOOL $2
SELECT 'HEADER1|HEADER2|HEADER3' FROM DUAL
UNION ALL
SELECT
COLUMN1||'|'||
COLUMN2||'|'||
COLUMN3
FROM $1;
SPOOL OFF
EXIT 0;
EOF
And submitting using
nohup sh sqlquery.sh intable outtable > log &
The query runs fine and is formatted exactly how I would like, but the rows returned by the query are written to both the spool file and the log... I thought 'set echo off' would take care of this, but I am obviously missing something.
Any ideas?

How to return value of shell script to rails?

I have written a shell script which executing some of the mysql commands.
I want to run that shell script from rails side. And want to get result back of commands to rails from shell script.
How may i handle it?
EDIT:
script.sh
mysql -u root -pmysql << eof
SELECT TABLE_NAME AS "Table Name", table_rows AS "Quant of Rows", ROUND((data_length + index_length)/1024/1024,2) AS "Total Size Mb" FROM information_schema.TABLES WHERE information_schema.TABLES.table_schema='database_name';
eof
This is my script. How may i return SELECT query result to rails ?
You can do something like this, it will block until the script is finished and any output that it sent to standard out will be contained in the output variable.
output = `/my/script.sh`
puts output
Use backticks ` `
`/path/to/script` #backticks
Edit
To get the output of a mysql statement, use the -e flag
Eg. mysql -e "SELECT * from information_schema"

Informix: How to get the table contents and column names using dbaccess?

Supposing I have:
an Informix database named "my_database"
a table named "my_table" with the columns "col_1", "col_2" and "col_3":
I can extract the contents of the table by creating a my_table.sql script like:
unload to "my_table.txt"
select * from my_table;
and invoking dbaccess from the command line:
dbaccess my_database my_table.sql
This will produce the my_table.txt file with contents like:
value_a1|value_a2|value_a3
value_b1|value_b2|value_b3
Now, what do I have to do if I want to obtain the column names in the my_table.txt? Like:
col_1|col_2|col_3
value_a1|value_a2|value_a3
value_b1|value_b2|value_b3
Why you don't use dbschema?
To get schema of one table (without -t parameter show all database)
dbschema -d [DBName] -t [DBTable] > file.sql
To get schema of one stored procedure
dbschema -d [DBName] -f [SPName] > file.sql
None of the standard Informix tools put the column names at the top of the output as you want.
The program SQLCMD (not the Microsoft newcomer - the original one, available from the IIUG Software Archive) has the ability to do that; use the -H option for the column headings (and -T to get the column types).
sqlcmd -U -d my_database -t my_table -HT -o my_table.txt
sqlunload -d my_database -t my_table -HT -o my_table.txt
SQLCMD also can do CSV output if that's what you need (but — bug — it doesn't format the column names or column types lines correctly).
Found an easier solution. Place the headers in one file say header.txt (it will contain a single line "col_1|col_2|col_3") then to combine the header file and your output file run:
cat header.txt my_table.txt > my_table_wth_head.txt

Resources