Queries assignment udf_and_triggers
Upcoming SlideShare
Loading in...5

Queries assignment udf_and_triggers






Total Views
Views on SlideShare
Embed Views



0 Embeds 0

No embeds



Upload Details

Uploaded via as Microsoft Word

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
Post Comment
Edit your comment

Queries assignment udf_and_triggers Queries assignment udf_and_triggers Document Transcript

  • -----------------------User Degine Function--------------------------------------------------------------------------- Type 1 > Scaler Functions : Returns single value.--Ex: Factorial of Given No.CREATE FUNCTION UDF_Factorial( @num int)RETURNS intASBEGINDECLARE @fact intSET @fact=1WHILE @num != 0BEGINSET @fact=@fact * @numSET @num=@num-1ENDRETURN @factEND-------------------------------------------------------------------Execute User Define FunctionSELECT dbo.UDF_Factorial(5) as Factorial------------------------------------------------------------------
  • --Ex: Function to Find Employee MAX Salary of Given DeptCREATE FUNCTION UDF_getMaxSal(@Deptid int)RETURNS NUMERIC(10,2)ASBEGINDECLARE @MaxSal NUMERIC(10,5)SET @MaxSal=(SELECT TOP 1 Salary FROM tblEmployee WHERE DeptId=@Deptid ORDERBY Salary DESC)RETURN @MaxSalENDGO--Execution of UDF in SELECT StatmentSELECT EmpId,EmpName,Address,Deptid,Salary FROM tblEmployee WHERESalary=dbo.UDF_getMaxSal(2)
  • -- Type 2 > Inline Table Valued Function : Returns Table Object--Ex: Function to Return Record of Emplyees whose Salary > given salryCREATE FUNCTION UDF_getEmpRecordsAboveGivenSal(@Salary int)RETURNS TableASreturn (SELECT EmpId,EmpName,Address,Gender,Salary FROM tblEmployee WHERESalary > @Salary)GO------------------------------------------------------------------------------- ExecutionSELECT * FROM dbo.UDF_getEmpRecordsAboveGivenSal(25000)
  • -- Type 3 > Multi Statement Table Valued Function :-- Explicitly defines the structure of the table to return.-- Defines column names and datatypes in the RETURNS clause.--Ex: Fuction to get Employee Records with Dept NameCREATE FUNCTION getEmpByDeptName()RETURNS @EmpWithDept Table(EmpId int,EmpName varchar(50),DeptName varchar(50))ASBEGININSERT INTO @EmpWithDept SELECT e.EmpId,e.Empname,d.DeptName FROMtblDepartment d INNER JOIN tblEmployee e ON d.DeptId=e.DeptIdreturnENDGO------------------------------------------------------------------------------- Execute Multi Statement Table Valued FunctionSELECT * FROM dbo.getEmpByDeptName()
  • --Modify or Alter UDF like FollowsAlter FUNCTION getEmpByDeptName(@DeptName varchar(50))RETURNS @EmpWithDept Table(EmpId int,EmpName varchar(50),DeptName varchar(50))ASBEGININSERT INTO @EmpWithDept SELECT e.EmpId,e.Empname,d.DeptName FROMtblDepartment d INNER JOIN tblEmployee e ON d.DeptId=e.DeptId WHEREDeptName=@DeptnameUPDATE @EmpWithDept SET DeptName=DOT NET WHERE DeptName=.NETreturnENDGO---------------------------------------------------------------------------------------------------------------------------------- Execute Multi Statement Table Valued FunctionSELECT * FROM dbo.getEmpByDeptName(.NET)
  • ----------------------------------Triggers------------------------------------------------------------------------------------------------------------------Defination : A trigger is an action that is performed behind-the-sceneswhen an event occurs on a table.-- Types of Trigger: 1) Instead of/Before 2) After/For-- There are Two Tables with Field and Diffrent name one is tblPersone andanother is tblPersonUpdateSELECT * FROM tblPersonSELECT * FROM tblPersonUpdate-------------------------------------------------------------------------------Inserting Records into tblPersonINSERT INTO tblPersonVALUES(Vinay,Sayaji,Indore,Convert(Varchar,GETDATE(),114))INSERT INTO tblPerson VALUES(Rahul,VijayNagar,Indore,Convert(Varchar,GETDATE(),114))INSERT INTO tblPersonVALUES(Hitesh,Khargone,Convert(Varchar,GETDATE(),114))
  • SELECT * FROM tblPersonSELECT * FROM tblPersonUpdate--Creating Trigger on Table tblPerson After Update will Insert Old Recordinto tblPersonUpdateCREATE TRIGGER UDT_PersonUpdateON tblPersonAfter UPDATEASDECLARE @id int;DECLARE @name varchar(50);DECLARE @address varchar(50);DECLARE @time varchar(50);select @id=U.id from deleted U;select @name=U.name from deleted U;select @address=U.address from deleted U;select @time=U.time from deleted U;BEGININSERT INTOtblPersonUpdate(id,name,address,time)VALUES(@id,@name,@address,@time)PRINT Record Has been Inserted into tblPersonUpdateEND
  • --Now Updating in Table tblPersoneUPDATE tblPerson SET address=Bhopal WHERE id=3-------------------------------------------------------------------------------Now Updated Records in tblPerson and tblPersonUpdateSELECT * FROM tblPersonSELECT * FROM tblPersonUpdate