SQL Server Management Studio command line? - sql

Is there a command line interface where I can run a script that says, for example:
Connect to server a
Run query 1
Connect to server b
Run query 2
Run query 3
I tried searching online but couldnt find anything built into SQL Server Management Studio.
Thanks!

The sqlcmd Utility
http://msdn.microsoft.com/en-us/library/ms162773.aspx
It usually ships with the SQL Server installer and is located in the following directory by default:
C:\Program Files\Microsoft SQL Server\100\Tools\Binn\SQLCMD.EXE

If you want to export in/out there is a command-line utility called BCP that will let you execute these types of processes.
bcp Utility
The bcp utility bulk copies data between an instance of Microsoft SQL
Server and a data file in a user-specified format. The bcp utility can
be used to import large numbers of new rows into SQL Server tables or
to export data out of tables into data files. Except when used with
the queryout option, the utility requires no knowledge of
Transact-SQL. To import data into a table, you must either use a
format file created for that table or understand the structure of the
table and the types of data that are valid for its columns.

Related

Insert data using powershell

We have a static sql file which consists of insert statements;basically test data.
is it possible to execute this script using powershell on azure sql db where alwaysencrypted is enabled. We use keyvault to store the certs.
Unfortunately, PowerShell (Invoke-SqlCmd) does not support insert statements against encrypted columns at this point. The only SQL tool from Microsoft that supports such statements at this point is SSMS - please see: https://learn.microsoft.com/en-us/sql/relational-databases/security/encryption/configure-always-encrypted-using-sql-server-management-studio#param .
An alternative could be to put your test data into a CSV file and use the Import Export Wizard to import the data into the database. You could save the import job as an SSIS package, which you could execute from the command line. Here is a blog article on using the I/E Wizard for importing (and encrypting) data from a database (importing from a file would be similar). https://blogs.msdn.microsoft.com/sqlsecurity/2015/10/31/ssms-encryption-wizard-enabling-always-encrypted-in-a-few-easy-steps/
Jakub

SQL Server 2012 : How to script all database stored procedures into separate .sql files?

I want to script all the stored procedures from SQL Server 2012 to Visual Studio 2012 as .sql files (in a different project). How do I do that? I want one .sql file for each stored procedure?
I get the scripts using the Generate Scripts in Tasks option after right clicking DB in SQL Server 2012. However, the name of the .sql file is spname.StoredProcedure in each case. I want the name to be spname.sql.
Again I don't want the file name to be database.spname.StoredProcedure, I want it to be just spname.sql
Please check following SQL tutorial showing how to generate a separate script file for each stored procedure in a SQL Server database
The solution uses sp_Helptext stored procedure with SQL BCP command for generating create script for target SP and then create the export .sql file on the file system.
I hope it helps,
Additionally on generate script wizard, if you configure selection options as seen in below picture, you will get the sp name as the output script file name
As #Ivan_Starostin said:
Step one
Step two
Step three

Inserting data into sql from csv files or insert data direct from remote computer into SQL Server

I have multiple remote machines on a network that have a csv file on them stored in the same location that has been created by a powershell script.
Whats the best way to insert that data into a Microsoft Sql Server express database?
One way of doing this is to collect all the CSV-files from all the servers to your script server. And then run the SQL INSERT query from the script server to put your data in an SQL table.
As described in one of my previous answers you can use this module Invoke-SQLCmd2 to do this.
The other way, when you don't want to collect all the CSV-files first, is running the SQL INSERT query from every server that has a CSV-file. To do this you have to connect to the other server and then import the module from the script server, so you can use it:
Import-Module -Name '\\SCRIPTSERVER\C$\Windows\system32\WindowsPowerShell\v1.0\Modules\Invoke-SQL'

Get .sql file from SQL Server 2012 database

