0% found this document useful (0 votes)
89 views6 pages

SQL Query, For Trigger, Store Procedure, Functionand View

This document contains examples of different SQL Server database objects including stored procedures, functions, triggers, and views. The stored procedures demonstrate using output parameters, temporary tables, and cursors. The functions show converting numbers to words, formatting dates, and other data transformations. The triggers provide an example of an insert trigger that updates related records. Views and other objects are also discussed at a high level. Overall, the document provides a variety of code samples for common SQL Server database programming tasks.

Uploaded by

cap.rohit550
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
Download as doc, pdf, or txt
0% found this document useful (0 votes)
89 views6 pages

SQL Query, For Trigger, Store Procedure, Functionand View

This document contains examples of different SQL Server database objects including stored procedures, functions, triggers, and views. The stored procedures demonstrate using output parameters, temporary tables, and cursors. The functions show converting numbers to words, formatting dates, and other data transformations. The triggers provide an example of an insert trigger that updates related records. Views and other objects are also discussed at a high level. Overall, the document provides a variety of code samples for common SQL Server database programming tasks.

Uploaded by

cap.rohit550
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1/ 6

Stored Procedure :

/**********Using output parameter*//


ALTER PROCEDURE [dbo].[SP_Adminlogin]
@userid nvarchar(20),
@password nvarchar(20),
@user1 nvarchar(20) output
AS
set @user1=(Select userid from Admin where userid=@userid and
password=@password and status='WEB')

return

/***Using Temparary table */


ALTER proc [dbo].[tds_service_report]
@payno INT
as
begin

create table #TEMP(mem_id int,tds NUMERIC(18,2),service


NUMERIC(18,2),payoutno int)

insert into #TEMP


select mem_id,sum(tds) as [tds],sum(service) as [service],pay_no from
level_comm WITH (NOLOCK) group by mem_id,pay_no having ((sum(tds)>0) or
(sum(service)>0)) and pay_no <=@payno order by mem_id

create table #temptds (mem_id int,tds NUMERIC(18,2),service


NUMERIC(18,2))
insert into #temptds
select mem_id,tds,service from #TEMP

create table #TEMP2(mem_id int,tds NUMERIC(18,2),service NUMERIC(18,2))

insert into #TEMP2

select mem_id,sum(tds) ,sum(service) from #temptds group by mem_id


having ((sum(tds)>0) or (sum(service)>0)) order by mem_id
select mem_id as [Memberid],tds as [TDS],service as [SERVICE] from
#TEMP2 order by mem_id
end

/******Using Cursor**/
ALTER PROCEDURE [dbo].[sp_ins_carry_ammount_paid_carryforward]
@Payout_No as nvarchar(20)

AS
declare @mem nvarchar(20),@lvlcomm numeric(12,2),@fwdstatus
int,@fwdstatus1 int

declare carryfwd_tomember cursor for SELECT


mem_id,final_commission from level_comm where final_commission >0 and
final_commission < 50 and pay_no=@Payout_No
open carryfwd_tomember
fetch carryfwd_tomember into @mem,@lvlcomm
while @@fetch_status=0
begin
update met_comm_details set amtpaid_carry=amtpaid_carry+@lvlcomm
where memberid=@mem
update level_comm set final_commission=0 where mem_id=@mem and
pay_no=@Payout_No

fetch carryfwd_tomember into @mem,@lvlcomm


end
close carryfwd_tomember
deallocate carryfwd_tomember
Functions:

/************To Display numbers in words******/


