加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

根据表批量生成视图(SQL Server和MYSQL)

(2013-12-03 17:01:58)
标签:

根据表批量创建视图

批量创建视图sql

根据表获取字段

sql查询表名和字段名

分类: 数据库学习

    在实际的需求处理中,遇到需要将当前库中某一批含有指定字段的表在另一个库中生成对应的视图。由于含有指定字段的表数据比较多,不可能一个一个的来生成对应的视图,因此需要采用批量生成视图的方法来处理。
    采用该方法批量生成创建视图的SQL语句后,在指定库下执行即可。处理过程中提供了基于SQL Server数据库和MYSQL数据库下的两种处理方法,详细的处理过程如下:

 

一、SQL Server数据库中

--1、获取该批表中含有指定字段的表名称和字段名称,放入表t_tmp01

--drop  table t_tmp01
create table t_tmp01(id int identity(1,1),tab_name varchar(80),col_name varchar(20))
insert  into t_tmp01(tab_name,col_name)
select  a.name,b.name
from   sysobjects a,syscolumns b 
where  a.id=b.id 
and b.colorder in(1,2)  --第一列或者第二列
and   b.name in('insert_dt','state_dt','begin_dt','开始日期','开始时间','日期','时间')  --指定字段类型
and  a.type='u'  
and     a.name like 't_state%'
and a.name not like '0%'  --排除以时间作为标识的临时表
and a.name not like '�k%'  --排除以'bak'作为标识的临时表

select * from t_tmp01

 

--2、查看含有指定字段的表名,排除可能忽略的情况
select  distinct a.name,b.name
from   sysobjects a,syscolumns b 
where  a.id=b.id 
and b.colorder in(1,2)  --第一列或者第二列
and   b.name in('insert_dt','state_dt','begin_dt','开始日期','开始时间','日期','时间')  --指定字段类型
and  a.type='u'  
and     a.name like 't_state%'
and a.name not like '0%'  --排除以时间作为标识的临时表
and a.name not like '�k%'  --排除以'bak'作为标识的临时表
order by a.name

 

--3、创建视图脚本,批量生成创建视图SQL语句
set nocount on
declare @i int,@cnt int
declare @tab_name varchar(100)
declare @sql varchar(1000)
declare @tab_tmp table(sqlstr varchar(2000))

set @i=1

select  @cnt=max(id)
from  t_tmp01

while  @i<<A href="mailto:=@cnt">=@cnt
begin
select @tab_name=tab_name
from  t_tmp01
where id=@i

select @sql='CREATE view '+stuff(@tab_name,1,1,'v')+'
as 
select * from mytest.dbo.'+@tab_name+' with(nolock) where '+col_name+' >= dateadd(mm , -3 , getdate())  '+char(10)+'Go'
from t_tmp01
where id=@i

select @i=@i+1
insert into @tab_tmp select @sql
end

select  * from @tab_tmp

 

注意:这里存在一个问题,在查询分析器中处理时需要以“文本显示结果”,才能实现换行处理;如果以“表格显示结果”时不能实现换行处理,从而导致后面批量执行创建视图脚本时报错。

 

二、Mysql数据库中
-- 一、获取该批表中含有指定字段的表名称和字段名称,放入表t_tmp02
drop table t_tmp02;

create  table t_tmp02
select table_name as table_name,column_name as table_column
from information_schema.COLUMNS
where table_schema='mytest' -- 数据库名
and  ordinal_position in(1,2)  -- 第一列或者第二列
and table_name like 't_state%'
and column_name in ('insert_dt','state_dt','begin_dt','开始日期','开始时间','日期','时间')
and table_name not like '0%'  -- 排除以时间作为标识的临时表
and table_name not like '�k%'; -- 排除以'bak'作为标识的临时表

select * from t_tmp02 order by table_column;

 

-- 二、查看含有指定字段的表名,排除可能忽略的情况
select distinct table_name
from information_schema.COLUMNS
where table_schema='mytest'
and table_name like 't_state%'
and table_name not in(select table_name from t_tmp02)
and table_name not like '0%'
and table_name not like '�k%'
order by table_name;

 

-- 三、创建视图脚本,批量生成创建视图SQL语句
select CONCAT('create view v_',SUBSTR(table_name,3,length(table_name)),' as select * from mytest.',table_name,' where ',table_column,'>= date_add(now(),INTERVAL -3 MONTH);')
from t_tmp02;

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有