Sql Query Concatenation未执行

时间:2015-06-09 12:40:38

标签: sql sql-server-2008

USE [Test] Go
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[SP_ViewAdvanceSearchDetails]
( 
 @JobStatus nvarchar(20)
)
AS BEGIN

DECLARE @SelectQuery nvarchar(max)
DECLARE @JobSeekerStatus nvarchar(20)

SET @SelectQuery= 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.Created_By, 
                  Tbl_JobSeekers.Created_Date
                  FROM Tbl_JobSeekers INNER JOIN
                  Tbl_City ON Tbl_JobSeekers.City = Tbl_City.City_Name INNER JOIN
                  Tbl_Qualification ON Tbl_JobSeekers.Education = Tbl_Qualification.Education_Name INNER JOIN
                  Tbl_Categories ON Tbl_JobSeekers.Category = Tbl_Categories.Category_Name INNER JOIN
                  Tbl_Experience ON Tbl_JobSeekers.Experience = Tbl_Experience.Experience_Years'

SET @JobSeekerStatus=@JobStatus

IF (@JobSeekerStatus='Selected')
    BEGIN
        SET @SelectQuery= @SelectQuery + ' WHERE Tbl_JobSeekers.JobSeekerStatus=' + @JobSeekerStatus + ' ORDER BY Tbl_JobSeekers.National_Id DESC'
    END
ELSE IF (@JobSeekerStatus='Working')
    BEGIN
        SET @SelectQuery= @SelectQuery + ' WHERE Tbl_JobSeekers.JobSeekerStatus=' + @JobSeekerStatus + ' ORDER BY Tbl_JobSeekers.National_Id DESC'
    END
ELSE IF (@JobSeekerStatus='Looking')
    BEGIN
        SET @SelectQuery= @SelectQuery + ' WHERE Tbl_JobSeekers.JobSeekerStatus=' + @JobSeekerStatus + ' ORDER BY Tbl_JobSeekers.National_Id DESC'
    END
ELSE IF (@JobSeekerStatus='All')
    BEGIN
        SET @SelectQuery= @SelectQuery + ' WHERE Tbl_JobSeekers.JobSeekerStatus=' + @JobSeekerStatus + ' ORDER BY Tbl_JobSeekers.National_Id DESC'
    END
ELSE 
    BEGIN
        SET @SelectQuery= @SelectQuery + ' ORDER BY Tbl_JobSeekers.National_Id DESC'
    END

PRINT @SelectQuery

EXEC sp_executesql @SelectQuery, N'@JobSeekerStatus nvarchar(20)', @JobSeekerStatus

END 

大家好,根据您的建议,我从Exec更正为EXEC sp_executesql并将参数作为值传递,Query仍未执行,显示错误消息“Invalid Column'Selected'”(我应该将其作为参数值传递) 。无论我传递什么值,它都会显示带有该参数的无效列。

3 个答案:

答案 0 :(得分:1)

你需要将@JobSeekerStatus包装在3个引号中

更新后使用QUOTENAME(感谢ughai)

E.G

SET @SelectQuery= @SelectQuery + ' WHERE Tbl_JobSeekers.JobSeekerStatus=' + QUOTENAME(@JobSeekerStatus, '''') + ' ORDER BY Tbl_JobSeekers.National_Id DESC'

答案 1 :(得分:1)

感谢您的所有建议,工作正常。

我的修改代码:

  SET @SelectQuery= @SelectQuery + ' WHERE Tbl_JobSeekers.JobSeekerStatus='''+ @JobSeekerStatus +''' ORDER BY Tbl_JobSeekers.National_Id DESC'

  EXEC sp_executesql @SelectQuery, N'@JobSeekerStatus nvarchar(20)',@JobSeekerStatus

答案 2 :(得分:0)

在SQL Books Online中查看sp_executesql存储过程。