Don’t use SELECT when you mean SET
October 5, 2006
This is just a pet pev of mine that I’ve seen about 95% of people do when writing MSSQL stored procedures
CREATE PROCEDURE myproc
AS
SET NOCOUNT ON
DECLARE @i int
SELECT @i = DATEPART(hh,GETDATE())
RETURN @i
SET NOCOUNT OFF
GO
There is nothing wrong with the above SP, it will compile and run just fine. The problem I have with it is using the SELECT keyword to set a variable when the SET keyword will work just fine. Not only that, but you don’t have to remember to use SET NOCOUNT ON declaration. So here is the same SP using the SET keyword
CREATE PROCEDURE myproc
AS
DECLARE @i int
SET @i = DATEPART(hh,GETDATE())
RETURN @i
GO
A lot less typing, cleaner looking and easier to read if you ask me.