How to take SQL Server 2012 schema + data dump using command prompt - sql-server-2012

I have a database in SQL Server 2012. Now I am looking for some windows commands that will generate an .sql file for the schema[create statement] and another .sql file for the data[insert statement] of that database. I need to do this using command prompt. So I can't use the Generate Scripts option provided in SQL Server Management Studio.
Thanks in advance

The schema dump from command-line can be done using SQLPackage.exe. The command is -
"C:\Program Files (x86)\Microsoft SQL Server\110\DAC\bin\sqlpackage.exe" /action:Extract /tf:<drive:\folder_name\file_name.dacpac> /SourceConnectionString:"Data Source=<server_name>;Initial Catalog=<db_name>;Integrated Security=SSPI;Persist Security Info=False; /su:<user_name> /sp=<password>
If using windows authentication then input username = password = space. The file paths that contain spaces must be enclosed in quotation marks.
See https://www.katieandemil.com/sqlpackage-exe-extract-dacpac-command-line?tab=article for more details.

Related

Restore BacPac database backup in sql server 2014

I have database backup file with extension .bacpac so how can i restore it in sql server 2014 ?
Two options:
In SQL Server Management Studio, right-click on the Databases folder in the Object Explorer tree. Then click on "Import Data-tier application". The wizard will guide you through the process.
From the command line, run this command: "c:\program files (x86)\Microsoft sql server\120\dac\bin\sqlpackage.exe" /a:import /sf:"PATH_TO_YOUR_BACPAC_FILE" /tcs:"Data Source=YOUR_SERVER_NAME;Initial Catalog=NEW_DATABASE_NAME;Integrated Security=true"
Aside: Note that the example connection string in the command line option above assumes you're using Windows Authentication instead of SQL Server authentication to access the server.

SQL LocalDB - Cannot delete a DB when its files are deleted

How can I delete a SQL LocalDB database that has had its files delete?
Dropping the database yields this message:
Unable to open the physical file "C:\Users\Public\Documents\LocalDB.Tests.3d0d7339-7cf2-45fe-a83b-b5079112ab80.mdf". Operating system error 2: "2(The system cannot find the file specified.)".
File activation failure. The physical file name "C:\Users\Public\Documents\LocalDB.Tests.3d0d7339-7cf2-45fe-a83b-b5079112ab80_log.ldf" may be incorrect.
Running master.sp_databases actually doesn't show them, but the Management Studio does.
The problem is that you are trying to drop a database when the physical file has been deleted or couldn't be found.
To get around this you can detach the database. Detaching will drop the database without attempting to remove the file from the filesystem.
EXEC sp_detach_db 'My_Db'
I'm assuming you're using (localdb) bundled with SQL Server 2012.
If you're using SQL Server 2014, use (localdb)\MSSQLLocalDB in place of (localdb)\v11.0 below
Open a command prompt
Start the localDb instance if it is not already running: “C:\Program Files\Microsoft SQL Server\110\Tools\Binn\sqllocaldb.exe” start “v11.0″
Drop the localDb database by running the following command: “C:\Program Files\Microsoft SQL Server\110\Tools\Binn\sqlcmd” -S (localdb)\v11.0 -E -d master -Q “DROP DATABASE [myDatabase]”
You can stop the localDb service now: “C:\Program Files\Microsoft SQL Server\110\Tools\Binn\sqllocaldb.exe” stop “v11.0″
Source: http://kazimnami.azurewebsites.net/techblog/2013/02/27/delete-localdb-database-after-physical-files-have-been-deleted/

How can I execute SQL scripts using TeamCity?

I´m new with TeamCity and I don´t know how to run SQL scripts with it.
Is the way simply selecting the path of those scripts in a Command Line Build Runner ?
I´m pretty lost.
Regards.
In a command line build step:
Command executable: c:\Program Files\Microsoft SQL Server\100\Tools\Binn\sqlcmd.exe
Command parameters: -S <server> -i <path_to_file> <== Note: that's a capital -S!
You may need to change the 100 to something else, depending on the version of the SQL Server tools that you have installed on the build agent.
I believe that SQLCMD / SQLPLUS / MYSQL are available as standalone executables which you can install on the TeamCity server.
Microsoft® SQL Server® 2008 R2 Feature Pack
Oracle SQL Plus
MYSQL Command Line
However, without knowing your actual SQL Platform this may differ and the provider should have an alternative.
You can then create a Command Line Runner to call the executeable and pass in the parameters required, which are further explained here.
Using the sqlcmd Utility
Using sqlplus utility
If you are looking at doing Database Migrations as part of your CI process, it would also be worth checking out RoundhousE

failed to load resource file sqlcmd.rll

