SQL
JavaScript
////////////////////////////////////////////////////////////////////////
////////NAME: Best practices
/*
NAMING CONVENTIONS
*/
--table (singular names!)
[User]
[Role]
-- all naming (e.g. column names) only in English and in Hungrian notation (sql instructions are in English so all code language must be in English)
--link table
[UserRoles]
-- stored procedures
Save...
Update...
Get...By...
-- views
...View
-- dictionaries fields: Id, Name, SortId, IsActive, ; the only question to answer is: is the data structure a kind of dictionary or a table
-- like: statuses, cities, roles...
/*
NOT NULL fields VS NULLABLE // + DEFAULT val?
*/
CREATE TABLE #Persons
(
Id INT
,UserCreated NVARCHAR(255) NOT NULL DEFAULT ''
)
INSERT INTO #Persons (Id) VALUES (1);
DROP TABLE #Persons;
/*
CALLING STORED PROCEDURES based on http://social.technet.microsoft.com/wiki/contents/articles/26944.calling-stored-procedures-using-transact-sql.aspx
*/
-- if there is ONE param and SP is called clearly naming this param (BTW it must be name in this way e.g. ...ById) we can call
-- in form based on parameter position
EXEC GetUserById 1345;
-- if there are many params (2 and more) we must specify each param's name (even default)
EXEC GetUser @FirstName = 'John', @SecondName = 'Smith';
EXEC SomeProc @Param1 = 1, @Param2 = DEFAULT; -- NOT EXEC SomeProc @Param1 = 1
-- a) custom error message? but default is quite clear (and it looks like different situation: No Param VS Null Param)
-- DEFAULT ERROR: Procedure or function 'CheckDefault' expects parameter '@Param', which was not supplied.
GO
CREATE PROC CheckDefault
@Param INT = NULL
AS
IF @Param IS NULL
BEGIN
RAISERROR(N'@Param cannot be NULL!', 16, 1);
RETURN;
END
SELECT @Param AS Param;
GO
/*
ПОЛУЧИТЬ ЛИСТ СТОЛБЦОВ ДЛЯ НАПИСАНИЯ UPDATE ПО ТАБЛИЦЕ
*/
SELECT TOP 100 [columns].name, [columns].is_nullable
FROM sys.tables [tables]
INNER JOIN sys.columns [columns]
ON [tables].object_id =...