Click here to Skip to main content
15,908,674 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,
I have a table. It has three columns: AccessionNo, MasterAccessionNo, Copies
I have to update the Copies column in rows having AccessionNo=MasterAccessionNo.

This is my table:
Id     AccessionNo    MasterAccessionNo   Copies
 1       001              001               3
 2       002              001               1
 3       003              001               1
 4       004              004               2
 5       005              004               1


Now if i am deleting a row manually with Id 2 from the table, then the copies should be updated from 3 to 2 where AccessionNo=MasterAccessionNo. If i am deleting more records then the corresponding copies should be updated checking that records AccessionNo=MasterAccessionNo


How can i do this
Posted
Updated 12-Jan-13 11:05am
v6
Comments
Reza Alipour Fard 11-Jan-13 3:19am    
your question has ambiguity.
you need update copies column? or delete some rows?
Do you need to group by table by MasterAccessionNo?
Please give more information.

This is a very basic question. Refer the below links for Update statment syntax
SQL UPDATE Statement[^]
UPDATE Basics in SQL Server[^]
More links here[^]

If you are looking for while loop example
refer Simple Example of WHILE Loop with BREAK and CONTINUE[^]
 
Share this answer
 
v2
Comments
Maciej Los 12-Jan-13 17:22pm    
Good answer, my 5!
First of all, you don't need to store "copies" in your table. Why? Because you can always get it in SELECT statement.
SQL
SELECT MasterAccessionNo, COUNT (MasterAccessionNo) AS Copies
FROM YourTable

But if you still want to store "Copies" in your table, use UPDATE command as _TR_ wrote.
SQL
UPDATE YourTable YT
SET
    YT.Copies = YT1.Copies
FROM YT INNER JOIN (
    SELECT MasterAccessionNo, COUNT(MasterAccessionNo) AS Copies
    FROM YourTable
    ) AS YT1 ON YT.AccessionNo = YT1.MasterAccessionNo
 
Share this answer
 
SQL
DECLARE @TableVar AS TABLE
(
   ID INT IDENTITY(1,1),
   AccessionNo INT, 
   MasterAccessionNo INT, 
   Copies INT
);

DECLARE @CurrentIndex AS INT = 1;
DECLARE @TotalCount AS INT;

SELECT @TotalCount = MAX(ID)
FROM @TableVar

INSERT INTO @TableVar
SELECT AccessionNo, MasterAccessionNo, Copies
FROM dbo.YourTableName WITH(NOLOCK)
WHERE AccessionNo =  MasterAccessionNo;

WHILE(@TotalCount >= @CurrentIndex)
BEGIN

  UPDATE ytn
     SET ytn.Copies = --What ever u want
  FROM dbo.YourTableName ytn WITH(NOLOCK)
  JOIN @TableVar tv
     ON tv.AccessionNo = ytn.AccessionNo
  WHERE tv.ID = @CurrentIndex

END
 
Share this answer
 
SQL
UPDATE dbo.YourTableName
SET Copies =  --What ever u want
WHERE AccessionNo =  MasterAccessionNo


this is another way without loop.
 
Share this answer
 
Comments
Minnu Sanju 11-Jan-13 2:14am    
This is my table:
Id AccessionNo MasterAccessionNo Copies
1 001 001 3
2 002 001 1
3 003 001 1
4 004 004 2
5 005 004 1

Now if i am deleting a row manually with Id 2 from the table, then the copies should be updated from 3 to 2 where AccessionNo=MasterAccessionNo. If i am deleting more records then the corresponding copies should be updated checking that records AccessionNo=MasterAccessionNo

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900