LINQ SQL 语句返回错误结果
我有一个 SQL 语句,据我所知是正确的,但 SQL 服务器的响应不正确。我已经调试了这个问题,发现如果我在没有包装存储过程的情况下执行 SQL 语句,我会得到不同的结果。我所做的就是用 Linq 生成的代码的实际值替换变量
:
exec sp_executesql N'SELECT [t0].[RoomId], [t0].[Title], [t0].[Detail], [t0].[ThumbnailPath], [t0].[PageId], [t0].[TypeId], [t0].[LocationId], [t0].[TimeStamp], [t0].[DeleteStamp]
FROM [dbo].[Room] AS [t0]
INNER JOIN [dbo].[RoomType] AS [t1] ON [t1].[RoomTypeId] = [t0].[TypeId]
WHERE ([t1].[Sleeps] >= @p0) AND ([t0].[DeleteStamp] IS NULL) AND (((
SELECT COUNT(*)
FROM [dbo].[Booking] AS [t2]
INNER JOIN [dbo].[Order] AS [t3] ON [t3].[OrderId] = [t2].[OrderId]
WHERE ([t2].[StartStamp] <= @p1)
AND ([t2].[EndStamp] >= @p2)
AND (([t3].[Status] = @p3)
OR ([t3].[Status] = @p4)
OR (([t3].[Status] = @p5) AND ([t3].[CreatedStamp] > @p6)))
AND ([t2].[RoomId] = [t0].[RoomId])
)) = @p7)
',N'@p0 int,@p1 datetime,@p2 datetime,@p3 int,@p4 int,@p5 int,@p6 datetime,@p7 int',
@p0=1,@p1='2011-04-05 00:00:00',@p2='2011-04-04 00:00:00',@p3=3,@p4=5,@p5=0,@p6='2011-04-04 12:36:09.490',@p7=0
没有 SP
SELECT [t0].[RoomId], [t0].[Title], [t0].[Detail], [t0].[ThumbnailPath], [t0].[PageId], [t0].[TypeId], [t0].[LocationId], [t0].[TimeStamp], [t0].[DeleteStamp]
FROM [dbo].[Room] AS [t0]
INNER JOIN [dbo].[RoomType] AS [t1] ON [t1].[RoomTypeId] = [t0].[TypeId]
WHERE ([t1].[Sleeps] >= 1) AND ([t0].[DeleteStamp] IS NULL) AND (((
SELECT COUNT(*)
FROM [dbo].[Booking] AS [t2]
INNER JOIN [dbo].[Order] AS [t3] ON [t3].[OrderId] = [t2].[OrderId]
WHERE ([t2].[StartStamp] <= '2011-04-05 00:00:00')
AND ([t2].[EndStamp] >= '2011-04-04 00:00:00')
AND (([t3].[Status] = 3)
OR ([t3].[Status] = 4)
OR (([t3].[Status] = 5) AND ([t3].[CreatedStamp] > '2011-04-04 12:36:09.490')))
AND ([t2].[RoomId] = [t0].[RoomId])
)) = 0)
第一个结果集返回 1 行,而第二个结果集返回 21!
谁能看出其中的区别,因为它让我发疯。
I have a SQL statement which afaik is correct but the response from the SQL server is incorrect. I've debugged this issue and found that if I execute the SQL statement without the wrapping store procedure I get different results. All I have done is replaced the variable with the actual values
Linq generated code:
exec sp_executesql N'SELECT [t0].[RoomId], [t0].[Title], [t0].[Detail], [t0].[ThumbnailPath], [t0].[PageId], [t0].[TypeId], [t0].[LocationId], [t0].[TimeStamp], [t0].[DeleteStamp]
FROM [dbo].[Room] AS [t0]
INNER JOIN [dbo].[RoomType] AS [t1] ON [t1].[RoomTypeId] = [t0].[TypeId]
WHERE ([t1].[Sleeps] >= @p0) AND ([t0].[DeleteStamp] IS NULL) AND (((
SELECT COUNT(*)
FROM [dbo].[Booking] AS [t2]
INNER JOIN [dbo].[Order] AS [t3] ON [t3].[OrderId] = [t2].[OrderId]
WHERE ([t2].[StartStamp] <= @p1)
AND ([t2].[EndStamp] >= @p2)
AND (([t3].[Status] = @p3)
OR ([t3].[Status] = @p4)
OR (([t3].[Status] = @p5) AND ([t3].[CreatedStamp] > @p6)))
AND ([t2].[RoomId] = [t0].[RoomId])
)) = @p7)
',N'@p0 int,@p1 datetime,@p2 datetime,@p3 int,@p4 int,@p5 int,@p6 datetime,@p7 int',
@p0=1,@p1='2011-04-05 00:00:00',@p2='2011-04-04 00:00:00',@p3=3,@p4=5,@p5=0,@p6='2011-04-04 12:36:09.490',@p7=0
Without the SP
SELECT [t0].[RoomId], [t0].[Title], [t0].[Detail], [t0].[ThumbnailPath], [t0].[PageId], [t0].[TypeId], [t0].[LocationId], [t0].[TimeStamp], [t0].[DeleteStamp]
FROM [dbo].[Room] AS [t0]
INNER JOIN [dbo].[RoomType] AS [t1] ON [t1].[RoomTypeId] = [t0].[TypeId]
WHERE ([t1].[Sleeps] >= 1) AND ([t0].[DeleteStamp] IS NULL) AND (((
SELECT COUNT(*)
FROM [dbo].[Booking] AS [t2]
INNER JOIN [dbo].[Order] AS [t3] ON [t3].[OrderId] = [t2].[OrderId]
WHERE ([t2].[StartStamp] <= '2011-04-05 00:00:00')
AND ([t2].[EndStamp] >= '2011-04-04 00:00:00')
AND (([t3].[Status] = 3)
OR ([t3].[Status] = 4)
OR (([t3].[Status] = 5) AND ([t3].[CreatedStamp] > '2011-04-04 12:36:09.490')))
AND ([t2].[RoomId] = [t0].[RoomId])
)) = 0)
The first result set returns 1 row where as the 2nd returns me 21!!
Can anybody spot the difference as its driving me crazy.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您替换变量时出错!
当您应该将 p4 替换为 5 时,您将 p4 替换为 4,将 p5 替换为 5 而不是 0。
You made an error replacing the variables!
You replaced p4 with 4 when you should have replaced it with 5 and p5 with 5 instead of 0.
好吧,其中一个区别是
@p5=0
,而另一个区别是[t3].[Status] = 5
。Well, one difference is
@p5=0
while you have[t3].[Status] = 5
in the other.