Here this script it used for check which no from 1 to 100 have sum of digit
--Here this script it used for check which no from 1 to 100 have sum of digit
--is equal to @outputTot.
--Example if @outputTot set to 5 means
-- first 5 = digit sum is 5.
-- than 14 = 1 + 4 = digit sum is 5.
-- than 23 = 2 + 3 = digit sum is 5.
-- than 32,41,50
-- than 59 = 5 + 9 = 14 than 1 + 4 = digit sum is 5.
-- above example is calculate digit sum untill sum is between 1 to 9.
SET NOCOUNT on
go
declare @val bigint
declare @tval bigint
declare @oval bigint
declare @rem bigint
declare @res bigint
declare @outputTot bigint
set @res = 0
set @val = 43
set @tval = @val
set @outputTot = 3
declare @inputTable table(_val int)
declare @i int
set @i = 1
while(@i<=100) begin insert into @inputTable values(@i) set @i = @i + 1 end declare ibuffer cursor fast_forward for select _val from @inputTable open ibuffer fetch next from ibuffer into @val while (@@fetch_status != -1) BEGIN set @tval = @val set @oval = @val set @res = 0 while(@val > 9)
begin
set @res = 0
while(@tval > 0)
begin
set @rem = @tval % 10
set @res = @res + @rem
set @tval = @tval / 10
end
set @val = @res
set @tval = @val
end
if(@res = @outputTot)
begin
print 'Total digit sum is '+ cast(@outputTot as varchar) +' of ' + cast(@oval as varchar)
end
fetch next from ibuffer into @val
End
deallocate ibuffer
Output will be...
Total digit sum is 3 of 12
Total digit sum is 3 of 21
Total digit sum is 3 of 30
Total digit sum is 3 of 39
Total digit sum is 3 of 48
Total digit sum is 3 of 57
Total digit sum is 3 of 66
Total digit sum is 3 of 75
Total digit sum is 3 of 84
Total digit sum is 3 of 93
About Me
-
Anil Desai.
Software Engineer
T M Technologies
જય વાળીનાથ - T-SQL is a basic of structure query language. So I always want to learn T-SQL in different way to get best performance in query. And its my passion.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment