今天整理以前的资料,看到这个东西,挺偏门,记录下备忘吧。感谢园子里的朋友们。
DECLARE @ConsName VARCHAR(1000) , @tableName VARCHAR(1000) , @colName VARCHAR(1000) SELECT @tableName = N'myOrder' , @colName = 'ShipCountry' SELECT @ConsName = b.name FROM syscolumns a , sysobjects b WHERE a.id = OBJECT_ID('myOrder') AND b.id = a.cdefault AND a.name = @colName AND b.name LIKE 'DF%' EXEC('alter table '+@tableName+ ' drop constraint '+@ConsName) EXEC('ALTER TABLE '+@tableName+ ' ADD CONSTRAINT '+@ConsName+' DEFAULT (N''Italy'') FOR '+@colName)
有更改,那查询就必须有的嘛。
-- ============================================= -- 获取表字段的默认值 -- ============================================= SELECT COLUMN_DEFAULT FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'Employees' -- ============================================= -- 获取有默认值的字段 -- ============================================= SELECT SO.NAME AS Name , SC.NAME AS ColumnName , SM.TEXT AS DefaultValue FROM dbo.sysobjects SO INNER JOIN dbo.syscolumns SC ON SO.id = SC.id LEFT JOIN dbo.syscomments SM ON SC.cdefault = SM.id WHERE SO.xtype = 'U' AND SM.TEXT IS NOT NULL -- AND SO.name='AuctionTstOrder' ORDER BY SO.[name] , SC.colid