SQL如何查询一个数据库中的表

来源:百度知道 编辑:UC知道 时间:2024/05/28 15:03:10
一个库有N个表,如何查询列名为name,并且值是'张三'的所有表?
就是 比如有ABCD四个表 ACD表里都有列名为name的列 AD里的name列里边的值为'张三' 那 所要的查询结果既为表A和表D。
这是个例子 我就是想知道公司数据库里某列的某值分别都在什么表里边
继续谢谢大家 大家继续出主意
数据库为SQLSERVER 2000

六楼的存储过程 运行会出错..

报错如下,我是新手 麻烦大家了:

(所影响的行数为 0 行)

服务器: 消息 105,级别 15,状态 1,行 1
字符串 '张' 之前有未闭合的引号。
服务器: 消息 170,级别 15,状态 1,行 1
第 1 行: '张' 附近有语法错误。

(所影响的行数为 0 行)

服务器: 消息 170,级别 15,状态 1,行 1
第 1 行: '=' 附近有语法错误。
......
-------------------------------------

存储过程和执行语句均为改动 都是按照您提供的复制过去的
创建存储过程没问题 执行存储过程(exec get_table 'name', '张三' ) 就报错了。

第一次回答:
写存储过程或者函数来做。

第二次回答:
过程如下,你还可以完善一下
create procedure get_table
@colname varchar(30),
@colvalue varchar(30),
@coltype varchar(30)=null,
@colformat varchar(30)=null
as
begin
declare @sql varchar(100), @tablename varchar(30)
create table #tables( tablename varchar(30))
declare cur_table cursor for
select name from sysobjects a
where type = 'U' and uid = 1 and exists( select 1 from syscolumns b where b.id = a.id and b.name = @colname)
open cur_table
fetch cur_table into @tablename
WHILE @@FETCH_STATUS = 0 begin
select @sql = 'insert into #tables select'''+ @tablename +''' where exists( select 1 from '+ @tablename +' where '+ @colname +' = '''+ @colvalue +''')'
exec(@sql)
fetch cur_table into @tablename
end
close cur_table
deallocate cur_table
select