views:

39

answers:

1

I'm using SqlBulkCopy to bulk insert some records from one table into another table. The query is using a SqlDataReader to get the data. The one difference that matters (besides column order which is handled in the mappings) between the tables is that the destination table has a date column into which the current date needs to be added. This date is not in the source table. How can I add this into the current process which is working fine minus this?

Current working code looks like this:

SqlCommand cmd = new SqlCommand("SELECT * from dbo.source", cn);
            SqlDataReader rdr = cmd.ExecuteReader();                

            using (SqlBulkCopy copy = new SqlBulkCopy(cn))
            {
                copy.ColumnMappings.Add(0, 0);
                copy.ColumnMappings.Add(1, 2);
                copy.ColumnMappings.Add(3, 3);
                copy.ColumnMappings.Add(2, 4);
                copy.ColumnMappings.Add(5, 5);
                copy.ColumnMappings.Add(14, 6);
                copy.DestinationTableName = "destination";
                copy.WriteToServer(rdr);
            }

The DB is sql 2008 ENT.

+6  A: 

You could just add it to be returned by your SELECT:

SELECT *, GETDATE() AS CurrentDate from dbo.source

And then just add another ColumnMapping for it.

AdaTheDev
yep, that worked great!
nelsonwebs