63

I am using SQL Server 2008 R2, I have a script to update the DB, that script is approx 50 MB in size and contains some about 800,000 lines.

Error:

TITLE: Microsoft SQL Server Management Studio

Cannot execute script.

ADDITIONAL INFORMATION:

Insufficient memory to continue the execution of the program. (mscorlib)

Can somebody please help me to run this script without getting this error?

Nikola K.
  • 7,093
  • 13
  • 31
  • 39
Vikrant More
  • 5,182
  • 23
  • 58
  • 90

6 Answers6

95

use the command-line tool SQLCMD which is much leaner on memory. It is as simple as:

SQLCMD -d <database-name> -i filename.sql

You need valid credentials to access your SQL Server instance or even to access a database

rball
  • 6,925
  • 7
  • 49
  • 77
62

Adding to @user1293068's answer, I needed to specify the instance name. Here is my full syntax:

sqlcmd -S <ComputerName>\<InstanceName> -d <DatabaseName> -i <MyScript.sql>

This is documented on Technet's Use the sqlcmd Utility article.

(Note that you must enter a switch value of "-S", not "-s". The switch is case-sensitive.)

Duncan C
  • 128,072
  • 22
  • 173
  • 272
Chris Schiffhauer
  • 17,102
  • 15
  • 79
  • 88
  • 2
    example of what I used --> `C:\Users\tstick\Documents>sqlcmd -S server.subdomain.companyname.com,4184 -d ebimap -i SQLscript.sql` – Tom Stickel Jun 15 '16 at 20:31
  • 2
    It looks like the default instance name (MSSQLSERVER) needs to be excluded to connect, e.g. `sqlcmd -S localhost -d db` and not `sqlcmd -S localhost\MSSQLSERVER -d db` – BurnsBA Aug 10 '16 at 14:01
  • +1 I also needed to add `-x` because my script contains `$(` - from http://stackoverflow.com/a/12301822/36036 – Aximili Oct 31 '16 at 10:32
  • Extra +1 for pointing out the switch is case sensitive – Karsten Dec 09 '20 at 13:44
16

If credentials are required

sqlcmd -S <ComputerName>\<InstanceName> -U <username> -P <password> -d <DatabaseName> -i <MyScript.sql>
Jude
  • 2,353
  • 10
  • 46
  • 70
14

You can also try increasing the Maximum Server Memory value in server properties.
To edit this setting, right click on server name and select Properties > Memory tab.

I encountered this error trying to execute a 30MB SQL script in SSMS 2012.
After increasing the value from 1024MB to 2048MB I was able to run the script.

Hakan Fıstık
  • 16,800
  • 14
  • 110
  • 131
dstetsenko
  • 521
  • 4
  • 5
6

Ok, none of the answers were sufficient to get my script successfully restoring.

So:

  1. Ensure that your network account has sufficient permissions to access both SQL Server instance and the specific database you intend to restore.

  2. It's best that the database you intend to restore actually exists, if it was merely generated from Generate Scripts (with modifications to include both Schema and Data)

  3. Run the command-line tool in Administrator mode (if necessary), e.g., type CMD from Run on the start menu

  4. In the command-line tool, type something like "SQLCMD -d CMS -i C:\Carnotaurus\Data\script.sql". Here, CMS is the name of the database I intend to restore and "C:\Carnotaurus\Data\script.sql" is the full file path of the restore script that was generated by the Generate Scripts in SMSS.

I hope this clears-up a few missing steps.

ISeeSharp
  • 315
  • 1
  • 9
Phil C
  • 3,687
  • 4
  • 29
  • 51
1

Open command Prompt

sqlcmd -S servername -i C:\Users\mycomputer\Downloads\script\yourscript.sql -d datebasename