当前位置: 移动技术网 > IT编程>数据库>MSSQL > 动态为表添加存储时间字段

动态为表添加存储时间字段

2019年05月30日  | 移动技术网IT编程  | 我要评论

孙艺洲身高,呼和浩特办毕业证,爱q资源

下面这篇《动态为表添加存储时间字段

添加字段,只能添加时间类型的字段。想把这个方法改良一下,尽量能做到通用。

 

set ansi_nulls on
go

set quoted_identifier on
go


-- =============================================
-- author:      insus.net
-- blog:        https://insus.cnblogs.com
-- create date: 2019-05-29
-- update date: 2019-05-29
-- description: 动态为表添加字段
-- =============================================
create procedure [dbo].[usp_dyanmicallyaddcolumn]
(
    @table_catalog sysname,
    @table_schema sysname,
    @table_name sysname,    
    @column_name sysname,
    @datatype sysname
)
as
begin
    if not exists(select top 1 1 from [tempdb].[sys].[columns] where object_id = object_id(@table_catalog + n'.'+ @table_schema + n'.' + @table_name) and [name] = @column_name)
    or not exists(select top 1 1 from [sys].[columns] where object_id = object_id(@table_catalog + n'.'+ @table_schema + n'.' + @table_name) and [name] = @column_name)    
        execute('alter table '+ @table_name +' add '+ @column_name +' ' + @datatype +'')
end
go

 

举例使用:

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网