I have a sql script file which is 1.5GB
i want to run it but i cant use it using SQL Management Console, cause it can't load the file. I came up to the solution that run the query with SQLCMD utility but when i want to execute the file it returns (Failed to load resource file SQMCMD.rll)
sqlcmd -s (local) -i C:\myScript.sql
anyone knows what is the problem and how i can run this large script file ?
I had the same problem, and I have solved.
In my case the problem was I had installed more than one version of SQL (2005, 2008, 2012), and I had uninstalled the two first ones, so at this moment I only have the Sql Sever Express 2012.
After playing a lot of minutes I discovered that old installations left his respective sqlcmd.exe files in my machine, but the uninstall process removed the corresponding rll file.
When I hit in the cmd window the sqlcmd it seems the system tries to run the sqlcmd for one of the old sql installation.
Then I went to the folder of the installed version and that works.
It is (in my case, in your case you can find the file sqlcmd.exe):
1- cd C:\Program Files\Microsoft SQL Server\100\Tools\Binn
Inside that folder, then
2- SQLCMD -d databaseName -i C:\MyScripts\specificScript.sql
And that did the trick!
http://www.microsoft.com/downloads/en/details.aspx?FamilyID=228de03f-3b5a-428a-923f-58a033d316e1&displaylang=en
I'd download the sqlcmd installer and run it - see if that fixes you up.
It's under "Microsoft SQL Server 2008 Command Line Utilities".
The installer should create that file and you'll be set. You could also try to run osql, which is the older command line SQL utility.
Edit:
Try this:
Please check the permission on the sqlcmd.exe and sqlcmd.rll and make sure the user has the read&execute permission on them. You also could specify the full path of the sqlcmd.exe in the command, such as:
"C:\Program Files\Microsoft SQL Server\100\Tools\Binn\sqlcmd.exe" -L
I just had this problem on SQL Server 2008 R2. Reinstalling the Command Line Utilities from the Feature Pack is what did it. The trick is if you are on 32-bit only install the x86 package, but if you are on 64-bit install both the x86 and the x64 package. The reason for this being is that some SQL Server tools are only 32-bit which means that both C:\Program Files\Microsoft SQL Server\100\Tools\Binn and C:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn are in your PATH variable in order to ensure all of the tools are accessible, which means either of them could be run, therefore both of them need to reinstalled to ensure you have them run properly.
In my case the issue arised after migrating from SQL Server 2008 R2 to SQL Server 2012.
The problem was pretty simple. It was the path. Just correct your SQL Server path in the System path pointing to the new directory.
I got a variant of this error in an Linux installation of the MS SQL Server CLI for Linux, which stated,
# bcp
Unable to load BCP resource DLL. BCP cannot continue.
# sqlcmd
Unable to load SQLCMD resource file(s)
I found that I had not followed the installation direction exactly, in that rather than modify the system PATH to add the installation directory (/opt/mssql-tools/bin), I had used 'install' to copy the binaries to /usr/local/bin without the corresponding /opt/mssql-tools/share directory.
for I in /opt/mssql-tools/bin/*; do install $I /usr/local/bin; done
When I removed those copies and did export PATH="$PATH:/opt/mssql-tools/bin" as the instructions show, then it worked.
# bcp
usage: bcp {dbtable | query} {in | out | queryout | format} datafile
[...]
# sqlcmd
Microsoft (R) SQL Server Command Line Tool
Version 17.1.0000.1 Linux
[...]

SQL Server 2005 backup restore failing (with folder permissions)

I am trying to restore a database (from file thedb.bak). I am using SQL Server Express edition 2005 on a Windows 7 Ultimate 64-bit machine.
When I try to restore I get the following error:
System.Data.SqlClient.SqlError: The operating system returned the error '5(Access is denied.)' while attempting 'RestoreContainer::ValidateTargetForCreation' on 'C:\Program Files (x86)\Microsoft SQL Server\MSSQL.2\MSSQL\Thedb.MDF'. (Microsoft.SqlServer.Express.Smo)
My username (antoniocs) is an Administrator. I have edited the permissions in the folder (C:\Program Files (x86)\Microsoft SQL Server\MSSQL.2\MSSQL\) so that the user AntonioCS has full control.
I really need to restore this backup. What am I missing?
Note: I am using the Windows authentication to login. Should I try another user (the one I use is an administrator in the machine)?
The account running the SQL Server service requires permissions on that folder.
You may be connected to SQL Server, but actions are done in the service account context: not you.
Run services.msc from command line, see what account is used, permission this folder accordingly.
Do you have a database that uses Thedb.MDF? I ran into this error when I tried to restore a database over a file that SQL Server used. From the restore window, choose Options and change the path or the filename from the "Restore the database files as".