ms sql server 生成表的视图

--select * from sys.types



declare @table_name nvarchar(100)
set @table_name='EC_Product'
if exists (select 1 from sys.tables where name=@table_name)
begin
declare @table_info table(id int identity(1,1) not null,info nvarchar(300))
insert into @table_info select 'create view V_'+@table_name+'_Diff'
insert into @table_info select 'as' 
insert into @table_info
select 'select '
insert into @table_info
select case when isnullable=0 then tablename+','       else tablename+','+default_value+') as '+name+','  end 
from (select a.isnullable,a.name,b.xtype,case when a.isnullable =0 then '  ['+a.name+']' else '  ISNULL('+a.name end as tablename,case when b.xtype in (48,52,56,127) then '100' 
--tinyint\smallint\int\bigint     when b.xtype in (59,60,62,106,108) then '1.01'
--real\money\float\decimal\numeric     

when b.xtype=40 then '''1900-01-01''' --date     
when b.xtype=41 then '''01:00:01''' --time     
when b.xtype=58 then '''1900-01-01 01:01:01''' --smalldatetime     
when b.xtype=61 then '''1900-01-01 01:01:01.001''' --datetime   
when b.xtype in(60,62) then '0.0' --money 
when b.xtype in(104,108) then '1' --money     
else '''@#''' end as default_value
from syscolumns a,systypes b   where a.id = OBJECT_ID(@table_name) and a.xtype = b.xusertype    )a where xtype not in(34,35,99,165,173,189,241)
--xml\timestamp\binary\varbinary\ntext\text\image
update @table_info set info=SUBSTRING(info,1,len(info)-1) where id=(select MAX(id) from @table_info)
insert into @table_info values('from dbo.['+@table_name+'] with(nolock)')
select info from @table_info
end
else  
print('Can not find the table '+@table_name)

ms sql server 生成表的视图,在使用 tabdiff 比较部分字段的时候使用
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SQL Server中,视图(View)和(Table)是数据库中的两种不同类型的对象,它们有一些明显的区别。 首先,数据库中存储数据的基本单位,它由一些列和行组成,每列定义了数据的类型和属性,每行示一个实体。中的数据可以通过SQL操作语句进行增删改查。 而视图是虚拟,它是根据一个或多个查询语句定义的,它不存储数据,只提供了一种关于数据的逻辑视图。通过查询视图,可以将多个中的数据整合起来,方便用户进行查询和分析。视图本身可以像一样使用,可以用于数据读取、过滤和连接等操作。 其次,是实际存储数据的物理结构,数据的插入、更新和删除操作会直接影响本身,而视图只是查询语句的封装,对视图进行的操作实际上是对底层的操作。 此外,视图还具有以下特点: 1. 视图可以隐藏底层的细节,保护数据的安全性。用户只能访问视图中允许显示的数据列,而不能直接访问底层。 2. 视图可以简化复杂的查询操作,通过提前对多个进行关联和筛选,减少了用户编写复杂查询语句的工作量。 3. 视图可以提供数据的逻辑展示,通过在视图中定义计算字段和筛选条件,可以生成更有用的数据展示形式。 总的来说,是实际存储数据的对象,而视图是对数据的逻辑示。视图可以整合数据、简化查询和保护数据,是SQL Server中非常常用和重要的对象。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值