'。'附近的SQL数据库语法错误

时间:2015-10-14 15:33:30

标签: asp.net sql-server

我在读取数据库时遇到错误。“。”附近的语法不正确。 这是我的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 个答案:

答案 0 :(得分: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