In this blog post we will be providing SQL Server TSQL Cheat Sheet for Database Programmers. We have tried to answer most common questions that are asked by beginners or advanced SQL Server Database Programmers or most forums. Therefore, I decided to do this post to answer the most common SQL Server related Questions.

  • How to CREATE a Table in SQL Server.
  • How to Create While Loop in SQL Server.
  • How to create Try / Catch Statements in SQL Server.
  • How to Create Multi-Line Comments in SQL Server.
  • How to create Single Line Comments in SQL Server.
  • How to Check Constraint in SQL Server.
  • How to Create an Index in SQL Server.
  • How to Set Default Value for Column in SQL Server.
  • How to Add a NULL Constraint in SQL Server.
  • How to Add a Foreign Key in SQL Server.
  • How to Rename Table in SQL Server.
  • How to Rename a Column in SQL Server.
  • How to Add A Column in SQL Server.
  • How to create a CASE Statement in SQL Server.
  • IF / ELSE IF / ELSE Statement in SQL Server.
  • How to DECLARE and SET Variables in SQL Server.

How to DECLARE and SET Variables in SQL Server TSQL

DECLARE @Jokoint
SET @Joko = 1
SELECT @Joko = Column FROM Table
WHERE id=1
 IF / ELSE IF / ELSE Statement in SQL Server TSQL
IF @Joko < 1
BEGINPRINT 'Joko Is less than 1'
END
ELSE IF @Joko = 1
BEGINPRINT 'Joko Is 1'
END
ELSE
BEGIN
PRINT 'Joko greater than 1'
END

 How to create a CASE Statement in SQL Server TSQL

SELECT Day = CASE 
WHEN (DateAdded IS NULL) THEN
'Unknown'
WHEN (DateDiff(day, DateAdded,
getdate()) = 0) THEN 'Today'
WHEN (DateDiff(day, DateAdded,
getdate()) = 1) THEN 'Yesterday'
WHEN (DateDiff(day, DateAdded,
getdate()) = -1) THEN 'Tomorrow'
ELSE DATENAME(dw , DateAdded)
END
FROM Table

How to Add A Column in SQL Server TSQL

ALTER TABLE YourTableName 
ADD[ColumnName] [int] NULL;

How to Rename a Column in SQL Server TSQL

EXEC sp_rename
'TableName.OldColName',
'NewColName','COLUMN';

How to Rename Table in SQL Server TSQL

EXEC sp_rename 'OldTableName',
'NewTableName';

How to Add a Foreign Key in SQL Server TSQL

ALTER TABLE Products WITH CHECK
ADD CONSTRAINT [FK_Prod_Man]
FOREIGN KEY(ManufacturerID)
REFERENCES Manufacturers (ID);

How to Add a NULL Constraint in SQL Server TSQL

ALTER TABLE TableName 
ALTER COLUMN ColumnName int NOT NULL;

How to Set Default Value for Column in SQL Server TSQL

ALTER TABLE TableName 
ADD CONSTRAINT
DF_TableName_ ColumnName DEFAULT 0 
FOR ColumnName;

How to Create an Index in SQL Server TSQL

CREATE INDEX IX_Index_Name ON Table(Columns)

How to Check Constraint in SQL Server TSQL

ALTER TABLE TableName 
ADD CONSTRAINT CK_CheckName CHECK 
(ColumnValue > 1)

How to create Single Line Comments in SQL Server TSQL

SET @mojo = 1 --THIS IS A COMMENT

How to Create Multi-Line Comments in SQL Server TSQL

/* This is a comment that can span multiple lines*/

How to create Try / Catch Statements in SQL Server TSQL

BEGIN TRY
-- try / catch requires
SQLServer 2017
-- run your code here
END TRY
BEGIN CATCH
PRINT 'Error Number: ' +
str(error_number())
PRINT 'Line Number: ' +
str(error_line())
PRINT error_message()
-- handle error condition
END CATCH

How to Create While Loop in SQL Server TSQL

DECLARE @i int
SET @i = 0
WHILE (@i < 10)
BEGIN
		SET @i = @i + 1
		PRINT @i
		IF (@i >= 10)
			BREAK
		ELSE
			CONTINUE
END

How to CREATE a Table in SQL Server TSQL

CREATE TABLE TheNameOfYourTable (
	ID INT NOT NULL IDENTITY(1,1),
	DateAdded DATETIME
DEFAULT(getdate()) NOT NULL,
	Description VARCHAR(100) NULL,
	IsGood BIT DEFAULT(0) NOT NULL,
	TotalPrice MONEY NOT NULL,
	CategoryID int NOT NULL
	REFERENCES Categories(ID),
	PRIMARY KEY (ID)
);

How to Create User Defined Function in SQL Server TSQL

CREATE FUNCTION dbo.DoStuff(@ID int)
RETURNS int
AS
BEGIN
DECLARE @result int
IF @ID = 0
BEGIN
RETURN 0
END
SELECT @result = COUNT(*)
FROM table WHERE ID = @ID 
RETURN @result
END
GO
SELECT dbo.DoStuff(0)

 Also Read:

http://techhowdy.com/wp-content/uploads/2018/04/SQL-Server-TSQL-Cheat-Sheet-For-Database-Programmers-1024x614.pnghttp://techhowdy.com/wp-content/uploads/2018/04/SQL-Server-TSQL-Cheat-Sheet-For-Database-Programmers-150x150.pngDemonDatabase ProgrammingHow to Add A Column in SQL Server TSQL,How to Add a Foreign Key in SQL Server TSQL,How to Add a NULL Constraint in SQL Server TSQL,How to Check Constraint in SQL Server TSQL,How to create a CASE Statement in SQL Server TSQL,How to CREATE a Table in SQL Server TSQL,How to Create an Index in SQL Server TSQL,How to Create Multi-Line Comments in SQL Server TSQL,How to create Single Line Comments in SQL Server TSQL,How to create Try / Catch Statements in SQL Server TSQL,How to Create While Loop in SQL Server TSQL,How to DECLARE and SET Variables in SQL Server TSQL,How to Rename a Column in SQL Server TSQL,How to Rename Table in SQL Server TSQL,How to Set Default Value for Column in SQL Server TSQL,IF / ELSE IF / ELSE Statement in SQL Server TSQLIn this blog post we will be providing SQL Server TSQL Cheat Sheet for Database Programmers. We have tried to answer most common questions that are asked by beginners or advanced SQL Server Database Programmers or most forums. Therefore, I decided to do this post to answer the most...Latest technology news