从选择不更新所有记录更新

时间:2016-02-04 21:31:14

标签: sql sql-server tsql sql-server-2014

我正在尝试“重新同步”一个表格,我将详细信息存储在EBay列表中。为此,我使用EBay API下载所有Active个列表,然后执行从select到resync的更新。

架构

CREATE TABLE [dbo].[tblEbayListings](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [EndTime] [datetime] NULL,
    [StartTime] [datetime] NULL,
    [ItemEbayURL] [varchar](500) NULL,
    [ListingDuration] [varchar](50) NULL,
    [ListingType] [varchar](50) NULL,
    [ListingStatus] [varchar](50) NULL,
    [SKU] [varchar](50) NULL
) ON [PRIMARY]

GO

用于存储通过API下载的当前活动列表。

CREATE TABLE [dbo].[tblEbayProductStatus](
    [PRProductId] [int] NOT NULL,
    [EbayListingId] [varchar](15) NULL,
    [DateListed] [datetime] NULL,
    [CostOfListing] [money] NULL,
    [DateOfStatusChange] [datetime] NULL,
    [CurrentStatus] [varchar](10) NULL,
    [ListingEndDate] [datetime] NULL,
 CONSTRAINT [PK_tblEbayProductStatus] PRIMARY KEY CLUSTERED 
(
    [PRProductId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO 

用于记录EBay列表状态和列出结束日期(我想要ReSync的表)

代码

 UPDATE
    eps
SET
    eps.CurrentStatus = CASE WHEN RTRIM(LTRIM(ebl.ListingStatus)) = 'Active' 
        THEN 'Listed' ELSE 'DeListed' END,
    eps.ListingEndDate = ebl.EndTime
FROM
    tblEbayProductStatus eps 
JOIN
    tblEbayListings ebl
ON
    eps.[PRProductId] = CAST(RTRIM(LTRIM(ebl.SKU)) As INT)

运行此代码后,此select

SELECT eps.[CurrentStatus]
      ,ebl.[ListingStatus]
      ,ebl.sku
      ,eps.[PRProductId]
      ,eps.ListingEndDate 
      ,ebl.EndTime
  FROM [tblEbayListings] ebl
  Join [tblEbayProductStatus] eps 
  ON eps.[PRProductId] = ebl.sku
  Where ebl.ListingStatus = 'Active'

显示有些更新,有些则没有.....

+---------------+---------------+--------+-------------+------------------+------------------+
| CurrentStatus | ListingStatus |  sku   | PRProductId | ListingEndDate   |     EndTime      |
+---------------+---------------+--------+-------------+------------------+------------------+
| DeListed      | Active        | 160008 |      160008 | 2016-01-15 14:30 | 2016-03-05 14:50 |
| Listed        | Active        | 160010 |      160010 | 2016-03-05 14:50 | 2016-03-05 14:50 |
| Listed        | Active        | 160012 |      160012 | 2016-03-05 14:50 | 2016-03-05 14:50 |
| Listed        | Active        | 160016 |      160016 | 2016-03-05 14:50 | 2016-03-05 14:50 |
| DeListed      | Active        | 160017 |      160017 | 2016-01-18 06:42 | 2016-02-17 06:50 |
| Listed        | Active        | 160018 |      160018 | 2016-03-05 14:51 | 2016-03-05 14:51 |
| DeListed      | Active        | 160026 |      160026 | 2016-02-02 10:30 | 2016-03-05 14:26 |
+---------------+---------------+--------+-------------+------------------+------------------+

如果我将选择的连接更改为

eps.[PRProductId] = CAST(RTRIM(LTRIM(ebl.SKU)) As INT)

我得到完全相同的结果。如果我将更新的连接更改为

eps.[PRProductId] = ebl.SKU

我得到完全相同的结果。

我知道我错过了一些愚蠢的事情,因为我之前已多次这样做,但我看不出有什么好看的。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:0)

您尝试此更新代码

UPDATE
    tblEbayProductStatus
SET
    CurrentStatus = CASE WHEN RTRIM(LTRIM(ebl.ListingStatus)) = 'Active' 
        THEN 'Listed' ELSE 'DeListed' END,
    ListingEndDate = ebl.EndTime
FROM
    tblEbayListings ebl
WHERE
    tblEbayProductStatus.[PRProductId] = CAST(RTRIM(LTRIM(ebl.SKU)) As INT)