2016-08-16 16 views
0

enter image description hereSQL Server查詢條件爲存儲在表列

我想我的查詢得到誰擁有這取決於條件 在Condition列在SQL Server查詢軍銜員工文本

+1

我不認爲這會在這裏結束。 SQL非常擅長存儲_data_,但用於查詢數據的邏輯屬於查詢本身。 –

+0

您需要閱讀有關動態SQL的信息。在你走下這條路之前,確保你瞭解了陷阱。您上面的條件可以輕鬆地存儲在表格中。 – shawnt00

+0

哦,所以我會得到其他方式。 – Hisham

回答

0

你可以通過在存儲過程中使用動態SQL來實現這一點。

CREATE PROCEDURE SelectEmpWithComdition @mgr int 
AS 
BEGIN 
declare @query varchar(max), @condition varchar(max) 
set @query = 'select empid, empname, rank from employees where ' 
select @condition = Condtition from Conditions where mgr = @mgr 

set @query = @query + @condition 
exec sp_executesql @query 

END 
0

它會給你一些想法。

Declare @Command varchar(Max) 
Declare @condition varchar(100) 

Select @condition = condition from [rank] where mgr = 2 

Set @Command = 'Select * from Empployee 
where ' + @condition 

Execute (@Command)