动态更改所有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类型列条件已注释。我的脚本将仅更改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