모든 proc 및 udf를 동적으로 변경하여 값 변경
DECLARE @Text NVARCHAR(max),
@spname NVARCHAR(max),
@Type CHAR(5),
@Sql NVARCHAR(max)
DECLARE @getobject CURSOR
SET @getobject = CURSOR
FOR SELECT sc.text,
so.NAME,
so.type
FROM sys.syscomments sc
INNER JOIN sysobjects so
ON sc.id = so.id
WHERE sc.[text] LIKE '%getdate()%'
--and type in('P','FN')
OPEN @getobject
FETCH next FROM @getobject INTO @Text, @spname, @Type
WHILE @@FETCH_STATUS = 0
BEGIN
IF ( @Type = 'P'
OR @Type = 'FN' )
SET @Text = Replace(@Text, 'getdate', 'dbo.getlocaldate')
SET @Text = Replace(@Text, 'create', 'alter')
EXECUTE Sp_executesql
@Text
PRINT @Text
--,@spname,@Type
FETCH next FROM @getobject INTO @Text, @spname, @Type
END
CLOSE @getobject
DEALLOCATE @getobject
CREATE PROCEDURE [dbo].[Testproc1]
AS
SET nocount ON;
BEGIN
DECLARE @CurDate DATETIME = Getdate()
END
주의 사항은 sysobjects Type 열 조건을 주석 처리했습니다. 내 스크립트는 proc 및 UDF 만 변경합니다.
이 스크립트는 Default Constraint
포함하는 모든 것을 변경 합니다GetDate()
DECLARE @TableName VARCHAR(300),
@constraintName VARCHAR(300),
@colName VARCHAR(300),
@Sql NVARCHAR(max)
DECLARE @getobject CURSOR
SET @getobject = CURSOR
FOR SELECT ds.NAME,
sc.NAME AS colName,
so.NAME AS Tablename
--,ds.definition
FROM sys.default_constraints ds
INNER JOIN sys.columns sc
ON ds.object_id = sc.default_object_id
INNER JOIN sys.objects so
ON so.object_id = ds.parent_object_id
WHERE definition LIKE '%getdate()%'
OPEN @getobject
FETCH next FROM @getobject INTO @constraintName, @colName, @TableName
WHILE @@FETCH_STATUS = 0
BEGIN
SET @Sql = 'ALTER TABLE ' + @TableName
+ ' DROP CONSTRAINT ' + @constraintName + '; '
+ Char(13) + Char(10) + ' ' + Char(13) + Char(10) + ''
SET @Sql = @Sql + ' ALTER TABLE ' + @TableName
+ ' ADD CONSTRAINT ' + @constraintName
+ ' DEFAULT dbo.GetLocaledate() FOR '
+ @colName + ';' + Char(13) + Char(10) + ' ' + Char(13)
+ Char(10) + ''
PRINT @Sql
EXECUTE sys.Sp_executesql
@Sql
--,@spname,@Type
FETCH next FROM @getobject INTO @constraintName, @colName, @TableName
END
CLOSE @getobject
DEALLOCATE @getobject