Click here to Skip to main content
15,891,777 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
have a text box where user enters name can be first name,last name or both.This all is entered in a single user input text box.Then the sql stored procedure should search for name(first,name,last name) can even be multiple records where the user can select a specific name from repetitive name in a popup or a simple table. I am don't have much skills in sql server.I am having problems getting the logic right.

The code till now is as follows-


SQL
create PROCEDURE [dbo].[getfullnamereturnTest]
@NameSearch nvarchar(max)
AS

begin

   if exists (select * from testingtable where firstname='@NameSearch') 
begin
select distinct testingtable.firstname,testingtable.lastname,testingtable.country  from testingtable inner join
(
select testingtable.firstname,COUNT(*) as repitingnamesno
from testingtable
where testingtable.firstname ='@NameSearch'
group by testingtable.firstname
having count(testingtable.firstname)>1)
subq ON testingtable.firstname = subq.firstname
end
else if exists (select * from testingtable where lastname='@NameSearch')
begin
select distinct testingtable.firstname,testingtable.lastname,testingtable.country  from testingtable inner join
(
select testingtable.lastname,COUNT(*) as repitingnamesno
from testingtable
where testingtable.lastname ='@NameSearch'
group by testingtable.lastname
having count (testingtable.lastname)>1 )
subq ON testingtable.lastname = subq.lastname
end

else
begin
select distinct testingtable.firstname,testingtable.lastname,testingtable.country  from testingtable inner join
(
select testingtable.firstname,testingtable.lastname,COUNT(*) as repitingnamesno
from testingtable
where testingtable.firstname+testingtable.lastname ='@NameSearch'
group by testingtable.firstname,testingtable.lastname
having count (testingtable.firstname+testingtable.lastname)>1 ) 
subq ON testingtable.firstname = subq.firstname AND testingtable.lastname = subq.lastname
end
end
go


Searching for last name with 'reddy'

SQL
exec getfullnamereturnTest @NameSearch='reddy'


The stored procedure returns empty table.Even when it consists of multiple records on name.But when i just executed the query without input parameter as below and it works fine.

SQL
 if exists (select * from testingtable where firstname='reddy') 
begin
select distinct testingtable.firstname,testingtable.lastname,testingtable.country  from testingtable inner join
(
select testingtable.firstname,COUNT(*) as repitingnamesno
from testingtable
where testingtable.firstname ='reddy'
group by testingtable.firstname
having count(testingtable.firstname)>1)
subq ON testingtable.firstname = subq.firstname
end
else if exists (select * from testingtable where lastname='reddy')
begin
select distinct testingtable.firstname,testingtable.lastname,testingtable.country  from testingtable inner join
(
select testingtable.lastname,COUNT(*) as repitingnamesno
from testingtable
where testingtable.lastname ='reddy'
group by testingtable.lastname
having count (testingtable.lastname)>1 )
subq ON testingtable.lastname = subq.lastname
end

else
begin
select distinct testingtable.firstname,testingtable.lastname,testingtable.country  from testingtable inner join
(
select testingtable.firstname,testingtable.lastname,COUNT(*) as repitingnamesno
from testingtable
where testingtable.firstname+testingtable.lastname ='reddy'
group by testingtable.firstname,testingtable.lastname
having count (testingtable.firstname+testingtable.lastname)>1 ) 
subq ON testingtable.firstname = subq.firstname AND testingtable.lastname = subq.lastname
end


It returns output as below:
firstname	  lastname	country
    amulya	  Chdary	Uk
    amulya	  reddy	        BZ


The table consists of the following values.

firstname	lastname	country
sruthi       	 reddy	         IND
sruthi	        chlka	        US
amulya	        Chdary	        Uk
sruthi	        K	        AUS
amulya	        reddy	        BZ
avasya          Koneru	        US
sruthi	        reddy	      AUS
amulya	        reddy	        BZ
avasya	        Koneru	        US
avasya	        koneru	        AUS


Table data

SQL
CREATE TABLE [dbo].[testingtable](  [firstname] [nvarchar](50) NULL,    [lastname] [nvarchar](50) NULL,     [country] [nvarchar](max) NULL ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

GO

INSERT INTO testingtable (firstname, lastname, country)
   VALUES ('sruthi','reddy' , 'IND'),
          ('sruthi','chlka' , 'US'),
          ('amulya', 'reddy', 'BZ'),
          ('amulya', 'Chdary', 'Uk'),
          ('avasya', 'Koneru', 'US'),
           ('avasya', 'Koneru', 'AUS'),
          ('amulya', 'reddy', 'BZ'),
           ('avasya', 'Koneru', 'US'),
           ('sruthi','K' , 'AUS'),
           ('sruthi','reddy' , 'AUS');
Posted
Updated 9-Feb-20 6:48am
v2

Try with below code:

Alter your StoredProc with following query.
SQL
SELECT DISTINCT firstname, lastname, country
FROM testingtable
WHERE firstname = @NameSearch OR lastname = @NameSearch

But I am not sure about your business logic with group by in your query. If you want then you can merge with this query.
 
Share this answer
 
Remove the single quotes from around the parameters.

Instead of:
SQL
where firstname='@NameSearch'

use:
SQL
where firstname=@NameSearch


The former is looking for records where the first name is equal to the literal string '@NameSearch'; the latter is looking for records where the first name is equal to the value in the @NameSearch parameter.

You should also read Falsehoods Programmers Believe About Names[^]. :)
 
Share this answer
 

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