Thursday 22 August 2013

Excel-to-SQL Server Data Connections Parameters Sending Wrong Data Type

Excel-to-SQL Server Data Connections Parameters Sending Wrong Data Type

a bit stumped on this one. Someone I work with is having some troubles
with Parameters in an Excel Data Connection query to SQL Server. When he
changes a SQL query from hard-coded date filters to using parameters and
cell values, he gets an error (cannot convert character string to
smalldatetime). It is worth noting that the query has multiple subqueries.
I did a bit of research and found the following:
1) In the NetMon capture of the original query with one hard-coded date
changed to a parameter reference, Excel is sending the parameter datatype
as varchar(50).
2) When I change the query to have all dates changed to parameter
references, only the final parameter is being sent as varchar(50), the
rest are being sent as smalldatetime (which matches the table's data
type).
3) When I change the query to remove the subqueries but nothing else, the
query returns properly and Excel sends the correct data type. In this
situation there is just one parameter.
4) When I dump the query into a stored procedure and call that from the
same Excel data connection, the parameters all work correctly.
I tried moving the cells around so that I could see if it was a JET-like
problem of Excel checking the top 8 rows of data to determine datatype -
not the problem.
Why would this be happening? As I mentioned - stumped.
The code is essentially like this:
SELECT Name
, Sum(Sales) AS Sales
, (SELECT SUM(Cost) FROM Costcenter where Date > ? AND Date <= ?) As Costs
FROM SalesTable
Where Date = ?
Group by name
Order by name
And the final ? (in the WHERE clause is the one giving me grief.

No comments:

Post a Comment