2012-07-05 82 views
1

格式此查詢確定,在調試時我看到價值更新,但只有告訴我這個消息{"Invalid column name 'Mohannad' "},請幫我傢伙無效列名「*******」

UPDATE Employee 
SET Name =Mohannad 
, Age=22 
, GenderID =1 
, CountryID=1 
, Mobile=8765 
FROM Employee 
    INNER JOIN Country ON Employee.CountryID = Country.CountryID 
    INNER JOIN Gender ON Employee.GenderID = Gender.GenderID 
WHERE EmployeeID=1 ; 

SELECT Employee.EmployeeID, Employee.Name, Employee.Age, Employee.GenderID, Gender.GenderName, Employee.CountryID, Country.CountryName, Employee.Mobile 
FROM Employee 
    INNER JOIN Country ON Employee.CountryID = Country.CountryID 
    INNER JOIN Gender ON Employee.GenderID = Gender.GenderID 
+1

順便問一下試試 - 你是如何產生這個命令?如果你使用了帶參數的命令,你可以避免這種混亂,並且有更安全的代碼:http://en.wikipedia.org/wiki/SQL_injection – Kobi 2012-07-05 19:41:20

回答

3

你需要Mohannad周圍的報價:

SET Name='Mohannad' 

如果沒有引號,數據庫引擎會推測它是列的名稱。

如果您在程序中生成此查詢,則應該使用預準備語句,而不是僅在名稱周圍加引號以避免錯誤和注入。

+3

SQL像這樣的字符串呢? – leppie 2012-07-05 19:42:41

0

Monhannad應該是在引號

UPDATE Employee 
SET Name = 'Mohannad', Age=22, GenderID =1, CountryID=1, Mobile=8765 
FROM Employee 
    INNER JOIN Country ON Employee.CountryID = Country.CountryID 
    INNER JOIN Gender ON Employee.GenderID = Gender.GenderID 
WHERE EmployeeID=1 ; 
3

與此代碼

SET Name='Mohannad' 
+2

+1表示正確的引用字符。 – Ishmael 2012-07-05 19:42:43

+0

謝謝你的工作:-) – 2012-07-09 06:43:25

+0

只將值放在你的值附近並不能逃脫它,並且讓你的數據庫保持開放狀態。這是做錯的方法。 – Walf 2012-07-25 01:54:01