Query to check whether a column is nullable

后端 未结 3 1262
自闭症患者
自闭症患者 2020-12-15 02:39

Query to check whether a column is nullable (null values are allowed in the column or not). It should preferably return yes/no or 1/0 or true/false.

相关标签:
3条回答
  • 2020-12-15 02:49

    You could also use the COLUMNPROPERTY and OBJECT_ID metadata functions:

    SELECT COLUMNPROPERTY(OBJECT_ID('SchemaName.TableName', 'U'), 'ColumnName', 'AllowsNull');
    
    0 讨论(0)
  • 2020-12-15 02:54

    You can also check all columns in a table for 'nullable' property or any other property that you want, for example table named Bank.Table we need to query

    column name, data type, Character Max Length, is nullable

    Use SQL Information_Schema like this example:

    SELECT 
    COL.COLUMN_NAME, 
    COL.DATA_TYPE, 
    COL.CHARACTER_MAXIMUM_LENGTH, 
    COL.IS_NULLABLE 
    FROM INFORMATION_SCHEMA.COLUMNS COL 
    WHERE COL.TABLE_NAME = 'Bank'
    

    The result should be like this:

    0 讨论(0)
  • 2020-12-15 03:04

    You could retrieve that from sys.columns:

    select  is_nullable 
    from    sys.columns 
    where   object_id = object_id('Schema.TheTable') 
            and name = 'TheColumn'
    
    0 讨论(0)
提交回复
热议问题