A handy .BAT file to update a local Sybase SQL DB - batch-file

I need to make a change in my local Sybase DB quite often,
changing "version2" value to "10.076" and sometimes to "10.080".
If instead of opening the Interactive SQL tool, typing in DB's credentials each time, I could just run a "76.bat" or "80.bat" file, it would be very handy.
The query is:
UPDATE "trogxxx"."xxversion" SET "version2"=10.076 WHERE "version"='2002'
And credentials:
UserID: Trogxxx
Password: Trogxxx2018
ServerName: dem8
How would a .BAT file look like, in order to log in and run the update?

You could create a basic bat file calling to isql on the command line and use the -i flag to indicate the SQL script containing the update statement which then gets called as an input file. You could prompt for the -U (username) and -P (password) options or hardcode as much as you want.
Syntax: isql -U username -P password -S server -i input_filename
This assumes your environment has sufficient settings for your normal Sybase client e.g. %SYBASE% etc.

Related

Shell scripts who call sql scripts - convert isql istructions to sqlcmd istructions

we are migrating some shell scripts from unix to linux and we are bsolutely newby.
Some scripts invoke slq scripts which contain instructions for operations on a syabase database that we are migrating to sql server database, so we are rewriting these sql scripts as well.
Our new shell scripts call directly the sqlcmd command and already pass db server, db name, user and password stored in environment variables:
sqlcmd -S $SERVER_DB -d $NAME_DB -U $USER_DB -P $PASSW_DB < /home/scripts/update_data.sql
but at the first line of each sql script we need to convert, we are finding similar statement:
isql -U$DBUSER -P$DBPASSWD -w 80<< EOF|grep -v "return status" >>/usr/local/abc/ABC.txt
Perhaps because for some users a certain connection to the syabase database was set by default, so it was sufficient to put the call to the isql command on the first line of the sql file passing only the user and password, but in our case using sqlcmd we already pass everything directly in the shell script, it is not necessary to invoke the sqlcmd command again at the first line of the sql script.
So what should we write in place of isql call but leaving the part that redirects the output to the ABC.txt file?
Thanks in advance

Executing set of SQL queries using batch file?

I am using a SQL Server database. I have these SQL queries:
Delete from TableA;
Delete from TableB;
Delete from TableC;
Delete from TableD;
Delete from TableE;
Is it possible to run these scripts using a batch file? The database is a remote database.
Thanks!
Save the commands in a .SQL file, ex: ClearTables.sql, say in your C:\temp folder.
Contents of C:\Temp\ClearTables.sql
Delete from TableA;
Delete from TableB;
Delete from TableC;
Delete from TableD;
Delete from TableE;
Then use sqlcmd to execute it as follows. Since you said the database is remote, use the following syntax (after updating for your server and database instance name).
sqlcmd -S <ComputerName>\<InstanceName> -i C:\Temp\ClearTables.sql
For example, if your remote computer name is SQLSVRBOSTON1 and Database instance name is MyDB1, then the command would be.
sqlcmd -E -S SQLSVRBOSTON1\MyDB1 -i C:\Temp\ClearTables.sql
Also note that -E specifies default authentication. If you have a user name and password to connect, use -U and -P switches.
You will execute all this by opening a CMD command window.
Using a Batch File.
If you want to save it in a batch file and double-click to run it, do it as follows.
Create, and save the ClearTables.bat like so.
echo off
sqlcmd -E -S SQLSVRBOSTON1\MyDB1 -i C:\Temp\ClearTables.sql
set /p delExit=Press the ENTER key to exit...:
Then double-click it to run it. It will execute the commands and wait until you press a key to exit, so you can see the command output.
Check out SQLCMD command line tool that comes with SQL Server. http://technet.microsoft.com/en-us/library/ms162773.aspx
Use the SQLCMD utility.
http://technet.microsoft.com/en-us/library/ms162773.aspx
There is a connect statement that allows you to swing from database server A to server B in the same batch.
:Connect server_name[\instance_name] [-l timeout] [-U user_name [-P password]]
Connects to an instance of SQL Server. Also closes the current connection.
On the other hand, if you are familiar with PowerShell, you can programmatic do the same.
http://technet.microsoft.com/en-us/library/cc281954(v=sql.105).aspx
Different ways:
Using SQL Server Agent (If local instance)
schedule a job in sql server agent with a new step having type as "T-SQL" then run the job.
Using SQLCMD
To use SQLCMD refer http://technet.microsoft.com/en-us/library/ms162773.aspx
Using SQLPS
To use SQLPS refer http://technet.microsoft.com/en-us/library/cc280450.aspx

