MartinCroft
Programmer
Hi
This ones a little weird, existing code that runs on a SQL2000 box fails when run against a SQL 2008 Server. Here's the scenario. SQL basically thinks the row size being inserted is bigger than the column being inserted into and its not.
A query that joins several tables together returns a source column from table A. This column in the source table is a VARCHAR(250) , but only VARCHAR(50) in the destination table. I know this is not best practice and is actually the fix for this issue. what I want to know is why this is doing what is is doing. The query returns a result set of 200 rows, if taking either MAX(LEN(Source)) or MAX(DATALENGTH(Source)) from this table you get 45, 5 less than the destination table.
If you just run a query against Table A with just the WHERE clause elements from the query ( so no other tables joined) and take the MAX values again it comes out as 78. If you try inserting the main query( all joins used) into a table variable it will fail at 77 characters VARCHAR, but work at 78. If you did a MAX again after the 78 value works its shows as 45.
The upshot of this is that SQL is assuming that the row size will be 78, but it isn't in the query its actually 45.
If you use a LEFT(source,50) this works,as does changing the column to VARCHAR(50), its the fact that SQL can get this wrong thats worrying. Has anyone seen this behaviour in SQL2008.
Additionally, tried created new source table and populated from existing, this had no indexes on (the new table) created cluster and an non clustered index on source column,.Thinking this was STATS related, also rebuild of stats with FULLSCAN. Next step I plan is to try on another server but can't replicate without using the query i.e in just using a single table
Also tried LTRIM, RTRIM with no difference
Any one come across this before
This ones a little weird, existing code that runs on a SQL2000 box fails when run against a SQL 2008 Server. Here's the scenario. SQL basically thinks the row size being inserted is bigger than the column being inserted into and its not.
A query that joins several tables together returns a source column from table A. This column in the source table is a VARCHAR(250) , but only VARCHAR(50) in the destination table. I know this is not best practice and is actually the fix for this issue. what I want to know is why this is doing what is is doing. The query returns a result set of 200 rows, if taking either MAX(LEN(Source)) or MAX(DATALENGTH(Source)) from this table you get 45, 5 less than the destination table.
If you just run a query against Table A with just the WHERE clause elements from the query ( so no other tables joined) and take the MAX values again it comes out as 78. If you try inserting the main query( all joins used) into a table variable it will fail at 77 characters VARCHAR, but work at 78. If you did a MAX again after the 78 value works its shows as 45.
The upshot of this is that SQL is assuming that the row size will be 78, but it isn't in the query its actually 45.
If you use a LEFT(source,50) this works,as does changing the column to VARCHAR(50), its the fact that SQL can get this wrong thats worrying. Has anyone seen this behaviour in SQL2008.
Additionally, tried created new source table and populated from existing, this had no indexes on (the new table) created cluster and an non clustered index on source column,.Thinking this was STATS related, also rebuild of stats with FULLSCAN. Next step I plan is to try on another server but can't replicate without using the query i.e in just using a single table
Also tried LTRIM, RTRIM with no difference
Any one come across this before