【发布时间】:2014-11-17 11:42:36
【问题描述】:
我有这个存储过程:
exec SearchResume @KeywordSearch=N'', @GreaterThanDate='2013-09-22 00:00:00',
@CityIDs=N'0,56,31,271,117,327,3,328,228',
@ProvinceIDs=N'0,1,12,13',
@CountryIDs=N'1',
@IndustryIDs=N'0,2,3,4,38,113,114,115,116,117'
它不会返回任何结果,因为 id 是 nvarchar 但实际值是整数。
现在,当我使用手动 int 值列表测试实际 SP 时,我能够得到结果,这就是示例:
SELECT DISTINCT
UserID,
ResumeID,
CASE a.Confidential WHEN 1 THEN 'Confidential' ELSE LastName + ',' + FirstName END as 'Name',
a.Description 'ResumeTitle',
CurrentTitle,
ModifiedDate,
CASE ISNULL(b.SalaryRangeID, '0') WHEN '0' THEN CAST(SalarySpecific as nvarchar(8)) ELSE c.Description END 'Salary',
h.Description 'Relocate',
i.Description + '-' + j.Description + '-' + k.Description 'Location'
FROM dbo.Resume a JOIN dbo.Candidate b ON a.CandidateID = b.CandidateID
LEFT OUTER JOIN SalaryRange c ON b.SalaryRangeID = c.SalaryRangeID
JOIN EducationLevel e ON b.EducationLevelID = e.EducationLevelID
JOIN CareerLevel f ON b.CareerLevelID = f.CareerLevelID
JOIN JobType g ON b.JobTypeID = g.JobTypeID
JOIN WillingToRelocate h ON b.WillingToRelocateID = h.WillingToRelocateID
JOIN City i ON b.CityID = i.CityID
JOIN StateProvince j ON j.StateProvinceID = b.StateProvinceID
JOIN Country k ON k.CountryID = b.CountryID
WHERE (b.CityID IN (0,56,31,125,229,5,219,8,228))
AND (b.StateProvinceID IN (0,1,13))
AND (b.CountryID IN (1))
AND (b.IndustryPreferenceID IN (0,2,3,4,5,6,115,116,117))
我想知道如何发送 int 值列表,而不是 nvarchar 值列表,因为您可以看到查询无法正常工作。
更新
原SP:
ALTER PROCEDURE [dbo].[SearchResume]
@KeywordSearch nvarchar(500),
@GreaterThanDate datetime,
@CityIDs nvarchar(500),
@ProvinceIDs nvarchar(500),
@CountryIDs nvarchar(500),
@IndustryIDs nvarchar(500)
AS
BEGIN
DECLARE @sql as nvarchar(4000)
SET @sql = 'SELECT DISTINCT
UserID,
ResumeID,
CASE a.Confidential WHEN 1 THEN ''Confidential'' ELSE LastName + '','' + FirstName END as ''Name'',
a.Description ''ResumeTitle'',
CurrentTitle,
ModifiedDate,
CurrentEmployerName,
PersonalDescription,
CareerObjectives,
CASE ISNULL(b.SalaryRangeID, ''0'') WHEN ''0'' THEN CAST(SalarySpecific as nvarchar(8)) ELSE c.Description END ''Salary'',
e.Description ''EducationLevel'',
f.Description ''CareerLevel'',
g.Description ''JobType'',
h.Description ''Relocate'',
i.Description + ''-'' + j.Description + ''-'' + k.Description ''Location''
FROM dbo.Resume a JOIN dbo.Candidate b ON a.CandidateID = b.CandidateID
LEFT OUTER JOIN SalaryRange c ON b.SalaryRangeID = c.SalaryRangeID
JOIN EducationLevel e ON b.EducationLevelID = e.EducationLevelID
JOIN CareerLevel f ON b.CareerLevelID = f.CareerLevelID
JOIN JobType g ON b.JobTypeID = g.JobTypeID
JOIN WillingToRelocate h ON b.WillingToRelocateID = h.WillingToRelocateID
JOIN City i ON b.CityID = i.CityID
JOIN StateProvince j ON j.StateProvinceID = b.StateProvinceID
JOIN Country k ON k.CountryID = b.CountryID
WHERE ( (ModifiedDate > ''' + CAST(@GreaterThanDate as nvarchar(55)) + ''')
'
IF (LEN(@CityIDs) >0)
BEGIN
SET @sql = @sql + 'AND (b.CityID IN (' + @CityIDs + '))'
END
IF (LEN(@ProvinceIDs) >0)
BEGIN
SET @sql = @sql + 'AND (b.StateProvinceID IN (' + @ProvinceIDs + '))'
END
IF (LEN(@CountryIDs) >0)
BEGIN
SET @sql = @sql + 'AND (b.CountryID IN (' + @CountryIDs + '))'
END
IF (LEN(@IndustryIDs) >0)
BEGIN
SET @sql = @sql + 'AND (b.IndustryPreferenceID IN (' + @IndustryIDs + '))'
END
IF (LEN(@KeywordSearch) > 0)
BEGIN
SET @sql = @sql + ' AND (' + @KeywordSearch + ')'
END
SET @sql = @sql + ') ORDER BY ModifiedDate desc'
--select @sql
exec sp_executesql @sql
END
【问题讨论】:
-
可以选择动态sql吗?
标签: sql sql-server sql-server-2008 stored-procedures sql-server-2008-r2