简体   繁体   中英

Executing a SQL script using Windows Powershell

I have a "SQL" script that needs to be run through Windows Powershell in order to create a database. The script already has the drop and create function in so no need to add a database name at the end of the command line. when trying to execute the command line I get an error that says

"Invoke-Sqlcmd : A network-related or instance-specific error occurred while establishing a connection to SQL Server
The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 25 - Connection string is not valid)
At line:1 char:14
+ invoke-sqlcmd <<<<  -inputfile "c:\A.sql" -serverinstance "Louis_dev\MSSQLSERVER"
    + CategoryInfo          : InvalidOperation: (:) [Invoke-Sqlcmd], SqlException
    + FullyQualifiedErrorId : SqlExectionError,Microsoft.SqlServer.Management.PowerShell.GetScriptCommand

Invoke-Sqlcmd :
At line:1 char:14
+ invoke-sqlcmd <<<<  -inputfile "c:\A.sql" -serverinstance "Louis_dev\MSSQLSERVER"
    + CategoryInfo          : ParserError: (:) [Invoke-Sqlcmd], ParserException
    + FullyQualifiedErrorId : ExecutionFailureException,Microsoft.SqlServer.Management.PowerShell.GetScriptCommand"

not sure if it is the invoke command and I have already followed the steps in this link How to execute .sql file using powershell? . Is there by any chance that there could be a workaround from this error. SQLSERVER instance has been configured to LocalSystem and not to Network instance

I have figured out what went wrong. When creating the database there was a line in the script that was giving a problem, when i removed the line from the script it went through with no problems. The Problem came in where:

   /****** Object:  Database [dbname]    Script Date: 01/03/2013 06:53:20 ******/
   CREATE DATABASE [Mdbname] -- ON  PRIMARY 
    **--( NAME = N'dbname', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\dbname.mdf' , SIZE = 30417920KB , MAXSIZE =   UNLIMITED, FILEGROWTH = 1024KB )
   -- LOG ON 
   --( NAME = N'dbname_log', FILENAME = N'C:\Program Files\Microsoft SQL  Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\dbname_1.ldf' , SIZE = 28333632KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)
 --GO**. Normally  its not the best idea to comment those sections out, but i had no          other choice. Databases were created and tables, stored procs, functions and views were all put into their rightful places. 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM