2

我正在尝试在现有 mdb 数据库中创建新表,但使用一个变量分配新表名,该变量的值从输入到表单的输入中设置。如果我为新表指定名称但在使用变量值时失败,它工作正常。

我看过一些提到使用动态 sql 但对 sql 完全不熟悉的帖子,我无法掌握这些建议。任何帮助,将不胜感激。

<%
table1 = Session("tableName")

set conn = server.CreateObject ("ADODB.Connection")
conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & server.MapPath ("database.mdb")

Dim strSQL

'this works with explict naming and creates a table named table1
'strSQL = "CREATE TABLE table1 (field1 int, field2 char)"

'this does not work when trying to use a variable to set the tables name
 strSQL = "CREATE TABLE" & table1 & "(field1 int, field2 char)"

conn.Execute strSQL
conn.Close
%>
4

1 回答 1

2

如果没有实际的错误消息,就很难给出更有用的答案。但是,您的生产代码中可能存在也可能不存在一个错误。您缺少表名周围的空格,因此:

<%
table1 = Session("tableName")

set conn = server.CreateObject ("ADODB.Connection")
conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & server.MapPath ("database.mdb")

Dim strSQL

'this does not work when trying to use a variable to set the tables name
 strSQL = "CREATE TABLE" & table1 & "(field1 int, field2 char)"

'this should be just fine - note the space after TABLE and before the quote. 
'   ditto for the space after the quote and before the open parens
 strSQL = "CREATE TABLE " & table1 & " (field1 int, field2 char)"

conn.Execute strSQL
conn.Close
%>
于 2013-07-22T19:46:50.840 回答