意外的sp_MSForEachDB行为(Unexpected sp_MSForEachDB behavior)
我正在努力增强我对一些系统sprocs的理解,我对我正在研究的这个脚本感到非常困惑。 为了理解
sp_MSForEachDB
我决定编写一个脚本来截断服务器上所有数据库的日志。 因此,我想出了以下脚本:sp_MSForEachDb 'IF LOWER(rtrim(''?'')) NOT IN ('''', ''master'', ''tempdb'', ''tempdev'', ''model'', ''msdb'') BEGIN declare @LogFile nvarchar(max) USE [?] SELECT @LogFile = sys.sysaltfiles.name FROM sys.sysdatabases INNER JOIN sys.sysaltfiles ON sys.sysdatabases.dbid = sys.sysaltfiles.dbid WHERE (sys.sysaltfiles.fileid = 1) AND (sys.sysdatabases.name = ''?'') print ''DB: [?], Log: '' + @LogFile CHECKPOINT DBCC SHRINKFILE (@LogFile, 1) END'
事实证明,这有时只会成功截断数据库的日志。 在它失败的数据库上(没有错误消息,只留下一个未截断的日志文件),它一致/可重复地失败。
但是,在print语句中,它会完全打印出我希望打印的内容。 但是,如果我手动为每个数据库键入此脚本的功能部分:
USE [Seed] CHECKPOINT DBCC SHRINKFILE('Seedlog', 1)
它100%的工作时间。
为什么我的
sp_MSForEachDB
“循环”不能按预期工作? 我错过了什么?I'm working to enhance my understanding of some system sprocs and I'm very confused by this script I was working on. To exercise my understanding of
sp_MSForEachDB
I decided to write a script that would truncate the logs of all databases on a server. As such, I came up with the following script:sp_MSForEachDb 'IF LOWER(rtrim(''?'')) NOT IN ('''', ''master'', ''tempdb'', ''tempdev'', ''model'', ''msdb'') BEGIN declare @LogFile nvarchar(max) USE [?] SELECT @LogFile = sys.sysaltfiles.name FROM sys.sysdatabases INNER JOIN sys.sysaltfiles ON sys.sysdatabases.dbid = sys.sysaltfiles.dbid WHERE (sys.sysaltfiles.fileid = 1) AND (sys.sysdatabases.name = ''?'') print ''DB: [?], Log: '' + @LogFile CHECKPOINT DBCC SHRINKFILE (@LogFile, 1) END'
It turns out that only sometimes does this successfully truncate the log of a database. On the databases it fails (no error message, just leaves me with an untruncated log file), it consistently/reproducibly fails.
In the print statement, however, it prints EXACTLY what I would expect it to print. However, if I manually just type out the functional part of this script for each database:
USE [Seed] CHECKPOINT DBCC SHRINKFILE('Seedlog', 1)
it works 100% of the time.
Why is my
sp_MSForEachDB
"loop" not working as expected? What am I missing?
原文:https://stackoverflow.com/questions/5567110
最满意答案
好的,我明白了。 我把代码放在一个类中,但不是一个方法。 加载或要求像这样运行开放代码。 我需要将它包装在方法中,并执行方法,以执行我想要做的事情。
我遵循的Sinatra示例并没有明确说明这一主题。 许多都是如此简单,它没有什么区别,有些只是在config.ru中编码。 我来自Rails,虽然我从Rails知道这一点,但它没有太大的区别,因为绝大多数代码已经存在于方法中。
Okay, I get it. I put the code in a class, but not it a method. Load or require both run open code like this. I need to wrap it in methods, and execute the methods, to do what I want to do.
Sinatra examples, which I followed, don't make this clear and simply avoid the topic. Many are so simple it doesn't make a difference, and some are just coded within the config.ru. I am coming from Rails and, while I knew this from Rails, it didn't make much of a difference since the vast majority of the code already exists in methods.
相关问答
更多-
似乎问题出在rbenv ,不一定是rbenv或者是whenever 。 这解决了我的问题: #schedlue.rb command "cd #{Dir.pwd} && RACK_ENV=#{environment} rbenv exec bundle exec rake reports:generate" 通过添加rbenv exec我们在运行rake任务之前成功加载了正确的ruby版本。 It seems the problem was with rbenv, and not necessarily ...
-
设置 set :database, {adapter: 'postgresql', database: '_your_database_name_' } 代替 set :database_file, "../../config/database.yml" 有帮助,但我仍然不确定错误来自哪里。 Setting set :database, {adapter: 'postgresql', database: '_your_database_name_'} instead of set :database_fil ...
-
Sinatra app在加载期间执行,而不是在发出run run方法之后执行(Sinatra app executes during load instead of after run method issued)[2022-03-02]
好的,我明白了。 我把代码放在一个类中,但不是一个方法。 加载或要求像这样运行开放代码。 我需要将它包装在方法中,并执行方法,以执行我想要做的事情。 我遵循的Sinatra示例并没有明确说明这一主题。 许多都是如此简单,它没有什么区别,有些只是在config.ru中编码。 我来自Rails,虽然我从Rails知道这一点,但它没有太大的区别,因为绝大多数代码已经存在于方法中。 Okay, I get it. I put the code in a class, but not it a method. Loa ... -
以下内容不会回复帖子: get '/hi' do "Hello World!" end 很可能你需要做这样的事情: post '/hi' do # do post stuff end I solved the issue. It was a problem with rack. I replaced use Rack::TryStatic, :root => File.join(App::SETTINGS.source, App::SETTINGS.site.config['destin ...
-
与sinatra一起使用(Use whenever with sinatra)[2023-11-04]
您可以使用rake任务代替script/runner 。 Whenever gem支持通过rake任务定义作业( 实际上更多 ) 示例:#config / schedule.rb every 3.hours do rake "destroy_all" end 在你的Rakefile :(缺少好的例子) task :destroy_all do puts "Do not do this" # sh "rm -rf ." end You can use a rake task in place ... -
Sinatra应用程序将无法运行(Sinatra app won't run)[2021-11-26]
你必须返回一些东西,如: get '/' do "Hello World" # the return is implicit end 然后它会工作。 puts print打印到命令行但返回nil,因此没有任何东西被返回到sinatra。 You have to return something, as in: get '/' do "Hello World" # the return is implicit end Then it will work. puts prints to the co ... -
我只需要添加此代码,我的sinatra应用程序现在还活着: set :server, 'webrick' I just needed to add this code, and my sinatra app is now alive: set :server, 'webrick'
-
Sinatra - 错误处理(Sinatra - Error Handling)[2023-09-20]
Sinatra在默认情况下在development环境中运行时会吞下异常并显示其调试错误页面。 因此,要触发自定义错误处理程序,您必须在除development (可能是production )之外的Rack环境中运行应用程序,或者最好告诉Sinatra在development模式下不使用其默认错误处理程序。 请考虑以下独立的Sinatra应用程序示例: require "sinatra" #disable :show_exceptions get "/" do raise RuntimeError ... -
我使用了以下代码: # Exit 'correctly' get '/exit' do # /exit causes: # 15:20:24 web.1 | Damn! # 15:20:24 web.1 | exited with code 1 Process.kill('TERM', Process.pid) end # Just terminate get '/fail' do Process.kill('KILL', Process.pid) end 看看config.ru ...
-
Sinatra的奇怪问题(Strange issue in Sinatra)[2022-02-11]
当您使用经典的Sinatra风格时,您require 'sinatra'使用require 'sinatra' ,然后将路线添加到顶层。 这些路由被添加到Sinatra::Application 。 当您直接运行此文件时,例如使用ruby my_app.rb ,Sinatra运行内置Web服务器,该服务器将为Sinatra::Application应用程序提供服务。 使用模块化样式时,使用require 'sinatra/base' ,然后将路由添加到Sinatra::Base子类。 在这种情况下,直接执行 ...