ALTER function [dbo].[ranjit] (@money bigint)returns nvarchar(max)
as
begin
DECLARE @i int, @temp char(1), @s VARCHAR(20), @result VARCHAR(255),@N
BIGINT /*INPUT */
select @n=@money
SELECT @s=convert(varchar(20), @n)
SELECT @i=LEN(@s)
SELECT @result=''
WHILE (@i>0)
BEGIN
SELECT @temp=(SUBSTRING(@s,@i,1))
IF ((LEN(@s)-@i) % 3)=1
IF @temp='1'
SELECT @result=CASE (SUBSTRING(@s,@i+1,1))
WHEN '0' THEN 'Ten'
WHEN '1' THEN 'Eleven'
WHEN '2' THEN 'Twelve'
WHEN '3' THEN 'Thirteen'
WHEN '4' THEN 'Fourteen'
WHEN '5' THEN 'Fifteen'
WHEN '6' THEN 'Sixteen'
WHEN '7' THEN 'Seventeen'
WHEN '8' THEN 'Eighteen'
WHEN '9' THEN 'Nineteen'
END+' '+CASE
WHEN ((LEN(@s)-@i)=4) THEN 'Thousand '
WHEN ((LEN(@s)-@i)=7) THEN 'Million '
WHEN ((LEN(@s)-@i)=10) THEN 'Billion '
WHEN ((LEN(@s)-@i)=13) THEN 'Trillion '
WHEN ((LEN(@s)-@i)=16) THEN 'Quadrillion '
ELSE ''
END+@result
ELSE
BEGIN
SELECT @result=CASE (SUBSTRING(@s,@i+1,1))
WHEN '0' THEN ''
WHEN '1' THEN 'One'
WHEN '2' THEN 'Two'
WHEN '3' THEN 'Three'
WHEN '4' THEN 'Four'
WHEN '5' THEN 'Five'
WHEN '6' THEN 'Six'
WHEN '7' THEN 'Seven'
WHEN '8' THEN 'Eight'
WHEN '9' THEN 'Nine'
END+' '+ CASE
WHEN ((LEN(@s)-@i)=4) THEN 'Thousand '
WHEN ((LEN(@s)-@i)=7) THEN 'Million '
WHEN ((LEN(@s)-@i)=10) THEN 'Billion '
WHEN ((LEN(@s)-@i)=13) THEN 'Trillion '
WHEN ((LEN(@s)-@i)=16) THEN 'Quadrillion '
ELSE ''
END+@result
SELECT @result=CASE @temp
WHEN '0' THEN ''
WHEN '1' THEN 'Ten'
WHEN '2' THEN 'Twenty'
WHEN '3' THEN 'Thirty'
WHEN '4' THEN 'Fourty'
WHEN '5' THEN 'Fifty'
WHEN '6' THEN 'Sixty'
WHEN '7' THEN 'Seventy'
WHEN '8' THEN 'Eighty'
WHEN '9' THEN 'Ninety'
END+' '+@result
END
IF (((LEN(@s)-@i) % 3)=2) OR (((LEN(@s)-@i) % 3)=0) AND
(@i=1)
BEGIN
SELECT @result=CASE @temp
WHEN '0' THEN ''
WHEN '1' THEN 'One'
WHEN '2' THEN 'Two'
WHEN '3' THEN 'Three'
WHEN '4' THEN 'Four'
WHEN '5' THEN 'Five'
WHEN '6' THEN 'Six'
WHEN '7' THEN 'Seven'
WHEN '8' THEN 'Eight'
WHEN '9' THEN 'Nine'
END +' '+CASE
WHEN (@s='0') THEN 'zero'
WHEN (@temp<>'0')AND( ((LEN(@s)-@i) % 3)=2) THEN
'Hundred '
ELSE ''
END + CASE
WHEN ((LEN(@s)-@i)=3) THEN 'Thousand '
WHEN ((LEN(@s)-@i)=6) THEN 'Million '
WHEN ((LEN(@s)-@i)=9) THEN 'Billion '
WHEN ((LEN(@s)-@i)=12) THEN 'Trillion '
WHEN ((LEN(@s)-@i)=15) THEN 'Quadrillion '
ELSE ''
END+ @result
END
SELECT @i=@i-1
END
set @result=(select REPLACE(@result,' ',' ')+' Rupees Only')
return @result
end
/******************************Convert od date in varchar****/
ALTER FUNCTION [dbo].[just_date_simple](@dtvalue datetime) RETURNS
nvarchar(10)
AS
BEGIN
DECLARE @display nvarchar(10)
SET @display = CAST(DATEPART(dd, @dtvalue) AS nvarchar) + '/' +
CAST(DATEPART(mm, @dtvalue) AS nvarchar) + '/' + CAST(DATEPART(yyyy,
@dtvalue) AS nvarchar)
RETURN @display
END

Triggers:

/**************Insert**/

ALTER TRIGGER [dbo].[Tr_franch_comm]


on [dbo].[Members] for insert

as
declare @franchid nvarchar(20),@member nvarchar(10),@pincomm
numeric(5),@joincomm numeric(5)

