SQL语法错误CREATE表

时间:2013-05-15 19:55:04

标签: sql-server vb.net tsql ddl

尝试执行此查询时出现语法错误:

USE MainDB1
IF EXISTS (SELECT * FROM sysobjects WHERE Name = 'logging' AND TYPE = 'u')
BEGIN
DROP TABLE MainDB1.dbo.logging
END
Create Table logging (TIME NVarChar(20) NOT NULL,1-Bit_Boolean NVarChar(20) NOT NULL,1-Bit_Boolean NVarChar(20) NOT NULL,1-Bit_Boolean NVarChar(20) NOT NULL,1-Bit_Boolean NVarChar(20) NOT NULL,64-Bit_IEEE_floating_point NVarChar(20) NOT NULL,64-Bit_IEEE_floating_point NVarChar(20) NOT NULL,64-Bit_IEEE_floating_point NVarChar(20) NOT NULL,64-Bit_IEEE_floating_point NVarChar(20) NOT NULL,64-Bit_IEEE_floating_point_array NVarChar(20) NOT NULL,32-Bit_unsigned_integer NVarChar(20) NOT NULL,32-Bit_unsigned_integer NVarChar(20) NOT NULL,32-Bit_unsigned_integer NVarChar(20) NOT NULL,32-Bit_unsigned_integer NVarChar(20) NOT NULL,32-Bit_unsigned_integer_array NVarChar(20) NOT NULL,32-Bit_IEEE_floating_point NVarChar(20) NOT NULL,32-Bit_IEEE_floating_point NVarChar(20) NOT NULL,32-Bit_IEEE_floating_point NVarChar(20) NOT NULL,32-Bit_IEEE_floating_point NVarChar(20) NOT NULL,32-Bit_IEEE_floating_point_array NVarChar(20) NOT NULL,32-Bit_signed_integer NVarChar(20) NOT NULL,32-Bit_signed_integer NVarChar(20) NOT NULL,32-Bit_signed_integer NVarChar(20) NOT NULL,32-Bit_signed_integer NVarChar(20) NOT NULL,32-Bit_signed_integer_array NVarChar(20) NOT NULL,16-Bit_signed_integer NVarChar(20) NOT NULL,16-Bit_signed_integer NVarChar(20) NOT NULL,16-Bit_signed_integer NVarChar(20) NOT NULL,16-Bit_signed_integer NVarChar(20) NOT NULL,16-Bit_signed_integer_array NVarChar(20) NOT NULL,16-Bit_unsigned_integer NVarChar(20) NOT NULL,16-Bit_unsigned_integer NVarChar(20) NOT NULL,16-Bit_unsigned_integer NVarChar(20) NOT NULL,16-Bit_unsigned_integer NVarChar(20) NOT NULL,16-Bit_unsigned_integer_array NVarChar(20) NOT NULL)

语法错误发生在第6行的第一个1-Bit_Boolean。查询保存在字符串中,稍后执行。

VB.NET代码:

    Dim sqlcreatetable As String = _
    "USE MainDB1" & vbCrLf & _
    "IF EXISTS (SELECT * FROM sysobjects WHERE Name = 'logging' AND TYPE = 'u')" & vbCrLf & _
    "BEGIN" & vbCrLf & _
    "DROP TABLE MainDB1.dbo.logging" & vbCrLf & _
    "END" & vbCrLf & _
    "Create Table logging (TIME NVarChar(20) NOT NULL,"
    For i As Integer = 1 To aantaltags - 1
        If (i = aantaltags - 1) Then
            sqlcreatetable += csvsql(i) & " NVarChar(20) NOT NULL)"
        Else
            sqlcreatetable += csvsql(i) & " NVarChar(20) NOT NULL,"
        End If
    Next

    Dim dbConnection As New SqlCommand(sqlcreatetable, connection)
    connection.Open()
    dbConnection.ExecuteNonQuery()
    connection.Close()

1 个答案:

答案 0 :(得分:1)

您无法在列名称的开头使用数字,也不能在列名称的任何位置使用“ - ”。你必须写一些类似的查询。

USE MainDB1 

IF EXISTS (SELECT * FROM sysobjects WHERE Name = 'logging' AND TYPE = 'u') 
BEGIN 
    DROP TABLE MainDB1.dbo.logging 
END 

Create Table logging  
   (TIME NVarChar(20) NOT NULL, Bit_1_Boolean NVarChar(20) NOT NULL)