• Surely we don't really need the dynamic SQL in there, we can just use:

    declare @newName sysname

    ...

    set @newName = @childname + '_' + @parentname + '_' + CAST(@soid as varchar(20)) + '_FK'

    exec sp_rename @name, @newName

    ...

    (And we don't really need the variable I added, it's just a bit neater).

    We probably also want to have some kind of check for long table names, since sysname is "only" 128 characters (or less in earlier versions of SQL).