如何在存储过程中退出?
我有一个存储过程,我想尽早摆脱(尝试调试).我已经打过电话RETURN,并RAISERROR和SP在后台继续运行:
CREATE PROCEDURE dbo.Archive_Session @SessionGUID uniqueidentifier AS
print 'before raiserror'
raiserror('this is a raised error', 18, 1)
print 'before return'
return -1
print 'after return'
[snip]
Run Code Online (Sandbox Code Playgroud)
我知道它继续运行,因为我遇到了进一步的错误.我看不到任何印刷品.如果我注释掉大部分存储过程:
CREATE PROCEDURE dbo.Archive_Session @SessionGUID uniqueidentifier AS
print 'before raiserror'
raiserror('this is a raised error', 18, 1)
print 'before return'
return -1
print 'after return'
/*
[snip]
*/
Run Code Online (Sandbox Code Playgroud)
然后我没有得到我的错误,我看到结果:
before raiserror
Server: Msg 50000, Level 18, State 1, Procedure Archive_Session, Line 5
this is a raised error …Run Code Online (Sandbox Code Playgroud) t-sql sql-server stored-procedures sql-server-2000 flow-control
一旦你进入finally条款,是否有可能判断是否有异常?就像是:
try:
funky code
finally:
if ???:
print('the funky code raised')
Run Code Online (Sandbox Code Playgroud)
我想要做更像这样的事情干:
try:
funky code
except HandleThis:
# handle it
raised = True
except DontHandleThis:
raised = True
raise
else:
raised = False
finally:
logger.info('funky code raised %s', raised)
Run Code Online (Sandbox Code Playgroud)
我不喜欢它需要捕获一个你不打算处理的异常,只是为了设置一个标志.
由于一些评论要求MCVE中的"M"较少,因此这里有一些关于用例的更多背景知识.实际问题是关于日志记录级别的升级.
logger.exception在except块中使用在此处没有帮助.因此,代码在日志捕获上下文(设置自定义处理程序以拦截日志记录)下运行,并且一些调试信息会被追溯重新记录:
try:
with LogCapture() as log:
funky_code() # <-- third party badness
finally:
mylog = mylogger.WARNING if <there was exception> else mylogger.DEBUG
for record in log.captured:
mylog(record.msg, record.args)
Run Code Online (Sandbox Code Playgroud) 我试图想出如何在TSQL中实现它
do
update stuff set col = 'blah' where that_row = 'the right one'
select trash from stuff ...
until some_condition
Run Code Online (Sandbox Code Playgroud)
Transact-SQL提供的唯一迭代控制流句是while (condition) sentences首先评估条件,如果该条件为真,则执行该句子.
我正在思考一个场景,比如在表上执行UPDATE语句,直到某个条件被触发,直到执行了最后一个UPDATE.
最重要的是,我正在寻找解决这个问题的不那么肮脏的方法(在WHILE之前复制UPDATE对我来说没有多大意义,因为UPDATE语句可以任意长而复杂)
编辑:我正在尝试解决的问题涉及同一个表下的多个UPDATE语句,每个语句都采用并转换前一次迭代的值.这在一个大的UPDATE语句中是不可能的,因为每行只会被评估和更新一次,所以循环是我能弄清楚这个混乱工作的唯一方法.
刚出现这个问题:我如何突破if声明?我有一个很长的if语句,但有一种情况我可以在早期就突破它.
在循环中,我可以这样做:
while (something ) {
last if $some_condition;
blah, blah, blah
...
}
Run Code Online (Sandbox Code Playgroud)
但是,我可以使用if语句执行相同的操作吗?
if ( some_condition ) {
blah, blah, blah
last if $some_other_condition; # No need to continue...
blah, blah, blah
...
}
Run Code Online (Sandbox Code Playgroud)
我知道我可以将if语句放在一个块中,然后我可以突破块:
{
if ( some_condition ) {
...
last if $some_other_condition; # No need to continue...
blah, blah, blah
...
}
}
Run Code Online (Sandbox Code Playgroud)
或者,我可以创建一个子程序(可能以编程方式更好):
if ( some_condition ) {
run_subroutine();
}
sub run_subroutine {
blah, blah, blah
return if $some_other_condition;
blah, …Run Code Online (Sandbox Code Playgroud) 对于以下代码
a =func()
if a != None:
b.append(a)
Run Code Online (Sandbox Code Playgroud)
a可以分配给None,有没有办法避免if语句只使用一行代码?
原始问题如下
import xml.etree.ElementTree as etree
r = etree.parse(f).getroot()
b = etree.Element('register',{})
a = r.find('tag_name') # a may get None if did not find it
if a != None:
b.append(a)
Run Code Online (Sandbox Code Playgroud)
好吧,我用了所有的答案并得到了这个,我个人认为这是迄今为止我写过的最复杂的python,哈哈,哈哈
NS_MAP = {
'spirit' : 'http://www.spiritconsortium.org/XMLSchema/SPIRIT/1.4',
'app' : 'http://www.app.com/SPIRIT-app'
}
mp=etree.Element('MemoryProperty', {'version':'alpha'})
mpt=etree.ElementTree(mp)
def copy_tags(tp, op, p, tn, ns='spirit'):
c = p.find('{%s}%s'%(NS_MAP[ns],tn))
if c is not None:
(op == '<-') and tp.append(c)
return c
for reg in regs:
te = etree.Element('register',{}) …Run Code Online (Sandbox Code Playgroud) 假设我有一段Perl代码,如:
foreach my $x (@x) {
foreach my $y (@z) {
foreach my $z (@z) {
if (something()) {
# I want to break free!
}
# do stuff
}
# do stuff
}
# do stuff
}
Run Code Online (Sandbox Code Playgroud)
如果something()是真的,我想打破('最后')所有循环.
我怎样才能做到这一点?我想到了两个选项,我不喜欢这两个选项:使用一些东西GOTO
添加一个将标记的布尔变量something()为true,在每个循环恢复之前检查这个var,last()如果它是真的.
有什么建议或想法?
谢谢.
通常,避免使用GOTO是一种好习惯.牢记这一点,我一直在与同事讨论这个话题.
请考虑以下代码:
Line:
while( <> ) {
next Line if (insert logic);
}
Run Code Online (Sandbox Code Playgroud)
使用循环标签计算为goto吗?
以下是perldoc中的perlsyn所说的:
以下是C程序员如何在Perl中编写特定算法的代码:
for (my $i = 0; $i < @ary1; $i++) {
for (my $j = 0; $j < @ary2; $j++) {
if ($ary1[$i] > $ary2[$j]) {
last; # can't go to outer :-(
}
$ary1[$i] += $ary2[$j];
}
# this is where that last takes me
}
Run Code Online (Sandbox Code Playgroud)
而这里的Perl程序员如何更熟悉这个成语可能会这样做:
OUTER: for my $wid (@ary1) {
INNER: for my $jet (@ary2) {
next OUTER if …Run Code Online (Sandbox Code Playgroud) 我想将maven生命周期中的某些阶段分解为子阶段.我想控制从一个子阶段到另一个子阶段的执行流程,有点像与ant依赖关系.
例如,我想使用NSIS插件,以便在我的项目被打包到war文件之后,将我的项目打包到包阶段的安装程序中.我想在包装阶段做所有这些.
那可能吗?
谢谢
在我的测试中,我在调用和外部库之前将数据播种到我们的后端,然后使用量角器运行一些ui测试.
'use strict'
var dataBuilder = require('data_builder.js');
describe('test', function () {
var testData = {
name: 'foo',
title: 'bar',
...
};
beforeEach(function () {
//create test data on the backend
dataBuilder.create(testData).then(function (id) {
testData.id = id.id;
});
});
it('test something', function () {
...
});
Run Code Online (Sandbox Code Playgroud)
因此,在it()实际完成之前,dataBuilder返回的promise未得到解决.如何将dataBuilder返回的promise添加到webDriver的流控制中?
是否可以在不使用if/else语句的情况下中断使用execfile函数调用的Python脚本的执行?我试过了exit(),但它不允许main.py完成.
# main.py
print "Main starting"
execfile("script.py")
print "This should print"
# script.py
print "Script starting"
a = False
if a == False:
# Sanity checks. Script should break here
# <insert magic command>
# I'd prefer not to put an "else" here and have to indent the rest of the code
print "this should not print"
# lots of lines below
Run Code Online (Sandbox Code Playgroud) flow-control ×10
perl ×3
python ×3
break ×2
if-statement ×2
loops ×2
sql-server ×2
t-sql ×2
angularjs ×1
execfile ×1
goto ×1
logging ×1
maven-2 ×1
phase ×1
plugins ×1
promise ×1
protractor ×1
sql ×1
try-finally ×1
while-loop ×1