Case Statement

The SQL Server case statement is a conditional statement that returns a single value based on the evaluation of a statement. Case statements can be used in a SELECT list, WHERE clause and even an ORDER BY clause. Case statement can also be nested. This provides a lot of pliability for evaluating multiple expressions. We’ll start by walking through a simple case statement. There are two ways to formulate a case statement. One is to present the column or variable to evaluate after the word case. It should be noted that the else portion of a case statement is completely optional. Basic Equality Evaluation [cc lang=”sql”] DECLARE @Season varchar(100) SET @Season = ‘Summer’ DECLARE @Climate varchar(50) SET @Climate = CASE @Season WHEN ‘Winter’ THEN ‘Cold’ WHEN ‘Spring’ THEN ‘Mild’ WHEN ‘Summer’ THEN ‘Hot’ WHEN ‘Fall’ THEN ‘Cool’ ELSE ‘Unknown’ END SELECT @Climate [/cc] That syntax works well when evaluating whether single variable is equal to a value. If we want to add additional criteria then we have to formulate the statement a different way. Basic Multi-Evaluation [cc lang=”sql”] DECLARE @Temperature int SET @Temperature = 64 DECLARE @Climate varchar(50) SET @Climate = CASE WHEN @Temperature < 0 THEN 'Way Too Cold' WHEN @Temperature BETWEEN 0 AND 50 THEN 'Cold' WHEN @Temperature BETWEEN 51 AND 60 THEN 'Cool' WHEN @Temperature BETWEEN 61 AND 70 THEN 'Mild' WHEN @Temperature BETWEEN 71 AND 80 THEN 'Warm' WHEN @Temperature BETWEEN 81 AND 100 THEN 'Hot' WHEN @Temperature > 100 THEN ‘Way Too Hot’ ELSE ‘Unknown’ […] Continue reading ...

Add Constraint

To add a constraint to an existing table use the alter table statement with the add constraint command. There are four different types of constraints: Primary Key Constraints – Enforces unique values for specified column, can be referenced. Foreign Key Constraints – Enforces a reference to a primary key Unique Constraints – Ensures unique values within a column Check Constraints – Limits values acceptable for a column based on an evaluation Add unique constraint [cc lang=”sql”] ALTER TABLE dbo.Presidents ADD CONSTRAINT President_unique UNIQUE (President) [/cc] Add constraint to test value [cc lang=”sql”] ALTER TABLE dbo.Presidents ADD CONSTRAINT President_unique CHECK (YearsInOffice >= 0 AND YearsInOffice < 13) [/cc]

Create Index Syntax

To add an index in SQL Server use the CREATE INDEX statements. When adding indexes remember there can only be one clustered index per table. The main options when creating an index are clutered or nonclustered or unique vs non unique. Using SQL 2005+ you can also specify columns to include at the leaf level of the index. Create a single nonclustered index [cc lang=”sql”] CREATE UNIQUE NONCLUSTERED INDEX IX_NC_PresidentNumber — specify index name ON dbo.Presidents (PresidentNumber) — specify table and column name [/cc] Create a multi-column (composite) nonclustered index [cc lang=”sql”] CREATE UNIQUE NONCLUSTERED INDEX IX_NC_PresidentNumber_PresidentName — specify index name ON dbo.Presidents (PresidentNumber,PresidentName) — specify table and column names [/cc] Create a multi-column (composite) clustered index [cc lang=”sql”] CREATE UNIQUE CLUSTERED INDEX IX_C_PresidentNumber — specify index name ON dbo.Presidents (PresidentNumber,PresidentName) — specify table and column names [/cc] Create a non clustered index with included columns [cc lang=”sql”] CREATE NONCLUSTERED INDEX IX_NC_PresidentNumber — specify index name ON dbo.Presidents (PresidentNumber) — specify table and column name INCLUDE (President,YearsInOffice,RatingPoints) — specify included columns [/cc] Create index with fill factor [cc lang=”sql”] CREATE NONCLUSTERED INDEX IX_NC_PresidentNumber — specify index name ON dbo.Presidents (PresidentNumber) — specify table and column name WITH (FILLFACTOR = 80) — specify the fill factor [/cc] SQL Server 2008 options SQL Server 2008 provides additional index options. Most notably it provides the ability to filter an index. This can help decrease the size of the index for very large or partitioned table to only include the records that are normally […] Continue reading ...

Add Primary Key

In order to add a primary key to an existing table we need to use the Alter Table command. Add a primary key for a new column [cc lang=”sql”] ALTER TABLE dbo.Presidents ADD PresidentID int NOT NULL IDENTITY(1,1) CONSTRAINT PK_PresidentID PRIMARY KEY NONCLUSTERED [/cc] Note however that the ordering of the identity field will not be predictable for the records that already exist in the table. For newly inserted records, the identity field will increment properly. Add a primary key for an existing column [cc lang=”sql”] ALTER TABLE dbo.Presidents WITH NOCHECK ADD CONSTRAINT PK_PresidentNumber PRIMARY KEY NONCLUSTERED (PresidentNumber) [/cc]

Add User Role

Roles can be created inside a database that encapsulate permissions. These permissions can be granular (as in denying read permissions to one table), or they can be global (as in being able to write to all tables). Using the CREATE ROLE state we will walk through some typical examples. First of all, we need to create an empty shell of a role that will not have any securables [cc lang=”sql”] CREATE ROLE [ExecDBO] AUTHORIZATION [dbo] [/cc] The code above did not define any permissions, it only created an empty role. The authorization keyword defines the dbo user as being the “owner” of this role. A securable is an object-security relationship Now we will define some securables for this role [cc lang=”sql”] GRANT EXECUTE ON SCHEMA::dbo TO [ExecDBO]; GRANT SELECT ON SCHEMA::dbo TO [ExecDBO]; [/cc] Above we have just granted read and execute on the entire ‘dbo’ schema. Continue reading ...

Featured Articles

 Site Author

  • Thanks for visiting!
css.php