Insert data from one table to another

Posts   
 
    
Posts: 8
Joined: 15-Mar-2007
# Posted on: 15-Mar-2007 12:23:43   

Dears

I would like to insert some records from table into another table, something like this command of SQL : Insert ito headerTable Select Column1,Sum(Column2),Max(Column3) From DetailTable Where Column2 > 100 Group by Column1

Remember that for performance reasons I cant use a loop in my developing language (c#) and I want to do it in manner like SQL commands. there can be thousands of records in the DetailTable and using a loop would be very very slow. confused

jmeckley
User
Posts: 403
Joined: 05-Jul-2006
# Posted on: 15-Mar-2007 13:52:43   

if your using MS SQL Server you can use the BulkInsert command. and MDSN search on BulkInsert .Net 2.0 should point you in the right direction. There have also been posts on these forums about it as well. In summary it would look something like this:

ResultSet fields = new ResultSet(3);
fields.DefineField(DetailTableFields.Column1, 0);
fields.DefineField(DetailTableFields.Column2, 1, AggregateFunction.Sum);
fields.DefineField(DetailTableFields.Column3, 2, AggregateFunction.Max);

IGroupByCollection grouping = new GroupByCollection();
grouping.Add(ableFields.Column1);

IPrediateExpression filter = new PredicateExpression(DetailTableFields.Column2 > 100);

DataTable results = new DataTable();
string cnn;
using(DataAccessAdatper adapter = new DataAccessAdatper())
{
     cnn = adapter.ConnectionString;
     adapter.FetchTypedList(fields, results, filter, 0, null, null, false, grouping);
}

using (SqlBulkCopy copy = new SqlBulkCopy(cnn))
{ 
    //begin sql transaction
    //configure field mappings and sql bulk copy options if necessary
    try
    {
         copy.DestinationTableName = "headerTable";
         copy.WriteToServer(table);

        //commit
      }
      catch
      {
         //rollback
      }
}
Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 15-Mar-2007 14:10:05   

You may also use a stored Procedure and call it from LLBLGen Pro code.