0

我有這個存儲過程:演員表中的SQL查詢/ INT列表存儲過程

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值列表,因爲您可以看到查詢無法正常工作。

由於提前,Laziale

UPDATE:

原來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 
+0

是動態sql的一個選項嗎? – 2014-09-23 10:01:50

回答

0

您可以創建一個表值函數,它接受了爲nvarchar並創建一個新的記錄每個值,你在哪裏告訴它分隔符。我在這裏的例子返回一個表,單值列,然後你可以使用它作爲一個子查詢,爲您在選擇:在

Create FUNCTION [dbo].[fnSplitVariable] 
(
    @List nvarchar(2000), 
    @delimiter nvarchar(5) 
) 
RETURNS @RtnValue table 
(

    Id int identity(1,1), 
    Variable varchar(15), 
    Value nvarchar(100) 
) 
AS 
BEGIN 
Declare @Count int 
set @Count = 1 
    While (Charindex(@delimiter,@List)>0) 
    Begin 
     Insert Into @RtnValue (Value, Variable) 
     Select 
      Value = ltrim(rtrim(Substring(@List,1,Charindex(@delimiter,@List)-1))), 
     Variable = 'V' + convert(varchar,@Count) 
      Set @List = Substring(@List,Charindex(@delimiter,@List)+len(@delimiter),len(@List)) 
     Set @Count = @Count + 1 
    End 

    Insert Into @RtnValue (Value, Variable) 
     Select Value = ltrim(rtrim(@List)), Variable = 'V' + convert(varchar,@Count) 

     Return 
END 

那麼你的where語句,你可以做到以下幾點:

WHERE (b.CityID IN (Select Value from fnSplitVariable(@CityIDs, ',')) 

我已經包含了您的原始程序,並將其更新爲使用上述功能:

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 = N' 
     DECLARE  @KeywordSearch nvarchar(500), 
        @CityIDs nvarchar(500), 
        @ProvinceIDs nvarchar(500), 
        @CountryIDs nvarchar(500), 
        @IndustryIDs nvarchar(500) 

     SET @KeywordSearch = '''[email protected]+''' 
     SET @CityIDs = '''[email protected]+''' 
     SET @ProvinceIDs = '''[email protected]+''' 
     SET @CountryIDs = '''[email protected]+''' 
     SET @IndustryIDs = '''[email protected]+''' 
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 + N'AND (b.CityID IN (Select Value from fnSplitVariable(@CityIDs,'','') ))' 
END 
IF (LEN(@ProvinceIDs) >0) 
BEGIN 
    SET @sql = @sql + N'AND (b.StateProvinceID IN (Select Value from fnSplitVariable(@ProvinceIDs,'','')))' 
END 
IF (LEN(@CountryIDs) >0) 
BEGIN 
    SET @sql = @sql + N'AND (b.CountryID IN (Select Value from fnSplitVariable(@CountryIDs,'','') ))' 
END 
IF (LEN(@IndustryIDs) >0) 
BEGIN 
    SET @sql = @sql + N'AND (b.IndustryPreferenceID IN (Select Value from fnSplitVariable(@IndustryIDs,'','')))' 
END 

IF (LEN(@KeywordSearch) > 0) 
BEGIN 
    SET @sql = @sql + N' AND (' + @KeywordSearch + ')' 
END 

SET @sql = @sql + N') ORDER BY ModifiedDate desc' 

--select @sql 
exec sp_executesql @sql 

END 
+0

這是我收到的錯誤http://i.gyazo.com/6f4100aa373904a58daeddeb8fb8f484.png – Laziale 2014-09-23 10:31:08

+0

編輯了答案,我猜你的where子句 – Mattgb 2014-09-23 10:52:06

+0

現在我可能有錯誤這個錯誤:http://gyazo.com/85434dc2865e055f8de4d7ac7adc0a3e謝謝 – Laziale 2014-09-23 12:18:20

0
DECLARE @SQL AS NVARCHAR(MAX) 

SET @SQL = '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 (' + @CityIDs + ')) 
    AND (b.StateProvinceID IN (' + @ProvinceIDs + ')) 
    AND (b.CountryID IN (' + @CountryIDs + ')) 
    AND (b.IndustryPreferenceID IN (' + @IndustryIDs + '))' 

EXEC @SQL 
0

你可以做的ID所進來的存儲過程的分裂。因此,考慮這種分裂功能:

CREATE FUNCTION [dbo].[Split] 
(
    @String NVARCHAR(4000), 
    @Delimiter NCHAR(1) 
) 
RETURNS TABLE 
AS 
RETURN 
(
    WITH Split(stpos,endpos) 
    AS(
     SELECT 0 AS stpos, CHARINDEX(@Delimiter,@String) AS endpos 
     UNION ALL 
     SELECT endpos+1, CHARINDEX(@Delimiter,@String,endpos+1) 
      FROM Split 
      WHERE endpos > 0 
    ) 
    SELECT 'Id' = ROW_NUMBER() OVER (ORDER BY (SELECT 1)), 
     'Data' = SUBSTRING(@String,stpos,COALESCE(NULLIF(endpos,0),LEN(@String)+1)-stpos) 
    FROM Split 
) 

然後,你可以寫你的where語句是這樣的:

WHERE 
    EXISTS(SELECT NULL FROM dbo.Split(@CityIDs,',') AS city 
       WHERE city.Data=b.CityID) 
    AND EXISTS(SELECT NULL FROM dbo.Split(@ProvinceIDs,',') AS Province 
       WHERE Province.Data=b.StateProvinceID) 
    AND EXISTS(SELECT NULL FROM dbo.Split(@CountryIDs,',') AS Country 
       WHERE Country.Data=b.CountryID) 
    AND EXISTS(SELECT NULL FROM dbo.Split(@IndustryIDs,',') AS Industry 
       WHERE Industry.Data=b.IndustryPreferenceID) 
+0

感謝您的回答。請檢查更新的說明,我已經添加了原始SP,能否請讓我知道如何使用原始SP的功能。非常感謝 – Laziale 2014-09-23 10:39:29