How to execute same SP in 2 different connection.
Ex: ALTER PROCEDURE test
...
....
I want to execute this SP in db called "database1" in 192.168.1.100 and same in 192.168.1.102.
I want this to be done using script not using the change connection window
You can use SQLCMD to run a .sql file against multiple server connections.
sqlcmd -S <ComputerName>\<InstanceName> -i <MyScript.sql> -d <database_name> -T
You can do that in SSMS Tools Pack using one of its features called "Run one script on multiple databases".
Editing this to add that this is a tiny and free add-in to SQL Server, that you would find extremely useful.
Related
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
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
I have a large query running that loops over multiple stored procedures. Based on business rules, I'm calling the appropriate stored procedures for each record that is in my loop.
The problem is that those stored procedures sometimes generate multiple result sets. What happens then is that in SQL Server Management Studio, the 'results pane' gets filled up with data, it slows down SQL Server Management Stidion and in the end Management Studio even crashes with an out of memory exception. Is there any way in which I can suppress the results from showing up?
You can also execute it from a command line using OSQL, and specify a logfile:
osql -E -S ServerName -d DBNAme -q "EXIT(<QUERY STUFF HERE>)" -o PathtoLogFile.txt
run the query as a job in Sql Server Agent.
I am looking for a command line program that extracts a script containing commands for creations of all database objects (tables, views, procedures, generators, types). It should work with MySQL, Firebird, Oracle and MS SQL Server.
As a part of our automatic build process we would like to put in our SVN repository a script for database creation in an automatic fashion. Firebird comes with a tool that do what we want but we prefer a generic one so we can include as an ANT task.
For SQL Server you can use the Database Publishing Wizard
I can't help you with the other DB's, however. Good Luck.
To Script the Schema and Data
sqlpubwiz script -d YourDB -S YourServer -U YourUsernam -P pwdhere C:\DestFile.sql
and to just script the Schema
sqlpubwiz script -d YourDB -S YourServer -U YourUsernam -P pwdhere C:\DestFile.sql -schema
I am building an Access database that functions as a reference library. I want to use links in the Access database to execute SQL queries in a different database. Presently when I click the Access hyperlink it tries to run SQL ServerManagemenrt Studio but then errors with 'The operation could not be completed'. I also tried using the Access hyperlink to open a folder containing the SQL queries so one can double click the SQL to run it. The folder opens but the same error message occurs when I try to run SQL from the folder. Clearly something is happening due to the SQL or folder containing the SQL being opened by MS Access. Can any advise what to do?
Since SQL Server Management Studio is openning, are these .sql text files you're trying to execute?
You can execute the t-sql script contained in the file by running this command line:
SQLCMD -S SQL_SERVER_NAME -d DATABASE_NAME -E -I -i "C:\QueryFile.sql" >> ResultBatch.txt
The results get sent to the file ResultBatch.txt which would be in the same folder as the sql file.
Create a batch file in the folder with all the scripts and this will execute all of them:
for %%X in (*.SQL) do SQLCMD -S SERVER_NAME-d DATABASE_NAME -E -I -i "%%X" >> ResultBatch.txt