Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more examples for SqlServer.Rules #62

Merged
merged 1 commit into from
Jan 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions SqlServer.Rules.Test/Docs/DocsGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,14 +202,6 @@ private static void GenerateRuleMarkdown(TypeComments comments, List<string> ele
stringBuilder.Append(CultureInfo.InvariantCulture, $"{TrimLeadingWhitespace(exampleMd)}");
}

if (!string.IsNullOrWhiteSpace(comments.Remarks))
{
stringBuilder.AppendLine(spaces);
stringBuilder.AppendLine("### Remarks");
stringBuilder.AppendLine(spaces);
stringBuilder.AppendLine(CultureInfo.InvariantCulture, $"{comments.Remarks}");
}

if (scriptExamples.Any())
{
stringBuilder.AppendLine(spaces);
Expand All @@ -221,6 +213,14 @@ private static void GenerateRuleMarkdown(TypeComments comments, List<string> ele
}
}

if (!string.IsNullOrWhiteSpace(comments.Remarks))
{
stringBuilder.AppendLine(spaces);
stringBuilder.AppendLine("### Remarks");
stringBuilder.AppendLine(spaces);
stringBuilder.AppendLine(CultureInfo.InvariantCulture, $"{comments.Remarks}");
}

stringBuilder.AppendLine(spaces);
stringBuilder.AppendLine("<sub><sup>Generated by a tool</sup></sub>");

Expand Down Expand Up @@ -297,6 +297,10 @@ private static void GenerateTocMarkdown(List<Type> sqlServerRules, List<string>
{
friendlyName = ruleAttribute.Description;
isIgnorable = " ";
}

if (exampleMd == " ")
{
exampleMd = ruleScripts.Any(x => x.Key.Contains(ruleAttribute.Id.ToId(), StringComparison.OrdinalIgnoreCase)) ? "Yes" : " ";
}

Expand Down
2 changes: 1 addition & 1 deletion TSQLSmellsTest/CreateProcedureNoSchema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ SET NOCOUNT ON;
select Col1 from [dbo].TestTableSSDT
RETURN 1

-- SML024
-- SML024, SRN0006
2 changes: 1 addition & 1 deletion TSQLSmellsTest/CreateTableNoSchema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ Create Table TestTableNoSchema
ID integer
)

-- SML027
-- SML027, SRD0002, SRP0020
2 changes: 1 addition & 1 deletion TSQLSmellsTest/DeclareCursor.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ FROM [dbo].[TestTableSSDT]
WHERE Col3=1
ORDER BY Col1;

-- SML029
-- SML029, SRP0016
2 changes: 1 addition & 1 deletion TSQLSmellsTest/DeprecatedTypes.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ ID integer primary key,
[SomeText] TEXT NULL
)

-- SML047
-- SML047, SRD0051, SRN0007
2 changes: 1 addition & 1 deletion TSQLSmellsTest/DeprecatedTypesSP.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ Set nocount on
Declare @DP1 text
Declare @DP2 ntext

-- SML047
-- SML047, SRD0012
2 changes: 1 addition & 1 deletion TSQLSmellsTest/Derived.sql
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ Select 1
Derived
on Derived.Col1 = TestTableSSDT.Col1;

-- SML035
-- SML035, SRD0038
2 changes: 1 addition & 1 deletion TSQLSmellsTest/DisabledForeignKey.sql
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ SomeData char(1)
go
alter table dbo.Child nocheck constraint fkParentChild

-- SML006
-- SML006, SRD0004
2 changes: 1 addition & 1 deletion TSQLSmellsTest/EqualsNull.sql
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ SET NOCOUNT ON
Select ID1 from dbo.EqualsNull where ID1 =NULL
END

-- SML046
-- SML046, SRD0011
2 changes: 1 addition & 1 deletion TSQLSmellsTest/ExecSQL.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ DECLARE @SQL NVARCHAR(2048)
INSERT INTO dbo.TestTableSSDT
EXEC (@sql)

-- SML021
-- SML021, SRD0015, SRD0024
2 changes: 2 additions & 0 deletions TSQLSmellsTest/Exists.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ Set nocount on;
if(Select count(*) from dbo.TestTableSSDT)>1 begin
print 'Rows exist'
end

-- SRP0023
2 changes: 1 addition & 1 deletion TSQLSmellsTest/ForceScan.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ set nocount on

RETURN 0

-- SML044
-- SML044, SRD0030
2 changes: 1 addition & 1 deletion TSQLSmellsTest/InsertMissingColumnSpecifiers.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ SET NOCOUNT ON;
INSERT INTO dbo.TestTableSSDT([IdCol],Col1,Col2,Col3,DateCol)
SELECT [IdCol],Col1,Col2,Col3,DateCol FROM dbo.TestTableSSDT;

