使用' WAITFOR DELAY'时,Invoke-Sqlcmd意外行为SQL Query

时间:2017-05-17 08:00:05

标签: powershell

我需要使用invoke-sqlcmd从PowerShell执行以下SQL查询,这将创建两个表&test;' testtable'和' testtable1'在数据库' TestDB'

SQL查询:

 create table testtable (id int null,
 names varchar(50) null);
 WAITFOR DELAY '00:00:59';
 go
 create table testtable1 (id int null,
 names varchar(50) null);

但是当我尝试通过' .sql'执行上面的SQL查询时使用以下PowerShell命令的文件我收到一个错误'已经有一个名为' testtable'在数据库中。'。

PowerShell命令:

Invoke-Sqlcmd -inputfile "E:\createtable.sql" -ServerInstance "ServerName" -Database "TestDB"

ERRORMESSAGE:

Invoke-Sqlcmd : There is already an object named 'testtable' in the database.
At line:1 char:2
+  Invoke-Sqlcmd -inputfile "E:\create ...
+  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Invoke-Sqlcmd], SqlPowerShellSqlExecutionException
    + FullyQualifiedErrorId : SqlError,Microsoft.SqlServer.Management.PowerShell.GetScriptCommand

即使我在PowerShell窗口,表格和测试表中收到错误已在数据库' Testdb'。

上创建

我期待着我的命令' WAITFOR DELAY' 00:59';'造成这个问题。

注意:我没有桌子' testtable'和' testtable1'在数据库' TestDB'在执行之前。

1 个答案:

答案 0 :(得分:0)

以下两个选项允许成功运行查询而没有任何错误。

  1. 删除WAITFOR DELAY' 00:29'来自sql查询。
  2. 将延迟时间从59秒减少到29秒。

    WAITFOR DELAY '00:00:29'