Restoring .bak file from batch file - batch-file

I am automating a test environment setup process and below are part of the manual steps involved:
Copy a .bak file from a network drive to my D: drive
Launch SQL Management Studio, use the Restore Files and Filegroups option to restore the .bak file
For point #1, I used:
robocopy \\10.xx.xxx.x\dirA\Build\%myVar%\DB\ D:\ myDB_V%myVar%.bak
It copied just fine!
Now for point 2, I am trying the below command using sqlcmd from command prompt:
sqlcmd -E -S localhost\myInstance -Q "RESTORE DATABASE myDB_V2.2.2.10 FROM DISK='D:\myDB_V2.2.2.10.bak' WITH FILE = 1, MOVE 'myDB_V2.2.2.10' TO 'C:\Program Files\Microsoft SQLServer\MSSQL10.SQLEXPRESS\MSSQL\DATA\myDB_V2.2.2.10.mdf', MOVE 'myDB_V2.2.2.10_log' TO 'C:\Program Files\Microsoft SQLServer\MSSQL10.SQLEXPRESS\MSSQL\DATA\myDB_V2.2.2.10.mdf';"
It says
Msg 102, Level 15, State 1, Server localhost\myInstance, Line 1
Incorrect syntax near '.2'.
Msg 319, Level 15, State 1, Server localhost\myInstance, Line 1
Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.
I could not interpret what it is saying.
Can anyone help me please?

Try this:
sqlcmd -E -S localhost\myInstance -Q "RESTORE DATABASE 'myDB_V2.2.2.10' FROM DISK='D:\myDB_V2.2.2.10.bak' WITH FILE = 1, MOVE 'myDB_V2.2.2.10' TO 'C:\Program Files\Microsoft SQLServer\MSSQL10.SQLEXPRESS\MSSQL\DATA\myDB_V2.2.2.10.mdf', MOVE 'myDB_V2.2.2.10_log' TO 'C:\Program Files\Microsoft SQLServer\MSSQL10.SQLEXPRESS\MSSQL\DATA\myDB_V2.2.2.10.mdf';"

Try this
sqlcmd -E -S localhost\myInstance -Q "RESTORE DATABASE [myDB_V2.2.2.10] FROM DISK='D:\myDB_V2.2.2.10.bak' WITH FILE = 1, MOVE 'myDB_V2.2.2.10' TO 'C:\Program Files\Microsoft SQLServer\MSSQL10.SQLEXPRESS\MSSQL\DATA\myDB_V2.2.2.10.mdf', MOVE 'myDB_V2.2.2.10_log' TO 'C:\Program Files\Microsoft SQLServer\MSSQL10.SQLEXPRESS\MSSQL\DATA\myDB_V2.2.2.10.mdf';"

It looks like the error is about your database name, "RESTORE DATABASE myDB_V2**.2**.2.10 [...]". I would suggest not using periods in your database name as periods are used to separate database objects in queries. If you are already using periods, try wrapping the database name in square brackets [].

Related

How to restore database when files are claimed?

I have to restore a database and am following this official documentation where I follow two steps:
- List the files
- Run the Restore command with respect to the files aforementioned.
However, I am facing "already claimed" error.
I tried to use different names but it is not possible since the backup has certain files. I also tried other answers across different domains, all have GUI.
The first command that I ran was:
sudo docker exec -it sql1 /opt/mssql-tools/bin/sqlcmd -S localhost \
-U SA -P '<YourStrong#Passw0rd>' \
-Q 'RESTORE FILELISTONLY FROM DISK = "/var/opt/mssql/backup/us_national_statistics.bak"' \
| tr -s ' ' | cut -d ' ' -f 1-2
I got the following output:
LogicalName PhysicalName
-------------------------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
us_national_statistics C:\Program
us_national_statistics_log C:\Program
Then, as per the documentation, I ran this command:
sudo docker exec -it sql1 /opt/mssql-tools/bin/sqlcmd \
-S localhost -U SA -P '<YourStrong#Passw0rd>' \
-Q 'RESTORE DATABASE US_NATIONAL FROM DISK = "/var/opt/mssql/backup/us_national_statistics.bak" WITH MOVE "us_national_statistics" TO "C:\Program", MOVE "us_national_statistics_log" TO "C:\Program"'
Here, I get the following error:
Msg 3176, Level 16, State 1, Server 0a6a6aac7476, Line 1
File 'C:\Program\New' is claimed by 'us_national_statistics_log'(2) and 'us_national_statistics'(1). The WITH MOVE clause can be used to relocate one or more files.
Msg 3013, Level 16, State 1, Server 0a6a6aac7476, Line 1
RESTORE DATABASE is terminating abnormally.
I expect the database to be restored.
You can't restore to C:\Program for multiple reasons. That's not a full path (you seem to have lost the string after the first space in Program Files); the data and log can't both be put in the same file; you don't typically have write access to the root of any drive; and C:\ is not valid in Docker or Linux.
You need the LogicalName, but you should not be using the PhysicalName directly, either in the case where you are restoring to Docker or Linux, or in the case where you are restoring a database alongside an existing copy that you want to keep, or in the case where you are restoring a database to a different instance (which will more than likely have a different data folder structure).
Try:
RESTORE DATABASE US_NATIONAL_COPY
FROM DISK = "/var/opt/mssql/backup/us_national_statistics.bak"
WITH REPLACE, RECOVERY,
MOVE "us_national_statistics" TO "/var/opt/mssql/data/usns_copy.mdf",
MOVE "us_national_statistics_log" TO "/var/opt/mssql/data/usns_copy.ldf";

