SQ L Server Information
SQ L Server Information
SQ L Server Information
@x
@x
@x
@x
@x
=
=
=
=
=
@x
@x
@x
@x
@x
+
*
/
%
@y
@y
@y
@y
@y
Example:
DEClARE @x INT =2 ,@y INT = 2
SET @x+=@y
SELECT @x as x,@y as y
Result:
x
y
----------- ----------4
2
4) Table-Valued Parameters in Sql Server:
This is a new feature intorduced in Sql Server 2008. Table-Valued
Parameters provides option for the Client Applications to pass multiple
rows of Data to Stored Procedure.Prior to this, if we were needed to
pass multiple rows of Data from client application to Sql Server, then
we use to model the input data as xml and pass it to the stored
procedure and in Stored Procedure convert this xml to a table
variable/temporary table.
5) MERGE Statement
6) Sparse Column
Sparse Column is one more new feature introduced in SQL SERVER
2008. Storing a null value in a sparse column doesnt take any space,
but storing a non-null value in sparse column takes 4 bytes extra space
than the non-sparse columns of the same data type.
7) Date and Time Data Types
Date, Time, DateTime2 etc are the new date and time data type
introduced in SQL SERVER 2008.
Ex:
Select getdate()
For Date time
Select Sysdatetime()
-> For Datetime2(7)
8) XML Implementation
9)Data Encrypting
Use schema name with object name: The object name is qualified if used
with schema name. Schema name should be used with the stored procedure
name and with all objects referenced inside the stored procedure. This
help in directly finding the complied plan instead of searching the
objects in other possible schema before finally deciding to use a cached
plan, if available. This process of searching and deciding a schema for
an object leads to COMPILE lock on stored procedure and decreases the
stored procedures performance. Therefore, always refer the objects with
qualified name in the stored procedure like
SELECT * FROM dbo.MyTable -- Preferred method
-- Instead of
SELECT * FROM MyTable -- Avoid this method
--And finally call the stored procedure with qualified name like:
EXEC dbo.MyProc -- Preferred method
--Instead of
EXEC MyProc -- Avoid this method
Do not use the prefix sp_ in the stored procedure name: If a stored
procedure name begins with SP_, then SQL server first searches in the
master database and then in the current session database. Searching in
the master database causes extra overhead and even a wrong result if
another stored procedure with the same name is found in master database.
Use IF EXISTS (SELECT 1) instead of (SELECT *): To check the existence
of a record in another table, we uses the IF EXISTS clause. The IF
EXISTS clause returns True if any value is returned from an internal
statement, either a single value 1 or all columns of a record or
complete recordset. The output of the internal statement is not used.
Hence, to minimize the data for processing and network transferring, we
should use 1 in the SELECT clause of an internal statement, as shown
below:
If we again execute the above batch using different @Age value, then the
execution plan for SELECT statement created for @Age =25 would not be
reused. However, if we write the above batch as given below,
DECLARE @Query NVARCHAR(100)
SET @Query = N'SELECT * FROM dbo.tblPerson WHERE Age = @Age'
EXECUTE sp_executesql @Query, N'@Age int', @Age = 25
the compiled plan of this SELECT statement will be reused for different
value of @Age parameter. The reuse of the existing complied plan will
result in improved performance.
Try to avoid using SQL Server cursors whenever possible: Cursor uses a
lot of resources for overhead processing to maintain current record
position in a recordset and this decreases the performance. If we need
to process records one-by-one in a loop, then we should use the WHILE
clause. Wherever possible, we should replace the cursor-based approach
with SET-based approach. Because the SQL Server engine is designed and
optimized to perform SET-based operation very fast. Again, please note
cursor is also a kind of WHILE Loop.
Keep the Transaction as short as possible: The length of transaction
affects blocking and deadlocking. Exclusive lock is not released until
the end of transaction. In higher isolation level, the shared locks are
also aged with transaction. Therefore, lengthy transaction means locks
for longer time and locks for longer time turns into blocking. In some
cases, blocking also converts into deadlocks. So, for faster execution
and less blocking, the transaction should be kept as short as possible.
Use TRY-Catch for error handling: Prior to SQL server 2005 version code
for error handling, there was a big portion of actual code because an
error check statement was written after every t-sql statement. More code
always consumes more resources and time. In SQL Server 2005, a new
simple way is introduced for the same purpose. The syntax is as
follows:BEGIN TRY
--Your t-sql code goes here
END TRY
BEGIN CATCH
(OR)
1- use SET NOCOUNT ON
2- avoid using UNION statements
3- use WITH (NOLOCK) with each SELECT statement
4- avoid using NESTED Select statements
5- use #temp tables
6- avoid renaming tables in SELECT statements, for example SELECT * FROM
tblClients C
You can use WHERE clause(conditions) with DELETE but you can't use WHERE clause
with TRUNCATE .
You cann't rollback data in TRUNCATE but in DELETE you can rollback data.TRUNCATE
removes(delete) the record permanently.
A trigger doesnt get fired in case of TRUNCATE whereas Triggers get fired in DELETE
command.
If tables which are referenced by one or more FOREIGN KEY constraints then
TRUNCATE will not work.
TRUNCATE resets the Identity counter if there is any identity column present in the
table where delete not resets the identity counter.
Delete and Truncate both are logged operation. But DELETE is a logged operation on a
per row basis and TRUNCATE logs the DE allocation of the data pages in which the
data exists.
TRUNCATE is faster than DELETE.
Procedure may return none or more values. Function must always return one value
either a scalar value or a table.
Procedure have input, output parameters. Functions have only input parameters.
Stored procedures are called independently by EXEC command whereas Functions are
called from within SQL statement.
Functions can be called from procedure. Procedures cannot be called from function.
Exception can be handled in Procedure by try-catch block but try-catch block cannot
be used in a function.(error-handling)
Transaction management possible in procedure but not in function
Both Primary Key and Unique Key give uniqueness of the column on which they are
defined.
By default Unique Key creates a nonclustered index whereas Primary Key creates a
clustered index on the column.
A Primary key value cannot be NULL whereas Unique Key allows only one NULL.
Data Types:
Syntax
SET NOCOUNT { ON | OFF }
Remarks
When SET NOCOUNT is ON, the count (indicating the number of rows affected
by a Transact-SQL statement) is not returned. When SET NOCOUNT is OFF, the
count is returned.
2. Set IDENTITY_INSERT TableName ON
Syntax
SET IDENTITY_INSERT [ database. [ owner. ] ] { table } { ON | OFF }
Arguments
database
Is the name of the database in which the specified table resides.
owner
Is the name of the table owner.
Table
Is the name of a table with an identity column.
Ex: SET IDENTITY_INSERT products ON
GO
-- Attempt to insert an explicit ID value of 3
INSERT INTO products (id, product) VALUES(3, 'garden shovel')
3.
4.
5.
6.
7.
8.
9.
Rank wise:
select rank() over(order by sal desc) ranks,e.* from emp e order by sal
desc
Populate the number from 1 to 10:
select level from emp connect by level<=10
Dense rank:
select dense_rank() over(order by sal asc) ranks,e.* from emp e order by
sal asc
Lessthan averase salary for each dept:
select e.empno,e.ename,e.sal,deptno from emp e where e.sal<=(select
avg(e1.sal) from emp e1 where e.deptno=e1.deptno group by e1.deptno)
order by e.deptno
How to get multiple row values with comma separate in single column:
select replace(''''|| RTRIM(XMLAGG(XMLELEMENT(e,rep_emp_code || ' ' ||
','|| ' ')).EXTRACT('//text()'),','),' ','''')||'''' AS REP_EMP_CODE
from auditee where aud_code='PSRH'
Stuff Function in SQL:
SELECT STUFF('abcdef', 2, 3, 'ijklmn');
Output: aijklmnef
Partition:
select employee_name, salary, rank() OVER (PARTITION BY department ORDER BY salary)
from employees where department = 'Marketing'
who are the employees in each reporting manager:
select distinct e.empid,e.empname,e.rptmgr from emp e,emp e1 where
e.rptmgr=e1.empno
-- Get Current Session ID
SELECT @@SPID AS Current_SessionID
-- Create Temp Table and insert three thousand rows
CREATE TABLE #TempTable (Col1 INT)
INSERT INTO #TempTable (Col1)
SELECT TOP 3000 ROW_NUMBER() OVER(ORDER BY a.name)
FROM sys.all_objects a
CROSS JOIN sys.all_objects b
GO
-- Create Table Variable and insert three thousand rows
DECLARE @temp TABLE(Col1 INT)
INSERT INTO @temp (Col1)
SELECT TOP 3000 ROW_NUMBER() OVER(ORDER BY a.name)
FROM sys.all_objects a
CROSS JOIN sys.all_objects b
-- Clean up
DROP TABLE #TempTable
Rollup
SELECT CASE WHEN (GROUPING(Item) = 1) THEN 'ALL'
Color
-------------------Blue
Red
ALL
Blue
Red
ALL
ALL
QtySum
-------------------------101.00
210.00
311.00
124.00
223.00
347.00
658.00
SELECT *
FROM products
GO
-- Attempt to insert an explicit ID value of 3;
-- should return a warning.
INSERT INTO products (id, product) VALUES(3, 'garden shovel')
GO
-- SET IDENTITY_INSERT to ON.
SET IDENTITY_INSERT products ON
GO
-- Attempt to insert an explicit ID value of 3
INSERT INTO products (id, product) VALUES(3, 'garden shovel').
GO
SELECT *
FROM products
GO
-- Drop products table.
DROP TABLE products
GO
Date Formats:
CONVERT(VARCHAR(19),GETDATE())
CONVERT(VARCHAR(10),GETDATE(),10)
CONVERT(VARCHAR(10),GETDATE(),110) only for Date
CONVERT(VARCHAR(11),GETDATE(),6)
CONVERT(VARCHAR(11),GETDATE(),106)
CONVERT(VARCHAR(24),GETDATE(),113) Date and Time
CONVERT(VARCHAR(5),GETDATE(),114)for Time OR 108 ALSO 100 for AM or PM
Convert(date,01-Oct-2013,106)
Sy: select replace(convert(varchar(24),getdate(),113),' ','-') date
Value
(century yyyy)
Input/Output
Standard
0 or 100
Default
101
mm/dd/yy
USA
102
yy.mm.dd
ANSI
103
dd/mm/yy
British/French
104
dd.mm.yy
German
105
dd-mm-yy
Italian
106
dd mon yy
107
Mon dd, yy
108
hh:mm:ss
9 or 109
Default+millisec
10
110
mm-dd-yy
USA
11
111
yy/mm/dd
Japan
12
112
yymmdd
ISO
13 or 113
14
114
hh:mi:ss:mmm (24h)
20 or 120
21 or 121
126
ISO8601
130
Hijiri
131
dd/mm/yy hh:mi:ss:mmmAM
Hijiri
Implicit cursors
These are created by default when DML statements like, INSERT, UPDATE, and
DELETE statements are executed. They are also created when a SELECT statement that
returns just one row is executed.
Explicit cursors
They must be created when you are executing a SELECT statement that returns more
than one row. Even though the cursor stores multiple records, only one record can be
processed at a time, which is called as current row. When you fetch a row the current row
position moves to next row.
Both implicit and explicit cursors have the same functionality, but they differ in the way
they are accessed.
To work with cursors you must use the following SQL statements:
DECLARE CURSOR
OPEN
FETCH
CLOSE
The following example demonstrates the basic use of a read-only cursor within an SQL
procedure:
CREATE PROCEDURE sum_salaries(OUT sum INTEGER)
LANGUAGE SQL
BEGIN
DECLARE SQLSTATE CHAR(5) DEFAULT '00000';
DECLARE p_sum INTEGER;
DECLARE p_sal INTEGER;
DECLARE c CURSOR FOR SELECT SALARY FROM EMPLOYEE;
SET p_sum = 0;
OPEN c;
FETCH FROM c INTO p_sal;
WHILE(SQLSTATE = '00000') DO
SET p_sum = p_sum + p_sal;
FETCH FROM c INTO p_sal;
END WHILE;
CLOSE c;
SET sum = p_sum;
END%
About TL:
Indexes
Triggers : Instead, After Triggers
Tempary Tables
Hash Tables
Functions
Common Table Expressions
---------------------------------------------------------------------------You can turn autocommit ON by setting implicit_transactions OFF:
SET IMPLICIT_TRANSACTIONS OFF
When the setting is ON, it returns to implicit transaction mode. In implicit transaction mode,
every change you make starts a transactions which you have to commit manually.
Maybe an example is clearer. This will write a change to the database:
SET IMPLICIT_TRANSACTIONS ON
UPDATE MyTable SET MyField = 1 WHERE MyId = 1
COMMIT TRANSACTION
This will not write a change to the database:
SET IMPLICIT_TRANSACTIONS ON
UPDATE MyTable SET MyField = 1 WHERE MyId = 1
ROLLBACK TRANSACTION
The following example will update a row, and then complain that there's no transaction to
commit:
SET IMPLICIT_TRANSACTIONS OFF
UPDATE MyTable SET MyField = 1 WHERE MyId = 1
ROLLBACK TRANSACTION
Like Mitch Wheat said, autocommit is the default for Sql Server 2000 and up.
System Variables:
@@error
Error number
@@identity
@@language
@@max_connections
@@rowcount
@@rowcount
@@servername
@@version
Database Name
SUSER_SNAME()
NT User Name
USER_NAME()
sp_helpdb
sp_helpdb pubs
sp_help authors
sp_helptext byroyalry
sp_depends authors
sp_changeowner
sp_rename
Rename an object.
Bulk Insert:
BULK INSERT TmpStList FROM '"+@PathFileName+"'
'"",""')
WITH (FIELDTERMINATOR =
Triggers:
In Orcle:
create or replace
TRIGGER "ACCTS".TR_USERS AFTER INSERT ON SLMST
FOR EACH ROW
BEGIN
if :new.typ = 5 then
insert into sale.users(user_id,pwd,uname,loc,rights,stat) values
(:new.code,:new.code,:new.name,0,'USER','N');
end if;
END;
In Sqlserver:
1. After Triggers
2. Instead of Triggers
After Insert Trigger
1.
declare
@empid
int,
@empname
varchar(55),
@empsal
@audit_action='Inserted
Record
--
After
Insert
Trigger.';
insert
into
Employee_Demo_Audit(Emp_ID,Emp_Name,Emp_Sal,Audit_Action,Audit_Tim
estamp)
9. values (@empid,@empname,@empsal,@audit_action,getdate());
10. PRINT 'AFTER INSERT trigger fired.'
12. insert
intoEmployee_Demo_Audit(Emp_ID,Emp_Name,Emp_Sal,Audit_Action,Audit
_Timestamp)
13. values (@empid,@empname,@empsal,@audit_action,getdate());
14. PRINT 'AFTER UPDATE trigger fired.'
into
Employee_Demo_Audit
(Emp_ID,Emp_Name,Emp_Sal,Audit_Action,Audit_Timestamp)
10. values (@empid,@empname,@empsal,@audit_action,getdate());
11. PRINT 'AFTER DELETE TRIGGER fired.'
@emp_id
int,
@emp_name
varchar(55),
@emp_sal
BEGIN TRAN
12.
SET NOCOUNT ON
13.
if(@emp_sal>=1000)
14.
begin
15.
16.
Insert
into
Employee_Demo_Audit(Emp_ID,Emp_Name,Emp_Sal,Audit_Action,Audit_Tim
estamp)
values(@@identity,@emp_name,@emp_sal,@audit_action,getdate());
17.
COMMIT;
18.
19. END
@emp_id
int,
@emp_name
varchar(55),
@emp_sal
BEGIN TRAN
11. if(@emp_sal>=1000)
12.
begin
13.
14.
else begin
15.
insert
into
Employee_Demo_Audit(Emp_ID,Emp_Name,Emp_Sal,Audit_Action,Audit_Tim
estamp)
values(@emp_id,@emp_name,@emp_sal,@audit_action,getdate());
16.
COMMIT;
17.
11.
end
12.
else begin
13.
14.
COMMIT;
15.
insert
into
Employee_Demo_Audit(Emp_ID,Emp_Name,Emp_Sal,Audit_Action,Audit_Tim
estamp)
16.
values(@empid,@empname,@empsal,'Deleted --
Instead Of
Delete
Trigger.',getdate());
17.
Syntax in SQl:
Alter PROCEDURE GetstudentnameInOutputVariable
(
@studentid INT,
--Input parameter , Studentid of the student
@studentname VARCHAR (200) OUT, -- Output parameter to collect the student name
@StudentEmail VARCHAR (200)OUT
-- Output Parameter to collect the student email
)
AS
BEGIN
SELECT @studentname= Firstname+' '+Lastname,
@StudentEmail=email FROM tbl_Students WHERE studentid=@studentid
END
Functions:
Syntax in Oracle:
create or replace FUNCTION FUN_PTYCR(SLCD VARCHAR2) RETURN NUMBER AS
TDS NUMBER(15,2);
TDS1 NUMBER(15,2);
BEGIN
TDS:=0;
select NVL(sum(tds),0) INTO TDS1 from rep_ptycr where v_no in(select v_no from
rep_ptycr where slcode=SLCD); --pcode= slcd;
TDS:=TDS+ tds1;
RETURN TDS;
END FUN_PTYCR;
Syntax in Sql:
CREATE FUNCTION dbo.ufnGetInventoryStock(@ProductID int)
RETURNS int
AS
-- Returns the stock level for the product.
BEGIN
DECLARE @ret int;
SELECT @ret = SUM(p.Quantity)
FROM Production.ProductInventory p
WHERE p.ProductID = @ProductID
AND p.LocationID = '6';
IF (@ret IS NULL)
SET @ret = 0;
RETURN @ret;
END;