-1

検索結果を絞り込むクエリを動的に作成する必要がある状況にあります。件名/標準チェックボックスからのユーザーの選択に応じて、クエリに「AND」条件を動的に追加する必要があります。

SELECT First_Name, Last_Name, City, Subject_1, Subject_2, Subject_3, 
       Standard_From, Standard_To FROM TutorProfile WHERE City = 'Mumbai'  AND (Subject_1 = 'Maths' OR Subject_2 = 'Maths' OR Subject_3
 = 'Maths') AND (Subject_1 = 'Physics' OR Subject_2 = 'Physics' OR Subject_3 = 'Physics') .. AND (Standar_From = 'Class XII') ..
4

1 に答える 1

0

次のようなことを試してください:

DECLARE @SQL NVARCHAR(500)
DECLARE @SubjectCommand VARCHAR(50)

SET @SubjectCommand = ''

IF (SubjectIsSelected)
BEGIN
    SET  @SubjectCommand = 'AND (Subject_1 = ''Maths'' OR Subject_2 = ''Maths'' OR Subject_3 = ''Maths'')'
END

SET @SQL = 'SELECT First_Name, Last_Name, City, Subject_1, Subject_2, Subject_3, 
   Standard_From, Standard_To FROM TutorProfile WHERE City = ''Mumbai''' +@SubjectCommand 

EXEC(@SQL)
于 2013-06-14T15:48:12.320 に答える