DECLARE
@
NAME
NVARCHAR(255);
DECLARE
@CUR
CURSOR
SET
@CUR=
CURSOR
SCROLL
DYNAMIC
FOR
SELECT
NAME
FROM
DBO.SYSOBJECTS
WHERE
NAME
NOT
IN
(
'SYSCONSTRAINTS'
,
'SYSSEGMENTS'
)
AND
(
OBJECTPROPERTY(ID, N
'IsView'
) = 1
OR
OBJECTPROPERTY(ID,N
'IsProcedure'
) = 1
OR
OBJECTPROPERTY(ID,N
'IsScalarFunction'
) = 1
OR
OBJECTPROPERTY(ID,N
'IsTableFunction'
) = 1
OR
OBJECTPROPERTY(ID,N
'IsInlineFunction'
) = 1
);
OPEN
@CUR;
FETCH
NEXT
FROM
@CUR
INTO
@
NAME
WHILE (@@FETCH_STATUS=0)
BEGIN
DECLARE
@OldText NVARCHAR(
MAX
);
DECLARE
@NewText NVARCHAR(
MAX
);
SELECT
@OldText=@OldText +
CHAR
(10) +
CHAR
(13) + RTRIM(TEXT)
FROM
SYSCOMMENTS
WHERE
ID = OBJECT_ID(@
NAME
);
SET
@NewText=
REPLACE
(@OldText,N
'CREATE VIEW'
,N
'ALTER VIEW'
);
SET
@NewText=
REPLACE
(@NewText,N
'CREATE PROCEDURE'
,N
'ALTER PROCEDURE'
);
SET
@NewText=
REPLACE
(@NewText,N
'CREATE FUNCTION'
,N
'ALTER FUNCTION'
);
BEGIN
TRY
EXEC
(@NewText);
END
TRY
BEGIN
CATCH
PRINT N
'---------------------------------------------------------------------------'
;
PRINT @
NAME
+ N
' : '
+ ERROR_MESSAGE();
PRINT N
'---------------------------------------------------------------------------'
;
END
CATCH
FETCH
NEXT
FROM
@CUR
INTO
@
NAME
END
CLOSE
@CUR;
DEALLOCATE
@CUR;