Before I attempt an update or delete, I always check to see if the primary key exists and, if not, return a message to that effect. For example:
Being a detail-oriented, anal-retentive with a complusion for consistency, I would like to use the same construct for the SELECTs as well, but I just can't ignore the simplicity of this (not to mention one less call):
So the question is, which would you choose and why? Consistent implementation or fewer lines of code? I'm so torn! ;-)
< M!ke >
Code:
IF EXISTS (SELECT PrimaryKey FROM dbo.TableName WHERE PrimaryKey = @InputParam)
--transaction wrapper removed for brevity...
BEGIN
UPDATE dbo.TableName
SET Column1 = @NewColumn1Value
, Column2 = @NewColumn2Value
, AndSoForth = @NewAndSoForthValue
WHERE PrimaryKey = @InputParam
END
ELSE
BEGIN
SET @vcException = 'No record found in table dbo.TableName with a primary key value of ' + RTRIM(CONVERT(varchar(15), @InputParam)) + '.'
RETURN
END
Being a detail-oriented, anal-retentive with a complusion for consistency, I would like to use the same construct for the SELECTs as well, but I just can't ignore the simplicity of this (not to mention one less call):
Code:
SELECT
PrimaryKey
, Column1
, Column2
, AndSoForth
FROM dbo.TableName
WHERE PrimaryKey = @InputParam
IF @@ROWCOUNT = 0
BEGIN
SET @vcExceptionMessage = 'No record found in table dbo.TableName with a primary key value of ' + RTRIM(CONVERT(varchar(15), @InputParam)) + '.'
RETURN
END
So the question is, which would you choose and why? Consistent implementation or fewer lines of code? I'm so torn! ;-)
< M!ke >