我正在尝试在现有 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
%>
最佳答案
如果没有实际的错误消息,就很难给出更有用的答案。但是,您的生产代码中可能存在也可能不存在一个错误。您缺少表名称周围的空格,因此:
<%
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
%>
关于sql - 使用变量名称创建新的 mdb 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17796001/