首页 \ 问答 \ 意外的sp_MSForEachDB行为(Unexpected sp_MSForEachDB behavior)

意外的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
更新时间:2023-11-03 18:11

最满意答案

好的,我明白了。 我把代码放在一个类中,但不是一个方法。 加载或要求像这样运行开放代码。 我需要将它包装在方法中,并执行方法,以执行我想要做的事情。

我遵循的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示例并没有明确说明这一主题。 许多都是如此简单,它没有什么区别,有些只是在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 ...
  • 您可以使用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 ...
  • 你必须返回一些东西,如: 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在默认情况下在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风格时,您require 'sinatra'使用require 'sinatra' ,然后将路线添加到顶层。 这些路由被添加到Sinatra::Application 。 当您直接运行此文件时,例如使用ruby my_app.rb ,Sinatra运行内置Web服务器,该服务器将为Sinatra::Application应用程序提供服务。 使用模块化样式时,使用require 'sinatra/base' ,然后将路由添加到Sinatra::Base子类。 在这种情况下,直接执行 ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。