sql - 使用变量名称创建新的 mdb 表

标签 sql ms-access asp-classic

我正在尝试在现有 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/

相关文章:

dll - 64 位操作系统上的 32 位 ActiveX DLL 问题

VBScript 执行方法不声明变量

mysql - 从表 B 中选择与表 A 匹配的所有值

vba - 如何在代码中 Access 控件的标签

sql - 由于是/否而产生多条记录

sql - IP 地址范围的 MS Access SQL 查询

asp-classic - 找到 Server.CreateObject 试图做什么

java - 如何从 java long 获取秒数,我将其作为 bigint 存储在 mssql DB 中

mysql - 两列的不同计数

sql - 从优化中删除