Sqlserver——日常维护——在表的指定列后面增加新的字段

有时候给表新增字段,想把新的字段增加到指定的字段后面,sqserver的规则是不允许的。

具体的思路是:重建表,排序字段,再导入数据

下面是示例语句



IF EXISTS ( SELECT  *  FROM tempdb..sysobjects WHERE id = OBJECT_ID('tempdb..#Temp_ColOrder') )
   DROP TABLE #Temp_ColOrder;

--输入参数

DECLARE @Table NVARCHAR(100)='KQHK_log_跨區匯款記錄'--表名

DECLARE @BeforeCol NVARCHAR(100)='HKNEIMA'--指定列名

DECLARE @InsertCol NVARCHAR(100)='TEST_3'--要插入的列名

DECLARE @InsertColType NVARCHAR(100)='INT'--要插入字段的类型

DECLARE @InsertColLength INT=200---要插入列的类型长度 除了('nchar', 'char', 'nvarchar','varchar'),其余类型不用输入

IF NOT EXISTS(SELECT * FROM sys.tables WHERE name=@Table)
BEGIN
    SELECT '不存在表:'+@Table AS N'错误信息'
	RETURN
END

IF NOT EXISTS(SELECT * FROM sys.TABLEs tb LEFT JOIN sys.columns col ON tb.object_id=col.object_id WHERE tb.name=@Table AND col.name=@BeforeCol)
BEGIN
    SELECT '表:'+@Table+' 不存在字段:'+@BeforeCol AS N'错误信息'
	RETURN
END

IF  EXISTS(SELECT * FROM sys.TABLEs tb LEFT JOIN sys.columns col ON tb.object_id=col.object_id WHERE tb.name=@Table AND col.name=@InsertCol)
BEGIN
    SELECT '表:'+@Table+' 已存在字段:'+@InsertCol AS N'错误信息'
	RETURN
END

IF NOT EXISTS(SELECT * FROM sys.types WHERE name=@InsertColType)
BEGIN
    SELECT '不存在数据类型:'+@InsertColType AS N'错误信息'
	RETURN
END


--内部调用参数

DECLARE @BeforeSumCol NVARCHAR(MAX)--插入前所有字段

DECLARE @AfterSumCol NVARCHAR(MAX)--插入后所有字段+类型

DECLARE @SQL NVARCHAR(MAX)--执行语句

SELECT  CONVERT(FLOAT,col.column_id) AS column_id ,
        col.name AS col_name ,
        tp.name AS type_name ,
        col.max_length col_length
		INTO #Temp_ColOrder
FROM    sys.columns col
        LEFT JOIN sys.tables tb ON col.object_id = tb.object_id
        LEFT JOIN sys.types tp ON col.system_type_id = tp.system_type_id
                                  AND tp.user_type_id = col.user_type_id
WHERE   tb.name = @Table;

SET @BeforeSumCol = ( SELECT  Temp.col_name+','
					  FROM    #Temp_ColOrder Temp FOR XML PATH('')
					);

SET @BeforeSumCol=SUBSTRING(@BeforeSumCol,1,LEN(@BeforeSumCol)-1)

INSERT INTO #Temp_ColOrder
VALUES  ( (SELECT column_id+0.5 FROM #Temp_ColOrder WHERE col_name=@BeforeCol ) , -- column_id - int
	        @InsertCol , -- col_name - sysname
	        @InsertColType , -- type_name - sysname
	        @InsertColLength  -- col_length - smallint
	    )
	
SET @AfterSumCol = ( SELECT  CASE WHEN Temp.type_name IN ( 'nchar', 'char', 'nvarchar','varchar' )
                            THEN Temp.col_name + ' ' + Temp.type_name +'('+ CASE WHEN Temp.col_length=-1 THEN 'MAX' ELSE CONVERT(NVARCHAR(100),Temp.col_length) END+')'
                            ELSE Temp.col_name + ' ' + Temp.type_name END + ','
					FROM    #Temp_ColOrder Temp
							ORDER BY Temp.column_id
							FOR XML PATH('')
				);

SET @SQL=N'CREATE TABLE TEMP_TEST 
		(
		'+@AfterSumCol+'
		)
		INSERT INTO TEMP_TEST
		(
		'+@BeforeSumCol+'
		)
		SELECT '+@BeforeSumCol+' FROM '+@Table+'

		DROP TABLE '+@Table+'

		EXEC sp_rename '+CHAR(39)+'TEMP_TEST'+CHAR(39)+','+CHAR(39)+@Table+CHAR(39)+'  
		
'

SET XACT_ABORT ON;

BEGIN  TRAN 

EXEC sys.sp_executesql @SQL

COMMIT


 

你可能感兴趣的:(Sqlserver——日常维护——在表的指定列后面增加新的字段)