0
我想創建一個存儲過程以從表格中選擇行,具體順序取決於輸入變量。我想這樣的代碼:如何在多個案例中選擇表格
ALTER proc [dbo].[advance_search] (
@keyword nvarchar(50),
@casi nvarchar(50),
@loai nvarchar(5),
@order nchar(10)
)
as
select *
from Nhac n
inner join CaSi cs on n.MaCS = cs.MaCS
where TenBH like '%'[email protected]+'%' AND TenCS like '%'[email protected]+'%'
ORDER BY
CASE
WHEN @loai = 'Ngayramat' AND @order = 'DESC' THEN n.Ngayramat
ELSE NULL
END DESC,
CASE
WHEN @loai = 'Ngayramat' AND @order = 'ASC' THEN n.Ngayramat
ELSE NULL
END ASC,
CASE
WHEN @loai = 'TenBH' AND @order = 'DESC' THEN n.TenBH
ELSE NULL
END DESC,
CASE
WHEN @loai = 'TenBH' AND @order = 'ASC' THEN n.TenBH
ELSE NULL
END ASC,
CASE
WHEN @loai = 'Luotnghe' AND @order = 'DESC' THEN n.Luotnghe
ELSE NULL
END DESC,
CASE
WHEN @loai = 'Luotnghe' AND @order = 'ASC' THEN n.Luotnghe
ELSE NULL
END ASC
我想從2個表Casi
和Nhac
與訂單都選擇依賴於輸入變量。
例如:如果輸入變量:
@loai = 'luotnghe', @order = 'DESC'
的命令是:
select *
from Nhac n inner join CaSi cs on n.MaCS = cs.MaCS
where TenBH like '%'[email protected]+'%' AND TenCS like '%'[email protected]+'%'
ORDER BY n.luotnghe DESC
任何幫助將是巨大的
當您在存儲過程之外嘗試查詢時會發生什麼?它是否正確解析?它是否產生了預期的結果,但順序不是你所期望的? – dasblinkenlight
您的查詢看起來像是在做你想做的事。問題是什麼? –
@GordonLinoff它不按我想要的那樣排序。作爲例子,它返回的結果沒有被n.luotnghe –