SELECT
@franchid=franchise_id,@member=memberid,@pincomm=franch_pin_comm,@joinco
mm=franch_join_comm FROM INSERTED

IF @franchid <>'NULL'
BEGIN
if exists (select franchise_id from franch_join_comm where
franchise_id=@franchid )
begin
update franch_join_comm set
totalcomm=(totalcomm+@pincomm+@joincomm) where franchise_id=@franchid
end
else
begin
insert into franch_join_comm (franchise_id,totalcomm)
values (@franchid,(@pincomm+@joincomm) )
end
END

Views
Function
/****** To display Date format***/
ALTER function [dbo].[fnFormatDate]
(
@inputDate datetime,
@formatString varchar(25)
)
returns varchar(20) as
begin
declare @returnValue varchar(25)

-- Declare local vairables


declare @formattedDate varchar(25),
@day varchar(20), @month varchar(20), @year varchar(20),
@dayFormat varchar(5), @monthFormat varchar(5), @yearFormat
varchar(5)

set @dayFormat = ''


set @monthFormat = ''
set @yearFormat = ''

-- Convert the supplied date to day mon year (25 Jan 2008)
set @formattedDate = convert(varchar, @inputDate, 106)

-- If the format string contains a format for the day


if charindex('d', @formatString) > 0
-- Get the day format string
set @dayFormat = master.dbo.fn_pcre_replace(@formatString, '.*?
(d{1,4}).*', '$1')

-- If the format string contains a format for the month


if charindex('m', @formatString) > 0
-- Get the month format string
set @monthFormat = master.dbo.fn_pcre_replace(@formatString,
'.*?(m{1,4}|M{1,4}).*', '$1')

-- If the format string contains a format for the year


if charindex('y', @formatString) > 0
-- Get the year format string
set @yearFormat = master.dbo.fn_pcre_replace(@formatString, '.*?
(y{2,4}).*', '$1')

-- Format the day value based on the format string for the day
select @day =
case @dayFormat
when 'dd' then master.dbo.fn_pcre_replace(@formattedDate,
'^(\d+).*', '$1')
when 'ddd' then substring(datename(dw, @formattedDate), 1, 3)
when 'dddd' then datename(dw, @formattedDate)
else convert(varchar, day(@formattedDate))
end

-- Format the month value based on the format string for the month
select @month =
case @monthFormat
when 'mm' then master.dbo.fn_pcre_replace(convert(varchar,
@inputDate, 101), '^(\d+)/.*', '$1')
when 'mmm' then master.dbo.fn_pcre_replace(@formattedDate,
'\d+\s(\w+)\s\d+', '$1')
when 'mmmm' then datename(m, @formattedDate)
else convert(varchar, month(@formattedDate))
end

-- Format the year value based on the format string for the year
select @year =
case @yearFormat
when 'yy' then substring(convert(varchar, year(@formattedDate)),
3, 2)
else convert(varchar, year(@formattedDate))
end

set @returnValue = @formatString

-- If the day format was specified


if @dayFormat <> ''
-- Replace the day format string with the actual day value
set @returnValue = master.dbo.fn_pcre_replace(@returnValue,
@dayFormat, @day)

-- If the month format was specified


if @monthFormat <> ''
-- Replace the month format string with the actual month
set @returnValue = master.dbo.fn_pcre_replace(@returnValue,
@monthFormat, @month)

-- If the year format was specified


if @yearFormat <> ''
-- Replace the year format string with the actual year
set @returnValue = master.dbo.fn_pcre_replace(@returnValue,
@yearFormat, @year)

-- Return the formated value


return @returnValue
end

/**** to fech indiantime ***/

ALTER function [dbo].[indiantime_dolp](@dtvalue datetime)returns


nvarchar(20)
as
begin
declare @inddate datetime
set @inddate=(select dateadd(Second,1,@dtvalue))
return @inddate
end

ALTER FUNCTION [dbo].[just_date_simple](@dtvalue datetime) RETURNS


nvarchar(10)
AS
BEGIN
DECLARE @display nvarchar(10)
SET @display = CAST(DATEPART(mm, @dtvalue) AS nvarchar) + '/' +
CAST(DATEPART(dd, @dtvalue) AS nvarchar) + '/' + CAST(DATEPART(yyyy,
@dtvalue) AS nvarchar)
RETURN @display
END

You might also like