-- SML012
-- SML012, SRD0009, SRD0013
2 changes: 1 addition & 1 deletion TSQLSmellsTest/MultiCteTest.sql
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ set nocount on ;

END;

-- SML005
-- SML005, SRD0016
2 changes: 1 addition & 1 deletion TSQLSmellsTest/SelectTopNoParen.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ AS
Select top 100 Col1 from dbo.TestTableSSDT;
RETURN 0

-- SML034
-- SML034, SRD0014
4 changes: 3 additions & 1 deletion TSQLSmellsTest/TestTokenize.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
CREATE PROCEDURE [dbo].[TestTokenize]
CREATE PROCEDURE [dbo].[TestTokenize]
@param1 int = 0,
@param2 int
AS
SELECT * from [LocalServer].[SalesDB].[dbo].[SomeTable]
RETURN 0

-- SRP0005
2 changes: 1 addition & 1 deletion TSQLSmellsTest/TestWithExists.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ IF EXISTS(SELECT * FROM dbo.TestTableSSDT) BEGIN
SELECT Idcol from dbo.TestTableSSDT
end

-- SML005
-- SML005, SRD0063
2 changes: 1 addition & 1 deletion TSQLSmellsTest/TestWithNoLock.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ SET nocount on
SELECT idcol FROM dbo.TestTableSSDT WITH(NOLOCK)
GO

-- SML003
-- SML003, SRD0034
4 changes: 3 additions & 1 deletion TSQLSmellsTest/err.sql
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
Create view [dbo].[err]
Create view [dbo].[err]
AS SELECT * FROM [TestTableSSDT]

-- SRD0006, SRD0039
2 changes: 1 addition & 1 deletion TSQLSmellsTest/orderbyordinal.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ SET nocount on;
SELECT Col1,col2,col3
FROM dbo.TestTableSSDT ORDER BY 1,2,3

-- SML007
-- SML007, SRD0025
2 changes: 1 addition & 1 deletion docs/CodeSmells/SML003.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ SET nocount on
SELECT idcol FROM dbo.TestTableSSDT WITH(NOLOCK)
GO

-- SML003
-- SML003, SRD0034
```
```sql
CREATE PROCEDURE dbo.TestWithNoLockIndexhint
Expand Down
4 changes: 2 additions & 2 deletions docs/CodeSmells/SML005.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ set nocount on ;

END;

-- SML005
-- SML005, SRD0016
```
```sql
Create PRocedure dbo.SelectAs
Expand Down Expand Up @@ -172,7 +172,7 @@ IF EXISTS(SELECT * FROM dbo.TestTableSSDT) BEGIN
SELECT Idcol from dbo.TestTableSSDT
end

-- SML005
-- SML005, SRD0063
```
```sql
CREATE PROCEDURE dbo.TestWithExistsAndNestedSelectStar
Expand Down
2 changes: 1 addition & 1 deletion docs/CodeSmells/SML006.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ SomeData char(1)
go
alter table dbo.Child nocheck constraint fkParentChild

-- SML006
-- SML006, SRD0004
```

<sub><sup>Generated by a tool</sup></sub>
2 changes: 1 addition & 1 deletion docs/CodeSmells/SML012.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ SET NOCOUNT ON;
INSERT INTO dbo.TestTableSSDT([IdCol],Col1,Col2,Col3,DateCol)
SELECT [IdCol],Col1,Col2,Col3,DateCol FROM dbo.TestTableSSDT;

-- SML012
-- SML012, SRD0009, SRD0013
```

<sub><sup>Generated by a tool</sup></sub>
2 changes: 1 addition & 1 deletion docs/CodeSmells/SML021.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ DECLARE @SQL NVARCHAR(2048)
INSERT INTO dbo.TestTableSSDT
EXEC (@sql)

-- SML021
-- SML021, SRD0015, SRD0024
```

<sub><sup>Generated by a tool</sup></sub>
2 changes: 1 addition & 1 deletion docs/CodeSmells/SML024.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ SET NOCOUNT ON;
select Col1 from [dbo].TestTableSSDT
RETURN 1

-- SML024
-- SML024, SRN0006
```

<sub><sup>Generated by a tool</sup></sub>
2 changes: 1 addition & 1 deletion docs/CodeSmells/SML027.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Create Table TestTableNoSchema
ID integer
)

