Tuesday, September 06, 2011

Linked Server "String data, length mismatch"

So I have a TSQL problem that has been bothering me for a few months now. I have a stored procedure that runs nightly via a SQL scheduled job. It has failed almost everynight since April. The stored procedure is still in test and has been in low priority so I have dabbled with figuring it out. Our DBAs really didn't have a clue what the problem was and I couldn't find an answer through search engines. The annoying part is that I could run it development just fine and it would only sporadically fail in the test environment for me. I finally got tired of it and started pulling the 2900+ lines of SQL. Here's the error that I got:

 OLE DB provider "SQLNCLI" for linked server "MySQLServer01" returned message "String data, length mismatch". 
Msg 7421, Level 16, State 2, Line 1
Cannot fetch the rowset from OLE DB provider "SQLNCLI" for linked server "MySQLServer01". .
So part of problem had to do with the fact that I was trying to join two bits of data from different servers. I had a join that tried to join a VARCHAR(30) field from one SQL Server to a NVARCHAR(MAX) field on another SQL Server. I know that this isn't going to be the fastest of JOINS but it was a requirement. Seems that there is a problem with joining the MAX to something in another server. Once I did a CONVERT(VARCHAR(30) to the VARCHAR(MAX) field, my query completed beautifully.

I'm not sure how I would resolve this issue if I had to really join VARCHAR(MAX) to VARCHAR(MAX), but I guess I'll handle that in the future if that day comes.

2 comments:

Manish said...

Make it collation comaptible in linked server connection

Cody Schouten said...

I double checked the linked server connection and it is set to Collation Compatible = true. It was worth a try. Thanks for the idea though.