create procedure sys.sp_MSgentablenickname
@tablenick int output, @nickname int, @objid int
declare @refcnt int
declare @retval int
/* Create a tablenickname from the following formula
** 1. Get the refcnt, use it for the high order digits so
** that processing inserts by ascending tablenickname works well
** and processing deletes by descending tablenickname works well.
** 2. Use a couple of digits from the nickname so that it is less likely
** to collide with tablenickname generated at another merge publisher.
** 3. Increment as necessary to make it unique within the publication and database.
exec @retval = sys.sp_MSrefcnt @objid, @refcnt output
if @retval <> 0 return (1)
if @nickname < 0
set @nickname = 0 - @nickname
/* the biggest value for int is 2147483647, we therefore use another algorithm when
** @refcnt is bigger than 200
if @refcnt < 200
set @tablenick = 1000 * ((@refcnt * 10000) + (@nickname % 10000))
while exists (select * from dbo.sysmergearticles where nickname = @tablenick)
set @tablenick = @tablenick + 1
select @tablenick=max(nickname) + 1 from dbo.sysmergearticles