我发现有几个列分布在十几个SQL Server 2014数据库中,这些数据库是不推荐使用的数据类型text
和ntext
。所以我想把这些改成nvarchar(max)
像这样:
ALTER TABLE <mytable>
ALTER COLUMN <mycolumn> NVARCHAR(MAX)
确切的陈述是:
ALTER TABLE contacts
ALTER COLUMN notes0 NVARCHAR(MAX)
我在一个循环中编写了一个脚本。对于大约170列,这很好,但有9列留下产生错误消息:
错误596,无法继续执行,因为会话处于kill状态
德文版消息: 消息596,级别21,状态1,行0 执行无法继续,因为会话具有KILL状态。
消息0,级别20,状态0,行0 当前命令发生致命错误。删除任何结果。)
然后我尝试在此表中添加一个新的nvarchar(max)
列并复制其中的数据,这会产生相同的结果。
所以我用一组类似的列创建了一个新表,只需要适当的数据类型并将数据复制到它=>这个工作正常!
但我不能只重命名这两个表来将新表放在旧表的位置,因为索引不适合。应用程序的名称是硬编码的(是的,我知道....)
所以我试图将数据从新表复制回我添加到旧表中的新列。结果相同。
我仍然需要更改数据类型或将数据移动到nvarchar(max)
的新列。我现在能做什么?
Nils,提前谢谢你
-- =============================================
-- Author: NSc
-- Create date: 180918
-- Description: sucht alle TEXT und NTEXT-Spalten und konvertiert sie in
NVARCHAR(MAX)
-- =============================================
CREATE PROCEDURE [dbo].[ConvertAllDeprecatedTEXTColumns2NvarcharMAX]
AS
BEGIN
SET NOCOUNT ON;
CREATE TABLE #DB
(
id INT,
databaseID INT NOT NULL,
DatabaseName NVARCHAR(50),
IsCobra BIT,
AnzahlAdressen INT
)
CREATE TABLE #CobraTables
(
id INT IDENTITY (1,1) NOT NULL,
DatabaseName NVARCHAR(50),
CobraTableName NVARCHAR(50),
CobraTableID INT,
SQLTableName NVARCHAR(50),
AnzahlZeilen INT
)
CREATE TABLE #CobraColumns
(
id INT IDENTITY (1,1) NOT NULL,
DatabaseName NVARCHAR(50),
CobraTableName NVARCHAR(50),
SQLTableName NVARCHAR (50),
CobraColumnName NVARCHAR(100),
SQLColumnName NVARCHAR(50),
AnzahlZeilen INT,
ColumnType NVARCHAR(50)
)
CREATE TABLE #SQL
(
id INT IDENTITY NOT NULL,
SQLtext NVARCHAR(1000)
)
INSERT INTO #DB (id, databaseID, DatabaseName, IsCobra)
EXEC CheckIfCobraDB
DELETE FROM #DB
WHERE IsCobra = 0
OR DatabaseName = 'cobra_main'
OR DatabaseName LIKE '%DEV%'
OR DatabaseName LIKE '%demo%'
OR DatabaseName LIKE '%invent%'
-- Tabellennamen ermitteln
INSERT INTO #SQL
SELECT
'INSERT INTO #CobraTables (DatabaseName, CobraTablename, SQLTablename, CobraTableID) SELECT ' + CHAR (39) + databasename + CHAR(39) + ', [Userlevel_Name], [Logical_Name], [ID] FROM [' + databasename + '].dbo.[AP_Tables]'
FROM #DB
DECLARE @i INT
SET @i = 1
DECLARE @j INT
SET @j = (SELECT MAX(id) FROM #SQL)
DECLARE @SQL NVARCHAR(1000)
WHILE @i <= @j
BEGIN
SET @SQL = (SELECT SQLtext FROM #SQL WHERE id = @i)
EXEC sp_executesql @SQL
SET @i = (@i + 1)
END
-- select * from #SQL
-- select * from #CobraTables
-- Spalten mit abgekündigten Text-Datentypen finden
DELETE FROM #SQL
DBCC CHECKIDENT (#SQL, reseed, 1)
-- insert into #SQL select 'insert into #CobraColumns (Databasename, cobratablename, sqltablename, cobracolumnname, sqlcolumnname) select distinct' + char (39) + databasename + char (39) + ', ' + char (39) + cobratablename + char (39) + ', ' + char (39) + SQLTableName + char (39) + ', userlevel_name, logical_name from [' + databasename + '].dbo.[AP_Fieldinfos] where len([virtual_field_def]) = 0 and logical_name not like (' + CHAR (39) + '%ID%' + CHAR (39) + ') and [Table_ID] = ' + CAST (CobraTableID as nvarchar (10)) from #CobraTables where SQLTableName not in ('Links', 'linklist', 'images','keywords','keywordlist', 'symbollist') and AnzahlZeilen > 0 order by DatabaseName, CobraTableName
insert into #SQL select 'use ['+ databasename + ']; insert into #CobraColumns (Databasename, SQLtablename, SQLcolumnname, ColumnType) select ' + CHAR (39) + databasename + char (39) + ', o.name, c.name , t.name from sys.all_columns as c inner join sys.types as t on t.system_type_id=c.system_type_id inner join sys.tables as o on o.object_id=c.object_id where t.name like ' + CHAR (39) + '%text%' + CHAR (39) + ' order by o.name' from #DB
-- select * from #SQL
SET @i = 1
SET @j = (SELECT MAX(id) FROM #SQL)
WHILe @i<=@j
BEGIN
SET @SQL = (SELECT SQLtext FROM #SQL WHERE id = @i)
EXEC sp_executesql @SQL
SET @i = (@i + 1)
END
SELECT * FROM #CobraColumns
-- delete from #CobraColumns where SQLTableName= 'Contacts' and SQLColumnName like 'NOTES%' -- and DatabaseName = 'BCM'
-- Spalten konvertieren
DELETE FROM #SQL
DBCC CHECKIDENT (#SQL, reseed, 1)
INSERT INTO #SQL
SELECT 'ALTER TABLE [' + DatabaseName + '].dbo.[' + SQLTableName + '] ALTER COLUMN ' + SQLColumnName + ' NVARCHAR(MAX)'
FROM #CobraColumns
SELECT * FROM #SQL
SET @i = 1
SET @j = (SELECT MAX(id) FROM #SQL)
-- /***
WHILE @i <= @j
BEGIN
SET @SQL = (SELECT SQLtext FROM #SQL WHERE id = @i)
PRINT @SQL
EXEC sp_executesql @SQL
SET @i = (@i + 1)
END
-- ***/
END
我以前遇到过这个问题,我在这篇文章中找到了答案,https://www.sqlservercentral.com/Forums/1809734/CHECKDB--Cannot-continue-the-execution-because-the-session-is-in-the-kill-state
此错误消息是由HAG(高可用性组)SQL Server从一个节点故障转移到另一个节点引起的。如果HAG处于同步提交模式,则在将主节点写入辅助节点之前,无法在主节点上提交日志条目。如果计算机之间的心跳失败,并且服务器尝试进行故障转移,则必须确保两个节点都处于一致状态。数据库从“已同步”变为“解析”模式,并且回滚主节点上尚未提交到辅助节点的所有事务。此回滚是为您提供错误消息的原因,因为自动回滚状态与您对SPID发出kill命令的状态相同。原因通常与网络相关,特别是如果节点在地理上是分开的,远程站点的DR副本就是这种情况。如果他们不是那么检查本地NIC和交换机。 Microsoft建议的解决方案是增加LeaseTimeout和/或HealthChecktimeout值。
我做的第一件事是增加磁盘大小,然后增加HealthChecktimeout,它就消失了。