sqlcmd won't accept password as part of the initial command

I am using sqlcmd to query a SQL server database from a UBUNTU machine (an ODBC driver was installed for this purpose)
If I pass the following command:
sqlcmd -S xyz -d xyz -U xyz
I am prompted for a password, and I can then successfully query the database in question. But, if pass the command below, I get the message 'Login failed for user ****':
sqlcmd -S xyz -d xyz -U xyz -P xyz
I need to be able to send the password in one pass, as I need to pass an input query file and send the output to a specific location. Once I log into the database by waiting for a password prompt, I can no longer specify input/output files. Any insights would be much appreciated
Have you tried it without the space between the -P and the value? The docs show them glued together.

Create batch file to copy contents of one SQL Server database to another

I need to create a batch file (script) to copy entire content of one SQL Server database to another. Source database is local for the machine. What command line tools and commands therein I could use? Destination database can be dropped entirely.
Also, I need to run a pair of SQL queries afterwards, also from batch file.
Thanks in advance!
you can read about Run Transact-SQL Script Files Using sqlcmd from here:
http://msdn.microsoft.com/en-us/library/ms170572.aspx
first u need to create your sql queries then u can use this command to run it via batch file
sqlcmd -S %computername%\%SName% -U %UName% -P %Pwd% -i SQL_DB.sql >> _Deploy.txt 2>&1
this command gets computer name and sqlserver instance name and user name and password and run SQL_DB.sql file near batch file that contains our queries and save the result in a text file named _deploy.txt

how to run multiple sqlcmd statements inside windows batch file after performing SQL authentication

I am trying to run multiple SQLCMD statements (that archive tables in SQL Server 2008 and create restore scripts) from windows .bat file. My scripts work like first I perform SQL Authentication and then I input SQL scripts to output restore files
sqlcmd -S <Server name>\<instance> -U user
sqlcmd -i ArchiveTable1.sql -o RestoreTable1.sql
sqlcmd -i ArchiveTable2.sql -o RestoreTable2.sql
sqlcmd -i ArchiveTable3.sql -o RestoreTable3.sql
Problem is that after sqlcmd authentication, there appears prompt 1> and does not execute my next statements. On entering Quit, my next statements get executed but in output files I see authentication invalid error.
I dont want to have sql authentication with each sqlcmd statement.
If there is issue with my approach, can somebody guide me to use other design.
Three sqlcmd execution, three authentications:
sqlcmd -S <Server name>\<instance> -U user -i ArchiveTable1.sql -o RestoreTable1.sql
sqlcmd -S <Server name>\<instance> -U user -i ArchiveTable2.sql -o RestoreTable2.sql
sqlcmd -S <Server name>\<instance> -U user -i ArchiveTable3.sql -o RestoreTable3.sql
I dont want to have sql authentication with each sqlcmd statement
What you want matters little. You must do what is correct. If you have a justification for your unusual requirement, state the problem clearly, don't offer a solution and ask us to make it work.
If you want to avoid repeating user info and password in batches, and more importantly if you want to avoid leaving these passwords lying around in batches, use the sqlcmd environment variables SQLCMDUSER and SQLCMDPASSWORD:
The SQLCMDPASSWORD environment variable lets you set a default password for the current session. Therefore, passwords do not have to be hard-coded into batch files.
Simply set the SQLCMDUSER, SQLCMDPASSWORD (and maybe SQLCMDSERVER) and then invoke the batch. This way the batch does not need to contain any hard coded password.

Resources