简体   繁体   中英

SQL Server : returning multiple rows by ROWID by using only one where / LIKE OPERATOR

Here are rows in my database which I want to get:

在此处输入图片说明

I want to get get all 3 rows by executing one query, this ids are received as procedure parameters, sometimes I can receive 1 id and sometimes I can receive 10 of them, depends what users sends to database.

I wrote something like this:

SELECT * 
FROM Products
WHERE CONVERT(NVARCHAR(MAX), ProductId) LIKE '%' + 'A9472294-CFDD-40AC-BC2D-00E39AF4A300, 9A817E40-E4B1-4487-A376-010DD6377E38, 078A3C75-C442-4D88-A1E0-0118B8706667' + '%'

SELECT * 
FROM Products
WHERE 'A9472294-CFDD-40AC-BC2D-00E39AF4A300, 9A817E40-E4B1-4487-A376-010DD6377E38, 078A3C75-C442-4D88-A1E0-0118B8706667' LIKE '%' + CONVERT(NVARCHAR(MAX), ProductId) + '%' 

How come the second example returns rows as expected and the first example does not return any rows?

The definition says: %or% finds any values that have "or" in any position

How does this actually work? Could anyone explain?

EDIT:

I thought this LIKE operator should be used on my column like

Select * From Products Where ProductId Like '%' + 'SomeStringId' + '%';

because LIKE operator is used in a WHERE clause to search for a specified pattern in a column and my column is ProductId so I can't understand how come example like this works:

Select * From Products Where 'SomeStringId' Like '%' + ProductId + '%';

How come this example above works if Like is not used on my column, it's used on some string acctually...

You should split the string and then use an inner join.

For SQL Server below 2016 with the cumbersome string split via XML:

SELECT p.*
       FROM products p
            INNER JOIN (SELECT arg_xml_node.xml_node.value('(.)[1]', 'uniqueidentifier') uniqueidenfier
                               FROM (SELECT convert(xml,
                                            concat('<x>',
                                                   replace('A9472294-CFDD-40AC-BC2D-00E39AF4A300, 9A817E40-E4B1-4487-A376-010DD6377E38, 078A3C75-C442-4D88-A1E0-0118B8706667',
                                                           ', ',
                                                           '</x><x>'),
                                                   '</x>')) xml) arg_xml
                                    CROSS APPLY arg_xml.xml.nodes('x') arg_xml_node (xml_node)) arg_uniqueidenfier
                       ON arg_uniqueidenfier.uniqueidenfier = p.productid;

For SQL Server 2016 and above with the elegant way using string_split() :

SELECT p.*
       FROM products p
            INNER JOIN (SELECT convert(uniqueidentifier, ltrim(value)) uniqueidenfier
                               FROM string_split('A9472294-CFDD-40AC-BC2D-00E39AF4A300, 9A817E40-E4B1-4487-A376-010DD6377E38, 078A3C75-C442-4D88-A1E0-0118B8706667',
                                                 ',')) arg_uniqueidenfier
                       ON arg_uniqueidenfier.uniqueidenfier = p.productid;

db<>fiddle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM