Skip to main content

Posts

Showing posts with the label sql

SqlBulkCopy and the "colid" error

I thought there was a page explaining this somewhere out there on the Internet, but I can't find it anymore. So here's what I re-discovered. When you try to insert the rows from a DataTable and the data in one of the columns of one of the rows is too big to fit into the destination column in the database, you get a SqlException with this error message: "Received an invalid column length from the bcp client for colid N." (Where "N" is a number.) It doesn't tell you which row, and it's a pain to figure out what column to look at. To determine what column it is referring to, you first need to get a listing of all columns in the table, listed in the order as they have been defined in the database. Next, you remove any columns in the list that are not represented in SqlBulkCopy.ColumnMappings (the order of the column mappings is irrelevant). The list that remains is what "colid" is referring to, with the first column corresponding to colid ...

UPDATE...FROM (Sql Server 2005/2008)

I keep forgetting how this concept works each time I go to do it. I always start off with a simple update , then find I need a quick join to another table to use a value there to complete the update. What I forget is how the table referenced after UPDATE relates to the tables/columns in the FROM (and SET) areas. From what I understand now, the "x" in "UPDATE x" either directly matches a table referenced in the FROM clause, or directly matches an alias for a table in the FROM. In the example below, there are 3 updates that are functionally equivalent. CREATE TABLE #table1 (t1_pk int, t1_months smallint); CREATE TABLE #table2 (t2_pk int, t2_t1_pk int, t2_starts datetime, t2_ends datetime); INSERT INTO #table1 VALUES (1, 2); INSERT INTO #table1 VALUES (2, 5); INSERT INTO #table2 VALUES (1, 1, '2000-01-01 11:45 PM', NULL); INSERT INTO #table2 VALUES (2, 1, '2001-01-01 02:50 AM', NULL); INSERT INTO #table2 VALUES (3, 2, '2004-01-04 06:45 PM', NUL...