I was using below code(as .bat file) to recursively execute the bulk of .sql files, having SQL SERVER 2008 R2 as backend:
for /R %%G in (*.sql) do sqlcmd /S [Database Server] /d [Database name] -U [Username] - P[Password] -i"%%G"
pause
Now, i i have to execute bulk of sql scripts but this time Sybase as backend.
Please suggest me what modification should i do to make it run for 'Sybase'!!
The connection string for Sybase is very similar
isql -U [username] -P [password] -S [servername] -D [dbname] -i [scriptname]
So your script will look something like:
for /R %%G in (*.sql) do isql -S ServerName -D DbName -U Username -P Password -i"%%G"
pause
It should require minimal changes to get it to work on Sybase vs SQLServer.
Related
Below is my batch file which is taking backup but showing an exception "unexcepted argument" on execution of restore command. Pl help me solving this issue, TIA.
One more doubt can I take backup of remote server on my local folder, here I am taking backup on remote machine itself.
set SqlServer=.
set InstanceName=MSSQLSERVER
set Username=xxx
set Password=xxxx
set Database=abc
set LocalFolder=C:\db_backups
SqlCmd -S %SqlServer% -U %Username% -P %Password% -Q "Backup Database %Database% To Disk='%LocalFolder%\%Database%-%CurrentDate%.bak'"
SqlCmd -S %SqlServer% -U %Username% -P %Password% –Q "RESTORE DATABASE [test] FROM DISK='%LocalFolder%\%Database%-%CurrentDate%.bak'"
I am using below command in bat file to get some information from the list of sql instance,i am using -E to login as windows authentication when i manually triggering the bat file.
Could anyone help how to configure it on SQLJOB.i am sure that - E will not work on SQLjob ..
Thanks in advance!!
FOR /F %%G IN (E:\serverlist\serverlist.txt) DO (echo %%G
sqlcmd -S %%G -iE:\serverlist\some.sql -h-1 -W -w1000 -s "," -m1 -E >>E:\output\result.txt
)
using proxy account helped to resolve the issue.just add the sqlcmd command under a job step with Run as 'Proxy account'
try this.
sqlcmd -S .\sqlexpress -E -i "c:\test.sql" -o "c:\output.txt"
(This command would execute a SQL script on a local SQLExpress server using Windows Authentication)
sqlcmd -S mysql -U sqluser -P sqlpassword -i "c:\test.sql" -o "c:\output.txt"
(This command would execute a SQL script on a SQL server using SQL authentication)
I want to restore a plain postgres backup, im doing it via psql.exe using this commands:
cd "C:\Program Files (x86)\PostgreSQL\9.0\bin\" <br>
psql -h 192.168.10.160 -p 5432 db_name postgres
then cmd displays db_name=# ,and I type, \i 'C:/Backup.sql' and the restore is done
Is there a way to do it using a batch file?
Yes, use the -f option to read from a file:
psql -h 192.168.10.160 -p 5432 -f C:\Backup.sql db_name postgres
When I do this I set the pgdump to include the database name in the sql before all the create tables
\connect mydatabasename
I have a stored procedure through which I am creating a dynamic .sql file on the server and the file have successfully generated. Now I want o execute the same file through query in the same stored procedure .
I have go through the links and found the below lines useful
osql -D db_name -S server_name -U username -P password -i sqlfile
or
sqlcmd -D db_name -S server_name -U username -P password -i sqlfile
I have tried to implement the above but it is giving me the error of
osql is not a recognized option
Please tell me the correct way. Thanks
sqlcmd is a command line tool and cannot be called directly from inside a stored procedure.
Use xp_cmdshell to execute it:
EXEC master.dbo.xp_cmdshell ‘sqlcmd -D db_name -S server_name -U username -P password -i sqlfile’
I am using the following command in my MSBuild file to drop a database
sqlcmd -E -S <ServerName> -Q "DROP DATABASE <DBName>"
But sometimes I get the error
Cannot drop database because it is currently in use.
What command should I use so that the DB is dropped even if it is in use?
You can set your database to SINGLE_USER to drop all existing connections.
sqlcmd -E -S <ServerName> -Q "ALTER DATABASE <DBName> SET SINGLE_USER WITH ROLLBACK IMMEDIATE"
sqlcmd -E -S <ServerName> -Q "DROP DATABASE <DBName>"
This is preferred to looping over each of the connections and dropping them one by one, because some applications will immediately reconnect.
To kill all connections to your database. See Kill All Active Connections To A Database
Now issue your DROP DATABASE command after the above procedure.
Please use something like this:
sqlcmd -S servername\instance -Q"use master;DROP DATABASE yourdatabase"
Try this:
sqlcmd -S .\MAPS -Q "RESTORE DATABASE Awards FROM DISK = 'C:\Awards_Project
\Awards_FULL.bak' WITH REPLACE, MOVE 'Awards' to 'C:\Awards_Project\Awards.mdf',
MOVE 'Awards_log' to 'C:\Awards_Project\Awards.ldf'"
You could alternatively restart the sql service and then perform the drop e.g. in a myfile.bat
#echo off
REM Requires administrative rights to restart service
REM open powershell and execute command in administrative mode
powershell -Command "Start-Process 'cmd' -Verb RunAs -ArgumentList '/c net stop MSSQLSERVER && net start MSSQLSERVER && exit'"
echo Please wait ~10 seconds for sql service to restart.
Timeout /t 10 /nobreak
echo Dropping database.
sqlcmd -S localhost -Q "drop database [database Name]"
You cannot drop a database that is in use. Check link for ref