Tuesday, 6 November 2012

STORED PROCEDURES

INSERTING RECORDS USING SPS

create procedure Insert_Empp(
                            
                             @ID int,
                             @name varchar(10),
                             @salary int
                             )
                             As
                             BEGIN

INSERT  INTO dbo.Empp(ID,name,salary)values
                                                                        (@ID,
                                                                     @name, 
                                                                     @salary)
                                                                      END

RETRIEVING RECORDS
create procedure select_Empp(
                                                   @Id int,
                                                    @name varchar(10),
                                                    @salary int
                                                     )
                                                  As
                                                Begin 
                                                End
         select * from Empp    

Getting details by ID                        
 create procedure Showdetails
                             (
                             @ID int
                             )
                             As
                             Begin
                             select name,salary from Empp
                             where ID=@ID
                             End
                             EXEC  Showdetails 3



procedure with output parameter
create procedure pro5(@AB int,@BC int,@CD int,@DE int)
as
begin
declare @abc int
set @abc=@DE+@AB+@BC+@CD
print 'RESULT AS '+cast(@abc as varchar)
end
EXEC pro5 200,300,500,600

SIMPLE EXAMPLES

--simple stored procedure
create procedure simmple_proc
as
begin
print'hi yjreddy doing first procedure'
End

Exec simmple_proc

--second one 
create procedure Secondonewithparameters(@x int,@y int)
as
begin
declare @z int
set @z=@x+@y
print 'The sum of two nos is'+cast(@z as varchar)
End

EXEC Secondonewithparameters 10,20
EXEC Secondonewithparameters 50,100

create procedure third(@a int,@b int,@c int)
AS
BEGIN
DECLARE @d int
set @d=@a+@b+@c
print 'result is '+cast(@d as varchar)
END



create procedure proc4(@e int,@f int,@g int)
as 
begin
declare @v int
set @v=@e+@f+@g
print 'result as '+cast(@v as varchar)
end
EXEC proc4 100,200,300
--procedure with output parameter
create procedure pro5(@AB int,@BC int,@CD int,@DE int)
as
begin
declare @abc int
set @abc=@DE+@AB+@BC+@CD
print 'RESULT AS '+cast(@abc as varchar)
end
EXEC pro5 200,300,500,600

DELETE RECORD USING STOREDPROCEDURE

--Delete recorb by id
create procedure Delete_procedure(@ID int,@title nvarchar(10),@averageSalary int)
as
begin
delete  from EMPJOB  where ID=@ID
end 




--updating records by sps

create procedure update_emp11(@ID int,@title nvarchar(10),@averageSalary int)
as
begin
update EMPJOB set title=@title , Id=@id where ID=@ID
end
EXEC update_emp11 107,'yjreddy',50000



--create procedure for inserting checking null values
create procedure condition_nulls(@Id int,
                                 @title nvarchar(10),
                                 @averageSalary int
                                 )
                                 as
                                 begin
                                 if @Id is null
                                 Begin
                                 print'dont enter null it wont accept'
                                 Return
                                 end
                                 
       insert into EMPJOB(ID,title,averageSalary) values(
                                                         @Id,
                                                         @title,
                                                         @averageSalary
                                                         )
                                                                                   
                                 end


--create procedure for inserting checking null values and restricting redundant values
create  procedure condition_nullsanduniquevalues(@Id int,
                                 @title nvarchar(10),
                                 @averageSalary int
                                 )
                                 as
                                 begin
                                 if @Id is null
                                 Begin
                                 print'dont enter null it wont accept'
                                 Return
                                 end
                                 if EXISTS(select * from EMPJOB where ID=@Id)
                                 begin
                                 print 'emp id should be unique '
                                 return
                                 end
                                 
       insert into EMPJOB(ID,title,averageSalary) values(
                                                         @Id,
                                                         @title,
                                                         @averageSalary
                                                         )
                                                                                   
                                 end


--create procedure for inserting checking null values and restricting redundant values
--and salary should be more than 25000
Create  procedure condition_nullsanduniquevalues(@Id int,
                                 @title nvarchar(10),
                                 @averageSalary int
                                 )
                                 as
                                 begin
                                 if @Id is null
                                 Begin
                                 print'dont enter null it wont accept'
                                 Return
                                 end
                                 if EXISTS(select * from EMPJOB where ID=@Id)
                                 begin
                                 print 'emp id should be unique '
                                 return
                                 end
                                 if @averageSalary>25000
                                 begin
                                 print'salary should be high'
                                 Return
                                 end
                                 
       insert into EMPJOB(ID,title,averageSalary) values(
                                                         @Id,
                                                         @title,
                                                         @averageSalary
                                                         )
                                                                                   
                                 end


