我正在尝试根据名字和姓氏获取数据。我有两个彼此的数据库副本,除了一个数据库中的一些额外数据。下面是相同的存储过程。问题是我希望获取数据的第一部分(即,如果我搜索 ar,它应该显示以 ar 开头的名称,如 arron,而不是在中间,如 Sharon)。查询在一个数据库上运行良好,而在另一个数据库上运行良好。
ALTER PROCEDURE [dbo].[SEARCH]
(
@firstName nvarchar(50),
@lastName nvarchar(50),
@state nvarchar(50),
@county nvarchar(50),
@searchspan nvarchar(5)
)
AS
BEGIN
declare @queryString nvarchar(max)
declare @countyId nvarchar(50)
SET QUOTED_IDENTIFIER OFF
Create table #temp1(
county nvarchar(50) NULL,
ID nvarchar(50) NULL,
name nvarchar(200) NULL,
state nvarchar(50) NULL,
FirstName nvarchar(50) NULL,
LastName nvarchar(50) NULL,
county nvarchar(50) NULL,
)
set @queryString = 'insert into #temp1 select distinct a.Source as county, a.ID as ID, a.FirstName +'' ''+ a.LastName as name, ''' +
@state +''' as state,a.FirstName,a.LastName,b.county, from Person a, CountySite b where 1=1 and a.Source=b.sourcecounty '
if(@searchspan<>'')
BEGIN
set @queryString = @queryString = 1'
END
EXECUTE sp_executesql @queryString
set @queryString ='select county, ID, name, state,col_FirstName,col_LastName,col_county, from #temp1 where 1=1 '
if(@firstName <> '')
BEGIN
set @queryString = @queryString+ ' and UPPER(col_FirstName) like ''' +@firstName +'%'''
END
if(@lastName <> '')
BEGIN
set @queryString = @queryString + ' and UPPER(col_LastName) like ''' +@lastName +'%'''
drop table #temp1
END