简体   繁体   English

如何处理SET ANSI_NULLS ON或OFF?

[英]How to Deal with SET ANSI_NULLS ON or OFF?

I want to call this procedure that sends one value that can be NULL or any int value. 我想调用此过程发送一个可以为NULL或任何int值的值。

SELECT DomainName, DomainCode FROM Tags.tblDomain WHERE SubDomainId =@SubDomainId

I simply want to use this single query rather than what im doing right now in below given code. 我只是想使用这个单一的查询,而不是现在我正在做的下面给出的代码。

I searched for this how could i do this then i got this Link . 我搜索了这个怎么能这样做然后我得到了这个链接

According to this I have to set ANSI_NULLS OFF 根据这个,我必须设置ANSI_NULLS OFF

I am not able to set this inside this procedure before executing my sql query and then reset it again after doing this. 在执行我的sql查询之前,我无法在此过程中设置此项,然后在执行此操作后再次重置它。

ALTER PROCEDURE [Tags].[spOnlineTest_SubDomainSelect] 
    @SubDomainId INT
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;
    -- Insert statements for procedure here
    IF @SubDomainId IS NULL
        SELECT DomainName, DomainCode FROM Tags.tblDomain WHERE SubDomainId IS NULL 
    ELSE
        SELECT DomainName, DomainCode FROM Tags.tblDomain WHERE SubDomainId =@SubDomainId
END

What will be the better practice to do deal with ANSI_NULLS or Using If Else 处理ANSI_NULLS或使用If Else更好的做法是什么

SET ANSI_NULLS is ony defined at stored proc create time and cannot be set at run time. SET ANSI_NULLS在存储过程创建时定义,不能在运行时设置。

From CREATE PROC 来自CREATE PROC

Using SET Options 使用SET选项

The Database Engine saves the settings of both SET QUOTED_IDENTIFIER and SET ANSI_NULLS when a Transact-SQL stored procedure is created or modified. 创建或修改Transact-SQL存储过程时,数据库引擎会保存SET QUOTED_IDENTIFIER和SET ANSI_NULLS的设置。 These original settings are used when the stored procedure is executed. 执行存储过程时使用这些原始设置。 Therefore, any client session settings for SET QUOTED_IDENTIFIER and SET ANSI_NULLS are ignored when the stored procedure is running. 因此,存储过程运行时,将忽略SET QUOTED_IDENTIFIER和SET ANSI_NULLS的任何客户端会话设置。 Other SET options, such as SET ARITHABORT, SET ANSI_WARNINGS, or SET ANSI_PADDINGS are not saved when a stored procedure is created or modified. 创建或修改存储过程时,不会保存其他SET选项,例如SET ARITHABORT,SET ANSI_WARNINGS或SET ANSI_PADDINGS。 If the logic of the stored procedure depends on a particular setting, include a SET statement at the start of the procedure to guarantee the appropriate setting. 如果存储过程的逻辑取决于特定设置,请在过程开始时包含SET语句以保证适当的设置。 When a SET statement is executed from a stored procedure, the setting remains in effect only until the stored procedure has finished running. 从存储过程执行SET语句时,该设置仅在存储过程完成运行之前保持有效。 The setting is then restored to the value the stored procedure had when it was called. 然后,该设置将恢复为存储过程在调用时所具有的值。 This enables individual clients to set the options they want without affecting the logic of the stored procedure. 这使各个客户端可以设置所需的选项,而不会影响存储过程的逻辑。

The same applies to SET QUOTED_IDENTIFIER 这同样适用于SET QUOTED_IDENTIFIER

In this case, use IF ELSE because SET ANSI_NULLS will be ON in the future. 在这种情况下,请使用IF ELSE,因为SET ANSI_NULLS将来会打开

Or Peter Lang's suggestion. 或者Peter Lang的建议。

To be honest, expecting SubDomainId = @SubDomainId to work when @SubDomainId is NULL is not really correct usage of NULL... 老实说,当@SubDomainId为NULL时,期望SubDomainId = @SubDomainId工作并不是真正正确使用NULL ...

Can't you use a single query? 你不能使用单个查询吗?

SELECT DomainName, DomainCode
FROM Tags.tblDomain
WHERE ( @SubDomainId IS NULL AND SubDomainId IS NULL )
   OR ( SubDomainId = @SubDomainId )

FYI, I'm pretty sure ... 仅供参考,我很确定......

ANSI_NULLS OFF

Applies to the procedure when you create/edit it, it's like a setting of the procedure. 在创建/编辑它时适用于该过程,它类似于过程的设置。

So either the procedure has it ON or OFF. 因此,程序要么打开,要么关闭。 Your example was a query not a procedure so I'm a little confused. 你的例子是查询而不是程序,所以我有点困惑。

But if you have SQL 2005/2008 for example if you "edit" procedure it opens up your procedure in a new tab you'll see the ANSI_NULLS OFF near the top. 但是,如果您有SQL 2005/2008,例如,如果您“编辑”过程,它会在新选项卡中打开您的过程,您将在顶部附近看到ANSI_NULLS OFF。

You can edit it there and set it ON or OFF and update it to change ... 您可以在那里编辑并将其设置为ON或OFF并更新它以更改...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM