为什么需要进一步嵌套动态查询?

huangapple go评论57阅读模式
英文:

Why is a further nesting of dynamic queries needed?

问题

我在遵循这篇文章:“我想对所有的Y执行X”。但我对为什么需要将整个一组指令转换为字符串感到困惑。

具体来说,文章提供了以下SQL代码:

USE msdb;
GO

DECLARE @sql nvarchar(max) = space(0);

SELECT @sql += N'
EXEC sys.sp_refreshsqlmodule @name = N' + char(39) -- 单引号
       + QUOTENAME(s.name) + char(46)              -- 点
       + QUOTENAME(v.name) + char(39) + char(59)   -- 分号 
  FROM sys.views AS v 
  INNER JOIN sys.schemas AS s
     ON v.[schema_id] = s.[schema_id];

PRINT @sql;
-- EXEC sys.sp_executesql @sql;

并指出,如果取消注释最后一行,那么代码将执行以完成预期的任务。

然后询问如何使查询的这部分变得动态。但我不清楚为什么需要使代码的这部分变得动态。

在最终脚本中,文章将原始代码给出为文本字符串,但除此之外没有更改:

DECLARE @outerSQL nvarchar(max) = N'
    DECLARE @innerSQL nvarchar(max) = space(0);

    SELECT @innerSQL += N''
    EXEC sys.sp_refreshsqlmodule N'' + char(39)
         + QUOTENAME(s.name) + char(46)          
         + QUOTENAME(v.name) + char(39) + char(59)
    FROM sys.views AS v 
    INNER JOIN sys.schemas AS s
       ON v.[schema_id] = s.[schema_id];

    PRINT DB_NAME();
    PRINT @innerSQL;
    EXEC sys.sp_executesql @innerSQL;';
 
EXEC @exec @outerSQL;

我很难理解为什么最终代码中的这种更改是必要的。

  • 最终代码中的文本生成似乎是静态的,而不是动态的。
  • 原始代码没有处理数据库的选择,但最终代码似乎也没有处理这一点。

将原始指令转换为字符串有什么用途?

英文:

I am following this article: "I want to do X to all the Ys". But am confused by why it is necessary to convert an entire set of instructions to a string.

In particular, the article gives the following SQL code:

USE msdb;
GO
 
DECLARE @sql nvarchar(max) = space(0);
 
SELECT @sql += N'
EXEC sys.sp_refreshsqlmodule @name = N' + char(39) -- single quote
       + QUOTENAME(s.name) + char(46)              -- dot
       + QUOTENAME(v.name) + char(39) + char(59)   -- semi-colon 
  FROM sys.views AS v 
  INNER JOIN sys.schemas AS s
     ON v.[schema_id] = s.[schema_id];
 
PRINT @sql;
-- EXEC sys.sp_executesql @sql;

And notes that if the last line were uncommented then the code would execute to complete the intended task.

It then asks how to make this part of the query dynamic. But it is unclear to me why this part of the code needs to be made dynamic.

In the final script the article gives the original code as text string, but otherwise unchanged:

DECLARE @outerSQL nvarchar(max) = N'
    DECLARE @innerSQL nvarchar(max) = space(0);
 
    SELECT @innerSQL += N''
    EXEC sys.sp_refreshsqlmodule N'' + char(39)
         + QUOTENAME(s.name) + char(46)          
         + QUOTENAME(v.name) + char(39) + char(59)
    FROM sys.views AS v 
    INNER JOIN sys.schemas AS s
       ON v.[schema_id] = s.[schema_id];
 
    PRINT DB_NAME();
    PRINT @innerSQL;
    EXEC sys.sp_executesql @innerSQL;';
 
EXEC @exec @outerSQL;

I am struggling to understand why this change in the final code is necessary.

  • The text generation in the final code appears static rather than dynamic
  • The original code did not handle the choice of database, but the final code does not appear to handle this either.

What purpose does it serve to convert the original instructions to a string?

答案1

得分: 0

回顾@Bart Hofland评论后,我能看到:

  • 指定执行此代码的数据库是通过构造字符串dbname.sys.sp_executesql来完成的,并使用此字符串来执行初始代码。

  • 因此,将原始代码转换为文本字符串允许使用sp_executesql调用来执行它。

因此,初始代码并不需要更加动态化(尽管可以这样做),而是允许上下文来执行代码变得更加动态。

英文:

Looking back at the article following @Bart Hofland's comment I can see:

  • Specifying the database to execute this code is done by constructing the string dbname.sys.sp_executesql and using this to execute the initial code.

  • Hence, converting the original code to a text string allows it to be executed using this sp_executesql call.

So it is not that the initial code needs to be made more dynamic (though it could be), but that this allows the context to execute the code to be dynamic.

huangapple
  • 本文由 发表于 2023年7月3日 13:15:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76601984.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定