HOW to create SQL Server 2017 database scripts using sqlcmd - sql-server

I have a database schema sitting in SQL Server 2017 running on Ubuntu. I have a separate SQL Server 2008 R2. I need the database structure to migrate from 2017 -> 2008 R2 server and cannot find the right direction to start with. I had created the backup and tried to restore but it seems we cannot restore in that fashion. I think "create scripts" will do the job but I do not have management studio. All I have is sqlcmd running on the instance with 2017 version. So can I run "create scripts" to get the structure of the database and store in a single .sql file.?

Check this answer: How to use sqlcmd to create a database
Here you go:
sqlcmd -i C:\path\to\file.sql
More options can be found in SQL Server books online.

Related

Restore database in SQL Server from .bkp file [duplicate]

The database was backed up on a server running version 8.00.2039. That version is incompatible with this server, which is running version 11.00.2100. Either restore the database on a server that supports the backup, or use a backup that is compatible with this server. (Microsoft.SqlServer.SmoExtended)
I am trying to restore and I get this message.. is there anyway to restore this backup without having to install SQL Server 2005?
SQL Server 2012 (v11.0) doesn't support direct upgrade from SQL Server 2000 (v8.0).
You'll have to use a "in-between" step restoring your SQL Server 2000 database to a SQL Server 2005 or 2008 instance, and then do a backup from there, in order to get your .bak file into the latest database server.
There's no trick or workaround for this - that's just the way it is, and you'll have to deal with it.
There are tons of blog posts on this topic, e.g.
Migration SQL Server 2000 to SQL Server 2012

back up DB to an older version

I have two SQL Servers of different versions: 2014 and 2012. I cannot upgrade the oldest to the newest one. Can I back up a DB on 2014 so that I can restore it on 2012? I would like to restore both schema and data. I would also prefer to do it through backing up, not through scripting out. Right now I am getting an error saying something like "the versions are incompatible, and you cannot restore the DB". I appreciate your help.
You can't back up & restore the database from a higher (2014) to lower (2012) version.
You can generate a script from SQL Server 2014 (DB creation and data creation script) and you can run on SQL server 2012. Only scripting is an option in this scenario to create a database/restore a database from SQL server 2014 to 2012.
You can certainly take a back from SQL server 2012 and restore in the SQL server 2014.

Export SQL Server 2014 database (2008 compatible) in a backup file compatible with 2012

I'm a bit stuck with this scenario. My dev VM has SQL Server 2014 on it, and I've been working on a SQL Server 2008 database. The 2008 database is listed as '2008 compatible'. I now need to put this on a demo server which has an older edition of SQL Server (2012), and I find that 2008 compatible DBs don't create 2008 compatible backups (which I would have thought would be a given).
I'm actually trying to backup from SQL 2014 and recover into SQL Server 2012. How do I get a SQL Server 2008 or SQL Server 2012 compatible backup file from my 2008 compatible database?
Thanks.
You have to script the SQL database creation, in SQL Server Management Studio :
right click on the DB -> Task -> Generate script
From there a series off dialog to choose what to generate with the SQL command creation and where, in clipboard or file (recommanded for big DB), with optionally the data as some Insert Into SQL command. To find the data option click on the "Advanced" button and search for "Type of data to script"

SQL Server - Copy database from Server 2008 to Server 2012

I have two servers one in Denmark the other here in the UK. I need to make a copy of a database out of SQL Server Management 2008 (1st server) and place it in SQL Server Management 2012(2nd server). I have tried connecting to the remote server but have been getting difficulties so instead of connecting both up and doing it that way I was hoping there is a function like copy database which could do this for me. I have gone into tasks and copy database in SQL Server 2008 but in the Copy Database Wizard that appears it says "You can use this wizard to move or copy databases from an instance of SQL Server 2000 or later to an instance of SQL Server 2008". I'm looking for something like this that will work for Server 2012.
Copy your database file .mdf and log file .ldf and paste in your SQL Server
Install file in Programs Files->Microsoft SQL Server->MSSQL10.SQLEXPRESS->MSSQL->DATA.
Open SQL Server. Right-click on Databases then Select Attach... option.
You can create a backup with the Management Studio on the source and restore it on the target.
You need Management Studio 2012 to make this step. You can then create empty database on 2012 server. Right click on this empty DB->Tasks->Copy database..
Wizard will guide you. You should to this under 2012 Management Studio because it is compatible with 2008 Server - vice versa it will not work.
Of course, cleanest way is to do this via backup (.bak file - Tasks->Backup.. then on 2012 server Tasks->Restore->Files or FileGroups) but you need remote access (RDP) to the server.

How to migrate from SQL Server 2005 to SQL Server 2012

I have this scenario:
Old Server -> SQL Server 2005 with the current Database
New Server -> SQL Server 2012 no database created.
I want to migrate the old database to the new one.
I've tried to create a backup from the Management Studio 2005 and restore in the 2012. Failed.
I've connected from the 2012 server using Management Studio 2012 to the old server and create the backup. Then restored in the new one. Failed.
The error said that the versions are incompatibles (tried to restored a version from 8.0 to 11.0)
Then, I tried to use the Copy database Wizard. But a error message shows up:
The destination server can not be an instance of SQL Server 2005 Express or higher .
So, how it is supposed to do the backup?
I'm getting the scripts in sql files as last resort...
PS: I've check this thread, I need to migrate to 2008 and then 2012?
How to migrate a database from SQL Server 2005 to 2008 as is?
When I have performed a SQL Server migration in the past I have normally performed a SQL Backup, copy to the new server and then restore, I see in your question that you got an error, update the question with the error and perhaps we can be more specific about what you need to do to make it succeed.
I have also performed a migration by detaching the database from the old server, copying the data and log files to the new server and attaching the databases. This is the method I tend to go with.
The SQL you would use to attach the MDF/LDF files is as follows:
CREATE DATABASE MyAdventureWorks
ON (FILENAME = 'C:\MySQLServer\AdventureWorks_Data.mdf'),
(FILENAME = 'C:\MySQLServer\AdventureWorks_Log.ldf')
FOR ATTACH;
You can refer to the documentation at: https://msdn.microsoft.com/en-AU/library/ms190209(v=sql.110).aspx
Please apply latest service pack SP4 on 2005 databases after this take a full backup of 2005 databases and restore it on 2012. I hope, it will work.

Resources