Search code examples
sql-servert-sqlstored-proceduresreplication

Adding all articles to a publication using TSQL


I know you can do this easily using the GUI in SSMS. But,is there a way to include all the articles with TSQL instead of doing them one by one using sp_addarticle?

My initial idea is returning all the tables names(using a sys query) and then using a loop to feed them to sp_addarticle. I'm wondering if there's a smarter way for example a built-in variable that I can assign 'all' to it?

enter image description here


Solution

  • This is how I did it

    USE [DatabaseName]
    DECLARE @name sysname
    DECLARE @getid CURSOR
    
    
    SET @getid = CURSOR FOR
    
    -- Select all tables name
    SELECT [name]
    FROM [DataBaseName].[sys].[tables] 
    WHERE is_ms_shipped=0
    
    -- While loop
    OPEN @getid
    FETCH NEXT
    FROM @getid INTO @name
    WHILE @@FETCH_STATUS = 0
    BEGIN
    -- add article 
    exec sp_addarticle  @publication =  @publication
        ,@article =  @name
        ,@source_object = @name
        ,@del_cmd = 'NONE' 
    
    FETCH NEXT
    FROM @getid INTO @name
    END
    
    CLOSE @getid
    DEALLOCATE @getid