sp for send the amount from one customer to another in same table

create Table bank
                 (
                 B_id int,
                 Balance Money,
                 Address varchar(50)
                 )
                 alter table bank
                 add Name varchar(50)
                 select * from bank
insert into bank values(106,38000,'Gnt','Ankireddy')
--stored procedure for trnasferring amount
create procedure Funds_Transfer
                               (@srcid int,
                               @Destid int,
                               @Balamt money)
                               as
                               begin
                               update bank set Balance=Balance-@Balamt where B_id=@srcid
                               update bank set Balance=Balance+@Balamt where B_id=@Destid
                               ENd 
                               
 Exec Funds_Transfer 110,104,2000


---a procedure can cause error
create procedure proc123(@X int,@y int)
as
begin
declare @z int
set @z=@X/@y
print 'the output is '+cast(@z as varchar)
End
EXEC proc123 200,0


-- procedure with try catch block
create procedure trycatch(@x int,@y int)
as
begin
Begin try
Declare @z int
set @z=0
set @z=@x/@y
print 'the output is'+cast(@z as varchar)
end try
begin catch
print Error_message()
end catch
end

EXEC trycatch 100,20
EXEC trycatch 500,50





             

Monday, 5 November 2012

Cursors in sql server


--First example
  select * from Employee
  DECLARE samplecursor CURSOR for select emp_no from Employee
  DECLARE @emp_no int
  open samplecursor
  FETCH NEXT FROM samplecursor into @emp_no
  while @@FETCH_STATUS=0
  BEGIN
  print @emp_no
  fetch next from samplecursor into @emp_no
  END
  CLOSE samplecursor
  --second example
   DECLARE SECONDCURSOR123 CURSOR FOR select emp_fname from Employee
   DECLARE @emp_fname varchar(50)
   open SECONDCURSOR123
   FETCH NEXT FROM SECONDCURSOR123 INTO @emp_fname
   WHILE @@FETCH_STATUS=0
   BEGIN
   PRINT @emp_fname
   FETCH NEXT FROM SECONDCURSOR123 into @emp_fname
   END
   CLOSE SECONDCURSOR123
   select * from EmployeeDummy sys.all_objects where type='u'
   --cursor Examples
   DECLARE CURSOR_EXAMPLE CURSOR FOR select emp_no from EmployeeDummy
   DECLARE @Employeeno int
   open CURSOR_EXAMPLE
   FETCH NEXT FROM CURSOR_EXAMPLE into @Employeeno
   WHILE @@FETCH_STATUS=0
   BEGIN
   PRINT @Employeeno
   FETCH NEXT FROM CURSOR_EXAMPLE
   END
   CLOSE CURSOR_EXAMPLE
 
 
   select * from dbo.EMPJOB
   DECLARE AVUSAL_CURSOR CURSOR FOR SELECT averagesalary from EMPJOB
   DECLARE @Avgsalary int
   open AVUSAL_CURSOR
   FETCH NEXT FROM AVUSAL_CURSOR
   WHILE @@FETCH_STATUS=0
   BEGIN
   PRINT @Avgsalary
   FETCH NEXT FROM AVUSAL_CURSOR
   END
   CLOSE AVUSAL_CURSOR
 
   select * from Employee
 
   --ksn
   DECLARE CURSOR_EMPLOYEE CURSOR FOR SELECT emp_no,emp_fname from Employee
   DECLARE @empno int,@emp_name varchar(50)
   open CURSOR_EMPLOYEE
   FETCH NEXT FROM CURSOR_EMPLOYEE INTO @empno,@emp_name
   WHILE @@FETCH_STATUS=0
   BEGIN
   PRINT 'empno'+@empno+'empname'+@emp_name
   FETCH NEXT FROM CURSOR_EMPLOYEE
   END
   CLOSE CURSOR_EMPLOYEE

Cursors in sql server?


--Tsql Programming
DECLARE @MONTH int
SET @MONTH=DATEPART(MM,GETDATE())
if @MONTH=1
 print 'jan'
 if @MONTH=2
 print 'feb'
 if @MONTH=3
 print 'march'
 else
 print 'year almost in ending'

--CASE STATEMENT
--simple case
DECLARE @CASEEXAMPLE int
SET @CASEEXAMPLE=MONTH(GETDATE())
select CASE @CASEEXAMPLE
when 1 then 'jan'
when 2 then 'feb'
when 10 then 'march'
when 11 then 'november'
END
--Search case
DECLARE @SECONDCASE DATETIME
SET @SECONDCASE=GETDATE()
select case
when @SECONDCASE>'10/05/2012' then 'year is in second quareter'
end
select DATEPART(ww,getdate())
-- while loop
DECLARE @X int
SET @X=0
WHILE @X<10
BEGIN
SET @X=@X+1
print 'x is incrementing'
END
--

 DECLARE @y int
  SET @y=0
  while @y<10
  begin
  set @y=@y+1
  if @y>6 continue
  print @y

  END

Indexes in sql server


INDEXES

select * from EmployeeDummy
--clustered index
create unique clustered index sample_cluster on Employeenotnull(ID desc)
create unique clustered index sample123_clust on dbo.EmployeeDummy(emp_no)
/*note:if a table has duplicate records then we can't create
 clustered index on that*/
--Nonclustered index
create index Noncluster on dbo.classificationdummy(classif_ID)
--can a table have both clusterd and nonclustered index?
--yes,but not on same columns
create unique clustered index sample123_clust on dbo.EmployeeDummy(emp_no)
create index Noncluster345 on dbo.EmployeeDummy(emp_fname)
create index Noncluster345 on dbo.EmployeeDummy(emp_no)
--indexed view
--first create view
select * from dbo.Empdummt
create view sampleindexview
WITH SCHEMABINDING
as
select EmpNo,EmpName,Salery from dbo.Empdummt
select * from sampleindexview
--indexed view
create unique clustered index indexviewsample on sampleindexview(EmpNo Desc)



Sunday, 4 November 2012

Rownumber in sql server


How to use ROW_NUMBER() to enumerate and partition records in SQL Server

By , 31 Dec 2011
 
I had a situation recently where I had a table full of people records, where the people were divided into families. The business logic that needed to be followed was that I had to assign a “Twin Code” to each record. This meant that for each family in the database, if two or more members were born on the same day they should be treated as twins. The twins should be assigned a number enumerating them in order of birth. If the member was not a twin, they should just receive the twin code of 1.
Here’s an example table:
PersonIDFamilyIDFirstNameLastNameDateOfBirth
11JoeJohnson2000-10-23 13:00:00
21JimJohnson2001-12-15 05:45:00
32KarlyMatthews2000-05-20 04:00:00
42KacyMatthews2000-05-20 04:02:00
52TomMatthews2001-09-15 11:52:00
There are lots of ways to achieve the desired result, but the simplest is to just use a simple SELECT statement combined with the ROW_NUMBER() function with a couple parameters as to how to number the rows!
ROW_NUMBER() provides you with the number of a row in a given recordset, where you provide details on how to number the records. For example, if I just had to number the records above based solely upon the date of birth (ignoring families) then I would use this query:
SELECT
     [PersonID]
    ,[FamilyID]
    ,[FirstName]
    ,[LastName]
    ,[DateOfBirth]
    ,ROW_NUMBER() over (ORDER BY DateOfBirth) AS Number
FROM
 People
ORDER BY
 PersonID
This just tells the ROW_NUMBER() function to order its numbering ascending by DateOfBirth. Notice that I apply an order myself later on in the query, which is different than the row_number() order. I would get these results:
PersonIDFamilyIDFirstNameLastNameDateOfBirthNumber
11JoeJohnson2000-10-23 13:00:003
21JimJohnson2001-12-15 05:45:005
32KarlyMatthews2000-05-20 04:00:001
42KacyMatthews2000-05-20 04:02:002
52TomMatthews2001-09-15 11:52:004
The number field that is assigned to each record is in the order of DateOfBirth.
Ordering my numbering by DateOfBirth is just half of the picture. I also need to “group” the records by the FamilyID. This is where a clause in T-, SQL that you might not be very familiar with comes into play: “PARTITION BY”. The PARTITION BY clause allows us to group the results within the call to ROW_NUMBER()without grouping them ourselves via a GROUP BY. It just tells the ROW_NUMBERR what groupings to use when it does its counting.
Here is our final SQL statement, which achieves the business logic we wanted to implement.
SELECT
       [PersonID]
     [FamilyID]
      ,[FirstName]
      ,[LastName]
      ,[DateOfBirth]
      ,ROW_NUMBER() over(PARTITION BY FamilyID,
                         CONVERT(NVARCHAR(25), DateOfBirth, 111)
                         ORDER BY DateOfBirth ASC) TwinCode

  FROM [People]
