2016-01-15 77 views
2

我從SSRS報表的數據集中複製了一個查詢,例如3個參數。在實際情況中,我有許多約30個參數。我想在SSMS中測試這個查詢,並用我自己的值處理參數。但我想要填充查詢一個參數 - @ par1,其他必須具有所有值。如何忽略查詢中的參數而不進行查詢查詢

drop table testtable; 
create table testtable(param1 int,param2 int,param3 int) 
insert into testtable values 
(1,10,20),(2,11,21),(3,12,22); 

--added parametrs to help query work 
declare @par1 int; 
declare @par2 int; 
declare @par3 int; 
set @par1=1; 
set @par2= ? --what i need to put here to have only @par1 condition implemented in query 
set @par3= ? --what i need to put here to have only @par1 condition implemented in query 

-- Dataset Copyed from SSRS Report. And I dont want to delete anything from here, because query is complex in real situation. 
select * 
from testtable 
where 
[email protected] and [email protected] and [email protected] 
+0

你想忽略的參數時,它是'NULL' –

+0

如果參數爲null。我不想忽視它。在這種情況下將是Where @par = null –

+1

我不認爲你可以做你正在尋找的東西。我認爲最好的選擇是修改查詢。此外,如果你能做到這一點,而且你對查詢沒有很好的理解,那麼你可能會引入一個錯誤。 – Dane

回答

3

我想這會爲你工作

select * 
from testtable 
where ([email protected] or @par1 is null) 
    and ([email protected] or @par2 is null) 
    and ([email protected] or @par3 is null) 
+0

您修改查詢,但我不想這樣做。因爲在實際情況下參數不僅位於何處。它們存在於連接,選擇域等。 –

2

看來是沒有辦法做你想要什麼......

0

如果你不想忽略,如果參數他們Null試試這個:

select * from testtable 
where 
((param1 = @par1) or (param1 is null)) 
and ((param2 = @par2) or (param2 is null)) 
and ((param3 = @par3) or (param3 is null)) 

更新

剛剛看到您不想更改查詢。所以這不是一個解決方案。我無法想出什麼。

0

這應該工作:

select * 
from testtable 
where ISNULL(@par1,0) IN (param1,0) 
    and ISNULL(@par2,0) IN (param2,0) 
    and ISNULL(@par3,0) IN (param3,0)