Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
基于子选择结果的T-sql更新值_Sql_Sql Server 2008_Tsql_Select_Sql Update - Fatal编程技术网

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

基于子选择结果的T-sql更新值,sql,sql-server-2008,tsql,select,sql-update,Sql,Sql Server 2008,Tsql,Select,Sql Update,MS SQL 2008正在使用,需要存储过程 所以在我的表中,我有一些行,其中IsDouble值是2。 现在,如果字符串的计数(由4列串联而成)大于1,并且字符串的计数为1到0,我需要从第一个select开始将每行的IsDouble从2更新为1 到目前为止,我有这个,但它很可能是不正确的: select * from TestInvoiceData where Isdouble='2'; update TestInvoiceData set testinvoicedata.Isdouble= c

MS SQL 2008正在使用,需要存储过程

所以在我的表中,我有一些行,其中IsDouble值是2。 现在,如果字符串的计数(由4列串联而成)大于1,并且字符串的计数为1到0,我需要从第一个select开始将每行的IsDouble从2更新为1

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

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):

根据这个例子,第一行应该在更新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
查询:

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 |

您需要提供示例数据。。。有了这样的查询,您将命名一个列
Isdouble
,它不是
bool/bit
?它不是bool/bit,它是nchar,我将添加示例数据您可以在update语句中使用
WHERE
,示例数据如下:
| 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 |