Function CreateCreateTableStatement(ByVal DBPath As String, ByVal TableName As String) As String
'CREATE [TEMPORARY] TABLE table (field1 type [(size)] [NOT NULL] [WITH COMPRESSION | WITH COMP] [index1] [, field2 type [(size)] [NOT NULL] [index2]
' [, â¦]] [, CONSTRAINT multifieldindex [, â¦]])
On Error GoTo EndErr
Dim cnn As New ADODB.Connection
Dim TablesSchema, ColumnsSchema, PrimaryKeysSchema As ADODB.Recordset
Dim tempsql, PrimaryKeyColumn, ColLen As String
Dim i As Integer
cnn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" & DBPath & "';"
cnn.Mode = ADODB.ConnectModeEnum.adModeShareExclusive
DoLog("Getting tables list of " & DBPath)
cnn.Open()
TablesSchema = cnn.OpenSchema(ADODB.SchemaEnum.adSchemaTables)
TablesSchema.Filter = "TABLE_NAME = '" & TableName & "'"
PrimaryKeysSchema = cnn.OpenSchema(ADODB.SchemaEnum.adSchemaPrimaryKeys)
PrimaryKeysSchema.Filter = "TABLE_NAME = '" & TableName & "'"
If PrimaryKeysSchema.EOF = False Then PrimaryKeyColumn = PrimaryKeysSchema("COLUMN_NAME").Value
PrimaryKeysSchema.Close()
ColumnsSchema = cnn.OpenSchema(ADODB.SchemaEnum.adSchemaColumns)
ColumnsSchema.Filter = "TABLE_NAME = '" & TableName & "'"
'ColumnsSchema.Sort = "`ORDINAL_POSITION`"
tempsql = "CREATE TABLE `" & TableName & "` ("
Do While Not ColumnsSchema.EOF
If ColumnsSchema("CHARACTER_MAXIMUM_LENGTH").Value.ToString = "" Or ColumnsSchema("CHARACTER_MAXIMUM_LENGTH").Value.ToString = "0" Then ColLen = "" 'Else ColLen = "(" & ColumnsSchema("CHARACTER_MAXIMUM_LENGTH").Value & ")"
tempsql = tempsql & "`" & ColumnsSchema("COLUMN_NAME").Value & "` " & DataCodeToName(ColumnsSchema("DATA_TYPE").Value) & " " & ColLen ' & ColumnsSchema("IS_NULLABLE").Value & ColumnsSchema("COLUMN_DEFAULT").Value & ", " & ColumnsSchema("IS_NULLABLE").Value & ", " & DataCodeToName(ColumnsSchema("DATA_TYPE").Value) & ", " & ColumnsSchema("CHARACTER_MAXIMUM_LENGTH").Value
If PrimaryKeyColumn = ColumnsSchema("COLUMN_NAME").Value Then tempsql = tempsql + " NOT NULL IDENTITY PRIMARY KEY, " Else tempsql = tempsql + ", "
ColumnsSchema.MoveNext()
Loop
tempsql = tempsql.Substring(0, Len(tempsql) - 2) + ");"
cnn.Close()
DoLog("Gotten tables list of " & DBPath)
Return tempsql
Exit Function
EndErr:
cnn.Close()
MsgBox(Err.Description)
End Function
谢谢你们。但今天我发现了创建表的正确SQL。
CREATE TABLE `Table3` (`Column1` VARCHAR , `Column11` BYTE , `Column12` SHORT , `Column13` SINGLE , `Column14` DOUBLE , `Column15` GUID , `Column16` DECIMAL , `Column2` VARCHAR , `Column3` LONG , `Column4` DateTime , `Column5` CURRENCY , `Column6` LONG NOT NULL IDENTITY PRIMARY KEY, `Column7` BIT , `Column8` OLEOBJECT , `Column9` VARCHAR );
一切正常,但由上述SQL创建的表与通过MS Access接口创建的表之间存在一些差异。