How to import .sql file into SQL Server Express - sql-server

I have a plain sql file with some SQL INSERT statements.
Is it possible to import it in my local SQL Server Express instance?

You can use Management Studio Express edition. You can download the latest version here - which will work against SQL Express 2005, 2008 and 2008 R2.
If you don't want to install SSMSE then you can use sqlcmd at a command prompt, e.g. something like this (assuming Windows auth and an instance called "SQLEXPRESS"):
sqlcmd -S .\SQLEXPRESS -E -i "C:\path\file.sql"

The easiest way would be simply open the file in the Sql Management Studio and run it. Since the target table is already created, of course.

You can open it via Query analyser and run

Here is the tool Sql_Server_Script_Executor
You can add single/multiple file/folder and your files will comes up in the list. Click the execute button and done
It contains three transaction modes.
1. Execute scripts within one transaction
2. Execute scripts on separate transaction
3. No transaction

All you have to do is open Microsoft SQL Server 2008 Management Studio.
Then use File -> Open.
Open the file from the proper location and you'll get all the SQL statements there. After that you can execute them.
Hope this helps.

Related

SQL backup file made with MSSMS is not working... on MSSMS (Microsoft SQL Server Management Studio)

I Generated an SQL File to backup a database using MSSMS:
Right click on database
Generate scripts
Marked "Schema and data" on "Types of data to script"
As a result I got a complete SQL that effectively seems to contain all the needed information to rebuild my db from scratch, data included.
To test it, I tried to run it locally, and found myself stuck because MSSMS is throwing errors on the SQL Script that it itself made.... I does not make any sense :(
Am I missing sth?:
This is the first lines of the sql file just as MSSMS made it:
Any ideas?
EDIT 1
This is the error I get after removing all "ALTER DATABASE" (I assumed those were the configuration specifications that were causing trouble)
It now says the tables are already created though they are not present in the database (it did create the database tough, It's a progress!)
The script you are trying to execute contains elements that are not valid because the edition of SQL you are running locally is not the same as the edition of SQL used to generate the script.
In your case, the script was generated from azure SQL, but you are running it on sql server express edition (localdb)
Remove the azure SQL specific elements and it will work.
The default scripting option in SSMS object explorer is the same database engine type and edition as the source database. It seems the source database is Azure SQL Database based on the CREATE DATABASE syntax so the script will not run against a stand-alone instance like your LocalDB instance.
You can either modify the script or generate a new script appropriate for your target instance. To generate a script for SQL Express LocalDB, click on the Advanced button on the Set Scripting Options page of the Generate Scripts wizard. Select these options:
Script for the database engine edition: Microsoft SQL Server Express Edition
Script for the database engine type: Stand-alone instance

Migrate a SQL Server database to a lower version

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.

Is there some way I can update the statistics on my SQL Server 2012 database?

I don't have the management studio installed and cannot install the express version as there seems to be a problem with my computer set up.
However I have VS2013 and I can open up query windows.
I created Indexes but then since then I have added a lot of new data. Is there some way I can update statistics for my SQL Server 2012 database from the SQL Script command line?
Try EXEC SP_updatestats to update all database statistics.
You can access the SQL command line using the tool sqlcmd that is installed as part of SQL 2012.
Here's a link to the update statistics command that you'll need to put together and execute.
http://technet.microsoft.com/en-us/library/ms187348.aspx
You can always create a script for the update statistics command, then execute that.
If you have both SQL and SQL Express installed, please make sure that you are connecting to the correct instance of SQL Server. the machine name alone or localhost should take you to the full sql 2012, (the default instance name for this is MSSQLSERVER) and the SQL Express should have an instance name of SQLEXPRESS (if I remember correctly) you'll need to use the naming convention server\instance to make the connection (you probably knew this bit already)
Try this
EXEC sp_updatestats;
MSDN

How to RESTORE a BACKUP from DB with different versions? [duplicate]

How to restore a higher version SQL Server database backup file onto a lower version SQL Server?
Using SQL Server 2008 R2 (10.50.1600), I made a backup file and now I want to restore it on my live server's SQL Server 2008 (10.00.1600).
When I tried to restore the backup onto SQL Server 2008 it gives an error i.e. Restore Failed because:
The database was backed up on a server running version 10.50.1600.
That version is incompatible with this server, which is running version
10.00.1600.
How do I restore the backup file on this server?
You can use functionality called Export Data-Tier Application which generates .bacpac file consisting database schema and data.
On destination server, you can use Import Data-Tier Application option which creates and populates new database from pre-created .bacpac file
If you want just to transfer database schema, you can use Extract Data-Tier Application for creating file and Deploy Data-Tier Application for deploying created database schema.
I've tried this process on different versions of SQL Server from SQL 2014 to SQL 2012 and from SQL 2014 to SQL 2008R2 and worked well.
No, is not possible to downgrade a database. 10.50.1600 is the SQL Server 2008 R2 version. There is absolutely no way you can restore or attach this database to the SQL Server 2008 instance you are trying to restore on (10.00.1600 is SQL Server 2008). Your only options are:
upgrade this instance to SQL Server 2008 R2 or
restore the backup you have on a SQL Server 2008 R2 instance, export all the data and import it on a SQL Server 2008 database.
You can not restore database (or attach) created in the upper version into lower version. The only way is to create a script for all objects and use the script to generate database.
select "Schema and Data" - if you want to Take both the
things in to the Backup script file select Schema Only -
if only schema is needed.
Yes, now you have done with the Create Script with Schema and Data of the Database.
Will not necessarily work
Backup / Restore - will not work when the target is an earlier MS SQL version.
Copy Database - will not work when the target is SQL Server Express: "The destination server cannot be a SQL Server 2005 or later Express instance."
Data import - Will not copy the schema.
Will work
Script generation - Tasks -> Generate Scripts. Make sure you set the desired target SQL Server version on the Set Scripting Options -> Advanced page. You can also choose there whether to copy schema, data, or both. Note that in the generated script, you may need to change the DATA folder for the mdf/ldf files if moving from non-express to express or vice versa.
Microsoft SQL Server Database Publishing Services - comes with SQL Server 2005 and above, I think. Download the latest version from here. Prerequisites: sqlncli.msi/sqlncli_x64.msi/sqlncli_ia64.msi, SQLServer2005_XMO.msi/SQLServer2005_XMO_x64.msi/SQLServer2005_XMO_ia64.msi (download here).
Here are my 2 cents on different options for completing this:
Third party tools: Probably the easiest way to get the job done is to create an empty database on lower version and then use third party tools to read the backup and synchronize new newly created database with the backup.
Red gate is one of the most popular but there are many others like ApexSQL Diff , ApexSQL Data Diff, Adept SQL, Idera …. All of these are premium tools but you can get the job done in trial mode ;)
Generating scripts: as others already mentioned you can always script structure and data using SSMS but you need to take into consideration the order of execution. By default object scripts are not ordered correctly and you’ll have to take care of the dependencies. This may be an issue if database is big and has a lot of objects.
Import and export wizard: This is not an ideal solution as it will not restore all objects but only data tables but you can consider it for quick and dirty fixes when it’s needed.
Another way to do this is to use "Copy Database" feature:
Find by right clicking the source database > "Tasks" > "Copy Database".
You can copy the database to a lower version of SQL Server Instance. This worked for me from a SQL Server 2008 R2 (SP1) - 10.50.2789.0 to Microsoft SQL Server 2008 (SP2) - 10.0.3798.0
You can try this.
Create a Database onto SQL Server 2008.
Using Import Data feature import data from SQL Server R2 (or any higher version).
use "RedGate SQLCompare" to synchronize script.
Go to Task->Generate Scripts...
In Advanced in "Types of data for script" select "Schema and data" and try to run this script in your lower version.
It's not pretty, but this is how I did it granted you have this option installed on your SQL 2008 R2 install..
1) Right click database in SQL Server 2008 R2 "Tasks".. "Generate scripts" in the wizard, select the entire database and objects in first step. On the "Set Scripting Options" step you should see a button "Advanced" , select this and make sure you select "Script for Server Version" = SQL Server 2008" not R2 version.
This is a crucial step, because "import data" by itself does not bring along all the primary keys, constriants and any other objects like stored procedures."
2) Run the SQL script generated on the new install or database instance SQL Express or SQL Server 2008 using the query window or open saved .sql script and execute and you should see the new database.
3) Now right click on the new database and select "Tasks".. "Import Data.." choose source as the R2 database and the destination as the new database. "Copy data from one or more tables or views", select the top checkbox to select all tables and then next step, run the package and you should have everything on a older version. This should work for going back to a 2005 version as well. Hope this helps someone out.
you can use BCP in and out for small tables.
BCP OUT command:-
BCP "SELECT * FROM [Dinesh].[dbo].[Invoices]" QUERYOUT C:\av\Invoices1.txt -S MC0XENTC -T -c -r c:\error.csv
BCP IN command:-
Create table structure for Invoicescopy1.
BCP [Dinesh].[dbo].[Invoicescopy1] IN C:\av\Invoices.txt -S MC0XENTC -T -c
I appreciate this is an old post, but it may be useful for people to know that the Azure Migration Wizard (available on Codeplex - can't link to is as Codeplex is at the moment I'm typing this) will do this easily.
You'd have to use the Import/Export wizards in SSMS to migrate everything
There is no "downgrade" possible using backup/restore or detach/attach. Therefore what you have to do is:
Backup the database from the server running the new SSMS/SQL version.
Import data from the generated .bak file, by expanding the "Tasks" menu(after right-clicking the target database) and selecting the "Import Data" option.
You can generate script from Task menu
For detailed reference
How to migrate a SQL Server database to a lower version

Import SQL server database using batch

I need to import a database into a SQL Server instance using a batch script and the database is provided by an sql file.
How can I do This?
The SQL file was generated by the SQL Server management studio.
SQL Server has a command line utility called SQLCMD. It will let you do things like run scripts or restore a database backup. The -i parameter allows you to specify an input file.
There is also an article here that has a quick intro to SQLCMD.

Resources