下面是我的存储过程,该过程执行正常且没有错误,但数据未根据参数值正确显示。
实施例
WHERE clause 1=1
默认情况下,即使数据在表
中不可用,数据也会显示整个列表我的问题在于 DECLARE @sql nvarchar(max)
SET @sql=N'SELECT Tbl_JobSeekers.National_Id, Tbl_JobSeekers.JobSeeker_EnglishName, Tbl_JobSeekers.JobSeeker_Arabicname, Tbl_JobSeekers.Mobile, Tbl_JobSeekers.City,
Tbl_JobSeekers.Nationality, Tbl_JobSeekers.Education, Tbl_JobSeekers.Experience, Tbl_JobSeekers.Category, Tbl_JobSeekers.JobSeekerStatus,
Tbl_JobSeekers.StatusEmployer, Tbl_JobSeekers.ImagePath, Tbl_JobSeekers.ResumePath, Tbl_JobSeekers.Remarks, Tbl_JobSeekers.Email,
Tbl_JobSeekers.Created_Date, Tbl_City.City_Name, Tbl_Qualification.Education_Name, Tbl_Experience.Experience_Years, Tbl_Categories.Category_Name,
Tbl_Employers.Employer_EnglishName
FROM Tbl_JobSeekers INNER JOIN
Tbl_City ON Tbl_JobSeekers.City = Tbl_City.City_Id INNER JOIN
Tbl_Qualification ON Tbl_JobSeekers.Education = Tbl_Qualification.Education_Id INNER JOIN
Tbl_Experience ON Tbl_JobSeekers.Experience = Tbl_Experience.Experience_Id INNER JOIN
Tbl_Categories ON Tbl_JobSeekers.Category = Tbl_Categories.Category_Id LEFT OUTER JOIN
Tbl_Employers ON Tbl_JobSeekers.StatusEmployer = Tbl_Employers.Employer_Id WHERE (1=0) '
--set @sql = @sql + ' 1=1 OR '
if (@National_Id is NOT NULL ) set @sql = @sql + ' OR Tbl_JobSeekers.National_Id LIKE ''%' + @National_Id + '%'' '
if (@JobSeeker_EnglishName is not NULL) set @sql = @sql + ' OR Tbl_JobSeekers.JobSeeker_EnglishName LIKE ''%' + @JobSeeker_EnglishName + '%'' '
if (@Mobile is not NULL) set @sql = @sql + ' OR Tbl_JobSeekers.Mobile LIKE ''%' + @Mobile + '%'' '
if (@City <> 'All') set @sql = @sql + ' OR Tbl_JobSeekers.City=@City '
if (@Nationality is not NULL) set @sql = @sql + ' OR Tbl_JobSeekers.Nationality=@Nationality '
if (@Education <> 'All') set @sql = @sql + ' OR Tbl_JobSeekers.Education=@Education '
if (@Experience <> 'All') set @sql = @sql + ' OR Tbl_JobSeekers.Experience=@Experience '
if (@Category <> 'All') set @sql = @sql + ' OR Tbl_JobSeekers.Category=@Category '
if (@JobSeekerStatus <> 'All') set @sql = @sql + ' OR Tbl_JobSeekers.JobSeekerStatus=@JobSeekerStatus '
if (@Email is not NULL) set @sql = @sql + ' OR Tbl_JobSeekers.Email LIKE ''%' + @Email + '%'' '
--PRINT @National_Id --+',' + @JobSeeker_EnglishName + ',' + @Mobile + ',' + @City + ',' + @Nationality + ',' + @Education + ',' + @Experience + ',' + @Category + ',' + @JobSeekerStatus + ',' + @Email
PRINT @sql
EXEC sp_executesql @sql,
N'@National_Id nvarchar(25),@JobSeeker_EnglishName nvarchar(50), @Mobile nvarchar(25), @City varchar(10), @Nationality varchar(10), @Education varchar(10) , @Experience nvarchar(25), @Category nvarchar(25), @JobSeekerStatus nvarchar(25), @Email nvarchar(100)',
@National_Id, @JobSeeker_EnglishName, @Mobile, @City, @Nationality, @Education, @Experience ,@Category, @JobSeekerStatus, @Email
END
(对所有默认值都正常工作但当我更改参数值数据时显示错误。)和1 = 0(当我传递一些值时工作正常)
我头疼,无法知道我该怎么做。
load
答案 0 :(得分:0)
完全忽略常量怎么样?然后,将where
子句放在一起:
declare @where varchar(max) = '';
if (@National_Id is NOT NULL )
set @where = @where + ' OR Tbl_JobSeekers.National_Id LIKE ''%'
+ @National_Id + '%'' '
. . .
最后,
set @sql = @sql + (case when @where <> ''
then 'where ' + stuff(@where, 1, 3, '')
else ''
end);
答案 1 :(得分:0)
正如所写,一旦你到达 p>
OR 1 = 1 OR ......
查询将返回所有记录,因为(1 = 1)为TRUE,其余的无关紧要。
这就是为什么你想使用另一个变量来构建WHERE子句并在之后添加它
假设:
DECLARE @SQL VARCHAR(max)
, @WHERE VARCHAR(MAX) = ''
然后
-- take the WHERE 1=0 off the END of your current @SQL
SET @SQL = '... Tbl_Employers ON Tbl_JobSeekers.StatusEmployer = Tbl_Employers.Employer_Id '
-- build your @WHERE
if (@National_Id is NOT NULL ) set @WHERE = @WHERE + ' OR Tbl_JobSeekers.National_Id LIKE ''%' + @National_Id + '%'' '
if (@JobSeeker_EnglishName is not NULL) set @WHERE = @WHERE + ' OR Tbl_JobSeekers.JobSeeker_EnglishName LIKE ''%' + @JobSeeker_EnglishName + '%'' '
if (@Mobile is not NULL) set @WHERE = @WHERE + ' OR Tbl_JobSeekers.Mobile LIKE ''%' + @Mobile + '%'' '
if (@City <> 'All') set @WHERE = @WHERE + ' OR Tbl_JobSeekers.City=@City '
if (@Nationality is not NULL) set @WHERE = @WHERE + ' OR Tbl_JobSeekers.Nationality=@Nationality '
if (@Education <> 'All') set @WHERE = @WHERE + ' OR Tbl_JobSeekers.Education=@Education '
if (@Experience <> 'All') set @WHERE = @WHERE + ' OR Tbl_JobSeekers.Experience=@Experience '
if (@Category <> 'All') set @WHERE = @WHERE + ' OR Tbl_JobSeekers.Category=@Category '
if (@JobSeekerStatus <> 'All') set @WHERE = @WHERE + ' OR Tbl_JobSeekers.JobSeekerStatus=@JobSeekerStatus '
if (@Email is not NULL) set @WHERE = @WHERE + ' OR Tbl_JobSeekers.Email LIKE ''%' + @Email + '%'' '
-- conditionally add @WHERE to @SQL
IF LEN(@WHERE) > 0 SET @SQL = @SQL + ' WHERE 1=0 ' + @WHERE;
-- note: if @WHERE is empty, there is NO WHERE clause at all
答案 2 :(得分:0)
我希望您的OR
声明不是必需的。在您使用WHERE 1=0 OR
匹配其他条件的代码中。在任何情况下,如果其中一个为真WHERE 1=0 OR <your sql condition>
,它将返回记录。第一个条件将默认返回false,第二个条件需要为true。我们不要求where 1=0 or where 1=1
在OR
条件下使用它。可以这样离开
SET @sql=N'SELECT Tbl_JobSeekers.National_Id, Tbl_JobSeekers.JobSeeker_EnglishName, Tbl_JobSeekers.JobSeeker_Arabicname, Tbl_JobSeekers.Mobile, Tbl_JobSeekers.City,
Tbl_JobSeekers.Nationality, Tbl_JobSeekers.Education, Tbl_JobSeekers.Experience, Tbl_JobSeekers.Category, Tbl_JobSeekers.JobSeekerStatus,
Tbl_JobSeekers.StatusEmployer, Tbl_JobSeekers.ImagePath, Tbl_JobSeekers.ResumePath, Tbl_JobSeekers.Remarks, Tbl_JobSeekers.Email,
Tbl_JobSeekers.Created_Date, Tbl_City.City_Name, Tbl_Qualification.Education_Name, Tbl_Experience.Experience_Years, Tbl_Categories.Category_Name,
Tbl_Employers.Employer_EnglishName
FROM Tbl_JobSeekers INNER JOIN
Tbl_City ON Tbl_JobSeekers.City = Tbl_City.City_Id INNER JOIN
Tbl_Qualification ON Tbl_JobSeekers.Education = Tbl_Qualification.Education_Id INNER JOIN
Tbl_Experience ON Tbl_JobSeekers.Experience = Tbl_Experience.Experience_Id INNER JOIN
Tbl_Categories ON Tbl_JobSeekers.Category = Tbl_Categories.Category_Id LEFT OUTER JOIN
Tbl_Employers ON Tbl_JobSeekers.StatusEmployer = Tbl_Employers.Employer_Id'
尝试像这样执行你的
exec dbo.[SP_ViewAdvanceSearchDetails] 'NULL','NULL','NULL','AP','Saudi','1','2','2','Looking','NULL'
之间,为什么所有列都是varchar类型?这是要求吗?