Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all,

actually I need a query to insert data into database.
I have a Epmloyee table and a Attendance table.



I need to insert some data into database. The thing is that I need to insert data for 10 users. see the fields below.

UserId, LoginDate, InTime, OutTime

the unique part is that LoginDate, InTime, OutTime for every User from 01/06/2012 to 31/12/2012.

and the InTime is 09:30 AM and Out Time is 6:30 PM for every date from 01/06/2012 to 31/12/2012 for every UserId.

It is possible in one program in C# Or in sql query for every UserId.

Please help. How can I do this.


I found the working days from June to Dec

SQL
DECLARE @STARTDATE datetime; 
DECLARE @EntDt datetime; 
set @STARTDATE = '06/01/2012';  
set @EntDt = '12/31/2012'; 
declare @dcnt int; 
;with DateList as   
 (   
    select @STARTDATE DateValue   
    union all   
    select DateValue + 1 from    DateList      
    where   DateValue + 1 < convert(VARCHAR(15),@EntDt,101)   
 )   
  select count(*) as DayCnt from (   
  select DateValue,DATENAME(WEEKDAY, DateValue ) as WEEKDAY from DateList
  where DATENAME(WEEKDAY, DateValue ) not IN ( 'Saturday','Sunday' )     
  )a
option (maxrecursion 365);


Thnaks

Gaurav
Posted
Comments
RDBurmon 28-Jan-13 23:50pm    
Hello demoninside9 , Mr Andrew have provide very good solution with clear explanation. Please implement and if helps then vote it and mark your query as "[SOLVED] by accepting the solution.

Great. 90% of job is done.
Now, all you need to do is join it with your "Users" table to get the list of all User IDs in the system and write a nice INSERT INTO SQL, similar to this (replace MyTable with your table):

SQL
DECLARE @STARTDATE datetime; 
DECLARE @EntDt datetime; 
set @STARTDATE = '06/01/2012';  
set @EntDt = '12/31/2012'; 
declare @dcnt int; 

 with DateList as   
 (   
    select @STARTDATE DateValue   
    union all   
    select DateValue + 1 from    DateList      
    where   DateValue + 1 < convert(VARCHAR(15),@EntDt,101)   
 )   
 INSERT INTO MyTable
 select u.UserID,
 Convert(DATETIME(CONVERT(VARCHAR(10), DateValue, 121) + ' 09:30:00', 121) As InTime, 
 Convert(DATETIME(CONVERT(VARCHAR(10), DateValue, 121) + ' 09:30:00', 121) As OutTime
 from DateList, Users u
 where DATENAME(WEEKDAY, DateValue ) not IN ( 'Saturday','Sunday' )     
 option (maxrecursion 365);
 
Share this answer
 
Comments
RDBurmon 28-Jan-13 23:49pm    
Thanks Andrew
demoninside9 29-Jan-13 0:15am    
shows error
Msg 156, Level 15, State 1, Line 16
Incorrect syntax near the keyword 'CONVERT'.
chaau 29-Jan-13 4:53am    
this is because you need to substitute the MyTable and the Users, as well as UserID in the query above with the real table names. I guess you have already designed and created a table that will hold the login information
demoninside9 29-Jan-13 23:47pm    
DECLARE @STARTDATE AS DATE;
DECLARE @EntDt AS DATE;
set @STARTDATE = '06/01/2012';
set @EntDt = '12/31/2012';
declare @dcnt AS int;

with DateList as
(
select @STARTDATE DateValue
union all
select DateValue + 1 from DateList
where DateValue + 1 < convert(VARCHAR(15),@EntDt,101)
)
INSERT INTO mtblAttendance
select u.User_Id,
Convert(VARCHAR(10),CONVERT(DATETIME,CONVERT(VARCHAR(10), DateValue, 121)) + ' 09:30:00', 121) As In_Time,
Convert(VARCHAR(10),CONVERT(DATETIME,CONVERT(VARCHAR(10), DateValue, 121)) + ' 09:30:00', 121) As Out_Time
from DateList, mtblEmployee u
where DATE(WEEKDAY, DateValue ) not IN ( 'Saturday','Sunday' )
option (maxrecursion 365);

shows error
'DATE' is not a recognized built-in function name.
see the link http://www.freeimagehosting.net/h3v76
chaau 29-Jan-13 23:55pm    
There is no DATE(WEEKDAY, DateValue ) in my answer. I used DATENAME.
HI,

Try like this:

SQL
DECLARE @STARTDATE datetime;
DECLARE @EntDt datetime;
set @STARTDATE = '06/01/2012';
set @EntDt = '12/31/2012';
declare @dcnt int;

 with DateList as
 (
    select @STARTDATE DateValue
    union all
    select DateValue + 1 from    DateList
    where   DateValue + 1 < convert(VARCHAR(15),@EntDt,101)
 )
 INSERT INTO MyTable
 select u.UserID,
 Convert(DATETIME,(DateValue) + ' 09:30:00', 121) As InTime,
 Convert(DATETIME, (DateValue) + ' 09:30:00', 121) As OutTime
 from DateList, Users u
 where DATENAME(WEEKDAY, DateValue ) not IN ( 'Saturday','Sunday' )
 option (maxrecursion 365);


Thanks
 
Share this answer
 
Comments
demoninside9 29-Jan-13 3:56am    
Column name or number of supplied values does not match table definition.
shows error

SQL
Msg 156, Level 15, State 1, Line 16
Incorrect syntax near the keyword 'CONVERT'.


SQL
DECLARE @STARTDATE AS DATE; 
DECLARE @EntDt AS DATE; 
set @STARTDATE = '06/01/2012';  
set @EntDt = '12/31/2012'; 
declare @dcnt AS int; 
 
 with DateList as   
 (   
    select @STARTDATE DateValue   
    union all   
    select DateValue + 1 from    DateList      
    where   DateValue + 1 < convert(VARCHAR(15),@EntDt,101)   
 )   
 INSERT INTO mtblAttendance
 select u.User_Id,
 Convert(VARCHAR(10),CONVERT(DATETIME,CONVERT(VARCHAR(10), DateValue, 121)) + ' 09:30:00', 121) As In_Time, 
 Convert(VARCHAR(10),CONVERT(DATETIME,CONVERT(VARCHAR(10), DateValue, 121)) + ' 09:30:00', 121) As Out_Time
 from DateList, mtblEmployee u
 where DATENAME (dw, DateValue ) not IN ( 'Saturday','Sunday' )     
 option (maxrecursion 365);
 
Share this answer
 
v9
Comments
RDBurmon 29-Jan-13 0:36am    
I have updated above solution Try now
demoninside9 29-Jan-13 0:56am    
still same error :(
RDBurmon 29-Jan-13 1:21am    
Try now
demoninside9 29-Jan-13 1:34am    
shows 'DATETIME' is not a recognized built-in function name.
RDBurmon 29-Jan-13 1:37am    
Try now

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