通过 ADODB 检查 SQL 字段是否具有 UNIQUE 约束 VBA
check if SQL field has UNIQUE constraint via ADODB VBA
我正在编写 vba 代码来更新 SQLSERVER tables。
在实际尝试更新它们之前,我想检查新记录的所有值是否正常。
如何检查字段是否具有 UNIQUE 约束?
我已经尝试列出所有属性和属性,但属性 ISUNIQUE 没有显示任何内容,尽管它是这样设置的,并且在尝试创建一个在此字段中具有重复实体的新记录时会产生错误
属性
BASECATALOGNAME = TEBUS_Templates
BASECOLUMNNAME = Nombre del Armario
BASESCHEMANAME =
BASETABLENAME = TBL_FAKOM_ARMARIOS
CLSID =
COLLATINGSEQUENCE =
COMPUTEMODE =
DATETIMEPRECISION =
DEFAULTVALUE =
DOMAINCATALOG =
DOMAINSCHEMA =
DOMAINNAME =
HASDEFAULT =
ISAUTOINCREMENT = Falso
ISCASESENSITIVE = Falso
ISSEARCHABLE = 4
ISUNIQUE =
OCTETLENGTH = 200
KEYCOLUMN = Falso
OPTIMIZE = Falso
Attributes
adFldUnknownUpdatable
FWIW 这是我为了得到上面的列表而放在一起的程序:
'2017-05-22 / B.Agullo /
Public Sub showFieldAtributesAndProperties(ByVal f As Field)
'description of sub
Dim p As Variant
Debug.Print f.Name
Debug.Print "Properties"
For Each p In f.Properties
Debug.Print p.Name & " = " & p.Value
Next
Debug.Print Chr(10) & "Attributes"
If ((adFldCacheDeferred And f.Attributes) = adFldCacheDeferred) Then Debug.Print "adFldCacheDeferred"
If ((adFldFixed And f.Attributes) = adFldFixed) Then Debug.Print "adFldFixed"
If ((adFldIsChapter And f.Attributes) = adFldIsChapter) Then Debug.Print "adFldIsChapter"
If ((adFldIsCollection And f.Attributes) = adFldIsCollection) Then Debug.Print "adFldIsCollection"
If ((adFldIsDefaultStream And f.Attributes) = adFldIsDefaultStream) Then Debug.Print "adFldIsDefaultStream"
If ((adFldIsNullable And f.Attributes) = adFldIsNullable) Then Debug.Print "adFldIsNullable"
If ((adFldIsRowURL And f.Attributes) = adFldIsRowURL) Then Debug.Print "adFldIsRowURL"
If ((adFldLong And f.Attributes) = adFldLong) Then Debug.Print "adFldLong"
If ((adFldMayBeNull And f.Attributes) = adFldMayBeNull) Then Debug.Print "adFldMayBeNull"
If ((adFldMayDefer And f.Attributes) = adFldMayDefer) Then Debug.Print "adFldMayDefer"
If ((adFldNegativeScale And f.Attributes) = adFldNegativeScale) Then Debug.Print "adFldNegativeScale"
If ((adFldRowID And f.Attributes) = adFldRowID) Then Debug.Print "adFldRowID"
If ((adFldRowVersion And f.Attributes) = adFldRowVersion) Then Debug.Print "adFldRowVersion"
If ((adFldUnknownUpdatable And f.Attributes) = adFldUnknownUpdatable) Then Debug.Print "adFldUnknownUpdatable"
If ((adFldUnspecified And f.Attributes) = adFldUnspecified) Then Debug.Print "adFldUnspecified"
If ((adFldUpdatable And f.Attributes) = adFldUpdatable) Then Debug.Print "adFldUpdatable"
release:
End Sub
此外,供您参考,这是创建 table
的 SQL 命令
CREATE TABLE TBL_FAKOM_ARMARIOS(
"ArmarioID" int IDENTITY(1,1) PRIMARY KEY NOT NULL
, "Nombre del Armario" nvarchar(100) UNIQUE NOT NULL
, "Fecha de Alta" dateTime NOT NULL
, "Fecha de Baja" dateTime
, "Usuario de Alta" nvarchar(50) NOT NULL
, "Usuario de Baja" nvarchar(50)
)
你可以使用数据库模式,像这样
Function IX_UNIQUE(strTableName As String, strFieldName As String) as Boolean
Dim c As ADODB.Connection
Dim r As ADODB.Recordset
Set c = New ADODB.Connection
c.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & "C:\TESTsb.accdb" & ";" & _
"Persist Security Info=False;"
c.Open
Set r = New ADODB.Recordset
Set r = c.OpenSchema(adSchemaIndexes, Array(Empty, Empty, strFieldName , Empty, strTableName))
If Not r.EOF Then
IX_UNIQUE = r.Fields("UNIQUE").value
Else
IX_UNIQUE = False
End If
End Function
我正在编写 vba 代码来更新 SQLSERVER tables。 在实际尝试更新它们之前,我想检查新记录的所有值是否正常。 如何检查字段是否具有 UNIQUE 约束?
我已经尝试列出所有属性和属性,但属性 ISUNIQUE 没有显示任何内容,尽管它是这样设置的,并且在尝试创建一个在此字段中具有重复实体的新记录时会产生错误
属性
BASECATALOGNAME = TEBUS_Templates
BASECOLUMNNAME = Nombre del Armario
BASESCHEMANAME =
BASETABLENAME = TBL_FAKOM_ARMARIOS
CLSID =
COLLATINGSEQUENCE =
COMPUTEMODE =
DATETIMEPRECISION =
DEFAULTVALUE =
DOMAINCATALOG =
DOMAINSCHEMA =
DOMAINNAME =
HASDEFAULT =
ISAUTOINCREMENT = Falso
ISCASESENSITIVE = Falso
ISSEARCHABLE = 4
ISUNIQUE =
OCTETLENGTH = 200
KEYCOLUMN = Falso
OPTIMIZE = Falso
Attributes
adFldUnknownUpdatable
FWIW 这是我为了得到上面的列表而放在一起的程序:
'2017-05-22 / B.Agullo /
Public Sub showFieldAtributesAndProperties(ByVal f As Field)
'description of sub
Dim p As Variant
Debug.Print f.Name
Debug.Print "Properties"
For Each p In f.Properties
Debug.Print p.Name & " = " & p.Value
Next
Debug.Print Chr(10) & "Attributes"
If ((adFldCacheDeferred And f.Attributes) = adFldCacheDeferred) Then Debug.Print "adFldCacheDeferred"
If ((adFldFixed And f.Attributes) = adFldFixed) Then Debug.Print "adFldFixed"
If ((adFldIsChapter And f.Attributes) = adFldIsChapter) Then Debug.Print "adFldIsChapter"
If ((adFldIsCollection And f.Attributes) = adFldIsCollection) Then Debug.Print "adFldIsCollection"
If ((adFldIsDefaultStream And f.Attributes) = adFldIsDefaultStream) Then Debug.Print "adFldIsDefaultStream"
If ((adFldIsNullable And f.Attributes) = adFldIsNullable) Then Debug.Print "adFldIsNullable"
If ((adFldIsRowURL And f.Attributes) = adFldIsRowURL) Then Debug.Print "adFldIsRowURL"
If ((adFldLong And f.Attributes) = adFldLong) Then Debug.Print "adFldLong"
If ((adFldMayBeNull And f.Attributes) = adFldMayBeNull) Then Debug.Print "adFldMayBeNull"
If ((adFldMayDefer And f.Attributes) = adFldMayDefer) Then Debug.Print "adFldMayDefer"
If ((adFldNegativeScale And f.Attributes) = adFldNegativeScale) Then Debug.Print "adFldNegativeScale"
If ((adFldRowID And f.Attributes) = adFldRowID) Then Debug.Print "adFldRowID"
If ((adFldRowVersion And f.Attributes) = adFldRowVersion) Then Debug.Print "adFldRowVersion"
If ((adFldUnknownUpdatable And f.Attributes) = adFldUnknownUpdatable) Then Debug.Print "adFldUnknownUpdatable"
If ((adFldUnspecified And f.Attributes) = adFldUnspecified) Then Debug.Print "adFldUnspecified"
If ((adFldUpdatable And f.Attributes) = adFldUpdatable) Then Debug.Print "adFldUpdatable"
release:
End Sub
此外,供您参考,这是创建 table
的 SQL 命令CREATE TABLE TBL_FAKOM_ARMARIOS(
"ArmarioID" int IDENTITY(1,1) PRIMARY KEY NOT NULL
, "Nombre del Armario" nvarchar(100) UNIQUE NOT NULL
, "Fecha de Alta" dateTime NOT NULL
, "Fecha de Baja" dateTime
, "Usuario de Alta" nvarchar(50) NOT NULL
, "Usuario de Baja" nvarchar(50)
)
你可以使用数据库模式,像这样
Function IX_UNIQUE(strTableName As String, strFieldName As String) as Boolean
Dim c As ADODB.Connection
Dim r As ADODB.Recordset
Set c = New ADODB.Connection
c.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & "C:\TESTsb.accdb" & ";" & _
"Persist Security Info=False;"
c.Open
Set r = New ADODB.Recordset
Set r = c.OpenSchema(adSchemaIndexes, Array(Empty, Empty, strFieldName , Empty, strTableName))
If Not r.EOF Then
IX_UNIQUE = r.Fields("UNIQUE").value
Else
IX_UNIQUE = False
End If
End Function