ORDER BY PersonID
IIn the ROW_NUMBER function above, I am doing several things. I’m grouping on FamilyID, and also grouping on a converted DateOfBirth. I convert the DateOfBirth to an nvarchar using the 111 conversion code, because that gets results like ‘2009/10/11′ and ‘2009/10/12′ which can easily be grouped by to achieve distinct dates.
Grouping on the Family, DateOfBirth, and then sorting by DateOfBirth ascending achieves the desired result for the ROW_NUMBER. Here are the results of the query:
PersonIDFamilyIDFirstNameLastNameDateOfBirthTwinCode
11JoeJohnson2000-10-23 13:00:001
21JimJohnson2001-12-15 05:45:001
32KarlyMatthews2000-05-20 04:00:001
42KacyMatthews2000-05-20 04:02:002
52TomMatthews2001-09-15 11:52:001
As you can see, the two people who qualify as twins above (Karly and Kacy) are enumerated correctly, with Karly receiving a 1 and Kacy receiving a 2. All the records that are not twins properly receive a 1.









Using ROW_NUMBER() to paginate your data with SQL Server 2005 and ASP.NET

By , 20 Nov 2005
 
Sample Image - row_number.gif
View Demo

Introduction

One of the great weaknesses of SQL Server 2000 was its inability to handle data pagination well. The solution to the problem was always to select all of your results and programmatically hide the results you did not want to show. As the user clicked Next or Previous, you would again select all of the rows and only display what the user asked for. Though the desired interface is achieved through this method, it is terribly inefficient. Why couldn't we select only the data that we wanted? If you have ever had to write a search that used "like" against thousands of records, you know how terribly slow SQL Server 2000 could perform.
During my SQL Server 2000 days, I would search endlessly for a solution to this problem. I tried implementations of using a select top where my last row was greater than a parameter. This works only in some cases, like when ordering by a primary key or by date. Otherwise, this failed because of the existence of duplicate data. I also tried building stored procedures that used crazy for loops to try and accomplish this. In every instance, I would always hit a brick wall. The client would request a feature that I could not support with my method and I would always default back to the poor performance of selecting all of the rows (or many of them) and handling the paging scheme programmatically.
Throughout this process, I often theorized of a SQL Server function that could add a sequential row number to my result set and allow me to use a where clause against that row the only selects what rows I needed. After a bit of research, I found out that this function did in fact exist. The only problem was, it existed only in Oracle! I was enraged, how could something so useful be simply left out of SQL Server 2000?
A few years pass by and Microsoft releases .NET which offers a partial solution to the problem. ASP.NET offers you the ability to output-cache the results of your web control. So essentially, you can select all of the rows once and as you page through the results, pull each subsequent set from the cached results. This seems to partially solve the performance problem though you are still faced with making the initial selection. But what if you want to view live changing data? As you decrease your cache time, your performance gets worse, as you increase it, your data gets old. Eventually, you fall back on your tired old method again.
With the release of SQL Server 2005, Microsoft introduces the long overdue ROW_NUMBER() function to solve this problem. In this article, we will walk through a C# implementation of pagination using the ROW_NUMBER()method.

The Code

The first step is writing your stored procedure. The SQL code for using ROW_NUMBER() is not as intuitive as you might think. When I originally attempted to do this, I tried to simply use the ROW_NUMBER() function like I wouldnewid(). I quickly found out, that was not going to work. After some research, I came up with the stored procedure below. Though I would have rather seen a more intuitive syntax than what is below when you think about it, it does make sense. I suppose they did not want to hide logic from the programmer and ask him to accept that something magical simply happens. In the following project, I will use a database of all zip codes in the United States.
CREATE PROCEDURE [dbo].[sp_getzipcodes] 
     -- Add the parameters for the stored procedure here
     @start int = 0

AS
BEGIN
     -- SET NOCOUNT ON added to prevent extra result sets from
     -- interfering with SELECT statements.
     Set NOCOUNT ON
     SELECT TOP 20 * FROM 
     (
          SELECT zip,city,state,latitude,longitude,timezone,dst,
          ROW_NUMBER() OVER (ORDER BY zip) AS num
          FROM dbo.zipcode
     ) AS a
     WHERE num > @start
END