I need to get .sql files from an existing database with SQL Server 2012. Is this possible because all I see in the export option is delimited files.
Ex.) I have table1 with 30 records. I need a file that would be something like what is below. (I know my syntax is incorrect, but you should get the point).
CREATE TABLE table1 (
INSERT INTO table1 values (
.......)
If you just want to generate a .sql script you can do this by right-clicking the database in Object Explorer and choosing Tasks > Generate Scripts:
Then you can select database and all objects, or you can select just the table you want:
Then select a location, and be sure to go into Advanced before continuing:
Under advanced, at the very least, change types of data to script to "Schema and Data" (if the reason you are doing this rather than a proper backup is because you need to import the data into a database earlier than SQL Server 2012, you should probably change the target server version option to whatever version you're targeting):
If you don't have that option in Management Studio, you are probably still running the RTM version of the client tools. Full functionality has finally been made free as of SP1. Download the most recent version - SP2 - here:
http://www.microsoft.com/en-us/download/details.aspx?id=43351
(You want one of the SQLManagementStudio files.)
You have all these alternatives to start the SQL Server Import and Export Wizard
On the Start menu, point to All Programs, point to Microsoft SQL Server, and then click Import and Export Data.
In SQL Server Data Tools (SSDT), right-click the SSIS Packages folder, and then click SSIS Import and Export Wizard.
In SQL Server Data Tools (SSDT), on the Project menu, click SSIS Import and Export Wizard.
In SQL Server Management Studio, connect to the Database Engine server type, expand Databases, right-click a database, point to Tasks, and then click Import Data or Export data.
In a command prompt window, run DTSWizard.exe, located in C:\Program Files\Microsoft SQL Server\100\DTS\Binn\ (or probably \110\ rather than \100\ in your case).
Saludos ;)
Right click on the database under Tasks click generate scripts and wizard will open and then you can export the table structure and data to another database.
You have to select the tables and other schema you want to export and on one of the pages is a check box to select export table data.
This will generate sql statements for your database.
This may not be available in the express version, I've not checked.

How do I import a sql data file into SQL Server?

I have a .sql file and I am trying to import it into SQL Server 2008. What is the proper way to do this?
If your file is a large file, 50MB+, then I recommend you use sqlcmd, the command line utility that comes bundled with SQL Server. It is easy to use and it handles large files well. I tried it yesterday with a 22GB file using the following command:
sqlcmd -S SERVERNAME\INSTANCE_NAME -i C:\path\mysqlfile.sql -o C:\path\output_file.txt
The command above assumes that your server name is SERVERNAME, that you SQL Server installation uses the instance name INSTANCE_NAME, and that windows auth is the default auth method. After execution output.txt will contain something like the following:
...
(1 rows affected)
Processed 100 total records
(1 rows affected)
Processed 200 total records
(1 rows affected)
Processed 300 total records
...
use readfileonline.com if you need to see the contents of huge files.
UPDATE
This link provides more command line options and details such as username and password:
https://dba.stackexchange.com/questions/44101/importing-sql-server-database-from-a-sql-file
If you are talking about an actual database (an mdf file) you would Attach it
.sql files are typically run using SQL Server Management Studio. They are basically saved SQL statements, so could be anything. You don't "import" them. More precisely, you "execute" them. Even though the script may indeed insert data.
Also, to expand on Jamie F's answer, don't run a SQL file against your database unless you know what it is doing. SQL scripts can be as dangerous as unchecked exe's
Start SQL Server Management Studio
Connect to your database
File > Open > File and pick your file
Execute it
Try this process -
Open the Query Analyzer
Start --> Programs --> MS SQL Server --> Query Analyzer
Once opened, connect to the database that you are wish running the script on.
Next, open the SQL file using File --> Open option. Select .sql file.
Once it is open, you can execute the file by pressing F5.
In order to import your .sql try the following steps
Start SQL Server Management Studio
Connect to your Database
Open the Query Editor
Drag and Drop your .sql File into the editor
Execute the import
A .sql file is a set of commands that can be executed against the SQL server.
Sometimes the .sql file will specify the database, other times you may need to specify this.
You should talk to your DBA or whoever is responsible for maintaining your databases. They will probably want to give the file a quick look. .sql files can do a lot of harm, even inadvertantly.
See the other answers if you want to plunge ahead.
Get the names of the server and database in SSMS:
Run the following command in PowerShell or CMD:
sqlcmd -S "[SERVER NAME]" -d [DATABASE NAME] -i .\[SCRIPT].sql
Here is a screenshot of what it might look like:
There is no such thing as importing in MS SQL. I understand what you mean. It is so simple. Whenever you get/have a something.SQL file, you should just double click and it will directly open in your MS SQL Studio.