Critical
String literals should not be duplicated
Rule description
- String literals should not be duplicated. While checking for the duplicate string ignoring string in the EXEC sys.sp_addextendedproperty. So string in the extended property call is ignored.
Non-compliant Code Example
DECLARE @Number int; SET @Number = 50; IF @Number > 100 AND @Number < 200 PRINT 'The number is large.'; --Non compliant code (String literals are duplicated) ELSE BEGIN IF @Number < 10 PRINT 'The number is small.'; --Non compliant code (String literals are duplicated) ELSE PRINT 'The number is medium.'; --Non compliant code (String literals are duplicated) END ; IF @Number > 200 PRINT 'The number is large.'; --Non compliant code (String literals are duplicated) IF @Number = 0 PRINT 'The number is small.'; --Non compliant code (String literals are duplicated) ELSE PRINT 'The number is medium.'; --Non compliant code (String literals are duplicated) GO
Compliant Code Example
Declare @SmallMsg varchar(200) = 'The number is small.'; Declare @MediumMsg varchar(200) = 'The number is medium.'; Declare @LargeMsg varchar(200) = 'The number is large.'; DECLARE @Number int; SET @Number = 50; IF @Number > 100 AND @Number < 200 PRINT @LargeMsg; --Compliant code (String literals declared above and used by variable at all locations) ELSE BEGIN IF @Number < 10 PRINT @SmallMsg; --Compliant code ELSE PRINT @MediumMsg ; --Compliant code END ; IF @Number > 200 PRINT @LargeMsg; --Compliant code IF @Number = 0 PRINT @SmallMsg; --Compliant code ELSE PRINT @MediumMsg ; --Compliant code GO