patternModerate
Changing ANSI_NULLS setting on a table
Viewed 0 times
tablechangingsettingansi_nulls
Problem
Can we use the following query to update ANSI_NULLS option.
I am not sure what it is doing. I got this after searching for a solution. Our db is SQL server 2008.
But while i am running this i got an error like this
Please give your suggestions
sp_configure 'allow updates', 1
reconfigure with override
update sysobjects set status = status | 0x20000000 where name = 'tblClient'I am not sure what it is doing. I got this after searching for a solution. Our db is SQL server 2008.
But while i am running this i got an error like this
"Ad hoc updates to system catalogs are not allowed."Please give your suggestions
Solution
Updating system tables directly is unsupported and unsafe. The structure of them has changed since 2000 so the code you have won't work against them anyway.
It is sometimes possible to use
A full worked example below.
One caveat however is that this does not always work. Adding a check constraint with definition
ALTER TABLE SWITCH statement failed. Table 'YourTableNew' has a column
level check constraint 'CK_YourTableNew_X__37A5467C' on column 'X'
that is not loadable for semantic validation.
This makes sense as the semantics of the constraint do change with the setting.
If you encounter this you could consider temporarily removing the problematic constraints etc and adding them back after the switch or just biting the bullet and recreating the table in a more conventional way.
It is sometimes possible to use
ALTER TABLE ... SWITCH to do this as a metadata only change however.A full worked example below.
/*Create table with option off*/
SET ANSI_NULLS OFF;
CREATE TABLE dbo.YourTable (X INT)
/*Add some data*/
INSERT INTO dbo.YourTable VALUES (1),(2),(3)
/*Confirm the bit is set to 0*/
SELECT uses_ansi_nulls, *
FROM sys.tables
WHERE object_id = object_id('dbo.YourTable')
GO
BEGIN TRY
BEGIN TRANSACTION;
/*Create new table with identical structure but option on*/
SET ANSI_NULLS ON;
CREATE TABLE dbo.YourTableNew (X INT)
/*Metadata only switch*/
ALTER TABLE dbo.YourTable SWITCH TO dbo.YourTableNew;
DROP TABLE dbo.YourTable;
EXECUTE sp_rename N'dbo.YourTableNew', N'YourTable','OBJECT';
/*Confirm the bit is set to 1*/
SELECT uses_ansi_nulls, *
FROM sys.tables
WHERE object_id = object_id('dbo.YourTable')
/*Data still there!*/
SELECT *
FROM dbo.YourTable
COMMIT TRANSACTION;
END TRY
BEGIN CATCH
IF XACT_STATE() <> 0
ROLLBACK TRANSACTION;
PRINT ERROR_MESSAGE();
END CATCH;One caveat however is that this does not always work. Adding a check constraint with definition
CHECK (X <> NULL) causes it to fail with ALTER TABLE SWITCH statement failed. Table 'YourTableNew' has a column
level check constraint 'CK_YourTableNew_X__37A5467C' on column 'X'
that is not loadable for semantic validation.
This makes sense as the semantics of the constraint do change with the setting.
If you encounter this you could consider temporarily removing the problematic constraints etc and adding them back after the switch or just biting the bullet and recreating the table in a more conventional way.
Code Snippets
/*Create table with option off*/
SET ANSI_NULLS OFF;
CREATE TABLE dbo.YourTable (X INT)
/*Add some data*/
INSERT INTO dbo.YourTable VALUES (1),(2),(3)
/*Confirm the bit is set to 0*/
SELECT uses_ansi_nulls, *
FROM sys.tables
WHERE object_id = object_id('dbo.YourTable')
GO
BEGIN TRY
BEGIN TRANSACTION;
/*Create new table with identical structure but option on*/
SET ANSI_NULLS ON;
CREATE TABLE dbo.YourTableNew (X INT)
/*Metadata only switch*/
ALTER TABLE dbo.YourTable SWITCH TO dbo.YourTableNew;
DROP TABLE dbo.YourTable;
EXECUTE sp_rename N'dbo.YourTableNew', N'YourTable','OBJECT';
/*Confirm the bit is set to 1*/
SELECT uses_ansi_nulls, *
FROM sys.tables
WHERE object_id = object_id('dbo.YourTable')
/*Data still there!*/
SELECT *
FROM dbo.YourTable
COMMIT TRANSACTION;
END TRY
BEGIN CATCH
IF XACT_STATE() <> 0
ROLLBACK TRANSACTION;
PRINT ERROR_MESSAGE();
END CATCH;Context
StackExchange Database Administrators Q#16229, answer score: 10
Revisions (0)
No revisions yet.