Click here to Skip to main content
15,902,492 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
SQL
dbo.usp_employee_history_fillDates
@employee_id UNIQUEIDENTIFIER
, @date AS DATETIME
AS
BEGIN
	DECLARE @empID UNIQUEIDENTIFIER
	DECLARE @day DATETIME
	
	SET @empID = @employee_id
	SET @day = @date

	IF EXISTS(SELECT history_id FROM employee_history WHERE employee_id = @employee_id AND CONVERT(VARCHAR(24),employee_timeIn,107) = @date)
	BEGIN
		SELECT
		el.employee_id
		, e.history_id
		, CONVERT(VARCHAR(24),e.employee_timeIn,107) AS rawDate
		, e.employee_workHours
		, e.dayType
		INTO #temp
		FROM employee_list el
		INNER JOIN employee_history e ON e.employee_id = el.employee_id

		WHERE e.employee_id = @employee_id
		AND CONVERT(VARCHAR(24),e.employee_timeIn,107) = @date
		AND el.delete_status = 0 
		AND isActive = '1'   
		AND e.delete_status = 0           
		ORDER BY e.employee_timeIn ASC
	END
	ELSE
	BEGIN
		INSERT INTO #temp
		(
		el.employee_id
		, e.history_id
		, rawDate
		, e.employee_workHours
		, e.dayType
		)
		VALUES
		(
		@empID
		, '00000000-0000-0000-0000-000000000000'
		, @day
		, ''
		, ''
		)
	END
	SELECT * FROM #temp
END


No error but if does not exists, data does not insert.
Thanks in advance.
Posted
Updated 8-Feb-12 20:54pm
v3

Try this
SQL
DECLARE @empID UNIQUEIDENTIFIER
DECLARE @day DATETIME
	
SET @empID = @employee_id
SET @day = @date

SELECT
	el.employee_id
	, e.history_id
	, CONVERT(VARCHAR(24),e.employee_timeIn,107) AS rawDate
	, e.employee_workHours
	, e.dayType
INTO #temp
FROM employee_list el
INNER JOIN employee_history e ON e.employee_id = el.employee_id
WHERE e.employee_id = @employee_id
	AND CONVERT(VARCHAR(24),e.employee_timeIn,107) = @date
	AND el.delete_status = 0 
	AND isActive = '1'   
	AND e.delete_status = 0           
ORDER BY e.employee_timeIn ASC

IF @@ROWCOUNT = 0
	BEGIN
	INSERT INTO #temp
	(
	employee_id
	, history_id
	, rawDate
	, employee_workHours
	, dayType
	)
	VALUES
	(
	@empID
	, '00000000-0000-0000-0000-000000000000'
	, @day
	, ''
	, ''
	)
	END

SELECT * FROM #temp
 
Share this answer
 
Comments
YuMih 9-Feb-12 3:17am    
THANK YOU SO MUCH !!! :)
Probably, the problem is your datatypes and conversions:
SQL
@date AS DATETIME
...
CONVERT(VARCHAR(24),employee_timeIn,107) = @date
I'm not sure why you are converting what appears to be a Date or DateTime column to a Varchar before you comparte it to a DateTime value, but I know I wouldn't - I would compare the DataTime cvlaues directly.

What is the column format? And why do you think it is a good idea to convert it to a Varchar before the comparison - because there are so many ways that that could go wrong.
 
Share this answer
 
Comments
YuMih 9-Feb-12 3:17am    
Someone before me used it. I copied the first part. Just inserting the last one.
I'm not supposed to "alter" anything from the original. Thanks anyway.

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