SQLDescribeCol returns the wrong column length and data type in a complex union query that has parameters
This article helps you resolve the problem that the
SQLDescribeCol command returns the wrong column length and data type in a complex union query that has parameters.
Original product version: SQL Server 2008, Microsoft SQL Server 2005
Original KB number: 2900760
Consider the following scenario:
- You use a version of Microsoft SQL Server Native Client that is earlier than Microsoft SQL Server Native Client 11.0.
- Your code makes a call to the
SQLDescribeColfunction for a complex union query that contains parameters and a WHERE clause.
In this scenario,
SQLDescribeCol returns an incorrect column length and data type.
This problem occurs because the driver truncates the query for the metadata on the UNION keyword. Therefore, SQL Server requests metadata for only the first query and ignores the second query. If you change the order of the queries,
SQLDescribeCol returns correct data.
To resolve this problem, use SQL Server Native Client 11.0 or a later version in your application. To obtain SQL Server Native Client 11.0, or for more information, see Microsoft SQL Server 2012 SP4 Feature Pack.
To work around this problem, take either of the following actions:
- Compile the query into a stored procedure that uses parameters.
- Reverse the order of the
SELECTstatements in the union query so that the constant field is in the last
Microsoft has confirmed this is a known problem in earlier versions of SQL Server and SQL Server Native Client.
Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.
- v R2 Enterprise
- SQL Server 2008 R2 Datacenter
- SQL Server 2008 R2 Standard
- SQL Server 2008 Enterprise
- SQL Server 2008 Standard
- Microsoft SQL Server 2005 Enterprise Edition
- Microsoft SQL Server 2005 Standard Edition