2

I have a rather complex SQL script that does several things:

  • Drops an existing database
  • Reloads the database from a backup
  • Sets permissions
  • A few other miscellaneous db-specific tasks

I've saved this as a .sql file for mobility and ease of use, but I'd like to incorporate this into a Powershell script to make it even simpler to run (and to encapsulate a few other tasks that need to be done around the same process). Here are the bits of code I have in Powershell for the script:

add-pssnapin sqlservercmdletsnapin100
invoke-sqlcmd -inputfile "D:\Scripts\loaddatabase31_Loadtest.sql" -serverinstance "PerfSQL02" -hostname "PerfSQL02"

Assume that the serverinstance, inputfile, and hostname exist and are put in correctly.

The database I am restoring is a couple hundred gigabytes, so the actual drop and restore process takes around 20 minutes. Right now, I'm getting the following error when I try to run that script (it's being run from a workstation within the same network but on a different domain):

invoke-sqlcmd : Timeout expired.  The timeout period elapsed prior to completion of the operation or the server is not responding.
At line:2 char:1

I've tried using the -connectiontimeout switch on invoke-sqlcmd, but that doesn't seem to work to resolve this issue (so it's probably not timing out on connecting). Most of the examples I've seen of running SQL via Powershell don't use a file, but instead define the SQL within the script. I'd prefer not to do this due to the complexity of the SQL needed to run (and since it's dropping and restoring a database, I can't really wrap all of this up in a SP unless I use one of the System DBs).

Anyone have some ideas as to what could be causing that timeout? Does invoke-sqlcmd not work gracefully with files?

4

1 回答 1

4

你不想要 -connectiontimeout,你可能想要 -QueryTimeout

http://technet.microsoft.com/en-us/library/cc281720.aspx

于 2013-05-07T13:08:09.227 回答