create procedure sys.sp_MSgetbeforetableinsert
@objid int,
@inscommand nvarchar(2000) output
AS
declare @before_objid int
declare @before_name sysname
declare @collist nvarchar(1000)
declare @colname sysname
declare @guidstr nvarchar(36)
declare @beforeview sysname
declare @retcode int
declare @artid uniqueidentifier
-- Do we have a before table?
select @before_objid = max(before_image_objid) from dbo.sysmergearticles where objid = @objid and
before_image_objid is not null
select @before_name = OBJECT_NAME(@before_objid)
if @before_name is null
begin
set @inscommand = ''
return 0
end
select @artid = artid from sysmergearticles where objid = @objid
exec @retcode=sp_MSguidtostr @artid, @guidstr out
if @retcode<>0 or @@ERROR<>0 return (1)
set @beforeview = 'MSmerge_bivw_' + @guidstr
set @collist = ''
-- Loop over columns to make the column list for the insert / select command
declare col_cursor CURSOR LOCAL FAST_FORWARD for select name from sys.columns
where object_id = @before_objid and name <> 'generation' and name <> 'system_delete' order by column_id
FOR READ ONLY
open col_cursor
fetch next from col_cursor into @colname
while (@@fetch_status <> -1)
begin
set @collist = @collist + QUOTENAME(@colname) + ', '
fetch next from col_cursor into @colname
end
close col_cursor
deallocate col_cursor
-- Our list has all of the columns except generation since that gets set to a local variable
-- Make the insert command
set @inscommand = 'insert into ' + QUOTENAME(@beforeview) + ' ( ' + @collist +
' generation) select ' + @collist + ' @newgen from deleted'
return 0