Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 183287
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T15:05:20+00:00 2026-05-11T15:05:20+00:00

I want to create dymamic SQL code to automatically create a table-replication. I’m stuck

  • 0

I want to create dymamic SQL code to automatically create a table-replication.

I’m stuck on how to make the following code dynamic, so that it’s possible to pass in SQL variables and use them in the code. I help myself at the moment with search and replacing the 'TODO:' parts, which is not very nice… Here is the code:

DECLARE @sql VARCHAR(MAX) SET @sql = ''  SELECT   @sql = @sql +           'exec sp_addarticle @publication = ''TODO:TREP_PUBLICATION_NAME'',@article = N''' +           name +           ''', @source_owner = N''dbo'', @source_object = N''' +           name +           ''', @type = N''logbased'', @description = N'''', @creation_script = N'''', @pre_creation_cmd = N''delete'', @schema_option = 0x0000000000000000, @identityrangemanagementoption = N''none'', @destination_table = N''' +           name +           ''', @destination_owner = N''dbo'', @status = 8, @vertical_partition = N''false'', @ins_cmd = N''SQL'', @del_cmd = N''SQL'', @upd_cmd = N''SQL''' FROM   TODO:PUBLICATION_DB.sys.tables WHERE   type      = 'U'   AND name IN (     SELECT        name     FROM       OPENROWSET('SQLOLEDB', 'TODO:SUBSCRIBER_SERVER';                  'TODO:SUBSCRIBER_LOGIN';                  'TODO:SUBSCRIBER_PASSWORD', 'select * from TODO:SUBSCRIBER_DB.sys.tables where type=''U''')   )   AND name IN (     SELECT       TABLE_NAME     FROM       TODO:PUBLICATION_DB.INFORMATION_SCHEMA.TABLE_CONSTRAINTS     WHERE       TODO:TABLE_NAME_FILTER   )  EXEC(@Sql) 

One problem is that I need dynamic SQL inside a dynamic SQL block. If somebody could help me to convert this code using variables I would really appreciate that!

Thanks Daniel

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T15:05:21+00:00Added an answer on May 11, 2026 at 3:05 pm

    Dynamic SQL might not be the answer – but that doesn’t mean you can’t make it easier to automate and ‘parameter-ize’.

    In other words, I once managed a huge replication topology where I frequently had to tear down subscribers, and even (occasionally) recreate publications, and so on. Rather than mucking with trying to generate T-SQL that dynamically queried things and did EVERYTHING in one fell-swoop, I create a number of T-SQL Templates (.tql files) that I could then use to populate with a few parameters, and then turn loose.

    And if you’re unfamiliar with templates, just check out this video – which will quickly get you up to speed:

    http://www.sqlservervideos.com/video/using-sql-server-templates

    And here’s an example of the kind of template I was using to add articles – as you’re shooting for:

    /* Add Article */   USE [<database,sysname,--Default>] GO  EXEC sp_addarticle     @publication = N'<publicationName,sysname,--Default>',     @article = N'<article_1,sysname,--Default>',     @source_owner = N'dbo',      @source_object = N'<article_1,sysname,--Default>',     @destination_table = N'<article_1,sysname,--Default>',     @type = N'logbased',      @creation_script = null,      @description = null,      @pre_creation_cmd = N'drop',      @schema_option = <bitmask_1,binary(8),0x000000000000CCD3>,     @status = 16,      @vertical_partition = N'false',      @ins_cmd = N'CALL sp_MSins_<article_1,sysname,--Default>',      @del_cmd = N'CALL sp_MSdel_<article_1,sysname,--Default>',      @upd_cmd = N'MCALL sp_MSupd_<article_1,sysname,--Default>',      @filter = null,      @sync_object = null,      @auto_identity_range = N'false' GO  EXEC sp_addarticle     @publication = N'<publicationName,sysname,--Default>',     @article = N'<article_2,sysname,--Default>',     @source_owner = N'dbo',      @source_object = N'<article_2,sysname,--Default>',     @destination_table = N'<article_2,sysname,--Default>',     @type = N'logbased',      @creation_script = null,      @description = null,      @pre_creation_cmd = N'drop',      @schema_option = <bitmask_2,binary(8),0x000000000000CCD3>,     @status = 16,      @vertical_partition = N'false',      @ins_cmd = N'CALL sp_MSins_<article_2,sysname,--Default>',      @del_cmd = N'CALL sp_MSdel_<article_2,sysname,--Default>',      @upd_cmd = N'MCALL sp_MSupd_<article_2,sysname,--Default>',      @filter = null,      @sync_object = null,      @auto_identity_range = N'false' GO  EXEC sp_addarticle     @publication = N'<publicationName,sysname,--Default>',     @article = N'<article_3,sysname,--Default>',     @source_owner = N'dbo',      @source_object = N'<article_3,sysname,--Default>',     @destination_table = N'<article_3,sysname,--Default>',     @type = N'logbased',      @creation_script = null,      @description = null,      @pre_creation_cmd = N'drop',      @schema_option = <bitmask_3,binary(8),0x000000000000CCD3>,     @status = 16,      @vertical_partition = N'false',      @ins_cmd = N'CALL sp_MSins_<article_3,sysname,--Default>',      @del_cmd = N'CALL sp_MSdel_<article_3,sysname,--Default>',      @upd_cmd = N'MCALL sp_MSupd_<article_3,sysname,--Default>',      @filter = null,      @sync_object = null,      @auto_identity_range = N'false' GO  EXEC sp_addarticle     @publication = N'<publicationName,sysname,--Default>',     @article = N'<article_4,sysname,--Default>',     @source_owner = N'dbo',      @source_object = N'<article_4,sysname,--Default>',     @destination_table = N'<article_4,sysname,--Default>',     @type = N'logbased',      @creation_script = null,      @description = null,      @pre_creation_cmd = N'drop',      @schema_option = <bitmask_4,binary(8),0x000000000000CCD3>,     @status = 16,      @vertical_partition = N'false',      @ins_cmd = N'CALL sp_MSins_<article_4,sysname,--Default>',      @del_cmd = N'CALL sp_MSdel_<article_4,sysname,--Default>',      @upd_cmd = N'MCALL sp_MSupd_<article_4,sysname,--Default>',      @filter = null,      @sync_object = null,      @auto_identity_range = N'false' GO 

    And in some cases… I also used these templates which in turn had some nested/dynamic SQL. But by using both, I found that I was able to very easily tame some of the more redundant tasks necessary without over-complicating things.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 91k
  • Answers 91k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If you aren't using namespaces or your schemas share the… May 11, 2026 at 6:19 pm
  • Editorial Team
    Editorial Team added an answer PHP 5.3 has namespaces, but PHP 5.3 is still under… May 11, 2026 at 6:19 pm
  • Editorial Team
    Editorial Team added an answer I'm the PHP guy (that also does .NET) that is… May 11, 2026 at 6:19 pm

Related Questions

I want to ask how other programmers are producing Dynamic SQL strings for execution
I have an application that creates database tables on the fly. I'd like to
I have created the following stored procedure.. CREATE PROCEDURE [dbo].[UDSPRBHPRIMBUSTYPESTARTUP] ( @CODE CHAR(5) ,
I have been tasked with creating a reusable process for our Finance Dept to
I'm looking to add an advanced search capability to my ASP.NET/SQL Server 2005 application.

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.