動態爲表添加字段 動態爲表添加存儲時間字段

下面這篇《動態爲表添加存儲時間字段http://www.javashuo.com/article/p-kfivrsds-v.htmlhtml

添加字段,只能添加時間類型的字段。想把這個方法改良一下,儘可能能作到通用。ide

 

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
Source Code

 

舉例使用:post

相關文章
相關標籤/搜索