53

I have been following this tutorial: http://www.misfitgeek.com/2010/07/adding-asp-net-membership-to-your-own-database/

I have installed SQL Server Management Studio Express from here: http://www.microsoft.com/download/en/details.aspx?id=8961.

1) How can I locate my database?

2) How do I run the SQL script on my database from external file?

5
  • Have you installed the actual database? SQL Server Management Studio is only a client tool. You probably want to install SQL Server (Express Edition is free) as well. Commented Apr 21, 2012 at 19:53
  • I have SQL Server Express installed as well. Commented Apr 21, 2012 at 21:07
  • Please refer to the youtube.com/watch?v=olgJOG70-vg for running sql scripts through SQL server management studio Commented Sep 16, 2014 at 12:58
  • 1
    sqlcmd -S . -U sa -P Secure@1234 -i db.sql ---> used in Linux Commented Nov 22, 2017 at 1:42
  • not useful if we do not have credentials. Commented Oct 16, 2022 at 20:45

4 Answers 4

83

This website has a concise tutorial on how to use SQL Server Management Studio. As you will see you can open a "Query Window", paste your script and run it. It does not allow you to execute scripts by using the file path. However, you can do this easily by using the command line (cmd.exe):

sqlcmd -S .\SQLExpress -i SqlScript.sql

Where SqlScript.sql is the script file name located at the current directory. See this Microsoft page for more examples

Sign up to request clarification or add additional context in comments.

8 Comments

‘you can open a "Query Window", paste your script and run it’ – I might be missing something, but if a script is saved in a file, you can open the file directly with SSMS, no need to open it elsewhere, copy the text and paste it in a query window.
You are correct. You may open the file directly and run one file at the time. The "sqlcmd" tip may come in handy if you need to execute a .sql file (script) that calls other .sql files.
And if your file is 300MB in size, for instance, SSMS will output an error trying to open it.
@marquito: I wasn't aware of such a limitation to SSMS, thanks by the way. Anyway, I was merely addressing the need to copy&paste the contents of an already existing file to a query window. If SSMS can choke on a 300MB file, I guess it would also choke on a 300MB-worth piece of text in a query window as well, or am I wrong?
@marquito You should never be in a situation where your script is 300MB+ if you are than your script must have seed data in it. The best practice here is to break your scripts up with SQL commands in *.sql files and import data from *.csv, *.txt, *.xml data files etc. here is one of many ways you can do this support.discountasp.net/kb/a1179/…
|
29

Open SQL Server Management Studio > File > Open > File > Choose your .sql file (the one that contains your script) > Press Open > the file will be opened within SQL Server Management Studio, Now all what you need to do is to press Execute button.

3 Comments

I actually think this is a better answer because the asker specified "using SQL Server Management Studio"
Wonderful. Worked with SSMS 17 and a 100mb .sql file
Remember to set to the SQLCMD mode in the query menu
5

Found this in another thread that helped me: Use xp_cmdshell and sqlcmd Is it possible to execute a text file from SQL query? - by Gulzar Nazim

EXEC xp_cmdshell  'sqlcmd -S ' + @DBServerName + ' -d  ' + @DBName + ' -i ' + @FilePathName

1 Comment

This should be the accepted answer, and is probably the reason there is no stored procedure for this. sqlcmd mode does not accept -i somefile. The preferred way is actual sqlcmd from a script or actual command line cmd shell.
1

You can also use the SQLCMD option in Management Studio.

With a blank SQL window, go to Query menu, select SQLCMD Mode, and run your script using the following syntax:

set nocount on
:r <path to your file>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.