46

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?

Dennis van Gils
  • 3,487
  • 2
  • 14
  • 35
Anajrob
  • 527
  • 1
  • 6
  • 12

3 Answers3

74

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

cederlof
  • 7,206
  • 4
  • 45
  • 62
Ulises
  • 13,229
  • 5
  • 34
  • 50
  • 3
    *‘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. – Andriy M Apr 22 '12 at 19:55
  • 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. – Ulises Apr 22 '12 at 20:17
  • 36
    And if your file is 300MB in size, for instance, SSMS will output an error trying to open it. – marquito Mar 06 '13 at 13:33
  • 1
    @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? – Andriy M Mar 07 '14 at 05:59
  • @user2070775: I am not sure what you are asking. Are you just re-iterating marquito's point? – Andriy M Mar 07 '14 at 05:59
  • Well, I believe you won't even be able to paste it to the query window. If you succeed, though, come back here and tell us! =D – marquito Mar 07 '14 at 13:47
  • 5
    @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 https://support.discountasp.net/kb/a1179/how-to-import-a-csv-file-into-a-database-using-sql-server-management-studio.aspx – Nathan Feb 08 '17 at 14:16
  • This answer is useless for large scripts, what is wanted is TSQL-like ability. Many installations report OOM issues once the script is into the GB's. – mckenzm Oct 16 '22 at 20:47
27

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.

Maryam
  • 1,043
  • 1
  • 12
  • 16
2

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
James S
  • 21
  • 1
  • 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. – mckenzm Oct 16 '22 at 21:07