动态 SQL Server 问题
我想创建动态 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
动态 SQL 可能不是答案 - 但这并不意味着您不能更轻松地实现自动化和“参数化”。
换句话说,我曾经管理过一个巨大的复制拓扑,我经常不得不拆除订阅者,甚至(偶尔)重新创建发布,等等。 我没有尝试生成动态查询事物并一次性完成所有操作的 T-SQL,而是创建了许多 T-SQL 模板(.tql 文件),然后可以使用它们来填充一些参数,并且然后松开。
如果您不熟悉模板,只需观看此视频 - 它将帮助您快速上手:
http://www.sqlservervideos.com/video/using-sql-server-templates
这是我用来添加文章的模板示例 - 当您拍摄时for:
在某些情况下...我还使用了这些模板,这些模板又具有一些嵌套/动态 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:
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.