SQL Best Practices

From Littledamien Wiki
Jump to navigation Jump to search

Determining if a record exists[edit]

SELECT EXISTS (SELECT 1 FROM users_table WHERE user_table.userid = p_userId) INTO p_isPresent;

Not

SELECT COUNT(*) FROM users_table WHERE user_table.userid = p_userId INTO p_isPresent;

SELECT EXISTS stops as soon as it finds a match. COUNT counts all matches.