【发布时间】: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
【问题讨论】: