저장 프로세스 응용 예

2076 단어 저장 프로세스
--  :        ,      
IF OBJECT_ID(N'dbo.RegexReplace') IS NOT NULL   
    DROP FUNCTION dbo.RegexReplace  
GO  
CREATE FUNCTION dbo.RegexReplace  
(  
    @string VARCHAR(MAX),   --         
    @pattern VARCHAR(255),  --      
    @replacestr VARCHAR(255),   --         
    @IgnoreCase INT = 0 --0      1        
)  
RETURNS VARCHAR(8000)  
AS   
BEGIN  
    DECLARE @objRegex INT, @retstr VARCHAR(8000)  
    --      
    EXEC sp_OACreate 'VBScript.RegExp', @objRegex OUT  
    --      
    EXEC sp_OASetProperty @objRegex, 'Pattern', @pattern  
    EXEC sp_OASetProperty @objRegex, 'IgnoreCase', @IgnoreCase  
    EXEC sp_OASetProperty @objRegex, 'Global', 1  
    --    
    EXEC sp_OAMethod @objRegex, 'Replace', @retstr OUT, @string, @replacestr  
    --    
    EXECUTE sp_OADestroy @objRegex  
    RETURN @retstr  
END  
GO 

sp_configure 'show advanced options', 1;
GO
RECONFIGURE;
GO
sp_configure 'Ole Automation Procedures', 1;
GO
RECONFIGURE;
GO

IF OBJECT_ID ( 'tester2', 'P' ) IS NOT NULL 
    DROP PROCEDURE tester2;
GO
CREATE PROCEDURE tester2
AS
BEGIN
	DECLARE Employee_Cursor CURSOR FOR
		SELECT tableName FROM MD_TreeTableStruct WHERE Type = '1';
	DECLARE @tableName1 varchar(50);
	OPEN Employee_Cursor;	
	FETCH NEXT FROM Employee_Cursor INTO @tableName1;
	WHILE @@FETCH_STATUS = 0
	BEGIN
		print @tableName1;
	  exec('update ' + @tableName1 + ' set innerCode = dbo.RegexReplace (innercode, ''([0-9][0-9][0-9])'',''00$1'',0)');
      FETCH NEXT FROM Employee_Cursor INTO @tableName1;
	END;
	CLOSE Employee_Cursor;
	DEALLOCATE Employee_Cursor;	    
END
GO

exec tester2;
GO

좋은 웹페이지 즐겨찾기