Click here to Skip to main content
15,888,257 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
i need help that how CTE query will be used for updating in asp.net c# web application gridview.

Just i want to update INTime and OUTTime result of values will auto calcualte as per define condition.

What I have tried:

SQL
<pre>PROCEDURE [dbo].[GetMachineAttendanceFinal2]
@StartDate Datetime,
@Enddate   Datetime,

@INTIME    Datetime,
@OUTTIME   DateTime
AS

BEGIN

      SET NOCOUNT ON;

     
;WITH Dates (ReportingDate) 
AS (
   SELECT CONVERT(date, '2018-11-26 00:00:00.000', 120) AS ReportingDate
   UNION ALL
   SELECT DATEADD(day, 1, d.ReportingDate)
   FROM Dates d
   WHERE d.ReportingDate < CONVERT(date, '2018-12-25 00:00:00.000', 120))

,CTE As
    (
    select EmployeeDetails.EmpID,EmployeeDetails.EmpName,EmployeeDetails.OTEntitled,EmployeeDetails.Empcur,EmployeeDetails.Dhour,EmployeeDetails.LTime from EmployeeDetails where Empcur='Join'
    )
    ,CTE4 As
    (
    Select MachineAttendance.EmpID,MachineAttendance.Datetime,MachineAttendance.INOUT from MachineAttendance
    ) 
    
    ,cte1 AS
    (
    SELECT CTE4.EmpID, CAST(CTE4.Datetime as Date) AS [Date], 
    CASE WHEN CTE4.INOUT = 1 THEN CTE4.DateTime END AS INOUT_INTIME,
    CASE WHEN CTE4.INOUT = 2 THEN CTE4.DateTime END AS INOUT_OUTTIME
    From 
     CTE4
    
    ), 
   cte2 
    as
    (
    select cte1.EmpID, Date, MAX(INOUT_INTIME) AS INTIME, 
                          MAX(INOUT_OUTTIME) AS OUTTIME
    , DATEDIFF(Hour, MAX(INOUT_INTIME), MAX(INOUT_OUTTIME)) as [Hours]
    FROM CTE1
    GROUP BY EmpID, [Date]
    )
    select cte.EmpID,cte.EmpName,d.ReportingDate,cte2.Date, cte.OTEntitled,CTE.Empcur,CTE.Dhour,cte.LTime,cte2.INTIME,  cte2.OUTTIME,  cte2.[Hours]
    , CASE WHEN  cte2.[Hours] >= 8 THEN 1
    WHEN  cte2.[Hours] = 0 THEN 0
    WHEN  cte2.[Hours] >= 6 THEN 0.5 END AS [Day],
    CASE WHEN  cte2.[Hours] > CTE.Dhour then  cte2.[Hours] - CTE.Dhour else 0 End as OT,
    CASE when   
    cte.OTEntitled = 'Yes'  AND cte2.[Hours] >= CTE.Dhour 
THEN (( cte2.[Hours] - 8) * 100) else 0 END AS OTAMount,  

   
    Convert(varchar(10), cte2.INTIME,108) as [Time],
   Case When Convert(Time, cte2.INTIME,108) > cte.LTime Then 1 else 0 end as Late    
    from cte Cross Apply Dates d 
		Left Join cte2 ON cte2.EmpId= cte.EmpID AND cte2.Date=d.ReportingDate
    order by cte.EmpID asc 
     
  
END
Posted
Updated 7-Jan-19 5:28am

1 solution

What you would need to do is to add an UPDATE query to your procedure; probably the easiest way to do this would be create yet another CTE in your procedure to wrap that final SELECT and then JOIN it to your destination table within the aforementioned JOIN

As you have not provided the table schema for the destination only assumptions can be made which I really do not care to do, so I will only provide the basics. I am also not going to be check your prior code even though it is visibly questionable.
SQL
-- earlier CTEs not listed
; WITH cteFinal AS (
	select	cte.EmpID,cte.EmpName
	,	d.ReportingDate,cte2.Date
	,	cte.OTEntitled
	,	CTE.Empcur
	,	CTE.Dhour
	,	cte.LTime
	,	cte2.INTIME
	,	cte2.OUTTIME
	,	cte2.[Hours]
	,	CASE
			WHEN cte2.[Hours] >= 8 THEN 1
			WHEN cte2.[Hours] = 0 THEN 0
			WHEN cte2.[Hours] >= 6 THEN 0.5
		END AS [Day]
	,	CASE
			WHEN cte2.[Hours] > CTE.Dhour then cte2.[Hours] - CTE.Dhour 
			else 0
		End as OT
	,	CASE
			when cte.OTEntitled = 'Yes' AND cte2.[Hours] >= CTE.Dhour tHEN (( cte2.[Hours] - 8) * 100)
			else 0
		END AS OTAMount
	,	Convert(varchar(10), cte2.INTIME,108) as [Time]
	,	Case
			When Convert(Time, cte2.INTIME,108) > cte.LTime Then 1
			else 0
		end as Late
	from	cte
	Cross Apply Dates d 
	Left Join cte2 ON cte2.EmpId= cte.EmpID AND cte2.Date=d.ReportingDate
)

UPDATE  dt
SET     dt.[Date] = cF.[Date]
,       dt.OTEntitled = cD.OTEntitled
-- other values being set
FROM   [DestinationTable]  dt
LEFT JOIN cteFinal         cF ON dt.EmpID = cF.EmpID
 
Share this answer
 
Comments
Member 12314309 7-Jan-19 12:02pm    
Dear MadMyche
i am little bit confuse,my table structure is that i have two table one table is Machineattendance and another one is Employee attendance in which INtime and Outime is coming where i will update INtime and OUtime of those employee whose intime and outime will not available
MadMyche 7-Jan-19 12:41pm    
You have all this data pieced together, but there is no UPDATE. That is what you need to do if you want to want to update the table.
Of if just want to view the data, pull back the result set from your procedure and put it into a data table.
... Or is there a problem with your query that you need help with?
Member 12314309 8-Jan-19 14:46pm    
Dear MadMyche, I just want to update INTIME and OUTTIME IN Employee Attendance against attendance ID ,rest of query as same execute as i mentioned above,now how will update INTIME and OUTTIME in cte query ,i tried which you said but i think this is not solution of my query...Please please help

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900