动态为表添加存储时间字段
程序员文章站
2023-01-28 23:16:36
下面这篇《动态为表添加存储时间字段》https://www.cnblogs.com/insus/p/10943614.html 添加字段,只能添加时间类型的字段。想把这个方法改良一下,尽量能做到通用。 SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ......
下面这篇《动态为表添加存储时间字段》
添加字段,只能添加时间类型的字段。想把这个方法改良一下,尽量能做到通用。
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
举例使用: