我正在尝试编写一个动态查询。搜索条件将来自 ASP 页面,并被传递到 SQL Server 2005 Express 数据库中的存储过程。搜索没有给出任何错误,但它返回数据库中的所有数据并且不根据变量进行过滤。有人请帮忙吗?
我在这里发布存储过程:
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[spSearchResource]
@ResourceID int,
@Name varchar(75),
@City varchar(75),
@State varchar(2),
@County varchar(30),
@Specialty varchar(100),
@Zip varchar(5),
@English bit,
@Spanish bit,
@French bit,
@Italian bit,
@Chinese bit,
@Japanese bit,
@GenderType varchar(8),
@Within int,
@Children bit,
@Adolescents bit,
@Adults bit,
@Geriatrics bit,
@Insurance varchar(50)
AS
BEGIN
DECLARE @strSql varchar(4000);
SET @strSql = 'select r.resourceid, r.flag, r.note, r.Name, r.ContactName, r.Website, r.Email, >r.GenderType, loc.Street, loc.city, loc.State, loc.Zip, loc.County,phone.areacode,
phone.phonenum,phone.extension,spec.specialty,
ins.insurance,pop.children,pop.Adolescents,
pop.adults,pop.geriatrics,lan.english,lan.spanish,lan.french,lan.italian,lan.chinese,lan.japanese
from resource r left outer join resourcelocation loc on (r.resourceid = loc.resourceid)
left outer join resourcephone phone on (r.resourceid = phone.resourceid)
left outer join resourceinsurance ins on (r.resourceid = ins.resourceid)
left outer join resourcepopulation pop on (r.resourceid = pop.resourceid)
left outer join resourcespecialty spec on (r.resourceid = spec.resourceid)
left outer join resourcelanguage lan on (r.resourceid = lan.resourceid) '
if (@ResourceID is not null)
SET @strSql = @strSql + 'and r.resourceid = ' + (CONVERT(VARCHAR(10),@ResourceID))
if (@Name is not null)
SET @strSql = @strSql + 'and r.Name like '+''''+ @Name+'%'''
if (@City is not null)
SET @strSql = @strSql + 'and loc.city like '+''''+ @City+'%'''
if (ltrim(rtrim(@State)) is not null)
SET @strSql = @strSql + 'and loc.State = trim(@State) '
if (ltrim(rtrim(@Zip)) is not null)
SET @strSql = @strSql + 'and loc.Zip = trim(@Zip) '
if (ltrim(rtrim(@County)) is not null)
SET @strSql = @strSql + 'and loc.County like trim(@County) '
if (ltrim(rtrim(@specialty)) is not null)
SET @strSql = @strSql + 'and spec.specialty = trim(@spcialty) '
if (ltrim(rtrim(@insurance)) is not null)
SET @strSql = @strSql + 'and ins.insurance = trim(@insurance) '
if (@English = 1)
SET @strSql = @strSql + 'and lan.english = @English'
if (@Spanish = 1)
SET @strSql = @strSql + 'and lan.spanish = @Spanish '
if (@French = 1)
SET @strSql = @strSql + 'and lan.french = @French '
if (@Italian = 1)
SET @strSql = @strSql + 'and lan.italian = @Italian '
if (@Chinese = 1)
SET @strSql = @strSql + 'and lan.Chinese = @Chinese '
if (@Japanese = 1)
SET @strSql = @strSql + 'and lan.japanese = @Japanese '
if (ltrim(rtrim(@GenderType)) != 0)
SET @strSql = @strSql + 'and r.GenderType like trim(@GenderType) '
if (@children = 1)
SET @strSql = @strSql + 'and pop.children = @children '
if (@Adolescents = 1)
SET @strSql = @strSql + 'and pop.Adolescents = @Adolescents '
if (@adults = 1)
SET @strSql = @strSql + 'and pop.adults = @adults '
if (@geriatrics = 1)
SET @strSql = @strSql + 'and pop.geriatrics = @geriatrics '
print @strSql;
execute (@strSql);
END