SQL Server Update particular record in a table

kalyan Dhanekula 0 Reputation points
2025-04-23T19:43:53.2666667+00:00

DROP TABLE if exists #test

create table #test

(

PID int,

Category varchar(100),

status varchar(10)

)

Insert into #test

values (123,'Lessthan8','compliant'),

   (123,'greatherthan8','compliant'),

   (456,'Lessthan8','Missing'),

   (456,'greatherthan8','Missing'),

    (789,'Lessthan8','compliant'),

    (789,'greatherthan8','Missing'),

	

    (999,'Lessthan8','Missing'),

	(999,'greatherthan8','compliant'),

    (11,'Lessthan8','Missing'),

    (111,'greatherthan8','Missing'),

	 (22,'Lessthan8','compliant'),

    (222,'greatherthan8','compliant')

--so outut should be

-- (123,'Lessthan8','compliant'),

-- (123,'greatherthan8','Missing')

Select * from #test

Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,705 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Viorel 121.3K Reputation points
    2025-04-23T20:04:06+00:00

    If the particular record can be identified by a simple condition, then use where:

    update #test
    set [status] = 'Missing'
    where PID = 123 and Category = 'greatherthan8'
    

  2. Bruce (SqlWork.com) 74,936 Reputation points
    2025-04-23T20:14:44.7666667+00:00

    you haven't defined what identifies a unique row, so you may need all columns:

    update #test
    set status = 'new value'
    where PID = 123 and Category = 'greatherthan8' and status = 'compliant'
    

    note: this is poor table design. a table should have a unique key.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.