執行此語句時遇到問題。SQL排序:帶參數的ORDER BY
我想在這裏實現的是我的結果將使用參數的條件排序。
例如,我想按升序對userName進行排序,因此,在我的web應用程序編碼中,我有一個參數@condition,當它讀取'userName'時,下面的sql語句會執行。
的錯誤是:
Msg 206, Level 16, State 2, Line 1
Operand type clash: int is incompatible with date
Msg 206, Level 16, State 2, Line 1
Operand type clash: int is incompatible with date
Msg 206, Level 16, State 2, Line 1
Operand type clash: int is incompatible with date
Msg 206, Level 16, State 2, Line 1
Operand type clash: int is incompatible with date
Msg 206, Level 16, State 2, Line 1
Operand type clash: int is incompatible with date
Msg 206, Level 16, State 2, Line 1
Operand type clash: int is incompatible with date
它出現6次。
我不確定CASE是否正確使用。
我想要的結果是,當一個條件被調用並插入到@條件,它應該排序該特定條件的結果。
數據庫的屬性在這種方式聲明:
C.joinDate - DATE
C.userName - VARCHAR(20)
C.firstName - VARCHAR(20)
C.lastName - VARCHAR(15)
C.contact - CHAR(8)
C.dob - DATE
C.userStatus - VARCHAR(8)
C.totalPoints - INT
R.resID - VARCHAR(8)
P.orderID - VARCHAR(8)
D.orderID - VARCHAR(8)
CR.securityCode - VARCHAR(10)
C.loginAttempted - INT
SELECT C.joinDate, C.userName, (C.firstName+' '+C.lastName) AS Name,
C.contact, C.dob, C.userStatus, C.totalPoints, COUNT(R.resID) AS Res,
COUNT(P.orderID) AS PreOrd, COUNT(D.orderID) AS DelOrd,
COUNT(CR.securityCode) AS Redeem, C.loginAttempted FROM Customer C
full join Reservation R ON C.userID = R.userID full join PreOrder P
ON R.resID = P.ResID full join DeliveryOrder D ON D.userID = C.userID
full join CustomerRedemption CR ON CR.userID = C.userID WHERE
(C.firstName+' '+C.lastName) LIKE '%%' and c.userName LIKE '%%' and
c.contact LIKE '%%' and c.userStatus LIKE '%%' GROUP BY C.joinDate,
C.userName, (C.firstName+' '+C.lastName), C.contact, C.dob,
C.userStatus, C.totalPoints, C.loginAttempted HAVING C.userName IS NOT
NULL ORDER BY CASE 'userName'
WHEN 'joinDate' THEN joinDate
WHEN 'userName' THEN userName
WHEN 'Name' THEN (C.firstName+' '+C.lastName)
WHEN 'dob' THEN dob
WHEN 'userSatus' THEN userStatus
WHEN 'totalPoints' THEN totalPoints
WHEN 'totalPoints' THEN totalPoints
WHEN 'Res' THEN COUNT(R.resID)
WHEN 'PreOrd' THEN COUNT(P.orderID)
WHEN 'DelOrd' THEN COUNT(D.orderID)
WHEN 'Redeem' THEN COUNT(CR.securityCode)
END
你使用了哪個數據庫? – Taryn
無論數據庫如何,如果查詢的結果將在數據庫之外使用,則由某種類型的客戶端在數據庫中不分段。在客戶端做這件事要便宜得多。 – Darek