Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
how can i insert some/part rows data from a old table[t_attendence_raw] into a new table [t_attendence]

here's my coding. please anyone help?!

those three rows are: memcode, m_cardno and t_scantime.

C#
String SQLQuery = "INSERT INTO [t_attendence] (memcode, m_cardno, t_scantime, t_type, t_del, t_post, create_date, create_by, modify_date, modify_by) VALUES ([t_attendence_r].[m_memcode], [t_attendence_r].[m_cardno], [t_attendence_r].[t_scantime], 'Y', 'Y', 'Y', @create_date, @create_by, @modify_date, @modify_by)";
            SqlCommand cmd = new SqlCommand(SQLQuery, conn);
            {   
                cmd.Parameters.AddWithValue("@modify_by", HttpContext.Current.User.Identity.Name);
                cmd.Parameters.AddWithValue("@modify_date", DateTime.Now.ToString(""));
                cmd.Parameters.AddWithValue("@create_by", HttpContext.Current.User.Identity.Name);
                cmd.Parameters.AddWithValue("@create_date", DateTime.Now.ToString(""));

                cmd.ExecuteNonQuery();
                
                cmd.Parameters.RemoveAt("@modify_by");
                cmd.Parameters.RemoveAt("@modify_date");
                cmd.Parameters.RemoveAt("@create_by");
                cmd.Parameters.RemoveAt("@create_date");
            }
Posted
Updated 27-Dec-11 20:04pm
v3

Have a look at the Select * Into query.
This[^] describes this in some detail.

Here is another example - http://www.w3schools.com/sql/sql_select_into.asp[^].
 
Share this answer
 
v2
Comments
tomy_hkcg 28-Dec-11 2:37am    
i think i should be doing it by adding some DataRows
Wendelius 28-Dec-11 2:43am    
Good answer, 5.
Abhinav S 28-Dec-11 3:10am    
Thank you Mika.
Amir Mahfoozi 28-Dec-11 3:26am    
+5
Abhinav S 28-Dec-11 3:27am    
Thanks.
Or if the table already exists, you can use:
SQL
INSERT INTO t_attendence
([list of fields to insert])
SELECT [list of fields to select]
FROM t_attendence_raw
WHERE [conditions to restrict the rows to insert]
 
Share this answer
 
Comments
tomy_hkcg 28-Dec-11 2:59am    
yea, this is what i want to do, but in my case, there are others rows like, create_date, i wanna insert into t_attendence also. so can i use the same SQL statement?
Wendelius 28-Dec-11 3:44am    
Yes you can, just add the necessary fields in both insert column list and in your select statement and it should be fine.
Amir Mahfoozi 28-Dec-11 3:26am    
+5
Wendelius 28-Dec-11 3:44am    
Thanks
Abhinav S 28-Dec-11 3:27am    
5!
According to your comment for Mika I should remind you that you can use also use functions and parameters in you insert into statement in addition to source table fields :

SQL
INSERT INTO [t_attendence] (memcode, m_cardno, t_scantime, t_type, t_del, t_post, create_date, create_by, modify_date, modify_by) 
select ([t_attendence_r].[m_memcode], [t_attendence_r].[m_cardno], [t_attendence_r].[t_scantime], 'Y', 'Y', 'Y', GETDATE(), @create_by, GETDATE(), @modify_by)
from [t_attendence_r]


Hope it helps.
 
Share this answer
 
Comments
Wendelius 28-Dec-11 3:45am    
Good addition, 5.
Amir Mahfoozi 28-Dec-11 4:07am    
Thank you Mika.
thanks so much guys, finally its worked. =)
 
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