TSQL new field into the data table

Abstract: TSQL new field into the data table


You know we've added a field in SSMS UI interface when using the Table, SQL to do what some of the thing?

As will have been understood Script can substantially SQL following steps:

(1) the establishment of a new data table (new Schema).

(2) Insert the old data into the new table data table.

(3) delete the old data tables.

(4) new data table changed its name to the old data tables.

(5) index.

So if you just want a new field added to the data table ' behind' , then do not do with the UI, directly Alter Table to do.

If you are between the data fields must be in the range of a field with a field, then do not use the Alter Table.

Alter Table [tablename] Add [NewClo1] Char(1),[NewCol2] Char(10)

If you do use UI, the entire process Script as follows:

/* 为了避免任何可能发生数据遗失的问题,您应该先详细检视此命令码,然后才能在数据库设计工具环境以外的位置执行。*/
BEGIN TRANSACTION
SET QUOTED_IDENTIFIER ON
SET ARITHABORT ON
SET NUMERIC_ROUNDABORT OFF
SET CONCAT_NULL_YIELDS_NULL ON
SET ANSI_NULLS ON
SET ANSI_PADDING ON
SET ANSI_WARNINGS ON
COMMIT
BEGIN TRANSACTION
GO
CREATE TABLE dbo.Tmp_indextest
	(
	id int NOT NULL IDENTITY (1, 1),
	id1 int NULL,
	id2 int NULL,
	aa nchar(10) NULL,
	name nchar(100) NULL,
	name2 nchar(100) NULL
	)  ON [PRIMARY]
GO
ALTER TABLE dbo.Tmp_indextest SET (LOCK_ESCALATION = TABLE)
GO
SET IDENTITY_INSERT dbo.Tmp_indextest ON
GO
IF EXISTS(SELECT * FROM dbo.indextest)
	 EXEC('INSERT INTO dbo.Tmp_indextest (id, id1, id2, name, name2)
		SELECT id, id1, id2, name, name2 FROM dbo.indextest WITH (HOLDLOCK TABLOCKX)')
GO
SET IDENTITY_INSERT dbo.Tmp_indextest OFF
GO
DROP TABLE dbo.indextest
GO
EXECUTE sp_rename N'dbo.Tmp_indextest', N'indextest', 'OBJECT' 
GO
CREATE UNIQUE CLUSTERED INDEX Cluster_index ON dbo.indextest
	(
	id
	) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX NonCluster_index ON dbo.indextest
	(
	id
	) INCLUDE (id1, id2) 
 WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
COMMIT

But what I want to simplify the process, so write a Script (below), with the Select Into data table to create a new, faster and basically can not produce

Transation Log, the following practice applies only to simple data tables, etc. If your data table is useful Trigger Replication or other functions, you have to be careful.

SET XACT_ABORT ON;
BEGIN TRAN
BEGIN TRY
	SELECT 
		id,
		Cast('' as nchar(20)) AS NewCol1,--加入的新字段1
		Cast('' as char(20)) AS NewCol2,--加入的新字段2
		id1,id2,name,name2 
	INTO DesTable 
	FROM SourceTable WITH (HOLDLOCK TABLOCKX);
	
	EXEC sp_rename N'SourceTable',N'SourceTable_old';
	EXEC sp_rename N'DesTable',N'SourceTable';
END TRY
BEGIN CATCH
	ROLLBACK;
	PRINT 'PROCESS FAIL!';
END CATCH 
IF @@TRANCOUNT > 0 COMMIT;

--完成后!注意权限设定及索引建立,都测试OK后Drop掉旧Table

I am ROCK

[email protected]

Original: Big Box  TSQL new field into the data table


Guess you like

Origin www.cnblogs.com/chinatrump/p/11490801.html