动态 SQL Server 问题

发布于 2024-07-16 14:57:12 字数 1394 浏览 5 评论 0原文

我想创建动态 SQL 代码来自动创建表复制。

我一直困惑于如何使以下代码动态化,以便可以传入 SQL 变量并在代码中使用它们。 我现在帮助自己搜索并替换 'TODO:' 部分,这不是很好...代码如下:

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)

一个问题是我需要在动态 SQL 块中使用动态 SQL 。 如果有人可以帮助我使用变量转换此代码,我将非常感激!

谢谢 丹尼尔

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

和我恋爱吧 2024-07-23 14:57:12

动态 SQL 可能不是答案 - 但这并不意味着您不能更轻松地实现自动化和“参数化”。

换句话说,我曾经管理过一个巨大的复制拓扑,我经常不得不拆除订阅者,甚至(偶尔)重新创建发布,等等。 我没有尝试生成动态查询事物并一次性完成所有操作的 T-SQL,而是创建了许多 T-SQL 模板(.tql 文件),然后可以使用它们来填充一些参数,并且然后松开。

如果您不熟悉模板,只需观看此视频 - 它将帮助您快速上手:

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

这是我用来添加文章的模板示例 - 当您拍摄时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

在某些情况下...我还使用了这些模板,这些模板又具有一些嵌套/动态 SQL。 但通过使用两者,我发现我能够非常轻松地处理一些必要的冗余任务,而不会过度复杂化事情。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文