简体   繁体   English

Azure Synapse Analytics SQL 数据库 function 获取两个分隔列表之间的匹配

[英]Azure Synapse Analytics SQL Database function to get match between two delimited lists

I'm using Azure Synapse Analytics SQL Database.我正在使用 Azure Synapse Analytics SQL 数据库。 I'm aware I can't use selects in a scalar function (hence the error The SELECT statement is not allowed in user-defined functions ).我知道我不能在标量 function 中使用选择(因此出现错误The SELECT statement is not allowed in user-defined functions )。 I'm looking for a work-around since this function does not rely on any tables.我正在寻找解决方法,因为此 function 不依赖于任何表格。 The goal is a scalar function that takes two delimited lists parameters, a delimiter parameter and returns 1 if the lists have one or more matching items, and returns 0 if no matches are found.目标是一个标量 function,它采用两个定界列表参数,一个定界符参数,如果列表具有一个或多个匹配项,则返回 1,如果未找到匹配项,则返回 0。

--The SELECT statement is not allowed in user-defined functions
CREATE FUNCTION util.get_lsts_have_mtch
(
    @p_lst_1 VARCHAR(8000),
    @p_lst_2 VARCHAR(8000),
    @p_dlmtr CHAR(1)
)
RETURNS BIT
/***********************************************************************************************************
Description: This function returns 1 if two delimited lists have an item that exists in both lists.
--Example run: 
SELECT util.get_lsts_have_mtch('AB|CD|EF|GH|IJ','UV|WX|CD|IJ|YZ','|') -- returns 1, there's a match
SELECT util.get_lsts_have_mtch('AB|CD|EF|GH|IJ','ST|UV|WX|YZ','|') -- returns 0, there's no match
**********************************************************************************************************/
AS
BEGIN
    DECLARE @v_result BIT;
    -- *** CAN THIS BE ACCOMPLISHED EFFICIENTLY WITHOUT ANY SELECTS? ***
    SET @v_result = (SELECT CAST(CASE WHEN EXISTS (SELECT 1
                                              FROM STRING_SPLIT(@p_lst_1, @p_dlmtr)       AS tokens_1
                                              INNER JOIN STRING_SPLIT(@p_lst_2, @p_dlmtr) AS tokens_2
                                                  ON     tokens_1.value = tokens_2.value)
                                     THEN 1
                                 ELSE 0
                            END) AS BIT);
    RETURN @v_result;
END;

I ditched the function and used this CASE statement.我放弃了 function 并使用了这个 CASE 语句。 I wanted a function to join on that would be reusable.我想要一个 function 加入,这样可以重复使用。 If anyone can find a function to do this, I will make that the accepted answer.如果有人能找到 function 来执行此操作,我会将其设为可接受的答案。

SELECT ...
FROM tbl_1
JOIN tbl_2
ON  
-- wanted: util.get_lsts_have_mtch(tbl_1.my_lst, tbl_2.my_lst, '|') = 1
-- but settled for:
CASE WHEN EXISTS 
          (SELECT [value] 
           FROM STRING_SPLIT(tbl_1.my_lst, '|') 
           INTERSECT 
           SELECT [value] 
           FROM STRING_SPLIT(tbl_2.my_lst, '|')) 
     THEN 1 
     ELSE 0 
     END = 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从 Azure SQL 数据库加载数据到 Azure Synapse Analytics - Loading data into Azure Synapse Analytics from Azure SQL Database Azure 突触分析 Json 展平 - Azure Synapse Analytics Json Flatten Azure function 能否将数据写回突触数据库 - Can a Azure function write back data to synapse database Azure Synapse中Delta Lake和Lake Database的区别 - Difference between Delta Lake and Lake Database in Azure Synapse Synapse pyspark - 在 Azure SQL 数据库上执行存储过程? - Synapse pyspark - execute stored procedure on Azure SQL Database? Azure Synapse Analytics 与 Google Bigquery - Azure Synapse Analytics vs Google Bigquery 通过不检测重复但有欺骗来分组。 奇怪 SQL 服务器 - Azure Synapse 数据库专用 SQL 池 - Group by not detecting duplicates but there are dupes. Strange SQL Server - Azure Synapse database dedicated SQL pool 将 Azure Synapse Analytics 链接服务设置为 ODBC 链接服务器 - Setting up Azure Synapse Analytics Linked Service to ODBC linked server 如何使用 SQLAlchemy 将数据插入 Azure Synapse 数据库 - How to insert data into Azure Synapse Database with SQLAlchemy Azure 突触,SQL 池作为数据流中的源 - Azure synapse, SQL pool as a source in dataflow
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM