26.4.09

Check option in SQL Server views

CHECK option in views:

Views are nothing but an external skeleton for a table. In order to avoid direct access to the table + If we have multiple tables to be linked to obtain a single output. We will use views.

if exists (select * from sys.objects where name='venkat')
begin
drop table venkat
end
if not exists (select * from sys.objects where name='venkat')
begin
create table venkat(id int, name varchar(20),name1 varchar(20))
end
insert into venkat
select 1,'venkat','venkat1'
union all
select 2,'arun','arun1'
union all
select 3,'lakshmi','lakshmi1'
union all
select 4,'karthi','karthi1'
go

insert into venkat select 6,'sajhklg','kjhl55'
--select * from sys.messages ]
drop view venkatview

-- Check option is used to restrict the data by validating at the view level
create view venkatview
as
select * from venkat where id>3 with check option

If we try to execute the below insert statement,
insert into venkatview select 0,'suba','suba1'
We will get an error message,
Msg 550, Level 16, State 1, Line 1
The attempted insert or update failed because the target view either specifies WITH CHECK OPTION or spans a view that specifies WITH CHECK OPTION and one or more rows resulting from the operation did not qualify under the CHECK OPTION constraint.
The statement has been terminated.

Thanks and Regards,
Venkatesan Prabu .J

Inserting multiple records using single insert statement

An Interesting topic with Insert statement,
Is it possible to insert multiple records using one single insert statement.
Let's see,
-- If table exists, drop the table
drop table venkat
create table venkat(id int, name varchar(10))
insert into venkat
select 1,'Venkat'
union all
select 2,'arun'
union all
select 3,'Lakshmi'
union all
select 4,'arun'
union all
select 5,'ve'
select * from venkat

Thanks and Regards,
Venkatesan Prabu .J

Error Messages in SQL Server

Error messages or exceptions in SQL Server:
Error messages thrown in case of error is stored in sys.messages SQL Server table.
Below are the columns available in this table.
1. Message_id - Unique id to identify the error.
2. Language_id - Specifies the language of the message stored. (1033 indicates English)
3. Severity - Severity of the message.
4. is_event_logged - 0 indicates, the message won't get logged in the Eventviewer.
1 indicates the message will get logged in the Eventviewer.
5. Text - text displayed to the user incase of showing error.



If we want to check the content of this table,
use master
select * from sys.messages



Happy Learning!!!

Thanks and Regards,

Venkatesan Prabu .J

Self Join + Case statement in SQL Server

Self Join:

Joining the table itself is referred to as self join.

Case statement:

Case statement is used to check the condition

-- Creating tables for our sample
drop table venkat
drop table original
create table venkat(id int)
insert into venkat values(10)
insert into venkat values(11)
insert into venkat values(12)
insert into venkat values(13)
insert into venkat values(14)
----------------------------------------------------
create table Original(id int)
insert into Original values(1)
insert into Original values(2)
insert into Original values(3)
----------------------------------------------------
select * from original
select * from venkat

-- How to use case statement in SQL Server
select a.id from venkat a inner join
(select
case id
when 1 then 10
when 2 then 11
when 3 then 12
end as id
from original)t on a.id=
t.id

--- Self join with the same table.
select a.id from venkat a inner join
(select id from venkat)t on a.id=
t.id

Happy Learning!!!
Thanks and Regards,

Venkatesan Prabu .J

Query governor in SQL Server

Query governor cost limit:

This option is used to set the time to be taken to execute a query.
If the value is 0, then the time limit is indefinite. Where as, if we provide
positive value. The query will be executed upto that particular time.

For server level settings [For all the users]:
If we want to set this value for the whole server irrespective of the users, we can acheive it using
sp_configure option.
sp_configure 'QUERY_GOVERNOR_COST_LIMIT',1
reconfigure

Whereas, If we need to set the same for specific connection. We can use the set command.

SET QUERY_GOVERNOR_COST_LIMIT 1 Thanks and Regards,
Venkatesan Prabu .J

T-SQL Challenges - Fetching the first and last record from the table

Problem statement:
Considering am having lot of records(Bank transaction) in a table.
1. I want the account number which repeats more than one times.
2. After filtering the records in the above criteria, I need only the first transaction and the last transaction.
How can we achieve it? Lets see,
-- Am creating the table
create table venkat(id int, id1 int,nam varchar(10))
insert into venkat values(1,1,'Venkat')
insert into venkat values(1,2,'Venkat')
insert into venkat values(1,3,'Venkat')
insert into venkat values(1,4,'Venkat')
insert into venkat values(1,5,'Venkat')
insert into venkat values(2,5,'Venkat')
select * from venkat

This kind of problem can be resolved using Common Table expression (a new feature in SQL Server 2005). CTE enable us to divide a huge query into smaller chunks and we can easily write a complex logic in simple terms.

with cte as
(
select * from
(
select count(id) as cnt,id from venkat group by id
-- Getting the records with more than 1 record
)t where cnt >1
),
cte1 as
(
select row_number()over(partition by id order by id1 ) as rownumber ,id,id1,nam from venkat
where id in (select id from cte)
-- Partitioning the records using rownumber partition concept
)
,
cte2 as
(
select * from cte1 where (rownumber=1 OR rownumber = (select max(rownumber) from cte1))
-- Fetching only 1 and last record in each individual clients
)
select * from cte2

Regards,
Venkatesan Prabu .J

14.4.09

Conditional Joins in SQL Server

An interesting article on conditional joins in sql server:
I have come across a very different peculiar scenario to join a column based on the conditions.
1. If the column value is not null join with the columnA.
2. If the column value is null join with columnB in the same table.
How can we achieve this?
-- creating temporary tables
drop table venkat
create table venkat(id int, name varchar(100))
insert into venkat values(1,'venkat1')
insert into venkat values(2,'venkat2')
insert into venkat values(3,'venkat3')
insert into venkat values(4,'venkat4')
drop table venkat1
create table venkat1(id int,id1 int, name1 varchar(100))
insert into venkat1 values(1,null,'venkat1')
insert into venkat1 values(2,null,'venkat2')
insert into venkat1 values(null,3,'venkat3')
insert into venkat1 values(4,null,'venkat4')
select * from venkat1
select * from venkat
-- Option 1
select * from venkat a
inner join venkat1 b on a.id=
case isnull(b.id,0)
when 0 then b.id1
else
b.id
end
-- Option2
select * from venkat a
inner join venkat1 b on a.id=isnull(b.id,b.id1)
-- column values
select a.id,a.name,isnull(b.id,b.id1) as TableBID from venkat a
inner join venkat1 b on a.id=isnull(b.id,b.id1)

Thanks and Regards,
Venkatesan Prabu .J

13.4.09

Pattern matching in SQL Server

Pattern matching is a very important topic in SQL Server. It enable us to get the relative or exact pattern of data from the database. Pattern matching can be easily achieved through LIKE operator in the where clause.

% - Operator is used to fetch 0 or any number of characters and
_ - Operato is used to identify one character at its place. L

Lets see some example to learn more,
-- Creating a table and inserting some dummy data into that table.
drop table venkat
create table venkat(id int, name varchar(10))
insert into venkat
select 1,'Venkat'
union all
select 2,'arun'
union all
select 3,'Lakshmi'
union all
select 4,'arun'
union all
select 5,'ve'
--String Starts with "V" and having any number of characters. % indicates any number of character
select * from venkat where name like 'V%'
--String Starts with "V" and having only one character. _ indicates only one number.
select * from venkat where name like 'V_'
--String should not start with "V"
select * from venkat where name not like 'V%'
Thanks and Regards,
Venkatesan Prabu .J

Recent data from sql server table - TSQL Challenges

Scenario:
Considering, lot of empoloyees with different roles were working in a office. I want to list down the employees recent job role. Is it possible?

What are the complications in this, am having a table listing down the employee role and its role change date. I need to fetch the recent date and the recent employees role.

-- Creating temporary tables
drop table emp
drop table emprole
create table Emp(Employeeid int, roleid int,Updtdate datetime)
create table EmpRole(Roleid int, RoleName varchar(10))

-- Inserting data into the temporary tables
insert into emp values(1,33,'2008-10-11')
insert into emp values(1,44,'2008-10-13' )
insert into emp values(2,55,'2008-10-14')
insert into emp values(2,66,'2008-10-16')


insert into emprole values(33, 'Mgr')
insert into emprole values(44, 'Dev')
insert into emprole values(55, 'Tester')
insert into emprole values(66, 'Lead')

-- Output for the below three select statements
select * from Emp

select * from EmpRole

select Employeeid,rolename from
(
select Employeeid, row_number() over (partition by employeeid
order by UpdtDate desc) as row ,r.rolename
from emp e inner join emprole r on e.roleid=r.roleid
)tblw where row=1



Thanks and Regards,
Venkatesan Prabu .J

Stored Procedure inside another stored procedure

Its pretty interesting topic, a basic one too.
This article will answer you the below questions,
1. Is it possible to execute a stored procedure inside a stored procedure?
2. How to insert a data into a table from stored procedure output?

-- Am trying to create temporary tables
create table venkattable(id int, name1 varchar(100))
create table venkattable1(id int, name1 varchar(100))
-- Inserting data into my table
insert into venkattable values(1,'Venkat1')
insert into venkattable values(2,'Venkat2')
insert into venkattable values(3,'Venkat3')
insert into venkattable values(4,'Venkat4')
insert into venkattable values(5,'Venkat5')
insert into venkattable values(6,'Venkat6')
select * from venkattable


--Creating first stored procedure

create procedure venkatproc1
as begin
select * from venkattable where id=2
end
exec venkatproc1

--Creating second stored procedure
create proc venkatproc2
as begin

-- Output of a stored procedure into a sql server table
insert venkattable1 exec venkatproc1
end
exec venkatproc2

-- Checking for the output
select * from venkattable1

Thanks and Regards,

Venkatesan Prabu .J