sp_executesql returns different results from view than running the query directly
-
10-02-2021 - |
Domanda
I'm trying to debug a SSRS report which is showing some dodgy results. I'm using sql profiler I've grabbed the exact query it's running which is executed with exec sp_executesql.
The query is returning results from a view.
If I run the query as it stands including the sp_executesql, I get one set of results. If I grab the query nested inside and run it on it's own, I get a different set of results.
I've got no idea how this is possible. The queries are identical and I was under the impression that sp_executesql simply executes the query.
Is there something I'm missing or how can I debug this further?
exec call looks something like this:
exec sp_executesql N'SELECT FirstName, LastName, DateOfBirth FROM ViewName WHERE
DateOfBirth >= @pStartDate AND DateOfBirth <= @pEndDate',N'@pStartDate datetime,
@pEndDate datetime',@pDate='2010-07-17 00:00:00:000',@pEndDate=''2010-07-17
23:59:59:000'
If I run that, I get one set of results.
Then if I run:
SELECT FirstName, LastName, DateOfBirth FROM ViewName WHERE
DateOfBirth >= '2010-07-17 00:00:00:000' AND
DateOfBirth <= '2010-07-17 23:59:59:000'
I get a slightly different set of results.
Soluzione
I would try pasting the query as is (doubling the single quotes that is) into the exec sp_executesql and verifying the results are the same as a manual run. If not, then it will be something to do with the date parameter, if the same, then its likely to be some kind of owner / security item.
Altri suggerimenti
- You are probably connecting with a different login.
This means results will be affectefd by a filter based on SUSER_SNAME or a different schema (dbo.View
vs OtherSchema.View
). It maybe inside the view.
You have NULL/empty string differences somewhere
Different databases or server