如何检查Field是否可以包含NULL(adFldIsNullable)

时间:2013-12-21 19:01:03

标签: mysql vbscript asp-classic ado

我试过这段代码(ASP CLASSIC):

Public Function IsNullable(MyField)
    Dim RS, SQL, Tmp
    SQL = "SELECT " & MyField & " FROM mytable WHERE 1;"
    Set RS = Server.CreateObject("ADODB.Recordset")
    RS.Open SQL, conn 'I have connection opened elsewhere, using Driver={MySQL ODBC 5.2w Driver}

    'Now check for Attributes
    Tmp = RS.Fields(MyField).Attributes
    IsNullable = 0 <> (Tmp And adFldIsNullable) '0x20
    RS.Close
    Set RS = Nothing
End Function

功能正常但有时结果是错误的。例如,它为ID字段返回True,由于它是Primary Index Autoincrement,因此绝对不可为空。我怎么能让它重新?感谢

补充:似乎当列设置为AutoIncrement Not Null时,函数运行错误...

2 个答案:

答案 0 :(得分:1)

在MySql中,您可以查询information_schema.columns视图:

SELECT is_nullable
FROM information_schema.columns
WHERE table_schema = 'my_schema'
  AND table_name   = 'table-name'
  AND column_name = 'column name'

请参阅此演示:http://www.sqlfiddle.com/#!2/743d6/3

答案 1 :(得分:1)

根据http://bugs.mysql.com/bug.php?id=3857

设计
  

[2004年7月21日22:26] Timothy Smith(当时的高级支持工程师)

     

这是因为MySQL将此类列的DEFAULT值报告为NULL。这意味着,如果你是   在列中插入NULL值,您将获得表的下一个整数值   auto_increment counter。

它仍然有效,@ kordirko的解决方案是完全可以接受的。