You are on page 1of 4

SELECT Name

FROM HumanResources.Department
WHERE (GroupName = 'Executive General and Administration')

Name
HR
Finance
Executive

transform rows into columns


pivot

How To Check If Column Exists for a Table Or View in Database


information_schema.columns

how to read uncommitted rows from sql server table


we can use with nolock hint to read dirty or uncommited data

select * from table_name with (nolock);

Can We Create Trigger on Temp Tables and System Tables in SQL Server
NOT on temp but we can on system

Question 10. Difference Between Delete & Truncate Statement? Which Statement Can Be
Rollbacked?

Answer :

With DELETE we can provide conditional WHERE clause to remove/delete specific rows,
which is not possible with TRUNCATE.
TRUNCATE is faster than DELETE as Delete keeps log of each row it deletes in
transaction logs, but truncate keeps log of only de-allocated pages in transaction
logs.
Both statements can be rolled backed if provided in a transaction (BEGIN TRANS). If
not then none of them can be rollbacked.
DELETE is DML just like INSERT, UPDATE, but TRANCATE is DDL, just like CREATE,
ALTER, DROP.

Question 24. What Is A Table Called That Has ## Before Its Name, What Is Its Scope?

Answer :

Table with ## (double pound signs) is called Global Temp table. Scope is outside
the session but only till the original session lasts.

Question 25. What Is The Scope Of A Temporary Table?

Answer :

Scope is limited to its session only.


63. What is the highest, lowest & default ISOLATION Level?
Highest: SERIALIZABLE
Lowest: READ UNCOMMITTED
Default: READ COMMITTED

Question 44. How Many Types Of Recovery Models Are Available For A Database?

Answer :

Simple
Bulk logged
Full.
Question 45. How Many Types Of Temporary Tables Are There In Sql Server?

Answer :

Local Temp tables (#)


Global temp tables (##)
Table variables (@).

37. What are the various Isolation levels?


a) Read Uncommitted Isolation Level
b) Read Committed Isolation Level
c) Repeatable Read Isolation Level
d) Serializable Isolation Level
e) Snapshot Isolation Level
f) Read Committed Snapshot Isolation Level

http://www.sql-server-performance.com/articles/dba/isolation_levels_2005_p1.aspx

101. What are the required/mandatory parameters that have to be passed with
RAISEERROR statement?

– message _id or message_str


– severity, and
– state

New THROW statement in SQL Server 2012 (vs RAISERROR)

Newly introduced THROW keyword in SQL server 2012 is an improvement over the
existing RAISERROR() statement. Yes, it’s single ‘E’ in RAISERROR.

Both RAISERROR & THROW can be used in T-SQL code/script to raise and throw error
within a TRY-CATCH block. Check my previous post for TRY-CATCH block, [link].
–> With RAISERROR developers had to use different ERROR_xxxx() system functions to
get the error details to pass through the RAISERROR() statement, like:
– ERROR_NUMBER()
– ERROR_MESSAGE()
– ERROR_SEVERITY()
– ERROR_STATE()

Question 30. Difference Between Varchar & Varchar2?

Answer :

VARCHAR2 is specific to Oracle. MS SQL Server has VARCHAR & VARCHAR(MAX) data
types.

What are Cursors and their types? What type do you use most and which one is fast?
FORWARD-ONLY, FAST-FORWARD or READ-ONLY cursors.
Fastest to slowest: Dynamic, Static, and Keyset.

21. What new indexes are introduced in SQL Server 2005 in comparison to 2000?
– Spatial
– XML

26. What are the virtual tables in Triggers?


Inserted & Deleted

84. How will you check if a stored procedure with 500 line of code is running very
slow? What steps will you take to optimize it?

72. How will you know Index usage on tables?


– Execution plan
– SET STATISTICS PROFILE ON

DIRTY reads and PHANTOM reads – SQL Server

DIRTY READS: Reading uncommitted modifications are call Dirty Reads. Values in the
data can be changed and rows can appear or disappear in the data set before the end
of the transaction, thus getting you incorrect or wrong data.

This happens at READ UNCOMMITTED transaction isolation level, the lowest level.
Here transactions running do not issue SHARED locks to prevent other transactions
from modifying data read by the current transaction. This also do not prevent from
reading rows that have been modified but not yet committed by other transactions.

To prevent Dirty Reads, READ COMMITTED or SNAPSHOT isolation level should be used.
–> PHANTOM READS: Data getting changed in current transaction by other transactions
is called Phantom Reads. New rows can be added by other transactions, so you get
different number of rows by firing same query in current transaction.

In REPEATABLE READ isolation levels Shared locks are acquired. This prevents data
modification when other transaction is reading the rows and also prevents data read
when other transaction are modifying the rows. But this does not stop INSERT
operation which can add records to a table getting modified or read on another
transaction. This leads to PHANTOM reads.

PHANTOM reads can be prevented by using SERIALIZABLE isolation level, the highest
level. This level acquires RANGE locks thus preventing READ, Modification and
INSERT operation on other transaction until the first transaction gets completed.

which iso level supports highets level of concurrency


intent locking
heap vs clustered index

declare

@date1 datetime,
@date2 datetime,
@diff int

begin

set @date1 = getdate();

set @date2 = '2022/05/01';

set @diff = round (datediff (day, @date2,@date1)/31,2) ;


print @diff;
end;

You might also like