I'm trying to deploy a database (.bak / sql server 2012) by a batch(.bat) file , could you give me an example or how could I do that?
Best Regards.
You can use the SqlCmd command-line tool in a batch-file to do that. This command-line tool is installed when you install SQL Server Management Tools – Basic.
There is plenty of online information, this is a pseudo-code example I found:
SqlCmd -E -S <Server_Name> –Q “RESTORE DATABASE [Name_of_Database] FROM DISK='X:PathToBackupFile[File_Name].bak'”
Related
I need to attach a new database to Microsoft Sql Server LocalDB from the command line.
All the examples, tutorials, etc that I've seen so far attach the database from Visual Studio or SSMS.
How can I do it from the command line?
You can use osql.exe:
osql -E -S "(LocalDb)\V11.0" -Q "exec sp_attach_single_file_db 'LocalDb', 'C:\whatever\backup.mdf'"
You might need to exchange "V11.0" with something different depending on your version of LocalDb ... find out with "SqlLocalDb i" on the command line ...
I'm still pretty new to SQL Server but have a ton of experience with Oracle. How do I run a SQL script while I'm inside a SQLCMD session? Is their something analogous to "SQL>#C:[mypath]\myscript.sql" (Oracle) for SQL Server?
I'm sick of opening scripts and running them using bloated SQL Server Studio.
sqlcmd -i file executes a file.
Can't recommend enough to check all the options in the docs.
I found what I was looking for. You can use this in SQLCMD and in SSMS Query as long as you open it up in "SQLCMD Mode"
:r [path]\myscript.sql
TransactSQL to run another TransactSQL script
Thanks for your responses. Much thanks.
I am using SQL Server 2008 (Express Edition).
I want to create a job which will delete all data from the all the table(>50) in the DB everyday at night 1:00.
Instead of Deleteting i decide to restore the DB from the Script.
It would have been easy by using SQL Server Agent, But this is limitation in SQL Server Express.
I figured out that we can create jobs "manually" by creating batch files and SQL script files, and running them via Windows Task Scheduler.
I have no clue what i have to write in bat file and sql file.This is my first time where i am working so deeply in SQL configuration. Can someone help please?
Name of the script which i need to restore is test.sql.
If any one has different approach , please share.
Thanks
Prat
Your batch files needs to look like this. Change the path to your .sql file and also put in the sql server info. You can read more about sqlcmd HERE. Also note the case on the switches -S and -i as it does matter.
sqlcmd -S <ComputerName>\<InstanceName> -i C:\test.sql
I am trying to downgrade a SQL Server 2014 database to a lower version (SQL Server 2012) by using the task Generating scripts found when right clicked on the database.
After I make the settings in order to generate the script, the server does that but when I'm trying to open the file (the script made) on a lower version instance of SQL Server, I get the following error.
System out of memory exception thrown
Could anyone provide some help? Thanks!
As suggested by #usr you can run the script from the command line using sqlcmd:
sqlcmd -S myServer\instanceName -i C:\myScript.sql
Alternatively you can download a trial edition of ApexSQL or Redgate SQL Compare (assuming you have not tried them already) and script over the changes using these tools.
The script is too big for SSMS (a shame!). Run it using SQL Server command line tools.
I find it easier to do this by using Redgate SQL Packager or the two compare tools. They can execute enormous scripts.
While installing with my NSIS installer script I need to run some script on SQL Server: select, update, create and insert.
How can I do that without having a SQL Server engine on the computer running the NSIS installer?
I thought about packing a SQL Server Compact Edition to my installer in order to use it to connect to the SQL Server. Is that the way I should go for?
You don't need the SQL Server engine to perform queries on a remote machine, you need a driver.
One approach is to use the command line client, which also requires the native driver. You probably want to bundle or search for the driver your application uses in your installer.
In SQL Server 2005 or later the command line client is called sqlcmd. It can be downloaded from the feature pack download pages (2005|2008|2008R2|2012).
So its simply a matter of bundling a SQL script with the installer and executing the script by calling sqlcmd with ExecWait.
You can run a script using a trusted connection via:
sqlcmd -S _SERVER\_INSTANCE_ -d _DBNAME_ -i _SCRIPT_FILE_
Or with a SQL Login:
sqlcmd -S _SERVER\_INSTANCE_ -d _DBNAME_ -U _USERNAME_ -P _PASSWORD_ -i _SCRIPT_FILE_
A SQL 2000 version of this approach can be found here on the nsis wiki.
I found a Plugin for NSIS that can connect to a MS SQL database: MSSQL_OLEDB_plug-in
It can be used like that:
${OLEDB}::SQL_Logon "$SQLSERVER" "$SQLUSER" "$SQLPASSWORD"
${OLEDB}::SQL_Execute "$SQLQUERY"
${OLEDB}::SQL_GetError
${OLEDB}::SQL_GetRow
Pop $0
DetailPrint $0
Pop $0
DetailPrint $0
${OLEDB}::SQL_Logout