我需要修改现有查询以返回在特定日期之后创建的记录

时间:2012-03-20 09:07:53

标签: sql sql-server-express

我有以下查询来返回所有已创建的项目代码,它们运行正常。

SELECT 
    ITM_CD, ITM_NAME, ITM_GROUP_NAME,
    ITM_ATTR01_DESC, ITM_ATTR02_DESC, ITM_ATTR03_DESC,
    ITM_ATTR04_DESC, ITM_ATTR05_DESC, ITM_ATTR06_DESC,
    ITM_ATTR07_DESC, ITM_ATTR08_DESC, ITM_ATTR09_DESC,
    ITM_ATTR10_DESC, ITM_ATTR11_DESC, ITM_ATTR12_DESC,
    ITM_ATTR13_DESC, ITM_ATTR14_DESC, BSP_SELL_PRICE,
    BCP_COST_PRICE, VEN_VENDOR_NAME 
FROM 
    ITEM_DESCRIPTION, 
    BASIC_SP_MST,
    BASIC_CP_MST,
    VENDOR_MST 
WHERE 
    ITM_STATUS='O'AND 
    BSP_ORG_CD='001'AND 
    ITM_CD=BSP_ITEM_CD AND 
    ITM_CD=BCP_VEND_ITEM_CD AND 
    BCP_VEND_CD=VEN_VENDOR_CD AND 
    BCP_ORG_cD='001'

但是,我希望看到在特定日期之后创建的项目代码,例如。 2012年3月16日。

为此我尝试了

SELECT 
    ITM_CD, ITM_NAME, ITM_GROUP_NAME,
    ITM_ATTR01_DESC, ITM_ATTR02_DESC, ITM_ATTR03_DESC,
    ITM_ATTR04_DESC, ITM_ATTR05_DESC, ITM_ATTR06_DESC,
    ITM_ATTR07_DESC, ITM_ATTR08_DESC, ITM_ATTR09_DESC,
    ITM_ATTR10_DESC, ITM_ATTR11_DESC, ITM_ATTR12_DESC,
    ITM_ATTR13_DESC, ITM_ATTR14_DESC, BSP_SELL_PRICE,
    BCP_COST_PRICE, VEN_VENDOR_NAME 
FROM 
    ITEM_DESCRIPTION, 
    BASIC_SP_MST,
    BASIC_CP_MST,
    VENDOR_MST 
WHERE 
    ITM_STATUS='O'AND 
    BSP_ORG_CD='001'AND 
    ITM_CD=BSP_ITEM_CD AND 
    ITM_CD=BCP_VEND_ITEM_CD AND 
    BCP_VEND_CD=VEN_VENDOR_CD AND 
    BCP_ORG_cD='001' AND 
    LAST_UPDATED_DATE > '16-MARCH-2012'

但得到了错误

  

Msg 209,Level 16,State 1,Line 3   不明确的列名称'LAST_UPDATED_DATE'。

我做错了什么,我该怎么做才能纠正它。

我正在使用MS SQL Server Management Studio Express

正如你所看到的,我甚至不是新手,因此感谢与我的关系。

任何帮助都将受到高度赞赏。

谢谢, RItin

3 个答案:

答案 0 :(得分:4)

这意味着LAST_UPDATED_DATE是2个表中的列名,而SQL引擎不知道要采用哪个。

您可以通过添加表格来告诉引擎:

table_name.LAST_UPDATED_DATE

您的查询:

SELECT ITM_CD,
   ITM_NAME,
   ITM_GROUP_NAME,
   ITM_ATTR01_DESC,
   ITM_ATTR02_DESC,
   ITM_ATTR03_DESC,
   ITM_ATTR04_DESC,
   ITM_ATTR05_DESC,
   ITM_ATTR06_DESC,
   ITM_ATTR07_DESC,
   ITM_ATTR08_DESC,
   ITM_ATTR09_DESC,
   ITM_ATTR10_DESC,
   ITM_ATTR11_DESC,
   ITM_ATTR12_DESC,
   ITM_ATTR13_DESC,
   ITM_ATTR14_DESC,
   BSP_SELL_PRICE,
   BCP_COST_PRICE,
   VEN_VENDOR_NAME 
FROM ITEM_DESCRIPTION,BASIC_SP_MST,BASIC_CP_MST,VENDOR_MST 
WHERE ITM_STATUS='O'
  AND BSP_ORG_CD='001'
  AND ITM_CD=BSP_ITEM_CD 
  AND ITM_CD=BCP_VEND_ITEM_CD 
  AND BCP_VEND_CD=VEN_VENDOR_CD 
  AND BCP_ORG_cD='001' 
  AND tablename.LAST_UPDATED_DATE > '16-MARCH-2012'  <-- here is a table name needed for LAST_UPDATED_DATE

更新: 顺便说一下,我认为使用字符串16-MARCH-2012是行不通的。请尝试使用16-03-2012进行日期比较。

答案 1 :(得分:0)

该消息表示您从中选择数据的多个表包含列LAST_UPDATED_DATE。在列名称之前添加前缀

答案 2 :(得分:0)

当多个表中存在具有相同名称的列时会发生这种情况....从您的方案中,“LAST_UPDATED_DATE”似乎出现在多个表中.....因此,请指定表名以及列名.........

table_name.LAST_UPDATED_DATE