连接表变量与连接视图
我有一个运行速度相当慢的存储过程。因此我想在单独的视图中提取一些查询。
我的代码看起来像这样:
DECLARE @tmpTable TABLE(..)
INSERT INTO @tmpTable (..) *query* (returns 3000 rows)
Select ... from table1
inner join table2
inner join table3
inner join @tmpTable
...
然后我提取(复制粘贴)*查询*并将其放入视图中 - 即 vView。
这样做会给我不同的结果:
Select ... from table1
inner join table2
inner join table3
inner join vView
...
为什么?我可以看到 vView 和 @tmpTable 都返回 3000 行,因此它们应该匹配(还执行了 except 查询来检查)。
任何评论都会非常感激,因为我觉得自己对此很困惑。
编辑:
这是获取结果的完整查询(使用 @tmpTable 或 vView 给我不同的结果,尽管看起来相同):
select dep.sid as depsid, dep.[name], COUNT(b.sid) as possiblelogins, count(ls.clientsid) as logins
from department dep
inner join relationship r on dep.sid=r.primarysid and r.relationshiptypeid=27 and r.validto is null
inner join [user] u on r.secondarysid=u.sid
inner join relationship r2 on u.sid=r2.secondarysid and r2.validto is null and r2.relationshiptypeid in (1,37)
inner join client c on r2.primarysid=c.sid
inner join ***@tmpTable or vView*** b on b.sid = c.sid
left outer join (select distinct clientsid from logonstatistics) as ls on b.sid=ls.clientsid
GROUP BY dep.sid, dep.[name],dep.isdepartment
HAVING dep.isdepartment=1
I have a stored procedure which is running quite slow. Therefore I want to extract some of the query in a separate view.
My code looks something like this:
DECLARE @tmpTable TABLE(..)
INSERT INTO @tmpTable (..) *query* (returns 3000 rows)
Select ... from table1
inner join table2
inner join table3
inner join @tmpTable
...
I then extract (copy-paste) the *query* and put it in a view - i.e. vView.
Doing this will then give me a different result:
Select ... from table1
inner join table2
inner join table3
inner join vView
...
Why? I can see that the vView and the @tmpTable both returns 3000 rows, so they should match (also did a except query to check).
Any comments would be much appriciated as I feel quite stuck with this..
EDITED:
This is the full query for getting the result (using @tmpTable or vView gives me different results, although the appear the same):
select dep.sid as depsid, dep.[name], COUNT(b.sid) as possiblelogins, count(ls.clientsid) as logins
from department dep
inner join relationship r on dep.sid=r.primarysid and r.relationshiptypeid=27 and r.validto is null
inner join [user] u on r.secondarysid=u.sid
inner join relationship r2 on u.sid=r2.secondarysid and r2.validto is null and r2.relationshiptypeid in (1,37)
inner join client c on r2.primarysid=c.sid
inner join ***@tmpTable or vView*** b on b.sid = c.sid
left outer join (select distinct clientsid from logonstatistics) as ls on b.sid=ls.clientsid
GROUP BY dep.sid, dep.[name],dep.isdepartment
HAVING dep.isdepartment=1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您更改为此,您可能不需要视图/表。
它连接到
client c
并且似乎只是为了连接到logonstatistics
并将
COUNT(b.sid)
更改为COUNT (c.sid)
在 SELECT 子句中否则,如果得到不同的结果,我可以看到两个选项:
最后,当您说“不同的结果”时,您的意思是您得到 x2 或 x3 行吗?不同的 COUNT?什么?
You maybe don't need the view/table if you change to this.
It joins on to
client c
and appears to be there only to JOIN ontologonstatistics
And change
COUNT(b.sid)
toCOUNT(c.sid)
in the SELECT clauseOtherwise, if you get different results you have two options I can see:
Finally, when you says "different results" do you mean you get x2 or x3 rows? A different COUNT? What?