-- SML027
-- SML027, SRD0002, SRP0020
```

<sub><sup>Generated by a tool</sup></sub>
2 changes: 1 addition & 1 deletion docs/CodeSmells/SML029.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ FROM [dbo].[TestTableSSDT]
WHERE Col3=1
ORDER BY Col1;

-- SML029
-- SML029, SRP0016
```

<sub><sup>Generated by a tool</sup></sub>
2 changes: 1 addition & 1 deletion docs/CodeSmells/SML034.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ AS
Select top 100 Col1 from dbo.TestTableSSDT;
RETURN 0

-- SML034
-- SML034, SRD0014
```

<sub><sup>Generated by a tool</sup></sub>
2 changes: 1 addition & 1 deletion docs/CodeSmells/SML044.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ set nocount on

RETURN 0

-- SML044
-- SML044, SRD0030
```

<sub><sup>Generated by a tool</sup></sub>
2 changes: 1 addition & 1 deletion docs/CodeSmells/SML046.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ SET NOCOUNT ON
Select ID1 from dbo.EqualsNull where ID1 =NULL
END

-- SML046
-- SML046, SRD0011
```

<sub><sup>Generated by a tool</sup></sub>
4 changes: 2 additions & 2 deletions docs/CodeSmells/SML047.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ ID integer primary key,
[SomeText] TEXT NULL
)

-- SML047
-- SML047, SRD0051, SRN0007
```
```sql
Create Procedure dbo.DepTypes
Expand All @@ -38,7 +38,7 @@ Set nocount on
Declare @DP1 text
Declare @DP2 ntext

-- SML047
-- SML047, SRD0012
```

<sub><sup>Generated by a tool</sup></sub>
11 changes: 11 additions & 0 deletions docs/Design/SRD0002.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,15 @@ Table does not have a primary key.

Tables should have a primary key

### Examples

```sql
Create Table TestTableNoSchema
(
ID integer
)

-- SML027, SRD0002, SRP0020
```

<sub><sup>Generated by a tool</sup></sub>
21 changes: 21 additions & 0 deletions docs/Design/SRD0004.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,27 @@ Columns on both sides of a foreign key should be indexed.

Consider indexing columns on both sides of a foreign key relationship.

### Examples

```sql
Create Table dbo.Parent
(
ParentId integer primary key,
SomeData char(1)
)
go
Create Table dbo.Child
(
ChildID integer primary key,
ParentId integer constraint fkParentChild references dbo.Parent(ParentID),
SomeData char(1)
)
go
alter table dbo.Child nocheck constraint fkParentChild

-- SML006, SRD0004
```

### Remarks

The rule checks for not indexed foreign keys in the current database. Create an index on any
Expand Down
9 changes: 9 additions & 0 deletions docs/Design/SRD0006.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,13 @@ Avoid using SELECT *.

Avoid the use of SELECT *. This can cause more columns than desired to be returned, and can also cause issues if the order of the columns changes.

### Examples

```sql
Create view [dbo].[err]
AS SELECT * FROM [TestTableSSDT]

-- SRD0006, SRD0039
```

<sub><sup>Generated by a tool</sup></sub>
15 changes: 15 additions & 0 deletions docs/Design/SRD0009.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,21 @@ Wrap multiple action statements within a transaction.

Wrap multiple action statements within a transaction

### Examples

```sql
CREATE PROCEDURE dbo.InsertMissingColumnSpecifiers
AS
SET NOCOUNT ON;
INSERT INTO dbo.TestTableSSDT
SELECT [IdCol],Col1,Col2,Col3,DateCol FROM dbo.TestTableSSDT;

INSERT INTO dbo.TestTableSSDT([IdCol],Col1,Col2,Col3,DateCol)
SELECT [IdCol],Col1,Col2,Col3,DateCol FROM dbo.TestTableSSDT;

-- SML012, SRD0009, SRD0013
```

### Remarks

Not wrapping multiple action statements in a transaction inside a stored procedure
Expand Down
20 changes: 20 additions & 0 deletions docs/Design/SRD0011.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,26 @@ Equality and inequality comparisons involving a NULL constant found. Use IS NULL

Equality and inequality comparisons involving a NULL constant found. Use IS NULL or IS NOT NULL.

### Examples

```sql
Create table dbo.EqualsNull
(
ID1 integer not null

)
go

Create Procedure dbo.EqualsNullTest
AS
BEGIN
SET NOCOUNT ON
Select ID1 from dbo.EqualsNull where ID1 =NULL
END

-- SML046, SRD0011
```

### Remarks

This rule scans stored procedures, views, functions and triggers to flag use of equality
Expand Down
Loading
Loading