-- 테이블, 컬럼(특정) Default 체크
DECLARE @tablename varchar(100)
DECLARE @columnname varchar(100)
set @tablename = 테이블
set @columnname = 컬럼
select d.*
from sys.default_constraints d
inner join sys.tables t on t.object_id = d.parent_object_id
inner join sys.columns c on c.object_id = t.object_id and c.column_id = d.parent_column_id
where t.name = @tablename
and c.name = @columnname
;
-- 테이블, 컬럼(특정) Default 삭제
DECLARE @tablename varchar(100)
DECLARE @columnname varchar(100)
DECLARE @conname varchar(100)
set @tablename = 테이블
set @columnname = 컬럼
SET @conname = (select d.name
from sys.default_constraints d
inner join sys.tables t on t.object_id = d.parent_object_id
inner join sys.columns c on c.object_id = t.object_id and c.column_id = d.parent_column_id
where t.name = @tablename
and c.name = @columnname
)
exec ('ALTER TABLE [' + @tablename + '] DROP CONSTRAINT [' + @conname + ']' )
-- 테이블, 컬럼(특정) Default 변경
DECLARE @tablename varchar(100)
DECLARE @columnname varchar(100)
DECLARE @defaultold varchar(100)
DECLARE @conname varchar(100)
set @tablename = 테이블
set @columnname = 컬럼
set @defaultold = 변경 대상 Default 값
if EXISTS (select 'X' from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = @tablename and COLUMN_NAME = @columnname and COLUMN_DEFAULT like '(' + @defaultold + '%)') begin
SET @conname = (
select d.name
from sys.default_constraints d
inner join sys.tables t on t.object_id = d.parent_object_id
inner join sys.columns c on c.object_id = t.object_id and c.column_id = d.parent_column_id
where t.name = @tablename
and c.name = @columnname
)
exec ('ALTER TABLE [' + @tablename + '] DROP CONSTRAINT [' + @conname + ']')
end
if isnull((select COLUMN_DEFAULT from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = @tablename and COLUMN_NAME = @columnname), '') = '' begin
exec ('ALTER TABLE [' + @tablename + '] ADD DEFAULT CONVERT([char](8),getdate(),(112))+replace(CONVERT([char](8),getdate(),(108)),'':'','''') FOR ' + @columnname)
end
- 빨간색은 알아서 잘.. 변경해서..
'공부방 > DB' 카테고리의 다른 글
[PostgreSQL] 세션에 걸려있는 프로세스 강제종료 하기 (0) | 2021.03.29 |
---|---|
[PostgreSQL] Owner 변경 (0) | 2019.08.26 |
[PostgreSQL] Lock 확인, 삭제 (0) | 2018.04.06 |
[MS-SQL] DB 정보 (용량) 및 TABLE 용량, 건수 확인 Query (0) | 2017.03.23 |
[MS-SQL] T-SQL 테이블 변수로 SELECT한 결과 값 접근하기 (0) | 2016.03.07 |