Mysql: How to call sql script file from other sql script file? - sql

Suppose I have wrote script Table_ABC.sql which creates table ABC. I have created many such scripts for each of required tables. Now i want to write a script that call all of these script files in a sequence so basically I want another script file createTables.sql. Mysql provides option to execute a script file from "mysql" shell application but could find some command like exec c:/myscripts/mytable.sql. Please tell me if there is any command that can be written in sql script itself to call other one in latest mysql versions or alternative for same.
Thanks

You can use source command. So your script will be something like:
use your_db;
source script/s1.sql;
source script/s2.sql;
-- so on, so forth

Related

How to run sql script in SQL Workbench/J from/using command line?

I have a small sql query(which uses Wbexport utility) that I need to run daily. I want to automate this task so I am thinking to write a batch file and schedule it using windows task scheduler. Could someone tell me how to run a sql query/.sql file from command line in SQL Workbench/J?
This is documented in the manual
You need to put the query you want to run in a .sql script and then pass the name of the script on the commandline:
java -jar sqlworkbench.jar -script=run_export.sql -profile=...
or
SQLWorkbench64.exe -script=run_export.sql -profile=...
There are various ways to define the connection, either through the -profile parameter or by specifying the complete connection information. When using the profile parameter you need to make sure that the profiles are stored in the default location or specify the location where the profiles are stored.

Saving an sqllite3 database as csv file in Ruby on Rails 5

I am currently working on a project and I want to know how to save an sqllite database in rails as a csv file. I want it when you click the button, the current database on the system download. Can anybody help me? Thanks!
Your problem isn't really specific to Rails. Instead, you're mostly dealing with an administrative issue. You should write a script to export your database as csv, something like this:
#!/bin/bash
./bin/sqlite3 ./my_app/db/my_database.db <<!
.headers on
.mode csv
.output my_output_file.csv
select * from my_table;
!
This script exports a single table. If you have additional tables, you'll want to add them to your script.
The only Rails related issue is the matter of calling that script. Save the script within your application structure; I'd suggest my_app/assets or some similar location.
Now you can run that script using system(command) where command is the absolute path for your script, within a set of double-quotes.

How to execute SQL queries from text files

I am using Aqua Data Studio 7.0.39 for my Database Stuff.
I have a 20 SQL files(all contains sql statements, obviously).
I want to execute all rather than copy-paste contains for each.
Is there any way in Aqua to do such things.
Note: I am using Sybase
Thank you !!
I'm also not sure of how to do this in Aqua, but it's very simple to create a batch/powershell script to execute .sql files
You can use the SAP/Sybase isql utility to execute files, and just create a loop to cover all the files you wish to execute.
Check my answer here for more information:
Running bulk of SQL Scripts in Sybase through batch
In the latest versions of ADS there is an integrated shell named FluidShell where you can achieve what you are looking for. See an overview here: https://www.aquaclusters.com/app/home/project/public/aquadatastudio/wikibook/Documentation15/page/246/FluidShell
The command you are looking for is source
source
NAME
source - execute commands or SQL statements from a file
SYNOPSIS
source [OPTION...] FILE [ARGUMENT...]
source [OPTION...]
DESCRIPTION
Read and execute commands or SQL statements from FILE in the current shell environment.
I have not used Aquafold before so I can't tell you exactly. However I have tackled a similar problem once before.
I once created a Powershell script. It opened a ODBC connection to my database and then executed stored procedures in a loop until end of file.
I suggest having a text document with each line being the name of an Stored Proc to run. Then in your powershell script read in a line from the file concatenate it into the call to execute a stored procedure. After each execution is completed you can delete the line from the text file and then read the next line until the EOF (end of file) is reached.
Hope this helps. If I have some time this morning I will try and do a working example for you and post it.

How to run an SQL script in a batch file

I have a an sql script that creates a database, and want to know how to run this from a batch file at a command prompt.
Do i create a batch file with a few lines of code pointing to the location of the .sql file, or create a new batch file containing the contents of the .sql file?
I've had a look at a couple of related questions, but can't seem to see a clear answer.
Thanks :)
You could create a batch file and use the -i flag with sqlcmd.exe, where -i sets the path to the .sql file you want to run:
sqlcmd.exe -i F:\wherever\the\file\is
See http://msdn.microsoft.com/en-us/library/ms162773.aspx for a full list of the flags and this post, How to use sqlcmd to create a database.
is quite easy, you only need 3 things:
SQL Client Path/
Script Path/
SQL Conection data/
Then you can run it directly from the batch without any complex script - let me know if you have the info I can arrange it.
regards

Executing sql files automatically

Suppose I am having 100 sql files and I need to execute all the files one by one in sequence. Is there any approach to do this with out executing the scripts manually?
You can write a bat file to execute them using sqlcmd Utility
Write a shell script or similar to run them sequentially.
We've had great success with the SQL Deploy tool by SSW Australia.
It's not free - but worth every penny, and saves you so much time, it pays for itself in no time at all!
(I have no affiliation with SSW Australia other than being a happy user of SQL Deploy)
Pipe the dir /b > foo.txt output to a file
Add sqlcmd at the start of each line etc using a decent text editor like notepad++
You can use PowerShell to do this. The following blog post describes such a script. As part of the foreach a pipe is used to sort the files in the manner that you want to process them. In this example it is being sorted by descending alphabetical file name, but you can also do it by other attributes, such as the date the file was created.
Also the following blog post describes how to run all the .sql files in a directory like the above linked post, but without the use of PowerShell
Assuming your files are named something like this:
001_my_script.sql
002_another_script.sql
003_foo_script.sql
004_bar_script.sql
You can do the following at the command line:
copy *.sql /a my_big_script.sql
And then run the resulting file as one script (via sqlcmd or Management Studio).