Can columns with a null column value NOT NULL?

In SQL 2005/8, I want to convert a column into a select query into a NOT NULL column.

coalease()and isnull()although subtle features, not what I'm looking for. I want the selection to throw an error: any of the data is NULL.

Is it possible?

[Update] Obviously, the best approach would be to modify the original table, unfortunately, in this case it is not a (cost-effective) option.

+3
source share
3 answers

An odd request, but I think this matches it:

DECLARE @TABLE TABLE
(
    ID INT IDENTITY(1,1) NOT NULL,
    Val VARCHAR(100) NULL
)

INSERT INTO @TABLE(Val)
SELECT 'Hello' 
UNION SELECT 'World' 
UNION SELECT NULL --Remove this line to prevent failure

SELECT 
  ID, 
  CASE WHEN Val IS NULL THEN CAST(1/0 AS VARCHAR) ELSE Val END AS ValCheck 
FROM @TABLE
+5
source

What you are asking for does not exist.

NOT NULL is a limitation . SELECT . INSERT/UPDATE/DELETE . , , ALTER TABLE... ADD...

, , ​​ ISNULL COALESCE.

+7

If you want to throw an error in the code part (not sql), your option is only

$result = db.query("SELECT count(1) FROM table WHERE column_that_should_not_be_null IS NULL")
assert($result = 0);

If you want to convert a column so that it doesn’t accept NULLwhen you execute ALTER TABLE ... SET NOT NULL, you will receive an error message.

0
source

Source: https://habr.com/ru/post/1726751/


All Articles