我正在尝试在现有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
%>
答案 0 :(得分: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
%>