You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
More details about SQL Server data types and mapping it with another databases you can find [here](https://github.com/ktaranov/sqlserver-kit/blob/master/SQL%20Server%20Data%20Types.md)
94
+
<aid="data-types-recommendation"></a>
95
+
More details about SQL Server data types and mapping it with another databases and program languages you can find [here](https://github.com/ktaranov/sqlserver-kit/blob/master/SQL%20Server%20Data%20Types.md)
82
96
83
97
| General Type | Type | ANSI | Recommended | What use instead | Why use or not |
@@ -156,10 +170,12 @@ More details about SQL Server data types and mapping it with another databases y
156
170
157
171
158
172
## T-SQL Programming Style
159
-
SQL Server T-SQL Coding Conventions, Best Practices, and Programming Guidelines
173
+
<aid="t-sql-programming-style"></a>
174
+
SQL Server T-SQL Coding Conventions, Best Practices, and Programming Guidelines.
160
175
161
176
162
177
### General programming style
178
+
<aid="#general-t-sql-programming-style"></a>
163
179
164
180
- For database objects names in code use only schema plus object name, do not hardcode server and database names in your code: `dbo.MyTable` is good and bad `PRODSERVER.PRODDB.dbo.MyTable`.
165
181
More details [here](https://www.red-gate.com/simple-talk/opinion/editorials/why-you-shouldnt-hardcode-the-current-database-name-in-your-views-functions-and-stored-procedures/),
@@ -309,8 +325,8 @@ ORDER BY t2.Value2;
309
325
**[⬆ back to top](#table-of-contents)**
310
326
311
327
312
-
<aid="programming-style"></a>
313
328
### Stored procedures and functions programming style
329
+
<aid="programming-style"></a>
314
330
315
331
- All stored procedures and functions should use `ALTER` statement and start with the object presence check (see example below)
316
332
-`ALTER` statement should be preceded by 2 line breaks
@@ -327,7 +343,7 @@ ORDER BY t2.Value2;
327
343
- Use `RAISERROR` instead `PRINT` if you want to give feedback about the state of the currently executing SQL batch without lags.
328
344
More details [here](http://sqlity.net/en/984/print-vs-raiserror/) and [here](http://sqlservercode.blogspot.com/2019/01/print-disruptor-of-batch-deletes-in-sql.html).
329
345
- All code should be self documenting
330
-
-TSQL code, triggers, stored procedures, functions, should have a standard comment-documentation banner:
346
+
-T-SQL code, triggers, stored procedures, functions, should have a standard comment-documentation banner:
331
347
```tsql
332
348
summary: >
333
349
This procedure returns an object build script as a single-row, single column
@@ -393,8 +409,116 @@ GO
393
409
**[⬆ back to top](#table-of-contents)**
394
410
395
411
396
-
<aid="reference"></a>
412
+
### Dynamic T-SQL Recommendation
413
+
<aid="dynamic-t-sql-recommendation"></a>
414
+
**Highly recommended to read awesome detailed article about dynamic T-SQL by Erland Sommarskog: [The Curse and Blessings of Dynamic SQL](http://sommarskog.se/dynamic_sql.html)**
415
+
416
+
Dynamic SQL is a programming technique that allows you to construct SQL statements dynamically at runtime.
417
+
It allows you to create more general purpose and flexible SQL statement because the full text of the SQL statements may be unknown at compilation.
418
+
For example, you can use the dynamic SQL to create a stored procedure that queries data against a table whose name is not known until runtime.
419
+
420
+
More details [here](http://www.sqlservertutorial.net/sql-server-stored-procedures/sql-server-dynamic-sql/).
421
+
422
+
- Do not use [nvarchar(max)] for your object’s name parameter, use [sysname] instead (synonym for nvarchar(128)).
DECLARE @tableName sysname = N'My badly named table!';
435
+
SET @tsql = N'SELECT object_id FROM ' + @tableName;
436
+
437
+
/* Good */
438
+
DECLARE @tsql nvarchar(max);
439
+
DECLARE @tableName sysname = N'My badly named table 111!';
440
+
SET @tsql = N'SELECT object_id FROM ' + QUOTENAME(@tableName);
441
+
```
442
+
- Always use [`sp_executesql`] instead [`EXEC`] to prevent sql injection.
443
+
Also [`sp_executesql`] can parameterizing your dynamic statement that means plans can be reused as well (when the value of the dynamic object is the same).
444
+
Also [`sp_executesql`] can even be used to output values as well (see example below).
-[General Database Conventions](http://kejser.org/database-naming-conventions/general-database-conventions/) (by Thomas Kejser)
@@ -422,5 +546,9 @@ GO
422
546
-[The Basics of Good T-SQL Coding Style – Part 3: Querying and Manipulating Data](https://www.simple-talk.com/sql/t-sql-programming/basics-good-t-sql-coding-style-part-3-querying-manipulating-data/)
0 commit comments