一条简单的SQL大家进来看下哦。。

yinxiaoqi 2012-03-28 04:07:45
select count(name) tablenum from bkysbase.dbo.sysobjects where type ='U' 查表的

select count(name) tablenum from bkysbase.dbo.sysobjects where type ='V' 查视图的

我想把这两条sql合成一个 显示结果
tablen tablev
500 20

还有就是我的服务器上有多个数据库,查询时候很慢,很慢,这个可以优化下吗?
...全文
91 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
黄_瓜 2012-03-28
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 的回复:]

引用 11 楼 的回复:
引用 10 楼 的回复:

大约有20 个库,,执行时间差不多10秒了,,是否可以优化下呢。




SQL code

--try
select * from
(select count(*) as tablen from sys.tables) a
,
(select count(*) as tablev from sys.views……
[/Quote]
这怎么会慢呢?你又关联了其他东西吧
99归一 2012-03-28
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 的回复:]
SQL code

select sum(case type when 'U' then 1 else 0 end) as tablen,
sum(case type when 'V' then 1 else 0 end) as tablev
from sysobjects
tablen tablev
131 362

本机测试结果
[/Quote]
正解
yinxiaoqi 2012-03-28
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 的回复:]
引用 10 楼 的回复:

大约有20 个库,,执行时间差不多10秒了,,是否可以优化下呢。




SQL code

--try
select * from
(select count(*) as tablen from sys.tables) a
,
(select count(*) as tablev from sys.views) b
[/Quote]

哎,,速度 还是慢啊,,晕了,,
快溜 2012-03-28
  • 打赏
  • 举报
回复
取@@RowCount试试,应该能快一些。
黄_瓜 2012-03-28
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 的回复:]

大约有20 个库,,执行时间差不多10秒了,,是否可以优化下呢。
[/Quote]



--try
select * from
(select count(*) as tablen from sys.tables) a
,
(select count(*) as tablev from sys.views) b
yinxiaoqi 2012-03-28
  • 打赏
  • 举报
回复
大约有20 个库,,执行时间差不多10秒了,,是否可以优化下呢。
yinxiaoqi 2012-03-28
  • 打赏
  • 举报
回复
执行通过了,,但是 执行的时间太慢了,
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]

引用 1 楼 的回复:
SQL code
select sum(case type when 'U' then 1 else 0 end) as tablen
sum(case type when 'V' then 1 else 0 end) as tablev
from bkysbase.dbo.sysobjects


Incorrect syntax near the ke……
[/Quote]

他没加逗号
  • 打赏
  • 举报
回复

select sum(case type when 'U' then 1 else 0 end) as tablen,
sum(case type when 'V' then 1 else 0 end) as tablev
from sysobjects
tablen tablev
131 362

本机测试结果
yinxiaoqi 2012-03-28
  • 打赏
  • 举报
回复
Incorrect syntax near the keyword 'sum'.
没有执行通过啊
AcHerat 2012-03-28
  • 打赏
  • 举报
回复

select sum(case type when 'U' then 1 else 0 end) as tablen, --,号
sum(case type when 'V' then 1 else 0 end) as tablev
from bkysbase.dbo.sysobjects
yinxiaoqi 2012-03-28
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]
SQL code
select sum(case type when 'U' then 1 else 0 end) as tablen
sum(case type when 'V' then 1 else 0 end) as tablev
from bkysbase.dbo.sysobjects
[/Quote]

Incorrect syntax near the keyword 'sum'.
没有通过
yinxiaoqi 2012-03-28
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]
SQL code
select sum(case type when 'U' then 1 else 0 end) as tablen
sum(case type when 'V' then 1 else 0 end) as tablev
from bkysbase.dbo.sysobjects
[/Quote]

Incorrect syntax near the keyword 'sum'.
没有通过
AcHerat 2012-03-28
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

SQL code
select sum(case type when 'U' then 1 else 0 end) as tablen
sum(case type when 'V' then 1 else 0 end) as tablev
from bkysbase.dbo.sysobjects
[/Quote]

++1
快溜 2012-03-28
  • 打赏
  • 举报
回复
select sum(case type when 'U' then 1 else 0 end) as tablen
sum(case type when 'V' then 1 else 0 end) as tablev
from bkysbase.dbo.sysobjects

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