Click here to Skip to main content
16,004,761 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Its taking single value and getting output and but giving two values are not executing and getting the values in output

What I have tried:

SQL
USE [HerdDinner]
GO
/****** Object:  StoredProcedure [dbo].[GetUSerListByTwoParameters]    Script Date: 12/12/2016 12:36:24 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[GetUSerListByTwoParameters]
@name varchar(50)=null,
@Address varchar(500)=null

AS
BEGIN

	SET NOCOUNT ON;
	
	Begin
	SELECT 

	u.id,
	u.name,
	u.Address,
	u.scholaid

	from [dbo].[Dept] u

	


	where  (ISNULL(u.name,'') like '%'+ISNULL(@name,'')+'%') and (ISNULL(u.Address,'') like '%'+ISNULL(@Address,'')+'%') 


	End
	

END
Posted
Updated 12-Dec-16 10:01am

1 solution

Hi
You have to think through your problem. Do you want people to search on either name or address and return all related data? Or do you want to search on name and address and they have to line up or no result will be returned. Logical operators are a good to understand when working with datasets. See Logical Operators (Transact-SQL)[^]

Here is an example that you can use for your code:

SQL
CREATE TABLE #Dept
(
	id INT IDENTITY(1,1) PRIMARY KEY,
	name VARCHAR(50) NULL,
	[Address] VARCHAR(120) NULL,
	scholaid VARCHAR(50) NULL
)

INSERT INTO #Dept
(name ,[Address], scholaid )
VALUES('Ben Smith','1 Smith Street','No'),
('Sam Coldwell','1 Sam Street','No'),
('John Max','1 Max Street','Yes'),
('Maggie Curtis','1 Curtis Street','No'),
('Susan Jason','1 Jason Street','Yes')

INSERT INTO #Dept
([Address], scholaid )
VALUES('2 Mystreet','No')

INSERT INTO #Dept
(name , scholaid )
VALUES('Chris Mason','No')

DECLARE @name VARCHAR(50) = NULL,
@Address VARCHAR(500) = NULL

--Need to know what the name is and the address and they have to line up or no result will be returned
SET @name = 'Smith'
SET @Address = 'Smith'

	SELECT u.id,
	u.name,
	u.Address,
	u.scholaid
    FROM #Dept u
	WHERE (ISNULL(u.name,'') like '%'+ISNULL(@name,'')+'%') AND (ISNULL(u.Address,'') like '%'+ISNULL(@Address,'')+'%') 

--to allow for multiple values where either one can be blank we need to use a union
--Example 1
SET @name = 'Smith'
SET @Address = 'Smith'

	SELECT u.id,
	u.name,
	u.Address,
	u.scholaid
    FROM #Dept u
	WHERE (ISNULL(u.name,'') like '%'+ISNULL(@name,'')+'%') 
	UNION
	SELECT u.id,
	u.name,
	u.Address,
	u.scholaid
    FROM #Dept u
	WHERE (ISNULL(u.Address,'') like '%'+ISNULL(@Address,'')+'%') 

--Example 2
SET @name = 'Smith'
SET @Address = 'Max'

	SELECT u.id,
	u.name,
	u.Address,
	u.scholaid
    FROM #Dept u
	WHERE (ISNULL(u.name,'') like '%'+ISNULL(@name,'')+'%') 
	UNION
	SELECT u.id,
	u.name,
	u.Address,
	u.scholaid
    FROM #Dept u
	WHERE (ISNULL(u.Address,'') like '%'+ISNULL(@Address,'')+'%') 


DROP TABLE #Dept
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900