【问题标题】:Error when inserting beers into beer table with T-SQL [closed]使用 T-SQL 将啤酒插入啤酒表时出错 [关闭]
【发布时间】:2013-03-26 10:04:02
【问题描述】:

我正在尝试将 7 种不同类型的啤酒插入我使用 T-SQL 创建的名为 Beers 的表中。我的 VALUES 中不断出现错误,但消息如下:

执行查询时出现以下错误:

Server: Msg 156, Level 15, State 1, Line 6
Incorrect syntax near the keyword 'VALUES'. 
Incorrect syntax near the keyword 'VALUES'. 
Incorrect syntax near the keyword 'VALUES'. 
Incorrect syntax near the keyword 'VALUES'. 
Incorrect syntax near the keyword 'VALUES'. 
Incorrect syntax near the keyword 'VALUES'. 
Incorrect syntax near the keyword 'VALUES'.

我有什么遗漏吗?我无法弄清楚错误是什么。

这是我的 SQL

--将啤酒添加到数据库

SET IDENTITY_INSERT [dbo].[Beers] ON

IF NOT EXISTS (SELECT [BeerId] FROM [dbo].[Beers] WHERE [BeerId] = 1)
 INSERT INTO [dbo].[Beers] ([BeerId], [BeerName], [BeerPricePerBottle], [BeerType],    [BeerRating], [Enabled], [DateInserted],[DateLastActivity],
 VALUES (1, 'Black_Butte', '$9.00', 'porter', NULL, 1, '20071111', '20071111')

IF NOT EXISTS (SELECT [BeerId] FROM [dbo].[Beers] WHERE [BeerId] = 2)
 INSERT INTO [dbo].[Beers] ([BeerId], [BeerName], [BeerPricePerBottle], [BeerType],  [BeerRating],  [Enabled], [DateInserted],[DateLastActivity],
 VALUES (2, 'Corona', '$5.00', 'lager', NULL, 1, '20071111', '20071111')

IF NOT EXISTS (SELECT [BeerId] FROM [dbo].[Beers] WHERE [BeerId] = 3)
 INSERT INTO [dbo].[Beers] ([BeerId], [BeerName], [BeerPricePerBottle], [BeerType], [BeerRating], [Enabled], [DateInserted],[DateLastActivity],
 VALUES (3, 'Duvel', '$12.00', 'pale ale', NULL, 1, '20071111', '20071111')

IF NOT EXISTS (SELECT [BeerId] FROM [dbo].[Beers] WHERE [BeerId] = 4)
 INSERT INTO [dbo].[Beers] ([BeerId], [BeerName], [BeerPricePerBottle], [BeerType], [BeerRating], [Enabled], [DateInserted],[DateLastActivity],
 VALUES (4, 'Guinness', '$7.00', 'stout', NULL, 1, '20071111', '20071111')

IF NOT EXISTS (SELECT [BeerId] FROM [dbo].[Beers] WHERE [BeerId] = 5)
 INSERT INTO [dbo].[Beers] ([BeerId], [BeerName], [BeerPricePerBottle], [BeerType], [BeerRating], [Enabled], [DateInserted],[DateLastActivity],
 VALUES (5, 'Heineken', '$6.00', 'lager', NULL, 1, '20071111', '20071111')

IF NOT EXISTS (SELECT [BeerId] FROM [dbo].[Beers] WHERE [BeerId] = 6)
 INSERT INTO [dbo].[Beers] ([BeerId], [BeerName], [BeerPricePerBottle], [BeerType], [BeerRating], [Enabled], [DateInserted],[DateLastActivity],
 VALUES (6, 'Pilsner_Urquell', '$6.50', 'pilsner', NULL, 1, '20071111', '20071111')

IF NOT EXISTS (SELECT [BeerId] FROM [dbo].[Beers] WHERE [BeerId] = 7)
 INSERT INTO [dbo].[Beers] ([BeerId], [BeerName], [BeerPricePerBottle], [BeerType], [BeerRating], [Enabled], [DateInserted],[DateLastActivity],
 VALUES (7, 'Stone', '$10.00', 'IPA', NULL, 1, '20071111', '20071111')


SET IDENTITY_INSERT [dbo].[Beers] OFF

【问题讨论】:

    标签: asp.net sql tsql database-design sql-insert


    【解决方案1】:

    您在 VALUES 之前缺少右括号:

    INSERT INTO [dbo].[Beers] ([BeerId], [BeerName], [BeerPricePerBottle], [BeerType], [BeerRating], [Enabled], [DateInserted],[DateLastActivity]) VALUES 
    

    【讨论】:

    • 你是绝对正确的。谢谢你!在漫长的一夜工作之后,让另一双眼睛看着某样东西会有所帮助。我会将此标记为已回答。再次感谢!
    【解决方案2】:

    您缺少右括号,并在每个 INSERT 语句的末尾添加了额外的逗号...

    像下面这样改变,它会起作用的,

    来自

    ,[DateLastActivity],
     VALUES (1, 'Black_Butte', '$9.00', 'porter', NULL, 1, '20071111', '20071111')
    

    ,[DateLastActivity])
     VALUES (1, 'Black_Butte', '$9.00', 'porter', NULL, 1, '20071111', '20071111')
    

    【讨论】:

      【解决方案3】:

      试试这个:

      如果不存在(从 [dbo].[Beers] WHERE [BeerId] = 1 中选择 [BeerId]) 插入([dbo].[Beers]([BeerId]、[BeerName]、[BeerPricePerBottle]、[BeerType]、[BeerRating]、[Enabled]、[DateInserted]、[DateLastActivity]) VALUES (1, 'Black_Butte', '$9.00', 'porter', NULL, 1, '20071111', '20071111')

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-08-03
        • 2014-12-09
        • 2016-10-06
        • 1970-01-01
        • 2021-04-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多