Kaj*_*aja 9 sql t-sql sql-server-2012
我有一个表,它有一个列(orderid),其IDENTITY设置为true.现在我想把它关掉.如何使用ALTER COLUMN做到这一点?有点像这样吗?
ALTER TABLE MyTable
ALTER Column MyColumn SET IDENTITY OFF
Run Code Online (Sandbox Code Playgroud)
设置标识列后,您无法将其删除,也无法将其设置为OFF.
您可能必须首先将数据复制到其他列(没有标识)中来删除列.因此,就像在表中添加一个新列并将现有标识列的值复制到其中.然后删除旧列(具有标识),最后将新列重命名为旧列名.
您必须使用SET IDENTITY_INSERT TO ON.如果将其设置为ON,则应显式将值传递给ID列.
你为什么要关掉身份?可能是你试图传递一些明确的价值观.
请参考此处的示例演示.
-- Create tool table.
CREATE TABLE dbo.Tool
(
ID INT IDENTITY NOT NULL PRIMARY KEY,
NAME VARCHAR(40) NOT NULL
);
GO
-- Inserting values into products table.
INSERT INTO dbo.Tool
(NAME)
VALUES ('Screwdriver'),
('Hammer'),
('Saw'),
('Shovel');
GO
-- Create a gap in the identity values.
DELETE dbo.Tool
WHERE NAME = 'Saw';
GO
SELECT *
FROM dbo.Tool;
GO
-- Try to insert an explicit ID value of 3;
-- should return a warning.
INSERT INTO dbo.Tool
(ID,
NAME)
VALUES (3,
'Garden shovel');
GO
-- SET IDENTITY_INSERT to ON.
SET IDENTITY_INSERT dbo.Tool ON;
GO
-- Try to insert an explicit ID value of 3.
INSERT INTO dbo.Tool
(ID,
NAME)
VALUES (3,
'Garden shovel');
GO
SELECT *
FROM dbo.Tool;
GO
-- Drop products table.
DROP TABLE dbo.Tool;
GO
Run Code Online (Sandbox Code Playgroud)