2014-09-30 36 views
0

我遇到以下SQL語句的問題。任何幫助,將不勝感激。更新聲明SQL錯誤不正確的語法SliderTitle和Where語句

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 

這是錯誤消息。

消息102,級別15,狀態1,行6
附近有語法錯誤SliderTitle'。

+2

看起來像你缺少你'UPDATE'子句中的字'SET'。可能你可能想爲該語句添加一個WHERE子句,因爲它會更新表中的所有行。 – sgeddes 2014-09-30 23:37:53

回答

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 
+0

+1 **和**可能還希望將WHERE SliderID = 17'子句添加到更新中 - 否則**整個表**將更新爲新值! – 2014-10-01 05:01:52

0

有幾件事情錯了你UPDATE聲明:

  1. 你需要一個SET上市前的列值改變
  2. 您(可能)需要WHERE子句,除非您想在表中更新每行
  3. 在結尾

需要一個分號試試這個:

... 

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 

...