Backup MSSQL DB Schema

I'm trying to back up my production database to my local dev machine with the following constraints:
It will be a regular backup, so using the UI should (ideally?) be avoided.
Some tables are marked to be deleted, so these should not be included in the backup.
I would like to be able to pass the solution (file/package/etc) to other members of the team and they should only have to change a couple of variables in one file and then they can execute and get their own backup.
The DB is over 100GB and contains data that I won't need. I have identified the top largest tables and would only like to take say 5k rows from each - this should provide me with enough data for my purposes and limit space used on my local drives.
I have tried beginning with backing up the schema only using the following methods:
Using the UI to backup Schema only (Tasks -> Generate Scripts)
Get the following error:
Microsoft.SqlServer.Management.Smo.FailedOperationException: Discover dependencies failed. ---> System.ArgumentException: Item has already been added. Key in dictionary: 'Server[#Name='PRODSQL']/Database[#Name='Database1']/UnresolvedEntity[#Name='SomeObjectName' and #Schema='Some.Schema.SomeObjectName']' Key being added: 'Server[#Name='PRODSQL']/Database[#Name='Database1']/UnresolvedEntity[#Name='SomeObjectName' and #Schema='Some.Schema.SomeObjectName']' at System.Collections.SortedList.Add(Object key, Object value) at Microsoft.SqlServer.Management.Smo.DependencyTree..ctor(Urn[] urns, DependencyChainCollection dependencies, Boolean fParents, Server server) at Microsoft.SqlServer.Management.Smo.DependencyWalker.DiscoverDependencies(Urn[] urns, Boolean parents) --- End of inner exception stack trace --- at Microsoft.SqlServer.Management.SqlScriptPublish.GeneratePublishPage.worker_DoWork(Object sender, DoWorkEventArgs e) at System.ComponentModel.BackgroundWorker.OnDoWork(DoWorkEventArgs e) at System.ComponentModel.BackgroundWorker.WorkerThreadStart(Object argument)
So I moved on.
Tasks -> Copy Database
I get a message saying there is not enough space on disk
Extract Data-tier Application
Get same error as in 1. above.
Powershell script, and a batch file calling sqlcmd on the generated .sql files after the PS script was run.
I was sure this method would work and it took me 2 days to get this far, but still working through multiple errors.
Basically I am doing the following:
Create db objects from the source DB (Schemas, SPs, Tables, Views, UDFs, Triggers, Indexes) and output them to .sql files - Roughly followed http://cfmumbojumbo.com/index.cfm/coding/using-powershell-to-backup-your-stored-procedures-and-triggers/ with some more work added.
If the database already exists on my server, kill, drop, then recreate it (DropCreate.sql):
IF(db_id(#DatabaseName) IS NOT NULL)
BEGIN
DECLARE #SQL VARCHAR(max)
SELECT #SQL = COALESCE(#SQL,'') + 'Kill ' + Convert(VARCHAR, SPId) + ';'
FROM MASTER..SysProcesses
WHERE DBId = DB_ID(#DatabaseName) AND SPId <> ##SPId
EXEC(#SQL);
END
DROP DATABASE MYDATABASE
CREATE DATABASE MYDATABASE ON PRIMARY (...)
The .bat file is essentially doing this
sqlcmd -S %Server% -U %UserName% -P %Password% -i C:\Database\DatabaseScripts\TestDatabase\DropCreateDB.sql
#loop through and execute multiple .sql files in the directory
for /f %f in (`dir /b C:\Database\DatabaseScripts\TestDatabase\2018-04-18-15-13-13\StoredProcedures\`) do sqlcmd -S %Server% -d %Database% -U %UserName% -P %Password% -i %f
#Just one sql file in this directory, execute it
sqlcmd -S %Server% -d %Database% -U %UserName% -P %Password% -i C:\Database\DatabaseScripts\TestDatabase\2018-04-18-15-13-13\Schemas\AllSchemas.sql
sqlcmd -S %Server% -d %Database% -U %UserName% -P %Password% -i C:\Database\DatabaseScripts\TestDatabase\2018-04-18-15-13-13\Tables\AllTables.sql
.............
The latest error I'm experiencing is:
Changed database context to 'master'.
Msg 6107, Level 14, State 1, Server MYSERVER, Line 1
Only user processes can be killed.
do was unexpected at this time.
Everywhere I turn I am experiencing new errors and have spent over 2 days on it, and I haven't even got to getting the data yet..
TLDR: Is there any easier way backup MSSQL Db schema and top n rows of data from certain tables?

Populating ms sql with sql files

I already have an existing database I am trying to keep up to date on a daily basis. I get a daily dump of sql files. The batch script below created and populated the database the first time I run it, but that doesn't work when I try to update the database with it.
`#echo off
ECHO %USERNAME% started the batch process at %TIME% >output.txt
for %%f in (*.sql) do (
sqlcmd.exe -S servername -E -d DatabaseName -i %%f >>output.txt
)
pause`
Is there a different command for updating a database with sql files?
This is the output i get.
HUTRC started the batch process at 9:55:12.25 Changed database context to 'master'. Msg 15416, Level 16, State 1, Server HUTRC1-HP, Procedure sp_dbcmptlevel, Line 67 Usage: sp_dbcmptlevel [dbname [, compatibilitylevel]] Valid values of the database compatibility level are 100, 110, or 120.
What the sql file looks like. It is very long. I just got the first few lines.
USE [master]
GO
IF NOT EXISTS (SELECT [name] FROM sys.databases WHERE name = N'Migration')
BEGIN
CREATE DATABASE [Migration] COLLATE SQL_Latin1_General_CP1_CI_AS
END
GO
EXEC dbo.sp_dbcmptlevel #dbname=N'Migration', #new_cmptlevel=90
The error message tells you precisely where to look for the problem and what it is (emphasis added):
Procedure sp_dbcmptlevel, Line 67 Usage: sp_dbcmptlevel [dbname [, compatibilitylevel]] Valid values of the database compatibility level are 100, 110, or 120.
Examining your SQL script for sp_dbcmptlevel shows that it uses a different value:
EXEC dbo.sp_dbcmptlevel #dbname=N'Migration', #new_cmptlevel=90
^^
You'll need to either edit the SQL script to a valid compatibility level, or downgrade your server version to the same version as the source server.

BCP utility incorrect syntax

What is incorrect about this syntax?
bcp transitschedule in calendar_dates.txt -T -f calenar_dates.fmt -F 2
I have tried this through sqlcmd and SSMS to a database on Azure. When I run the command I get:
Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'in'.
Yet, the examples here are not very much different syntactically. If I add database name or schema name error shifts towards the period.
Like SQLCMD, BCP is a stand-alone command-prompt utility that must be invoked from a command prompt. If the source text and format files reside on your client, you'll need to add the -S -U and -P parameters like you do with SQLCMD. For example:
bcp databasename.schemaname.transitschedule in calendar_dates.txt -f calenar_dates.fmt -F 2 /S azure-database-server /U azure-database_login /P azure-database-password

r: "$(fileName)" in a script called from sqlcmd

I'm trying to create a script just to run other scripts and do some extra stuff in case of successful or failure.
You have the full code on this link just to try to be clear what I'm trying to achieve.
Basically I want to:
-- DO SOME STUFF HERE
r: "$(fileName)"
-- MORE STUFF HERE
and call it from sqlcmd this way:
sqlcmd -i "RunScript.sql" -v fileName="someFileName.sql" -s server -d database
But I can't, I'm getting the following error:
Msg 102, Level 15, State 1, Server SERVER, Line 19 Incorrect syntax
near 'someFileName.sql'.
So, it seems that the little r: couldn't be used with a parameter on his side.
Just to clarify, someFileName.sql isn't in the SQL Server, but in my machine, so I couldn't use this way to read the file. In fact, I just tried it later.
Is there a workaround to archive this? Any ideas to solve it?
You could break your current RunScript.sql script into separate header and footer SQL scripts and concatenate them together with a SQL script in the middle that is denoted by an input parameter to a CMD script. For example:
RunSQL.CMD consists of:
#ECHO OFF
COPY /V /Y RunScriptHeader.sql + %1 + RunScriptFooter.sql RunScriptTemp.sql
SQLCMD -i "RunScriptTemp.sql" -s server -d database
The /V does a verify on the copy
The /Y suppresses prompting to confirm overwriting an existing file
You would run it as follows:
RunSQL.CMD someFileName.sql

Resources