循环记录并根据条件更新数据库

时间:2016-12-30 19:50:05

标签: c# sql asp.net sql-server

我有一张表Person

enter image description here

ID是主键,Male是男性总数,Female是女性总数,Person_ID列固定为4,所以男性和女性的最大数量是4(我们不必验证这一点)。

我需要插入/更新Is_ValidYes的最后一列No值,以确定该记录是否有效。

这是逻辑:例如,男性和女性的总数是3,那么必须有3个人ID,或者如果总男性和女性是2,那么必须有2个人ID数据。

因此记录ID 3和4无效,因为记录3 - 有2个人(1个男性1个女性)但只有1个人ID,记录4个有1个人(1个女性)但没有人ID数据。

我需要在c#代码中以编程方式执行此操作,但首先我想知道可以解决此问题的查询吗?

我一直在想的逻辑是:

  • 遍历每一行
  • 获得男性+女性的总和= a
  • 以某种方式计算非空人ID ID = b
  • 如果a == b则插入YES,否则插入NO

我不知道如何编写查询(或存储过程),我想这会有点复杂。任何帮助将不胜感激。

5 个答案:

答案 0 :(得分:2)

UPDATE TABLE
SET Is_Valid = CASE WHEN (
               Case when Person1_ID != '' or Person1_ID IS NOT NULL Then 1 else 0 end +
               Case when Person2_ID != '' or Person2_ID IS NOT NULL Then 1 else 0 end +
               Case when Person3_ID != '' or Person3_ID IS NOT NULL Then 1 else 0 end +
               Case when Person4_ID != '' or Person4_ID IS NOT NULL Then 1 else 0 end ) != Male + Female THEN 'NO' ELSE 'YES' END

答案 1 :(得分:1)

CREATE TABLE #Demo   
(   
id integer not null primary key,
male integer NULL,   
female integer NULL,   
PersonId_1 integer null,
PersonId_2 integer null,   
PersonId_3 integer null,
PersonId_4 integer null,
IsValid bit not null
);  

insert into #Demo
(id, male, female, PersonId_1,PersonId_2,PersonId_3,PersonId_4,IsValid)
values
(1,1,1,2,2,null,null,0)

update #Demo
set IsValid = 1
where (#Demo.male + #Demo.female) = 
(case when #Demo.PersonId_1 IS NOT NULL Then 1 Else 0 end)+
(case when #Demo.PersonId_2 IS NOT NULL Then 1 Else 0 end)+
(case when #Demo.PersonId_3 IS NOT NULL Then 1 Else 0 end)+
(case when #Demo.PersonId_4 IS NOT NULL Then 1 Else 0 end)

select * from #Demo

答案 2 :(得分:1)

无需循环

getSnapshot()

答案 3 :(得分:1)

您可以为此运行更新语句,而不必循环遍历它。或者您可以将is_valid设为计算列。

DECLARE @test TABLE (
id INT IDENTITY(1,1)
,male INT
,female INT
,person1_id INT
,person2_id INT
,person3_id INT
,person4_id INT
,is_valid AS CASE
    WHEN  (male + female = 1 AND person1_id IS NOT NULL AND person2_id IS NULL AND person3_id IS NULL AND person4_id IS NULL)
        OR (male + female = 2 AND person1_id IS NOT NULL AND person2_id IS NOT NULL AND person3_id IS NULL AND person4_id IS NULL)
        OR (male + female = 3 AND person1_id IS NOT NULL AND person2_id IS NOT NULL AND person3_id IS NULL AND person4_id IS NULL)
        OR (male + female = 4 AND person1_id IS NOT NULL AND person2_id IS NOT NULL AND person3_id IS NOT NULL AND person4_id IS NULL)
    THEN 1
    ELSE 0
    END
)

INSERT @test (male,female,person1_id,person2_id,person3_id,person4_id)
VALUES
(1,1,200,700,NULL,NULL)
,(2,0,205,210,NULL,NULL)
,(1,1,240,NULL,NULL,NULL)
,(0,1,NULL,NULL,NULL,NULL)

SELECT * FROM @test

答案 4 :(得分:1)

添加Male + Female列值,并将它们与具有非null值或空值的列数进行比较。如果它们相等,则将Is_Valid设置为1,否则将其设置为0.

update p
set Is_Valid =
                case
                    when Male + Female = (iif(Isnull(Person1_ID, '') = '', 0, 1) + iif(Isnull(Person2_ID, '') = '', 0, 1) +
                        iif(Isnull(Person3_ID, '') = '', 0, 1) + iif(Isnull(Person4_ID, '') = '', 0, 1)) then 'YES'
                    else 'NO'
                end

from Person p

只是为了澄清下面的代码,检查列是否具有空值并使其为空,然后检查它是否为空,如果是,则将其设为0,否则使其成为1.对每列执行此操作:

iif(Isnull(Person1_ID, '') = '', 0, 1)

您也可以这样做,以便更加小心并检查空格:

iif(Isnull(rtrim(ltrim(Person1_ID))), '') = '', 0, 1)