Learn more about SQL Server 2016 :
https://www.microsoft.com/en-us/cloud-platform/sql-server-features
Get Started with SQL Server 2016:
https://www.microsoft.com/en-us/cloud-platform/sql-server-resources
Discuss, Talk , Share and Explore MS SQLSERVER.
In this example, we will evaluate the values of two
variables and return the result of a variable which is bigger in value.
|
DECLARE @A INT=40
DECLARE @B INT=30
SELECT IIF(@A > @B, 'A IS GREATER THAN B', 'B IS GREATER THAN A')
GO;
Executing the above T-SQL will return the
following result:
-------------------
A IS GREATER THAN B
(1 row(s) affected)
|
In this example, we will evaluate the age of John and
Julie and identify who is older between them. Please observe the use of sub
functions within IIF.
|
DECLARE @JOHN_AGE INT=35
DECLARE @JULIE_AGE INT=29
SELECT IIF(@JOHN_AGE > @JULIE_AGE, 'JOHN IS OLDER THAN JULIE
BY '+LTRIM(STR(@JOHN_AGE-@JULIE_AGE))+' YEARS', 'JULIE IS OLDER THAN JOHN
BY '+LTRIM(STR(@JULIE_AGE-@JOHN_AGE))+' YEARS')
GO;
Executing the above statement will return the follocwing
result:
--------------------------------------------
JOHN IS OLDER THAN JULIE BY 6 YEARS
(1 row(s) affected)
|
CREATE SEQUENCE SYNTAX
|
CREATE
SEQUENCE [schema_name . ] sequence_name
[ AS [
built_in_integer_type | user-defined_integer_type ] ]
[ START
WITH
[ INCREMENT
BY
[ {
MINVALUE [
[ {
MAXVALUE [
[ CYCLE | {
NO CYCLE } ]
[ { CACHE [
[ ; ]
|
STEP 1 - CREATING A SEQUENCE
CALLED MYSEQUENCE1
|
CREATE SEQUENCE ITEMIDSEQUENCE
START WITH 1
INCREMENT BY 1;
|
STEP 2 - CREATING ITEM TABLE
|
CREATE TABLE ITEM
(
ITEMNO INT,
ITEMNAME VARCHAR(50)
);
GO
|
STEP 3 – USING SEQUENCE FOR
INSERTING DATA IN A TABLE
|
INSERT INTO ITEM
VALUES (NEXT VALUE FOR
ITEMIDSEQUENCE, 'MANGO');
INSERT INTO ITEM
VALUES (NEXT VALUE FOR
ITEMIDSEQUENCE, 'APPLE');
INSERT INTO ITEM
VALUES (NEXT VALUE FOR
ITEMIDSEQUENCE, 'BANANA');
INSERT INTO ITEM
VALUES (NEXT VALUE FOR
ITEMIDSEQUENCE, 'ORANGE');
INSERT
INTO ITEM VALUES (NEXT VALUE FOR ITEMIDSEQUENCE,
'GRAPES');
GO
|
STEP 4 – QUERYING DATA FROM A
TABLE USING SEQUENCE
|
||||||||||||
SELECT
* FROM ITEM
YOU’LL SEEE THE FOLLOWING RESULT SET.
|
ALTER, DELETE, AND
QUERY SEQUENCES
|
To delete a sequence:
DROP SEQUENCE ITEMIDSEQUENCE
To alter a sequence:
ALTER SEQUENCE ITEMIDSEQUENCE
RESTART
WITH 900
INCREMENT
BY 1
GO
To query the next value of
Sequence for a particular SEQUENCE object:
SELECT NEXT VALUE FOR ITEMIDSEQUENCE
To query all sequences
available in a database, you can use the below query:
SELECT * FROM SYS.SEQUENCES
|
THINGS TO KEEP IN MIND:
|
§
Sequence values are
not automatically protected after insertion in to a table. You should use
Update Trigger on the table to prevent sequence values from being changed.
§
Sequences does not
automatically enforce uniqueness for sequence values. You should create
unique index on the Sequence column to enforce uniqueness.
§
If you have created
a sequence for example assigns values 1 through 100, and if the rows in the
table grows beyond 100 rows, then Sequence would start assigning values 1
through 100 again.
§
By default, if you
do not specify data type for a sequence, BIGINT data type is used. Keep
in mind that you can create sequence that are of any integer data type.
credits to Saleem Hakani. my SQL Hero.
|
RENAME SA ACCOUNT
|
ALTER LOGIN SA WITH NAME = [NEW_ACCOUNT];
GO
|
DISABLE SA ACCOUNT
|
ALTER LOGIN SA DISABLE;
GO
credits to Saleem Hakani. my SQL Hero. |