您好,登錄后才能下訂單哦!
這篇文章給大家介紹Sqlserver中怎么修改在線表的表字段類型,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。
原本很多字段都是nvarchar類型,因?yàn)閚varchar類型的字段無法添加索引,所以要在線把表的字段類型修改為varchar。
執(zhí)行腳本如下:
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_events
(
id int NOT NULL IDENTITY (1, 1),
transaction_id varchar(255) NULL,
trans_no varchar(255) NULL,
broker varchar(32) NULL,
code varchar(100) NULL,
consumer_id varchar(255) NULL,
consumer_code varchar(100) NULL,
consumer_name nvarchar(255) NULL,
description varchar(100) NULL,
comment nvarchar(255) NULL,
execution_group varchar(50) NULL,
input_ts bigint NOT NULL,
output_ts bigint NOT NULL,
provider_id varchar(100) NULL,
provider_code varchar(50) NULL,
provider_name nvarchar(255) NULL,
request_body nvarchar(MAX) NULL,
request_header nvarchar(MAX) NULL,
request_ts bigint NOT NULL,
response_body nvarchar(MAX) NULL,
response_header nvarchar(MAX) NULL,
response_ts bigint NOT NULL,
service_id varchar(50) NULL,
service_code varchar(50) NULL,
service_name nvarchar(255) NULL,
service_version varchar(50) NULL,
service_url varchar(255) NULL,
category_name varchar(50) NULL,
source varchar(50) NULL,
type varchar(50) NULL
) ON [PRIMARY]
TEXTIMAGE_ON [PRIMARY]
GO
ALTER TABLE dbo.Tmp_events SET (LOCK_ESCALATION = TABLE)
GO
SET IDENTITY_INSERT dbo.Tmp_events ON
GO
IF EXISTS(SELECT * FROM dbo.events)
EXEC('INSERT INTO dbo.Tmp_events (id, transaction_id, trans_no, broker, code, consumer_id, consumer_code, consumer_name, description, comment, execution_group, input_ts, output_ts, provider_id, provider_code, provider_name, request_body, request_header, request_ts, response_body, response_header, response_ts, service_id, service_code, service_name, service_version, service_url, category_name, source, type)
SELECT id, CONVERT(varchar(255), transaction_id), CONVERT(varchar(255), trans_no), CONVERT(varchar(32), broker), CONVERT(varchar(100), code), CONVERT(varchar(255), consumer_id), CONVERT(varchar(100), consumer_code), CONVERT(nvarchar(255), consumer_name), CONVERT(varchar(100), description), CONVERT(nvarchar(255), comment), CONVERT(varchar(50), execution_group), input_ts, output_ts, CONVERT(varchar(100), provider_id), CONVERT(varchar(50), provider_code), CONVERT(nvarchar(255), provider_name), request_body, request_header, request_ts, response_body, response_header, response_ts, CONVERT(varchar(50), service_id), CONVERT(varchar(50), service_code), CONVERT(nvarchar(255), service_name), CONVERT(varchar(50), service_version), CONVERT(varchar(255), service_url), CONVERT(varchar(50), category_name), CONVERT(varchar(50), source), CONVERT(varchar(50), type) FROM dbo.events WITH (HOLDLOCK TABLOCKX)')
GO
SET IDENTITY_INSERT dbo.Tmp_events OFF
GO
DROP TABLE dbo.events
GO
EXECUTE sp_rename N'dbo.Tmp_events', N'events', 'OBJECT'
GO
ALTER TABLE dbo.events ADD CONSTRAINT
PK__esb_even__3213E83FA9B6FB29 PRIMARY KEY CLUSTERED
(
id
) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
COMMIT
可以看出,sqlserver進(jìn)行修改表字段類型其實(shí)就是建立一個(gè)新表,把老表的數(shù)據(jù)轉(zhuǎn)換類型后插入到新表,然后drop掉老表。
再對(duì)新表的字段添加索引即可。
因?yàn)檎麄€(gè)過程是在一個(gè)事務(wù)中進(jìn)行的,所以會(huì)鎖表。
測(cè)試下來。
在非業(yè)務(wù)繁忙期的一個(gè)在線表369W條左右的數(shù)據(jù)量,整個(gè)在線修改表字段類型消耗了14分32秒。
關(guān)于Sqlserver中怎么修改在線表的表字段類型就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。