简体   繁体   English

用户在其中输入名称的文本框,可以是名字或姓氏,或同时输入名字和姓氏,然后在屏幕上显示记录数据。存储过程逻辑失败

[英]Text box where user enters name,can be first name or last name or both then record data is displayed on to screen.Stored procedure logic fails

i 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. 我有一个文本框,用户输入的名称可以是名字,姓氏或两者均可。所有这些都在单个用户输入文本框中输入。然后sql存储过程应搜索名称(名字,姓氏,姓氏)甚至是多个记录,用户可以在其中从弹出窗口或简单表中的重复名称中选择一个特定名称。 I am don't have much skills in sql server.I am having problems getting the logic right. 我在sql server中没有太多技能,我在正确理解逻辑方面遇到问题。

The code till now is as follows- 到目前为止的代码如下:

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' 用“ reddy”搜索姓氏

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. 存储过程返回空表。即使它由名称上的多个记录组成,但是当我只执行如下没有输入参数的查询时,它也可以正常工作。

 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: enter image description here 它返回如下输出: 在此处输入图像描述

The table consists of the following values. 该表由以下值组成。 enter image description here 在此处输入图片说明

Table data- 表格数据

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','Yarramreddy' , 'IND'), 
          ('sruthi','chiluka' , 'US'),
          ('amulya', 'reddy', 'BZ'), 
          ('amulya', 'Choudary', 'Uk'),
          ('avasya', 'Koneru', 'US'),
           ('avasya', 'Koneru', 'AUS'),
          ('amulya', 'reddy', 'BZ'),
           ('avasya', 'Koneru', 'US'),
           ('sruthi','K' , 'AUS'),
           ('sruthi','yaramreddy' , 'AUS');

Not sure if this is really what you are after but here is my best guess. 不知道这是否真的是您想要的,但这是我的最佳猜测。

select firstname, lastname, country
from testingtable
where firstname = @NameSearch
    OR lastname = @NameSearch

Also, Country should NOT be varchar(max). 另外,国家/地区不得为varchar(max)。 It should a char(2) or char(3) depending on which standard you are using. 根据您使用的标准,它应该为char(2)或char(3)。 You seem to be mixing 2 and 3 character country codes. 您似乎混合了2个和3个字符的国家/地区代码。 It should also be a foreign key to a Country table. 它也应该是Country表的外键。

For querying basics the Stairways series on SQL Server Central is awesome. 对于查询基础知识,SQL Server Central上的Stairways系列很棒。 http://www.sqlservercentral.com/stairway/75773/ http://www.sqlservercentral.com/stairway/75773/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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