How to do multiple wildcard search in sql server

ma8fv8wu  于 2023-04-19  发布在  SQL Server
关注(0)|答案(1)|浏览(146)

Example : I've table with Name,Contact,City

Then from UI I get Multiple Cities list

Select * from Person where City = 'Frankfort','Texas','Washington'

I need the Condition like above.

Note: I've total search Parameters 57 For all 57 Columns I need multiple search.

This is how I written the condition Do we have any better Approach to Do It.

((@CityIS NULL) OR (';' + @City+ ';' like '%;' + City+ ';%'))
bjp0bcyl

bjp0bcyl1#

If you want to search 57 values in one column (city) .This code is correct

You can use table-valued parameter to solve the problem

you must create a table-valued parameter and Create PROCEDURE

CREATE TYPE SourceKeyList AS TABLE ( 
  SourceKey NVARCHAR(50)
)
GO


go

CREATE PROCEDURE dbo.stp_GetPerson
@source_key_list SourceKeyList READONLY
AS
BEGIN

 
 
 SELECT c.*
 FROM dbo.Person c
 JOIN @source_key_list k ON k.SourceKey = c.City
END
GO

DECLARE @source_key_list SourceKeyList

INSERT INTO @source_key_list
SELECT 'Frankfort' UNION ALL
SELECT 'Texas' UNION ALL
SELECT 'Washington'

EXEC dbo.stp_GetPerson @source_key_list
GO

You can create insert Base data with the following statements:

CREATE TABLE dbo.Person (
 Id  INT IDENTITY NOT NULL,
 Name NVARCHAR(50) NOT NULL,
 City  NVARCHAR(50) NOT NULL 
)
insert into dbo.Person
(Name,City)
select 'A','Frankfort'
union  select 'b','Texas'
union  select 'c','San'
union  select 'c','Washington'

相关问题