简体   繁体   中英

Loop through records and update database on conditions

I have a table Person :

在此处输入图片说明

Where ID is the primary key, Male is the total number of males, Female is the total number of females, Person_ID column is fixed to 4, so maximum number of males + females is 4 (we don't have to validate this).

I need to insert/update into the last column Is_Valid values of Yes or No , to determine whether the record is valid.

Here is the logic: for example total number of males and females is 3, then there have to be 3 person IDs, or if total males and females is 2 then must have 2 person ID data.

So the record ID 3 and 4 is invalid, because record 3 - there are 2 people (1 male 1 female) but there is only 1 person ID, record 4 there is 1 person (1 female) but there is no person ID data at all.

I need to do this programmatically in the c# code, but first I want to know the queries that can solve this problem?

The logic I've been thinking is that:

  • Loop through each row
  • Get sum of Males + Females = a
  • Somehow count the none-null Person ID cells = b
  • if a == b then insert YES else insert NO

I don't know how to write the query (or stored procedure), I guess it would be a little bit complicated. Any help would be much appreciated.

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

No need of looping

;WITH CTE
AS (
    SELECT id
        ,CASE 
            WHEN (male + female) = isnull(personid_1 / personid_1, 0) 
                                  + isnull(personid_2 / personid_2, 0)
                                  + isnull(personid_3 / personid_3, 0)
                                  + isnull(personid_4 / personid_4, 0)
                THEN 'Yes'
            ELSE 'No'
            END AS valid
    FROM PERSON
    )
UPDATE PERSON
SET IS_VALID = CTE.VALID
FROM CTE
WHERE PERSON.ID = CTE.ID

You can run an update statement for this, and don't have to loop through it. Or you can make the is_valid a computed column.

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

Add Male + Female column values and compare them with number of columns that have non-null or empty values. If they are equal, set Is_Valid to 1 otherwise set it to 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

Just to clarify the code below checks to see if the column has null value and makes it empty and then checks if it is empty, if yes makes it 0 otherwise makes it 1. This is done for each column:

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

You can do this too to be more careful and check for whitespaces:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM