Redirect input from another command in windows batch files - redirect

In linux I can do something like this:
mysql -u user -p=pass somedb <(echo "create database foo;")
How can I do that with windows batch scripts?
Basically, I want to make a batch file that runs a sql script without having to keep the script in a separate file.
Thanks

One way is to echo the SQL commands into a file, run the mysql command with option to include the SQL file, and then remove the file (if you really don't want it.)

You can do
echo create database foo;|mysql ...
just fine, but for multiple lines you really want to make a temporary file you just pass to MySQL to read.

Related

PostgreSQL copy 0?

I have written a simple batch script which loops a directory and echoes some details about each file.When I view its results in the CMD terminal or output it to some file, I can view the results as expected.
The problem comes with PostgreSQL: when I try to import its results into a table, executing the following command:
copy schema.table(field) from program 'C:\\...\\my_bat.bat' with CSV header delimiter E'\t';
It imports 0 results, whereas if I run the same command pointing to a similar batch file in another directory, it works as expected.
What's going on? I am using windows.
Update: I have tried running the copy command calling program again on another batch script and this time, only a part of the string output is being imported.
The service user postgres needs sufficient permissions to run the program.
I remember that it was hard to change settings for that account on windows XP, I have not tried on more recent windows - service users are hidden by most GUI tools.

trying to locate output file from postgresql \o -o command

I am making an automated script from terminal that creates a file with the output of \l
But I do not know where the \o command in postgresql prints out the file that it has made. The documentation doesn't inform where.
I did read this, but no luck:
Sincerely
\o points at the named file in current working directory of psql. As you found out, this has some issues in automated environments.
This means you have basically two options:
use absolute paths with \o
Alternatively you can use \cd to set your current working directory inside your psql script.
In your particular case, however, you know that psql -l gives you the same info? That may be easier to manage in an automated environment.

DB2 executing a Script in another Script

I am facing a problem in DB2. In my Oracle environment it was very easy for me to include multiple scripts in one master script, which were executed sequentially. e.g.:
Master.sql:
connect ....
#script1.sql
#script2.sql
Now I have to build up same logic in DB2 LUW. Is there a simple way to include multiple scripts in one master script? I would like to have one single db2 call from shell, which executes the master script and within all subscripts.
Regards
Jan
There is notrhing to stop you from creating a single file with multiple sql batches. In the Windows world, it would look like this:
Note: First you initialize the db2 command prompt.
db2cmd -c -w -i %1.bat
With as many of these as you want in the .bat file:
db2 -txf c:\Example\db2html.sql
In Linux, the db2clp is included in the shell once you load the db2profile ('. /home/db2inst1/sqllib/db2profile). In windows, you need to call db2cmd in order to use db2clp.
With a interactive db2clp, you cannot call db2 scripts via #scriptX, however, you can call them from the shell like
db2 -tvf script
However, if you use the CLP*Plus you can do almost everything you do in SQL*Plus. For more information: https://www.ibm.com/developerworks/community/blogs/IMSupport/entry/tech_tip_db2_s_new_clp_plus_utility?lang=en

How to use \COPY command inside a function in PostgreSQL

I want to export the table into a .CSV file using \COPY command. I can able to do that as a stand-alone command. I can't embed the same line inside a function in PostgreSQL. Actually that call should come from a ECPG. I chose \COPY over COPY command as I don't have super-user account! Please guide me on this.
Thanks and Regards,
Siva.
Unfortunately, all of the \ commands are psql commands. You can run psql -E to see what those commands expand into and get sent to the server as, but if you need a super-user account to run COPY, you're going to need a super-user account to do this.
If you are talking about using copy with plpgsql, I think this might help:
Dynamically-generated table-name in PostgreSQL COPY command

postgresql load sql script

I want to load and execute a sql file from another sql file, but do not the syntax. Is this possible? In detail, I have a file install_tables.sql which should load a file tables_definition.sql.
If you run your script using psql you can embedd the \i command in your main script:
\i tables_defininition.sql