Solved

While Statement in SQL

Posted on 2013-01-16
3
319 Views
Last Modified: 2013-01-16
Dear Experts,
I am using below mentioned statement; the system date is 17th Jan-13. The loop should be run only two times because I set @DT date 16th Jan-13 but it is running continuously till I stop it physically.
Please help what I am doing wrong.
Rgds.
Mehram


Set @CC='01'
Set @CYear='1213'
Set @Branch='KHI'
--Set @Sd='1/1/2013'
Set @Dt='01/16/2013'
Select @Holiday=dt from Holidays Where dt=@Dt

While @Dt < Convert(DateTime, Convert(Varchar(12), GetDate()),112)
Select Transno=CC+EmpCode+cast(datepart(yy,@dt)as varchar) + right('00'+cast(datepart(mm,@dt)as varchar),2)+ right('00'+cast(datepart(dd,@dt)as varchar),2), TransNoEmpInfo=TransNo
            , Months=((CONVERT([varchar](10),left(datename(month,@Dt),(3)),(0))+'-')+CONVERT([varchar](10),datepart(year,@Dt),(0)))
            ,WeekDay=(datename(weekday,@Dt))
            ,AttDt=@Dt, CC, cATEGORY, EmpCode,EmpName, FatherName, NICNo, Designation, Department,
            Status=Case When (DateName(dw,@Dt)='Sunday' or DateName(dw,@Dt)='Saturday') then 'O' else
                     Case When @Holiday=@Dt Then 'O' else 'P' end end
            ,FA1=Case When FA=1 then 1 else 0 end
            ,HA1=Case When HA=1 and (@Holiday is not null or (datename(w,@dt)='Sunday' or DateName(dw,@Dt)='Saturday')) then 1 else 0 end
            ,Holiday=Case When (@Holiday is not null or (DateName(dw,@Dt)='Sunday' or DateName(dw,@Dt)='Saturday')) Then 'Y' else 'N' end
            ,Cyear=@Cyear --, RegsinedOn
from emp_info a
Where CC=@CC and @Dt Between coalesce(JoiningDate,@Dt) and coalesce(RegsinedOn,@Dt) and Branch=@Branch
ORDER BY Department,cATEGORY, empCode  
set @Dt = @Dt+1
0
Comment
Question by:Mehram
  • 2
3 Comments
 
LVL 2

Accepted Solution

by:
thatmsftbuguy earned 500 total points
ID: 38785904
I would add a Begin and End Statement to your code:

Set @CC='01'
Set @CYear='1213'
Set @Branch='KHI'
--Set @Sd='1/1/2013'
Set @Dt='01/16/2013'
Select @Holiday=dt from Holidays Where dt=@Dt

While @Dt < Convert(DateTime, Convert(Varchar(12), GetDate()),112)
BEGIN
Select Transno=CC+EmpCode+cast(datepart(yy,@dt)as varchar) + right('00'+cast(datepart(mm,@dt)as varchar),2)+ right('00'+cast(datepart(dd,@dt)as varchar),2), TransNoEmpInfo=TransNo
            , Months=((CONVERT([varchar](10),left(datename(month,@Dt),(3)),(0))+'-')+CONVERT([varchar](10),datepart(year,@Dt),(0)))
            ,WeekDay=(datename(weekday,@Dt))
            ,AttDt=@Dt, CC, cATEGORY, EmpCode,EmpName, FatherName, NICNo, Designation, Department,
            Status=Case When (DateName(dw,@Dt)='Sunday' or DateName(dw,@Dt)='Saturday') then 'O' else
                     Case When @Holiday=@Dt Then 'O' else 'P' end end
            ,FA1=Case When FA=1 then 1 else 0 end
            ,HA1=Case When HA=1 and (@Holiday is not null or (datename(w,@dt)='Sunday' or DateName(dw,@Dt)='Saturday')) then 1 else 0 end
            ,Holiday=Case When (@Holiday is not null or (DateName(dw,@Dt)='Sunday' or DateName(dw,@Dt)='Saturday')) Then 'Y' else 'N' end
            ,Cyear=@Cyear --, RegsinedOn
from emp_info a
Where CC=@CC and @Dt Between coalesce(JoiningDate,@Dt) and coalesce(RegsinedOn,@Dt) and Branch=@Branch
ORDER BY Department,cATEGORY, empCode  
set @Dt = @Dt+1

END
0
 

Author Comment

by:Mehram
ID: 38785913
Great, Can you share what would do Begin and End in this case
0
 
LVL 2

Expert Comment

by:thatmsftbuguy
ID: 38785932
Begin and END are always used in a control a flow statement like While Loops or Do Until Loops
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Recently, when I was asked to create a new SQL 2005 cluster, Microsoft released a new service pack for MS SQL 2005 what is Service Pack 3. When I finished the installation of MS SQL 2005 I found myself troubled why the installation of SP3 failed …
INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now