日期:2014-05-18  浏览次数:20560 次

请熟悉存储过程的大神帮帮忙
我想写一个存储过程.用来update某个表,把里面的RecId这列的前缀都改变,表是作为参数传入存储过程.原前缀,新前缀都是作为参数传进来

例如:
RecId
aa00001
aa00002
aa00003

全部改为
bb00001
bb00002
bb00003
SQL code

ALTER PROCEDURE [dbo].[ModifySeq]
    @ProTableName varchar(20),
    @NewPrefix varchar(5),
    @Prefix varchar(5),
    
AS

begin    
    exec ('UPDATE '+ @ProTableName + 'set RecId=REPLACE('+@ProTableName+'.RecId '+','+ @Prefix+','+@NewPrefix+')')
    
END



------解决方案--------------------
SQL code

IF EXISTS (SELECT 1 FROM SYSOBJECTS WHERE name = 'ModifySeq')
BEGIN
    DROP PROC ModifySeq
END
GO
CREATE PROCEDURE [dbo].[ModifySeq]
    @ProTableName varchar(20),
    @NewPrefix varchar(5),
    @Prefix varchar(5)
    
AS

begin    
    EXEC ('UPDATE '+ @ProTableName + ' set RecId=REPLACE('+@ProTableName+'.RecId '+','+ '''' + @Prefix+ '''' + ','+ '''' + @NewPrefix + '''' +')')
    
END

GO