TSQL – How to Say Limit 1

Apparently there is no simple method of saying “limit 1″ as in:
update social_security_benefits set monthly_payment=0 where payee='poor old lady' limit 1
(That’s how you would do it in MySQL for example.)

So here are two work-arounds for T-SQL (MS SQL Server) that my co-worker came up with:

update social_security_benefits set monthly_payment=0 where keyfield= (select top 1 keyfield from social_security_benefits where payee='poor old lady')

or

update social_security_benefits set monthly_payment=0 where keyfield= (select max(keyfield) from social_security_benefits where payee='poor old lady')

Enjoy and good luck!

[tags]TSQL, T-SQL, MSSQL, SQL Server, SQL, Limit 1, Limit, Query, Where clause[/tags]

Comments are closed.