拉記錄假設你有WIDGETS
表:SQL可以由可能的三個值或者根本沒有
- 爲widgetid INT
- ColorID INT(查找值:紅,藍,綠,黃,黑,布朗)
- SizeID INT(查找值:小,中,大,大)
- 重量INT(查找值:超輕,輕,正常,重,UltraHeavy
好吧,這是表的一般想法。我不需要查找名稱,我所擁有的是lookupvalue ID。所以從這個表,我就能夠拉列表回到符合下面要說的crieria:
- 告訴我誰是紅色或藍色或黑色的所有部件
- 告訴我誰是紅色或所有小工具藍,和小或醫學
- 告訴我誰是沉重的所有部件只
- 告訴我誰是沉重和黃所有小
- 告訴我誰是沉重和黃大或小的所有部件
- 告訴我所有隻有大號的小配件只有
- 讓我看看所有隻有綠色的小工具
想到這個主意嗎?我一直在嘗試使用Stored Proc,它允許我發送某種類型的參數。即使嘗試了動態SQL,但得到奇怪的錯誤。現在不記得了。
我嘗試3,排序的作品,如果我能得到它在一個Proc工作,並能夠調節出其加入的我想要什麼,我已經試過
例子:
注:以我的例子:(CourseID是爲widgetid)和(STATEID是ColorID)和(CreditTypeID是SizeID)和(SubjectID是WeightID)
嘗試方法1
ALTER PROCEDURE [dbo].[CourseListFullInfoByStateCreditSubject]
@StateIDs VARCHAR(200) = '',
@CreditTypeIDs VARCHAR(200) = '',
@SubjectTypeIDs VARCHAR(200) = ''
AS
BEGIN
DECLARE @SQL AS NVARCHAR(MAX)
SET @SQL = 'SELECT DISTINCT
C.CourseID,
LU.FirstName,
LU.LastName,
(SELECT COUNT(ReviewID) FROM Review AS R WHERE R.CourseID = C.CourseID) AS ReviewCount
FROM [Course] AS C WITH(NOLOCK)
JOIN LexUser AS LU ON LU.LexUserID = C.PresenterID '
If @StateIDs IS NOT NULL AND @StateIDs <> '''' AND @StateIDs <> '0'
BEGIN
SET @SQL = @SQL + ' JOIN CourseToState AS CS ON CS.CourseID = C.CourseID AND CHARINDEX('','' + CAST(CS.StateID AS VARCHAR) + '','', '','' + @StateIDs + '','') > 0 '
END
If @CreditTypeIDs IS NOT NULL AND @CreditTypeIDs <> '' AND @CreditTypeIDs <> '0'
BEGIN
SET @SQL = @SQL + 'JOIN CourseToCreditType As CC ON CC.CourseID = C.CourseID AND CHARINDEX('','' + CAST(CC.CreditTypeID AS VARCHAR) + '','', '','' + @CreditTypeIDs + '','') > 0 '
END
If @SubjectTypeIDs IS NOT NULL AND @SubjectTypeIDs <> '' AND @SubjectTypeIDs <> '0'
BEGIN
SET @SQL = @SQL + 'JOIN CourseToSubject As CSu ON CSu.CourseID = C.CourseID AND CHARINDEX('','' + CAST(CSu.SubjectID AS VARCHAR) + '','', '','' + @SubjectTypeIDs + '','') > 0 '
END
EXEC sp_executesql @SQL
有了嘗試1我嘗試發送的ID的:
[CourseListFullInfoByStateCreditSubject] ''1,2,4'', ''0'', ''0''
...但我得到一個錯誤,「附近有語法錯誤 '1'。「
嘗試方法2,使同樣的錯誤
DECLARE @SQL AS NVARCHAR(MAX)
SET @SQL = 'SELECT DISTINCT
C.CourseID,
LU.FirstName,
LU.LastName,
(SELECT COUNT(ReviewID)
FROM Review AS R
WHERE R.CourseID = C.CourseID) AS ReviewCount
FROM [Course] AS C WITH(NOLOCK)
JOIN LexUser AS LU ON LU.LexUserID = C.PresenterID '
If @StateIDs IS NOT NULL AND @StateIDs <> '''' AND @StateIDs <> '0'
BEGIN
SET @SQL = @SQL + ' JOIN CourseToState AS CS ON CS.CourseID = C.CourseID AND CHARINDEX('','' + CAST(CS.StateID AS VARCHAR) + '','', '','' + @StateIDs + '','') > 0 '
END
If @CreditTypeIDs IS NOT NULL AND @CreditTypeIDs <> '' AND @CreditTypeIDs <> '0'
BEGIN
SET @SQL = @SQL + 'JOIN CourseToCreditType As CC ON CC.CourseID = C.CourseID AND CHARINDEX('','' + CAST(CC.CreditTypeID AS VARCHAR) + '','', '','' + @CreditTypeIDs + '','') > 0'
END
If @SubjectTypeIDs IS NOT NULL AND @SubjectTypeIDs <> '' AND @SubjectTypeIDs <> '0'
BEGIN
SET @SQL = @SQL + 'JOIN CourseToSubject As CSu ON CSu.CourseID = C.CourseID AND CHARINDEX('','' + CAST(CSu.SubjectID AS VARCHAR) + '','', '','' + @SubjectTypeIDs + '','') > 0'
END
嘗試方法3 - 排序作品 此方法僅如果我把它的ID對每個菲爾德的作品,我不能離開一個空白得到「所有」,我不能把它在一個存儲過程,而這正是我試圖嘗試#做2
DECLARE @StateIDs VARCHAR(200) = ''
DECLARE @CreditTypeIDs VARCHAR(200) = ''
DECLARE @SubjectTypeIDs VARCHAR(200) = ''
SET @StateIDs = '1,3,2,'
SET @CreditTypeIDs = '1,3'
SET @SubjectTypeIDs = '1,2,3,4'
SELECT DISTINCT
C.CourseID,
LU.FirstName,
LU.LastName,
(SELECT COUNT(ReviewID) FROM Review AS R WHERE R.CourseID = C.CourseID) AS ReviewCount
FROM [Course] AS C WITH(NOLOCK)
JOIN LexUser AS LU ON LU.LexUserID = C.PresenterID
JOIN CourseToState AS CS ON CS.CourseID = C.CourseID AND CHARINDEX(',' + CAST(CS.StateID AS VARCHAR) + ',', ',' + @StateIDs + ',') > 0
JOIN CourseToCreditType As CC ON CC.CourseID = C.CourseID AND CHARINDEX(',' + CAST(CC.CreditTypeID AS VARCHAR) + ',', ',' + @CreditTypeIDs + ',') > 0
JOIN CourseToSubject As CSu ON CSu.CourseID = C.CourseID AND CHARINDEX(',' + CAST(CSu.SubjectID AS VARCHAR) + ',', ',' + @SubjectTypeIDs + ',') > 0