What is Stored Procedure?
A
stored procedure is a named group of SQL statements that have been previously
created and stored in the server database. Stored procedures accept input
parameters so that a single procedure can be used over the network by several
clients using different input data. And when the procedure is modified, all
clients automatically get the new version. Stored procedures reduce network
traffic and improve performance. Stored procedures can be used to help ensure
the integrity of the database.e.g. sp_helpdb, sp_renamedb, sp_depends etc.
SYNATX Create
Proc Proc_Name
//declaring
variables
@variable_name data_type =value
As
Begin
//Body
of SP.
End
What is Trigger?
A
trigger is a SQL procedure that initiates an action when an event (INSERT,
DELETE or UPDATE)occurs. Triggers are stored in and managed by the
DBMS.Triggers are used to maintain the referential integrity of data by
changing the data in a systematic fashion. A trigger cannot be called or
executed; the DBMS automatically fires the trigger as a result of a data
modification to the associated table.Triggers can be viewed as similar to
stored procedures in that both consist of procedural logic that is stored at
the database level. Stored procedures, however, are not event-drive and are not
attached to a specific table as triggers are. Stored procedures are explicitly
executed by invoking a CALL to the
procedure
while triggers are implicitly executed. In addition, triggers can also execute
stored procedures.
Nested Trigger: A trigger
can also contain INSERT, UPDATE and DELETE logic within itself, so when the trigger
is fired because of data modification it can also cause another data
modification, thereby firing another trigger. A trigger that contains data
modification logic within itself is called a
nested trigger.
What is View?
A
simple view can be thought of as a subset of a table. It can be used for
retrieving data, as well as updating or
deleting rows. Rows updated or deleted in the view are updated or deleted
in the table the view was created with.
It should also be noted that as data in the original table changes, so does
data in the view, as views are the way to look at part of the original table.
The results of using a view are not permanently stored in the database. The
data accessed through a view is actually constructed using standard T-SQL
select command and can come from one to many different base tables or even
other views.
What is cursors?
Cursor
is a database object used by applications to manipulate data in a set on a
row-by-row basis, instead of the typical SQL commands that operate on all the
rows in the set at one time. In order to work with a cursor we need to perform
some steps in the following order: Declare
cursor
Open cursor
Fetch row from the cursor
Process fetched row
Close cursor
Deallocate cursor
What is a Linked Server?
Linked
Servers is a concept in SQL Server by which we can add other SQL Server to a
Group and query both the SQL Server dbs using T-SQL Statements. With a linked
server, you can create very clean, easy to follow, SQL statements that allow
remote data to be retrieved, joined and combined with local data. Storped
Procedure sp_addlinkedserver, sp_addlinkedsrvlogin will be used add new Linked
Server.
What is Collation?
Collation
refers to a set of rules that determine how data is sorted and compared.
Character data is sorted using rules
that define the correct character sequence, with options for specifying
casesensitivity, accent marks, kana character types and character width.
What's
the difference between a primary key and a unique key?
Both
primary key and unique enforce uniqueness of the column on which they are
defined. But by default primary key creates a clustered index on the column,
where are unique creates a nonclustered index by default. Another major
difference is that, primary key doesn't allow NULLs, but unique key allows one
NULL only.
What is difference between DELETE & TRUNCATE commands?
Delete
command removes the rows from a table based on the condition that we provide
with a WHERE clause. Truncate will actually remove all the rows from a table
and there will be no data in the table after we run the truncate command.
Difference between Function and Stored Procedure?
UDF can
be used in the SQL statements anywhere in the WHERE/HAVING/SELECT section where
as Stored procedures cannot be. UDFs that return tables can be treated as
another rowset. This can be used in JOINs with other tables. Inline UDF's can
be though of as views that take parameters and can be used in JOINs and other Rowset
operations.
What is the difference between a HAVING CLAUSE
and a WHERE CLAUSE?
Specifies
a search condition for a group or an aggregate. HAVING can be used only with
the SELECT statement. HAVING is typically used in a GROUP BY clause. When GROUP
BY is not used, HAVING behaves like a WHERE clause. Having Clause is basically
used only with the GROUP BY function in a query. WHERE Clause is applied to
each row before they are part of the GROUP BY function in a query.
What is SQL server agent?
SQL
Server agent plays an important role in the day-to-day tasks of a database
administrator (DBA). It is often overlooked as one of the main tools for SQL
Server management. Its purpose is to ease the implementation of tasks for the
DBA, with its full-function scheduling engine, which allows you to schedule
your own jobs and scripts.
What is Raiseerror?
Stored
procedures report errors to client applications via the RAISERROR command.
RAISERROR doesn't change the flow of a procedure; it merely displays an error
message, sets the @@ERROR automatic variable, and optionally writes the message
to the SQL Server error log and the NT
application event log.
What is the difference between a local and a global variable?
A local
temporary table exists only for the duration of a connection or, if defined
inside a compound statement, for the duration of the compound statement.
A
global temporary table remains in the database permanently, but the rows exist
only within a given connection. When connection are closed, the data in the
global temporary table disappears. However,
the table definition remains with the database for access when database
is opened next time.