基于子选择结果的T-sql更新值

时间:2013-02-07 11:47:30

标签: sql sql-server-2008 tsql select sql-update

需要使用MS SQL 2008和存储过程。

所以在我的表中我有一些IsDouble值为2的行。 现在我需要将第一个选择中每行的IsDouble从2更新为1,如果字符串的数量(从4列连接)大于1并且字符串的计数是1到0

到目前为止,我有这个,但它很可能不正确:

select * from TestInvoiceData where Isdouble='2';
update TestInvoiceData
set testinvoicedata.Isdouble=
case 
    when  
    (
        select COUNT (InvoiceDate+InvoiceNumber+VendorCode+Invoicetype) 
        from      TestInvoiceData
     ) >1 then 1
     else 0
end;

修改

以下是示例数据(invoicetype,invoicenumber,invoicedate,vendorcode):

INVO    322760-262  2012-05-10  0000081964  2
INVO    322760-262  2012-05-10  0000081964  0
INVO    322756-262  2012-05-10  0000081964  2
INVO    7011200072  2012-05-10  0000046172  0
INVO    7011200071  2012-05-10  0000046172  0
INVO    7011200070  2012-05-10  0000046172  0
INVO    7011200069  2012-05-10  0000046172  0
INVO    7011200068  2012-05-10  0000046172  0
INVO    12106563    2012-04-24  0000010171  0
INVO    06649       2012-04-24  0000067987  0

并基于此示例,第一行应该在更新1之后,第三行应该在0

之后

2 个答案:

答案 0 :(得分:2)

查询:

<强> SQLFIDDLEEXAmple

UPDATE TestInvoiceData
SET Isdouble = (
SELECT   CASE WHEN count(*)>1 THEN 1
         ELSE 0 END AS cnt
FROM TestInvoiceData t1
  WHERE t1.invoicetype =TestInvoiceData.invoicetype 
  AND   t1.invoicenumber = TestInvoiceData.invoicenumber
  AND   t1.invoicedate = TestInvoiceData.invoicedate
  AND   t1.vendorcode = TestInvoiceData.vendorcode
GROUP BY t1.invoicetype,
         t1.invoicenumber,
         t1.invoicedate,
         t1.vendorcode)
WHERE Isdouble = '2'

结果:

| INVOICETYPE | INVOICENUMBER |                  INVOICEDATE | VENDORCODE | ISDOUBLE |
--------------------------------------------------------------------------------------
|        INVO |    322760-262 |   May, 10 2012 00:00:00+0000 |      81964 |        1 |
|        INVO |    322760-262 |   May, 10 2012 00:00:00+0000 |      81964 |        0 |
|        INVO |    322756-262 |   May, 10 2012 00:00:00+0000 |      81964 |        0 |
|        INVO |    7011200072 |   May, 10 2012 00:00:00+0000 |      46172 |        0 |
|        INVO |    7011200071 |   May, 10 2012 00:00:00+0000 |      46172 |        0 |
|        INVO |    7011200070 |   May, 10 2012 00:00:00+0000 |      46172 |        0 |
|        INVO |    7011200069 |   May, 10 2012 00:00:00+0000 |      46172 |        0 |
|        INVO |    7011200068 |   May, 10 2012 00:00:00+0000 |      46172 |        0 |
|        INVO |      12106563 | April, 24 2012 00:00:00+0000 |      10171 |        0 |
|        INVO |         06649 | April, 24 2012 00:00:00+0000 |      67987 |        0 |

答案 1 :(得分:0)

我想你需要

update TestInvoiceData set

Isdouble=CASE 
            WHEN (select count(*) from TestInvoiceData as Tid
                  where 
                  tId.InvoiceDate=InvoiceDate
                  AND
                  tId.InvoiceNumber=InvoiceNumber
                  AND
                  tId.VendorCode=VendorCode
                  AND
                  tId.Invoicetype =Invoicetype 
                 ) > 1
                 THEN 1
                 ELSE 0
             END

where Isdouble=2