Click here to Skip to main content
16,018,394 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a table named Attendance Register and Its Column are
EntryId , EmpId, AttMonth, D1, D2 ,D3,D4........D31

In This Table every day check Is Recoard exists then Update else Insert.

My Problam is how to update Day to Day After Punch

What I have tried:

INSERT INTO dbo.EmpPunch
		(
			EmpId, PunchId, PunchInDate,[Status]
		)
		VALUES
		(
			@EmpId, @PunchId, GETDATE(), 'P'
		)
	
	IF NOT EXISTS(SELECT * from dbo.AttendanceRegister WHERE EmpId = @EmpId AND MONTH(AttMonth) = MONTH(GETDATE()) AND YEAR(AttMonth) = YEAR(GETDATE()))
		BEGIN

			INSERT INTO dbo.AttendanceRegister 
			(
				EmpId,AttMonth 
			) 
			VALUES
			(
				@EmpId, GETDATE()
			)
		END

		UPDATE dbo.AttendanceRegister SET
			D16 = 'P'
		WHERE EmpId = @EmpId AND MONTH(AttMonth) = MONTH(GETDATE()) AND YEAR(AttMonth) = YEAR(GETDATE())
Posted
Updated 26-Aug-17 0:44am

i have a table named Attendance Register and Its Column are
EntryId , EmpId, AttMonth, D1, D2 ,D3,D4........D31

And My Problam is How To Update Day Column Dynamically...

Ex.
August - 2011
If Date 1 August Then Update D1
If Date 2 August Then Update D2
If Date 3 August Then Update D3
If Date 4 August Then Update D4
.
.
.
.
.
If Date 31 August Then Update D31

If Possible Update Without Case Using an Dynamically

ANd After Inser a New Recoard in Attandance Table To Update All Sunday Column Value Week Off.......
 
Share this answer
 
Comments
CHill60 17-Sep-17 9:51am    
Use the red "Ask a question" link if you have a question
The following will create a record only if no record already exists. By <value of empid> I mean the parameter @empid in your code:

SQL
insert into attendanceregister(empid, attmonth)
select a.*
from
(
select <value of empid>, getdate()
) as a,
(
select <value of empid>, month(getdate())
except
select empid, month(attmonth) from attendanceregister
) as b
SQL

 
Share this answer
 
v3
Because SQL is inherently multiuser, a better solution is to do the UPDATE, and if that doesn't work, then do an INSERT:
BEGIN TRANSACTION;
UPDATE AttendanceRegister SET ... WHERE EmpId = @EmpId AND MONTH(AttMonth) = MONTH(GETDATE()) AND YEAR(AttMonth) = YEAR(GETDATE()));
IF @@ROWCOUNT = 0
BEGIN
  INSERT INTO EmpPunch (EmpId, PunchId, PunchInDate,[Status]) VALUES(@EmpId, @PunchId, GETDATE(), 'P');
END
COMMIT TRANSACTION;
 
Share this answer
 

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