Passing parameters to Oracle SQL file from Batch file - sql

I have a SQL file that updates certain table. However, the table name depend on the machine name on which the software is installed. (Something like: TableFooBar-MyMachine). I have to write a batch file that calls an Oracle SQL script which will update this table.
So, BatchFile --> Update. Sql --> TableFooBar-MyMachine
The Update. Sql will have statement like:
Update TableFooBar-<Machine-Name> where fooid = -99;
This batch file needs to run on many machines. There are actually many update statements on such tables. I do not want people to edit the sql files. Instead if I set the machine name in the batch file and pass it to the sql, I'm pretty much done!
How do I pass the machine name parameter to the .sql file to achieve this?

you can use substitution variables
update.sql
--
Update TableFooBar-&1 set column_foo='bar' where fooid = -99;
--
and then call
sqlplus foo/bar#db #update.sql <Machine-Name>

Yes, you can do this, by creating the SQL file from the BATCH file.
It would look like this:
#echo off
set SQL = update.sql
ECHO connect username/password#database
ECHO Prompt Updating tables > %SQL%
ECHO Update TableFooBar-%1 where fooid = -99; >> %SQL%
sqlplus #update.sql

Related

Large File export to postgreSQL

I need to export a 50gb file with inserts to a table in postgreSQL to be able to count the time it takes to perform the inserts, but I can't find any way to load that file, can someone help me?
If the file have you have contains syntactically valid SQL (like INSERT statements), this is very straightforward using the command line psql client that comes with a Postgres installation:
psql DATABASE_NAME < FILE_NAME.sql
You may also want to replace DATABASE_NAME with a connection string like postgres://user:pass#localhost/database_name.
This causes your shell to read the given file and pass it off to psql's stdin, which will cause it to execute commands against the database it's connected to.

SQL Server commands in string

I need a command in a .sql file for SQL Server 2012 that lets me run strings as commands, like:
set #command= 'create table mytable (...);';
run(#command);
Preferably with some kind of string format for strings and/or numbers. Usually I do this in C# but I was wondering if I can keep it all in a .sql file.
Yes you can, save your SQL command in a *.sql file say test.sql and use SQLCMD Utility to run the sql file in a command prompt like below
sqlcmd -S <ComputerName>\<InstanceName> -i test.sql -o result.txt
Not sure but if you mean running the *.sql file in your c# code behind; then I would suggest, create a stored procedure and pull up your SQL queries in that stored procedure. Then you can simply call that procedure in your application code instead of reading the SQL file and running them one by one.

Moving results of T-SQL query to a file without using BCP?

What I want to do is output some query results to a file. Basically, when I query the table I'm interested in, my results look like this:
HTML_ID HTML_CONTENT
1 <html>...
2 <html>...
3 <html>...
4 <html>...
5 <html>...
6 <html>...
7 <html>...
The field HTML_CONTENT is of type ntext and each record's value is around 500+ characters (that contains HTML content).
I can create a cursor to move each record's content to a temp table or whatever.
But my question is this: instead of temp table, how would I move this without using BCP?
BCP isn't an option as our sysadmin has blocked access to sys.xp_cmdshell.
Note: I want to store each record's HTML content to individual files
My version of sql is: Microsoft SQL Server 2008 (SP1) - 10.0.2531.0
You can make use of SSIS to read the table data and output the content of the table rows as files. Export column transformation available within Data Flow Task of SSIS packages might help you do that.
Here is an example, The Export Column Transformation
MSDN documentation about Export Column transformation.
This answer would have worked until you added the requirement for Individual Files.
You can run the SQL from command line and dump the output into a file. The following utilities can be used for this.
SQLCMD
OSQL
Here is an example with SQLCMD with an inline query
sqlcmd -S ServerName -E -Q "Select GetDate() CurrentDateAndTime" > output.txt
You can save the query to a file (QueryString.sql) and use -i instead
sqlcmd -S ServerName -E -i QueryString.sql> output.txt
Edit
Use SSIS
Create a package
Create a variable called RecordsToOutput of type Object at the package level
Use an EXECUTE SQL task and get the dataset back into RecordsToOutput
Use a For-Each loop to go through the RecordsToOutput dataset
In the loop, create a variable for each column in the dataset (give it the same name)
Add a Data Flow task
Use a OleDB source and use a SQL statement to create one row (with data you already have)
use a flat-file destination to write out the row.
Use expressions on the flat file connection to change the name of the destination file for each row in the loop.

How to view tables in sql?

Hai i am a beginner of Database,
i have a .sql file which contains some tables of data, i want to know how to import them and how to view the list of tables.
presently im using the following:-
software or editor : navicat lite
server : localhost.
databse file format: .sql
Maybe you can try to execute the script in sql server, then type
select * from [database_name].information_schema.tables
to view tables and relevant information.
Remember that a sql file is not really a database, it is a script. You can run the script from any tool, but I'd use command line. This is navicat connected to mysql?
mysql -u username -p databasename < script.sql
password: **
And then the results can be seen using navicat or any other tool
If the .sql file has statements such as "CREATE TABLE..." and then later on "INSERT INTO..." then the script is possibly creating the tables and inserting the data.
To allow that to happen, the tables need to not exist in the database. You can then run the script and it will create the tables and fill in the data.
If the tables do exist, you can always either delete them, or change the CREATE to an ALTER and the script should then run.
Hope that helps.

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

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