CREATE TABLE testA (
id INT,
name varchar(10),
root INT
);
go
INSERT INTO testA
SELECT 1, 'a', 0 UNION ALL
SELECT 2, 'a1', 1 UNION ALL
SELECT 3, 'a2', 1 UNION ALL
SELECT 4, 'a3', 1;
GO
CREATE PROCEDURE Test_Rename
@id INT,
@name varchar(10)
AS
BEGIN
-- 定义变量, 暂存旧数据.
DECLARE @oldName varchar(10);
-- 根据 @id 查询旧数据.
SELECT @oldName = name FROM testA WHERE id = @id;
-- 如果数据不存在, 直接返回.
IF @@rowcount = 0
BEGIN
RETURN;
END;
-- 更新根节点.
UPDATE testA SET name = @name WHERE id = @id;
-- 更新子节点.
UPDATE testA SET name = REPLACE(name, @oldname, @name) WHERE root = @id;
END;
GO
-- 测试执行
EXECUTE Test_Rename 1, 'B';
GO
-- 数据核对.
SELECT * FROM testA
GO
id name root
----------- ---------- -----------
1 B 0
2 B1 1
3 B2 1
4 B3 1
(4 行受影响)
update 表名 set name = REPLACE(name, 'a', 'b') where root=1;
希望能够帮到你
repalce 函数 有什么问题?