how to write query for following request?
my table:
id designation
1 developer,tester,projectlead
1 developer
1 techlead
if id=1,designation="'developer'"
Then need to first,second records.Because 2 rows are having venkat.
if id=1,designation="'developer','techlead'" then need to get 3 records as result.
i wrote one service for inserting records to that table .so that i am maintaining one table to store all designation with same column with comas.
By using service if user pass id=1 designation=”‘developer’,’techlead'” then need to pull the above 3 records.so that i am maintaining only one table to save all designations
SP:
ALTER PROCEDURE [dbo].[usp_GetDevices]
@id INT,
@designation NVARCHAR (MAX)
AS
BEGIN
declare @idsplat varchar(MAX)
set @idsplat = @UserIds
create table #u1 (id1 varchar(MAX))
set @idsplat = 'insert #u1 select ' + replace(@idsplat, ',', ' union select ')
exec(@idsplat)
Select
id FROM dbo.DevicesList WHERE id=@id AND designation IN (select id1 from #u1)
END
You need to use the boolean operators AND and OR in conjunction with LIKE:
The above example will return all rows with
empidequals 1 andempnamecontainingvenkatorvasu.Apparently you need to create that query based on the input from user, this is just an example of how the finally query should look like.
Edit: Trying to do this within SqlServer can be quite hard so you should really change your approach on how you call the stored procedure. If you can’t do this then you could try and split your
designationparameter on,(the answers to this question show several ways of how to do this) and insert the values into a temporary table. Then you canJOINon this temporary table withLIKEas described in this article.