2015-10-14 60 views
2

我在阅读数据库时遇到错误。在'。'附近有正确的语法。 这是我的SQL查询。我应该改变什么吗?'。'附近的sql数据库语法错误

SELECT 
    Item.item_id AS 'ID', 
    Item.item_type AS 'Item Type', 
    Item.item_description AS 'Item Description', 
    Item.purchase_year AS 'Purchase Year', 
    Item.purchase_price AS 'Purchase Price', 
    Player.fname AS 'First Name', 
    Player.lname AS 'Last Name', 
    Player.sport AS 'Sport', 
    Player.position AS 'Position', 
    Player.team_f AS 'Current Team', 
    Item.inscription AS 'Inscription', 
    Item.dedication AS 'Dedication', 
    Item.condition AS 'Condition', 
    Item.value AS 'Estimate Value' 
FROM Item 
INNER JOIN Player 
    ON Item.player_id = Player.player_id 
WHERE Item.item_type LIKE @SearchPam 
OR Item.item_description LIKE @SearchPam 
OR Player.fname LIKE @SearchPam 
OR Player.lname LIKE @SearchPam 
OR Player.sport LIKE @SearchPam 
OR Player.position LIKE @SearchPam 
OR Player.team_f LIKE @SearchPam 
OR Player.team_s LIKE @SearchPam 
OR Player.team_t LIKE @SearchPam 
+1

我在运行查询时没有收到任何语法错误。你确定这是你得到错误的实际查询吗? – Guffa

回答

2

我会改变你的整个方法。你应该使用表别名,你需要规范化你的数据(team_f,team_s表示你有重复的列),你需要格式化你的代码,以便清晰易读,而不是将几十行代码放入一个。我担心你对每一个谓词都喜欢。如果这有一个领先的通配符,你的表现将会很糟糕。我所做的最后一项更改是使用存储过程而不是通过sql,因此可以将数据与应用程序分开。

SELECT i.item_id AS ID 
    , i.item_type AS ItemType 
    , i.item_description AS ItemDescription 
    , i.purchase_year AS PurchaseYear 
    , i.purchase_price AS PurchasePrice 
    , p.fname AS FirstName 
    , p.lname AS LastName 
    , p.sport AS Sport 
    , p.position AS Position 
    , p.team_f AS CurrentTeam 
    , i.inscription AS Inscription 
    , i.dedication AS Dedication 
    , i.condition AS Condition 
    , i.value AS EstimateValue 
FROM Item AS i 
INNER JOIN Player AS p ON i.player_id = p.player_id 
WHERE i.item_type LIKE @SearchPam 
    OR i.item_description LIKE @SearchPam 
    OR p.fname LIKE @SearchPam 
    OR p.lname LIKE @SearchPam 
    OR p.sport LIKE @SearchPam 
    OR p.position LIKE @SearchPam 
    OR p.team_f LIKE @SearchPam 
    OR p.team_s LIKE @SearchPam 
    OR p.team_t LIKE @SearchPam 
+0

感谢您错过了别名@M。绿蓝。 AS字不是必需的,但许多人更喜欢它。 –

+1

我在查询中更改了一个额外的东西,我没有在我的回复中提及,因为您不应将列别名换成单引号。最好的方法是从列名中删除空格,但如果您真的认为需要,最好将名称封装在[方括号]中。 –

+1

没有问题 - 是的,但我加了它,因为它是你的反馈意见 - 使它易于阅读:) –