?
Solved

While Statement in SQL

Posted on 2013-01-16
3
Medium Priority
?
330 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 2

Accepted Solution

by:
thatmsftbuguy earned 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

So every once in a while at work I am asked to export data from one table and insert it into another on a different server.  I hate doing this.  There's so many different tables and data types.  Some column data needs quoted and some doesn't.  What …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

777 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