Skip to content
Advertisement

Using top N in sql

statement 1

UPDATE TOP(1) employee 
    SET GivenName = 'Alex', 
        LastName = 'Smith' 
WHERE ID = 1

statement 2

UPDATE employee 
    SET GivenName = 'ALEX', 
        LastName = 'Smith' 
WHERE ID IN (
                SELECT TOP (1) 
                FROM employee 
                WHERE ID = 1 
                ORDER BY ID ASC 

I have tried the above but both are not working.

Does anybody know the reason?

Advertisement

Answer

If you want to update one row, then you can use limit:

UPDATE employee
    SET GivenName = 'Alex',
        LastName = 'Smith'
    WHERE ID = 1
    LIMIT 1;

That said, I would expect id to be unique, so no LIMIT is necessary.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement