• <bdo id='dg1Vb'></bdo><ul id='dg1Vb'></ul>
    <i id='dg1Vb'><tr id='dg1Vb'><dt id='dg1Vb'><q id='dg1Vb'><span id='dg1Vb'><b id='dg1Vb'><form id='dg1Vb'><ins id='dg1Vb'></ins><ul id='dg1Vb'></ul><sub id='dg1Vb'></sub></form><legend id='dg1Vb'></legend><bdo id='dg1Vb'><pre id='dg1Vb'><center id='dg1Vb'></center></pre></bdo></b><th id='dg1Vb'></th></span></q></dt></tr></i><div id='dg1Vb'><tfoot id='dg1Vb'></tfoot><dl id='dg1Vb'><fieldset id='dg1Vb'></fieldset></dl></div>
      <tfoot id='dg1Vb'></tfoot>

      <small id='dg1Vb'></small><noframes id='dg1Vb'>

    1. <legend id='dg1Vb'><style id='dg1Vb'><dir id='dg1Vb'><q id='dg1Vb'></q></dir></style></legend>
    2. 将函数与 OUTER APPLY 一起使用时,将返回该值而不

      时间:2023-08-21
    3. <legend id='QMBJh'><style id='QMBJh'><dir id='QMBJh'><q id='QMBJh'></q></dir></style></legend>
        <tbody id='QMBJh'></tbody>
            <i id='QMBJh'><tr id='QMBJh'><dt id='QMBJh'><q id='QMBJh'><span id='QMBJh'><b id='QMBJh'><form id='QMBJh'><ins id='QMBJh'></ins><ul id='QMBJh'></ul><sub id='QMBJh'></sub></form><legend id='QMBJh'></legend><bdo id='QMBJh'><pre id='QMBJh'><center id='QMBJh'></center></pre></bdo></b><th id='QMBJh'></th></span></q></dt></tr></i><div id='QMBJh'><tfoot id='QMBJh'></tfoot><dl id='QMBJh'><fieldset id='QMBJh'></fieldset></dl></div>
            <tfoot id='QMBJh'></tfoot>

              <bdo id='QMBJh'></bdo><ul id='QMBJh'></ul>

                <small id='QMBJh'></small><noframes id='QMBJh'>

                本文介绍了将函数与 OUTER APPLY 一起使用时,将返回该值而不是 NULL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我在使用内联函数时得到奇怪的结果.代码如下:

                I am getting strange results when using inline function. Here is the code:

                IF EXISTS (
                SELECT * FROM sys.objects AS o WHERE name = 'vendor_relation_users'
                ) DROP FUNCTION dbo.vendor_relation_users;
                GO
                CREATE FUNCTION [dbo].[vendor_relation_users]
                (
                    @user_name CHAR(12)
                )
                RETURNS TABLE
                AS
                    RETURN (SELECT @user_name AS user_name WHERE @user_name NOT LIKE '06%');
                GO
                
                DECLARE @u CHAR(12) = '066BDLER'
                SELECT a.user_name, is_v.user_name 
                FROM (SELECT @u AS user_name) a
                OUTER APPLY [dbo].[vendor_relation_users](@u) AS is_v
                
                SELECT a.user_name, is_v.user_name 
                FROM (SELECT @u AS user_name) a
                OUTER APPLY (SELECT @u AS user_name WHERE @u NOT LIKE '06%') AS is_v
                
                
                SELECT * FROM [dbo].[vendor_relation_users](@u)
                

                所以在第一个 SELECT 语句中,我只是对函数进行了 OUTER APPLied 并返回结果.

                So in the first SELECT statement I've just OUTER APPLied the function and it returns the result.

                在下一个语句中,我从函数中取出代码并将其直接放入 OUTER APPLY 语句中.

                In the next statement I've took the code from function and put it straight to the OUTER APPLY statement.

                最后一个语句只是直接的函数调用.

                And the last statement is just the direct function call.

                我不明白为什么 FIRST 查询会返回值...

                推荐答案

                这是一个非常有趣的查询.第一个查询的行为取决于您是否使用 OPTION (RECOMPILE).

                This is a very interesting query. The behaviour of your first query depends upon whether you use OPTION (RECOMPILE) or not.

                正如您指出的那样:

                DECLARE @u CHAR(12) = '066BDLER'
                SELECT a.user_name, is_v.user_name 
                FROM (SELECT @u AS user_name) a
                OUTER APPLY [dbo].[vendor_relation_users](@u) AS is_v
                

                返回:

                user_name       user_name
                066BDLER        066BDLER
                

                但是如果你像这样添加OPTION (RECOMPILE):

                but if you add OPTION (RECOMPILE) like this:

                SELECT a.user_name, is_v.user_name 
                FROM (SELECT @u AS user_name) a
                OUTER APPLY [dbo].[vendor_relation_users](@u) AS is_v
                OPTION (RECOMPILE)   
                

                你正确理解:

                user_name       user_name
                066BDLER        NULL
                

                我怀疑这是由于查询优化器如何根据基数估计使这些内联函数短路的错误.如果您查看这两个查询的查询计划,您会发现没有 OPTION RECOMPILE 的那个只返回一个常量.

                I suspect this is due to a bug in how the query optimiser short circuits these inline functions due to cardinality estimates. If you look at the query plan for the two queries you will see that the one without the OPTION RECOMPILE just returns a constant.

                这篇关于将函数与 OUTER APPLY 一起使用时,将返回该值而不是 NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:当我们在 SQL Server 2012 中进行交叉应用和内部连接 下一篇:Microsoft SQL Server 2008 中 JOIN 与 APPLY 的相对效率

                相关文章

                最新文章

              • <small id='c2xNH'></small><noframes id='c2xNH'>

                  <legend id='c2xNH'><style id='c2xNH'><dir id='c2xNH'><q id='c2xNH'></q></dir></style></legend>
                1. <tfoot id='c2xNH'></tfoot>
                    <bdo id='c2xNH'></bdo><ul id='c2xNH'></ul>

                  1. <i id='c2xNH'><tr id='c2xNH'><dt id='c2xNH'><q id='c2xNH'><span id='c2xNH'><b id='c2xNH'><form id='c2xNH'><ins id='c2xNH'></ins><ul id='c2xNH'></ul><sub id='c2xNH'></sub></form><legend id='c2xNH'></legend><bdo id='c2xNH'><pre id='c2xNH'><center id='c2xNH'></center></pre></bdo></b><th id='c2xNH'></th></span></q></dt></tr></i><div id='c2xNH'><tfoot id='c2xNH'></tfoot><dl id='c2xNH'><fieldset id='c2xNH'></fieldset></dl></div>