我正在嘗試編寫動態查詢。搜索標準將來自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
那麼什麼打印時,它打印字符串? – JNK