ARITHABORT,ARITHIGNORE - 我需要重新打开它们吗?

时间:2016-07-05 13:47:47

标签: tsql stored-procedures error-handling azure-sql-database

这可能从根本上说是愚蠢的,但这里有:

我有一个存储过程,我需要运行而不会抛出溢出错误。如果发生溢出,我很满意NULL。

我看到它建议所有互联网(例如herehere)只使用

#include<iostream>

int main(void)
{
    int i;
    char ch;
std::cout<<"Enter any character : ";
std::cin.get(ch);  // For testing enter a string at this step say "String"

/* The input to cin is line-buffered, so after reading 'S' to ch,
 * the remaining "tring" is still in the buffer.
 */

std::cout<<"Entered character : "<<ch<<"\n";
while(std::cin.get()!='\n')
;; 
/* cin.get() is an overloaded function in the istream class.
 * If no arguments are passed to 'get()' this function reads single next character
 * In essence, we wait for the cin.get() to clear the buffer that is
 * read all characters including '\n'
 */

std::cout<<"Press any key to continue..\n";
std::cin.get();
/* Since we have already cleared the buffer using the loop
 * 'get()' expects us to enter a character this time
 */

return 0;
}

那很好,但是我应该在我的SP结束时将它们重新打开吗?

SET ARITHABORT OFF;
SET ARITHIGNORE ON;

喜欢这个?这些甚至是默认值吗?

另外,我是否需要两个开关才能忽略溢出错误(在我的情况下,试图将&#39; 606006000&#39;放在SMALLINT字段中)?

使用Azure DB v12。

1 个答案:

答案 0 :(得分:1)

有关这些设置的一些信息。默认情况下均为真。

SET ARITHIGNORE:
SET ARITHIGNORE设置仅控制是否返回错误消息。无论此设置如何,SQL Server都会在涉及溢出或被零除错误的计算中返回NULL

SET ARITHABORT:
SET ARITHABORT设置可用于确定查询是否终止

此外,这是MSDN对这些更多的说法..

  

此设置不会影响INSERT,UPDATE和DELETE语句期间发生的错误。   如果SET ARITHABORT或SET ARITHIGNORE为OFF且SET ANSI_WARNINGS为ON,则SQL Server在遇到被零除或溢出错误时仍会返回错误消息。

我做了一些测试,似乎 SET ARITHIGNORE OFF 设置仅适用于您需要的行为..

此外你还需要设置 SET_ANSI_WARNINGS_OFF ,否则会抛出错误

SET ARITHABORT OFF
--SET ARITHIGNORE OFF
SET ANSI_WARNINGS OFF

select 1/0

declare @a tinyint
set @a=10000
select @a