更新语句SQL错误语法SliderTitle和Where语句不正确

时间:2014-09-30 23:36:15

标签: sql

我遇到以下SQL语句的问题。任何帮助将不胜感激。

Select * 
From dbo.Site_tblSliderFeatures 
WHERE SliderID =17 
GO

BEGIN TRAN

Update dbo.Site_tblSliderFeatures  

SliderTitle = 'Racing to the Future',
SliderCaption = 'caption.',
SliderStart = '2014-09-30',
SliderEnd = '2014-10-7',
SliderPhoto = 'images/structure/RacingtotheFuture.png',
SliderLink = 'link'

SELECT * 
FROM dbo.Site_tblSliderFeatures 
WHERE SliderID = 17

-- COMMIT TRAN
ROLLBACK TRAN

以下是错误消息。

  

Msg 102,Level 15,State 1,Line 6
  ' SliderTitle'附近的语法不正确。

2 个答案:

答案 0 :(得分:2)

'错误的语法'事实上SET应该在UPDATE声明中 - 这应该有效:

USE HomePage

Select * From dbo.Site_tblSliderFeatures WHERE SliderID =17 

GO

BEGIN TRAN

Update dbo.Site_tblSliderFeatures  
Set SliderTitle='Racing to the Future',
SliderCaption='caption.',
SliderStart='2014-09-30',
SliderEnd='2014-10-7',
SliderPhoto='images/structure/RacingtotheFuture.png',
SliderLink='link'

SELECT * FROM dbo.Site_tblSliderFeatures Where SliderID = 17

-- COMMIT TRAN
ROLLBACK TRAN

此外,作为sgeddes pointed out,在更新语句中添加Where子句可能是明智之举,因为此查询将更新表中的所有行。

我猜你真的想要这个:

SliderEnd='2014-10-7',
SliderPhoto='images/structure/RacingtotheFuture.png',
SliderLink='link'
Where SliderID=17 -- Limit it to only rows where SliderID=17

SELECT * FROM dbo.Site_tblSliderFeatures Where SliderID = 17

答案 1 :(得分:0)

您的UPDATE声明存在一些问题:

  1. 在列出列值更改
  2. 之前,您需要SET
  3. 您(可能)需要一个WHERE子句,除非您想要更新表中的每一行
  4. 你最后需要一个分号
  5. 试试这个:

    ...
    
    UPDATE dbo.Site_tblSliderFeatures SET -- Added SET
    SliderTitle='Racing to the Future',
    SliderCaption='caption.',
    SliderStart='2014-09-30',
    SliderEnd='2014-10-7',
    SliderPhoto='images/structure/RacingtotheFuture.png',
    SliderLink='link'
    Where SliderID = 17; -- Added where clause and ";"
    
    SELECT * FROM dbo.Site_tblSliderFeatures Where SliderID = 17; -- Added ";" here too
    
    ...