Click here to Skip to main content
15,888,053 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
I have store Procedure, now i want to insert result of store procedure into table of EmployeeAttendance Col(AttID,EMPID,INTIME,OUTTIME,HOURS,DUTY,OT,OTAmount,Late

here is my store procedure....

What I have tried:

<pre>ALTER PROCEDURE [dbo].[GetMachineAttendanceFinal5]

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.MDate,MachineAttendance.INOUT from MachineAttendance
    ) 
    
    ,cte1 AS
    (
    SELECT CTE4.EmpID, CAST(CTE4.MDate as Date) AS [Date], 
    CASE WHEN CTE4.INOUT = 1 THEN CTE4.MDate END AS INOUT_INTIME,
    CASE WHEN CTE4.INOUT = 2 THEN CTE4.MDate 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,d.ReportingDate,cte2.Date,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 
Posted
Updated 8-Jan-19 6:17am
Comments
OriginalGriff 8-Jan-19 11:29am    
And?
You wrote the SP, you know what it does.
So what is the problem in modifying it to do a relatively trivial task?
Member 12314309 8-Jan-19 11:33am    
i defined col above in attendance table,that is the result of this SP,this result i want to insert into a table of employeeattendance
OriginalGriff 8-Jan-19 11:37am    
At the risk of repeating myself:
"And?
You wrote the SP, you know what it does.
So what is the problem in modifying it to do a relatively trivial task?"

Remember, we can't see your screen, we can't access your HDD, we can;t read your mind. We only get exactly what you type to work with.
So we have no idea why something is proving difficult, particularly for someone who can evidently write relatively complex SQl without any problems ... and if we don't know why it's giving you difficulty, we can't really answer a question you haven't asked!
Member 12314309 8-Jan-19 11:44am    
Dear Original ,kindly do not take it on headache ,,,just i simply ask that how SP result which are coming in column to insert into table ,
I have attendance machine talbe in which employee INtime and OUttime is coming 1 and 2,from this INTIME and OUTTIME i calculated hours,OT,OTamount and Late,now these column result i want to insert into another table....
OriginalGriff 8-Jan-19 11:50am    
I ran that through Google Translate which said
"I'm a student and this is my homework. I've found some code on the internet, but I don't understand it at all, and it doesn't do exactly what I need to hand it in to my tutor. Please do my homework for me."

Is that correct?

1 solution

Your SP ends with a SELECT statement which returns the values to the calling application.
(EXACTLY the same as yesterdays post requesting this to update a table)

What you would need to do if you wanted this data to be inserted into your table would be to build an INSERT statement for the EmployeeAttendance table.
(as opposed to the UPDATE statement I wrote for you yesterday)

You are going to need to double check the mappings of these columns, did the best I could with what you had
SQL
INSERT EmployeeAttendance (EMPID, INTIME, OUTTIME, [HOURS], DUTY, OT, OTAmount, Late)  
select	cte.EmpID
	,	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

Please note
- ORDER BY was removed, is immaterial to this
- You have dates in the select portion, but not in EmployeeAttendanceTable
- No clue if DUTY mapped to Date
- The DATE type does not have a "format" to it. While you can include the code it does nothing
 
Share this answer
 
v2

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