【问题标题】:Ruby with MS SQL to truncate all table in a DBRuby 与 MS SQL 截断数据库中的所有表
【发布时间】:2019-10-31 03:03:06
【问题描述】:

我正在使用 ruby​​ 和 tiny_tds (ms sql)。我想截断数据库中的所有表。

以下是我现在拥有的:

require 'tiny_tds'
@client = TinyTds::Client.new(username: 'sa', database: 'test123', password: 'Auto1', host: 'localhost', port: 1433)
puts 'Connecting to SQL Server'

if @client.active? == true then puts 'Done' end

out = @client.execute("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE='BASE TABLE' AND TABLE_CATALOG='test123'");
#@client.do
#puts "out ==> #{out}"
out.each do |x|
        puts "tabname1 ==> #{x['TABLE_NAME']}"
        next if x['TABLE_NAME'] =~ /api_user|version_info/
        @client.execute("TRUNCATE TABLE #{x['TABLE_NAME']}")
        @client.do
end
@client.close

输出:

$ ruby mssql.rb
Connecting to SQL Server
Done
tabname1 ==> test
mssql.rb:14:in `execute': Attempt to initiate a new Adaptive Server operation with results pending (TinyTds::Error)
    from mssql.rb:14:in `block in <main>'
    from mssql.rb:10:in `each'
    from mssql.rb:10:in `<main>'

我知道我们需要使用do [@client.execute(sql).do]。但仍然收到错误

更新:

test123 db 中的表

测试, 测试1

【问题讨论】:

    标签: ruby tiny-tds


    【解决方案1】:

    我认为问题在于您试图在 out.each 块内运行新的执行,尽管最初的 out = @client.execute(...) 执行尚未完成。

    这里执行一些sql:

    out = @client.execute("SELECT TABLE_NAME FROM INFORMATION_SCHEMA....
    

    ...然后在您的 each 循环内您尝试再次执行:

    @client.execute("TRUNCATE TABLE #{x['TABLE_NAME']}")
    

    ...在您按照您的建议使用out.do 完成初始out 执行之前。

    【讨论】:

      猜你喜欢
      • 2011-08-27
      • 2015-09-19
      • 1970-01-01
      • 1970-01-01
      • 2011-03-13
      • 2010-09-14
      • 2011-02-19
      • 2011-08-12
      • 1970-01-01
      相关资源
      最近更新 更多