Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

While Statement in SQL

Posted on 2013-01-16
3
Medium Priority
?
331 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

Independent Software Vendors: 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

Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

636 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