DROP TABLE TMP
CREATE TABLE TMP
(
ID INT,
TEST VARCHAR(10) DEFAULT ''
)
GO
INSERT INTO TMP(ID) SELECT 1 UNION SELECT 2 UNION SELECT 3
INSERT INTO TMP(ID, TEST) SELECT 4, 'AB' UNION SELECT 5, 'CD'
GO
DECLARE @ConstraintName varchar(200)
DECLARE @SQLSTR varchar(2000)
SELECT @ConstraintName=OBJECT_NAME(constid) FROM syscolumns AS A LEFT JOIN sysCONSTRAINTs AS B
ON A.ID=B.ID AND A.colid=B.colid
--WHERE object_name(a.id)= '表名 'and a.name= '列名'
WHERE object_name(a.id)= 'TMP 'and a.name= 'TEST'
SET @SQLSTR='ALTER TABLE TMP DROP CONSTRAINT '+@ConstraintName
EXEC(@SQLSTR)
GO
UPDATE TMP SET TEST=NULL
ALTER TABLE TMP ALTER COLUMN TEST INT
CREATE TABLE TMP
(
ID INT,
TEST VARCHAR(10) DEFAULT ''
)
GO
INSERT INTO TMP(ID) SELECT 1 UNION SELECT 2 UNION SELECT 3
INSERT INTO TMP(ID, TEST) SELECT 4, 'AB' UNION SELECT 5, 'CD'
GO
DECLARE @ConstraintName varchar(200)
DECLARE @SQLSTR varchar(2000)
SELECT @ConstraintName=OBJECT_NAME(constid) FROM syscolumns AS A LEFT JOIN sysCONSTRAINTs AS B
ON A.ID=B.ID AND A.colid=B.colid
--WHERE object_name(a.id)= '表名 'and a.name= '列名'
WHERE object_name(a.id)= 'TMP 'and a.name= 'TEST'
SET @SQLSTR='ALTER TABLE TMP DROP CONSTRAINT '+@ConstraintName
EXEC(@SQLSTR)
GO
UPDATE TMP SET TEST=NULL
ALTER TABLE TMP ALTER COLUMN TEST INT