求一sql语句,这样的功能能否实现?

猿敲月下码 2010-09-08 10:49:08
在数据库MY_DB中, 把含有username或user_name字段的表名列出来?

sql或者存储过程都可以.

能否实现?感觉应该可以
...全文
187 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
猿敲月下码 2010-09-08
  • 打赏
  • 举报
回复
对的 少加了个b.type='u' 不然的话函数,视图, 过程都进来了 谢谢 结贴咯,随便贴下答案:
稍作下修改,在schema上做了下排序

-- 轻骑兵的
SELECT SS.name+'.'+SO.name
FROM sys.objects SO
INNER JOIN sys.schemas SS ON SO.schema_id = SS.schema_id
INNER JOIN sys.columns SC ON SO.object_id = SC.object_id
WHERE SO.type ='U' AND SC.name in ( 'username' ,'user_name')
order by a.name

-- 国哥的
select a.[name],b.[name]
from sys.schemas as a join sys.objects as b
on a.schema_id=b.schema_id
where b.type='u' and b.object_id in (SELECT id FROM [syscolumns] WHERE [name]='username' OR [name]='user_name'
)
order by a.name

hao1hao2hao3 2010-09-08
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 thc1987 的回复:]
谢谢国哥,轻骑兵~

恩,11L是我想要的答案,就是返回的条件在一个单元格里面.

但是11L跟12L跑出来的结果不一样.12L 多一条记录

不知道为什么多出来那张表在我的数据库里没有
[/Quote]

好像少了一个过滤条件

select a.[name],b.[name]
from sys.schemas as a join sys.objects as b
on a.schema_id=b.schema_id
where b.type='u' and b.object_id in (SELECT id FROM [syscolumns] WHERE [name]='username' OR [name]='user_name'
)
猿敲月下码 2010-09-08
  • 打赏
  • 举报
回复
谢谢国哥,轻骑兵~

恩,11L是我想要的答案,就是返回的条件在一个单元格里面.

但是11L跟12L跑出来的结果不一样.12L 多一条记录

不知道为什么多出来那张表在我的数据库里没有
hao1hao2hao3 2010-09-08
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 thc1987 的回复:]
引用 7 楼 hlq_zist 的回复:
引用 5 楼 thc1987 的回复:

貌似有点问题.. 表名出来了,但是表名前面的schema没出来哦


你自己说“把含有username或user_name字段的表名列出来”

国哥的回答完全满足你的要求!

好吧 我承认我没说清楚,我的表名前面有schema,现在能否把schema也带出来

schema.表名
[/Quote]

这样?

select a.[name],b.[name]
from sys.schemas as a join sys.objects as b
on a.schema_id=b.schema_id
and object_id in (SELECT id FROM [syscolumns] WHERE [name]='username' OR [name]='user_name'
)

Mr_Nice 2010-09-08
  • 打赏
  • 举报
回复
SELECT SS.name+'.'+SO.name 
FROM sys.objects SO
INNER JOIN sys.schemas SS ON SO.schema_id = SS.schema_id
INNER JOIN sys.columns SC ON SO.object_id = SC.object_id
WHERE SO.type ='U' AND SC.name in ( 'username' ,'user_name')
Mr_Nice 2010-09-08
  • 打赏
  • 举报
回复
SELECT SS.name,SO.name 
FROM sys.objects SO
INNER JOIN sys.schemas SS ON SO.schema_id = SS.schema_id
INNER JOIN sys.columns SC ON SO.object_id = SC.object_id
WHERE SO.type ='U' AND SC.name = 'username'
猿敲月下码 2010-09-08
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 hlq_zist 的回复:]
引用 5 楼 thc1987 的回复:

貌似有点问题.. 表名出来了,但是表名前面的schema没出来哦


你自己说“把含有username或user_name字段的表名列出来”

国哥的回答完全满足你的要求!
[/Quote]
好吧 我承认我没说清楚,我的表名前面有schema,现在能否把schema也带出来

schema.表名
ws_hgo 2010-09-08
  • 打赏
  • 举报
回复
select [name] from sysobjects where xtype ='u' and 
id in
(select id from syscolumns where [name]='username' or [name]='user_name')
hlq_zist 2010-09-08
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 thc1987 的回复:]

貌似有点问题.. 表名出来了,但是表名前面的schema没出来哦
[/Quote]

你自己说“把含有username或user_name字段的表名列出来”

国哥的回答完全满足你的要求!
hlq_zist 2010-09-08
  • 打赏
  • 举报
回复

SELECT [name] FROM [sysobjects] WHERE [id] in(
SELECT id FROM [syscolumns] WHERE [name]='username' OR [name]='user_name'
)
/*这样完全OK,2000 OR 2005都执行正常,无误。*/
猿敲月下码 2010-09-08
  • 打赏
  • 举报
回复
貌似有点问题.. 表名出来了,但是表名前面的schema没出来哦
hao1hao2hao3 2010-09-08
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 thc1987 的回复:]
引用 2 楼 hao1hao2hao3 的回复:
SQL code

use MY_DB
go
----2005写法
select [name] from sysobjects where xtype ='u' and id in(
select id from syscolumns where [name]='username' or [name]='user_name')

……
[/Quote]


select [name] from sysobjects where xtype ='u' and id in(
select id from syscolumns where [name]='username' or [name]='user_name')


这样就行了。
猿敲月下码 2010-09-08
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 hao1hao2hao3 的回复:]
SQL code

use MY_DB
go
----2005写法
select [name] from sysobjects where xtype ='u' and id in(
select id from syscolumns where [name]='username' or [name]='user_name')

--2000写法,好像是有一个字段名字不一样

……
[/Quote]

我2005的
hao1hao2hao3 2010-09-08
  • 打赏
  • 举报
回复

use MY_DB
go
----2005写法
select [name] from sysobjects where xtype ='u' and id in(
select id from syscolumns where [name]='username' or [name]='user_name')

--2000写法,好像是有一个字段名字不一样

select [name] from sysobjects where type ='u' and id in(
select id from syscolumns where [name]='username' or [name]='user_name')

hao1hao2hao3 2010-09-08
  • 打赏
  • 举报
回复


select [name] from sysobjects where xtype ='u' and id in(
select id from syscolumns where [name]='username' or [name]='user_name')

34,576

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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