4. INSERT INTO SELECT Statement
The INSERT INTO SELECT statement copies data from
one table and inserts it into an existing table.
The INSERT INTO SELECT statement selects data
from one table and inserts it into an existing table. Any
existing rows in the target table are unaffected.
SQL INSERT INTO SELECT Syntax
We can copy all columns from one table to another,
existing table:
INSERT INTO table2 SELECT * FROM table1;
Or we can copy only the columns we want to into
another, existing table:
INSERT INTO table2 (column_name(s))
SELECT column_name(s)
6. SQL Server Functions
function is a database object in Sql Server.
Basically it is a set of sql statements that accepts only
input parameters, perform actions and return the result.
Function can return only single value or a table.
We can’t use function to Insert, Update, Delete records
in the database table(s). F
Types of Function
System Defined Function
Scalar Function
Aggregate Function
User Defined Function
Scalar Function
Inline Table-Valued Function
Multi-Statement Table-Valued Function
7. System Defined Function
These functions are defined by Sql Server for different
purpose. We have two types of system defined function
in Sql Server
Scalar Function
Scalar functions operates on a single value and returns a
single value. Below is the list of some useful Sql Server
Scalar functions.
8. Aggregate Function
Aggregate functions operates on a collection of values and
returns a single value. Below is the list of some useful Sql
Server Aggregate functions.
9.
10. User Defined Function
These functions are created by user in system database
or in user defined database. We three types of user
defined functions.
Scalar Function
User defined scalar function also returns single value as a result of
actions perform by function. We return any datatype value from
function.
11. Inline Table-Valued Function
User defined inline table-valued function returns a table
variable as a result of actions perform by function.
The value of table variable should be derived from a
single SELECT statement.
12. Multi-Statement Table-Valued
Function
User defined multi-statement table-valued function
returns a table variable as a result of actions perform
by function.
In this a table variable must be explicitly declared and
defined whose value can be derived from a multiple
sql statements.
13.
14. Procedure and Function in SQL
Server
Stored Procedures are pre-compile objects which are
compiled for first time and its compiled format is
saved which executes (compiled code) whenever it is
called.
But Function is compiled and executed every time
when it is called.
Basic Difference
unction must return a value but in Stored Procedure it
is optional( Procedure can return zero or n values).
Functions can have only input parameters for it
whereas Procedures can have input/output
parameters .
Functions can be called from Procedure whereas
15. Advance Difference
Procedure allows SELECT as well as
DML(INSERT/UPDATE/DELETE) statement in it
whereas Function allows only SELECT statement in it.
Procedures can not be utilized in a SELECT statement
whereas Function can be embedded in a SELECT
statement.
Stored Procedures cannot be used in the SQL
statements anywhere in the WHERE/HAVING/SELECT
section whereas Function can be.
Functions that return tables can be treated as another
rowset. This can be used in JOINs with other tables.
Exception can be handled by try-catch block in a
Procedure whereas try-catch block cannot be used in a
Function.
18. Syntax for Trigger
Syntax of Trigger
CREATE TRIGGER trigger_name ON {table|view}
[WITH ENCRYPTION|EXECUTE AS]
{FOR|AFTER|INSTEAD OF} {[CREATE|ALTER|DRO
P|INSERT|UPDATE|DELETE ]}
[NOT FOR REPLICATION]
AS
sql_statement [1...n ]
19. Type of Trigger
After Trigger: These kinds of triggers fire after the
execution of an action query that can be either DDL
statements like Create, Alter and Drop or DML
statements like Insert, Update and Delete.
Instead of Trigger: These kinds of triggers fire before
the execution of an action query that can only be DML
statements like Insert, Update and Delete but after the
execution of that query. The table data will not be
affected, in other words if you want to insert or update
the data of the table then you need to write it in the
trigger using "inserted" or "deleted" virtual tables.
21. SQL HAVING Clause
The HAVING clause was added to SQL because the
WHERE keyword could not be used with aggregate
functions.
SQL HAVING Syntax
SELECT column_name,
aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name
HAVING aggregate_function(column_name) operator
value;
22. GROUP BY Statement
Aggregate functions often need an added
GROUP BY statement.
The GROUP BY statement is used in conjunction
with the aggregate functions to group the result-
set by one or more columns.
SQL GROUP BY Syntax
SELECT column_name,
aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name;
24. Declare Variables
In SQL Server a variable allows a programmer to
store data temporarily during the execution of
code.
Syntax
DECLARE @variable_name datatype [ =
initial_value ], @variable_name datatype [ =
initial_value ],
...;
26. BREAK statement
In SQL Server, the BREAK statement is used when
you want to exit from a WHILE LOOP and execute the
next statements after the loop's END statement.
Syntax
BREAK;
27. CONTINUE Statement
In SQL Server, the CONTINUE statement is used
when you are want a WHILE LOOP to execute again.
It will ignore any statements after the CONTINUE
statement
Syntax
CONTINUE;
28. GOTO Statement
The GOTO statement causes the code to branch to
the label after the GOTO statement.
GOTO statement
The GOTO statement consists of the GOTO
keyword, followed by a label_name.
GOTO label_name;Label Declaration
The Label Declaration consists of
the label_name, followed by at least one statement to
execute.
label_name: {...statements...}
29.
30. IF...ELSE statement
In SQL Server, the IF...ELSE statement is used to
execute code when a condition is TRUE, or execute
different code if the condition evaluates to FALSE.
Syntax
IF condition
{...statements to execute when condition is TRUE...}
[ ELSE {...statements to execute when condition is
FALSE...} ]
31. WHILE LOOP in SQL Server
In SQL Server, you use a WHILE LOOP when you are
not sure how many times you will execute the loop
body and the loop body may not execute even once.
Syntax
WHILE condition BEGIN {...statements...} END;
32. FOR LOOP
In SQL Server, there is no FOR LOOP. However, you
simulate the FOR LOOP using the WHILE LOOP.
Syntax
DECLARE @cnt INT = 0;
WHILE @cnt < cnt_total
BEGIN {...statements...}
SET @cnt = @cnt + 1;
END;
34. Difference Between Union and Union
All in SQL Server
UNION UNION ALL
UNION removes duplicate rows.
“UNION ALL” does not remove the
duplicate row. It returns all from all
queries.
UNION uses a distinct sort
“UNION ALL” does not use a distinct
sort, so the performance of “UNION
ALL” is slightly higher than “UNION”.
UNION cannot work with a column that
has a TEXT data type.
UNION ALL can work with all data type
columns.