Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Ruby/DBI中运行带有批处理分隔符的MS SQL脚本?_Ruby_Sql Server_Dbi - Fatal编程技术网

在Ruby/DBI中运行带有批处理分隔符的MS SQL脚本?

在Ruby/DBI中运行带有批处理分隔符的MS SQL脚本?,ruby,sql-server,dbi,Ruby,Sql Server,Dbi,我正试图从Ruby/DBI中可能包含批处理分隔符(关键字GO的文件中运行MS SQL脚本。整个脚本都包装在一个事务中,因此脚本中的任何错误都将导致回滚 scriptContents = File.read(@path) SqlDb.conn['AutoCommit'] = false begin SqlDb.conn.do scriptContents SqlDb.conn.commit rescue Exception SqlDb.conn.rollba

我正试图从Ruby/DBI中可能包含批处理分隔符(关键字
GO
的文件中运行MS SQL脚本。整个脚本都包装在一个事务中,因此脚本中的任何错误都将导致回滚

  scriptContents = File.read(@path)
  SqlDb.conn['AutoCommit'] = false
  begin
    SqlDb.conn.do scriptContents
    SqlDb.conn.commit
  rescue Exception
    SqlDb.conn.rollback
    @@log.error "Executing #{@path} resulted in error.  Transaction has been rolled back: #{$!}"
  end
  SqlDb.conn['AutoCommit'] = true
执行包含
GO
的简单脚本,例如:

PRINT 'this script is pointless'
GO
SELECT 1
导致以下错误:

错误SqlUpgradeScript:执行test/noop.sql导致错误。事务已回滚:37000(102)[Microsoft][ODBC SQL Server驱动程序][SQL Server]靠近“GO”的语法不正确

删除
GO
将导致脚本成功运行


使用Ruby/DBI运行可能包含多个批处理的SQL脚本,同时仍将整个脚本作为单个事务运行,最干净的方法是什么?

GO
不是T-SQL语句。它是一个控制台命令,仅由SQL Server Management Studio(或与isq;/osql等效的命令行)用于在脚本文件中分隔批

发送到SQL Server的任何命令文本都将作为单个批处理。请记住,调用
.do
时,您是在向服务器发送命令,而不是向控制台发送脚本文件

在某些情况下,您可能必须使用分号(
)分隔多个语句,例如在使用CTE(
with()…SELECT…
)时,否则不需要使用任何分隔符

只需删除
GO
并将所需命令发送到服务器。如果确实要执行脚本文件,则必须在代码中将它们分批分解,然后将每个批作为单个命令发送