2012-08-16 59 views
1

我具有類似於下面是否可以在存儲過程的參數中放置一個列名?

Date |field1 | qty1 | qty2 | qty3 
1 Aug | xyz | 0 | 0 | 3 
1 Aug | xyz | 3 | 0 | 0 
1 Aug | abc | 0 | 5 | 0 
2 Aug | abc | 0 | 15 | 0 
2 Aug | xyz | 0 | 12 | 0 
2 Aug | xyz | 5 | 0 | 0 

我寫三個存儲過程,以如下面分別顯示每個數量的表。

這是我的第一個程序

create procedure firstprocedure 
@startdate datetime, @enddate datetime 
As 
select date, sum (case when field1 = 'xyz', qty1) as XYZ, 
sum (case when field1 = 'abc', qty1) as ABC 
from table1 
where date between @startdate and @enddate 
group by date 

這是我的第二個過程

Create procedure secondprocedure 
@startdate datetime, @enddate datetime 
As 
select date, sum (case when field1 = 'xyz', qty2) as XYZ, 
sum (case when field1 = 'abc', qty2) as ABC 
from table1 
where date between @startdate and @enddate 
group by date 

這是我的第三個過程

Create procedure thirdprocedure 
@startdate datetime, @enddate datetime 
As 
select date, sum (case when field1 = 'xyz', qty3) as XYZ, 
sum (case when field1 = 'abc', qty3) as ABC 
from table1 
where date between @startdate and @enddate 
group by date 

我不知道有沒有機會,我只需將列(不管是qty1,qty2還是qty3)在參數和執行時只是提及,如果我想qty1 o r qty2或qty3。它應該產生相應的輸出。

+0

如果可能的話,在繼續之前將表格標準化會更好。應該有一個列('qtytype'),用於存儲處理的數量類型('1','2'或'3'之一),然後是包含該值的* single *'qty'列。 – 2012-08-16 08:13:33

回答

1

試試這個:

創建這樣一個過程:

create procedure my_procedure(@startdate datetime, @enddate datetime,@qty_type varchar(50)) 
as 
begin 
    with cte as( 
    select 'qty1' [col_name],date, sum (case when field1 = 'xyz' then qty1 end) as XYZ, 
    sum (case when field1 = 'abc' then qty1 end) as ABC 
    from table1 
    where date between @startdate and @enddate 
    group by date 
    union all 
    select 'qty2' [col_name],date, sum (case when field1 = 'xyz' then qty2 end) as XYZ, 
    sum (case when field1 = 'abc' then qty2 end) as ABC 
    from table1 
    where date between @startdate and @enddate 
    group by date 
    union all 
    select 'qty3' [col_name],date, sum (case when field1 = 'xyz' then qty3 end) as XYZ, 
    sum (case when field1 = 'abc' then qty3 end) as ABC 
    from table1 
    where date between @startdate and @enddate 
    group by date 
    )select * from cte where [col_name][email protected]_type 
end 

然後執行它:

exec '2012-04-01','2012-05-01','qty1' 

exec '2012-04-01','2012-05-01','qty2' 
+0

謝謝約瑟夫。它的工作原理 – user1449596 2012-08-16 08:52:18

1

你可以使用動態SQL作爲如下所示:

CREATE PROCEDURE procedureName 
    @ColumnName NVARCHAR(100), 
    @startdate datetime, 
    @enddate datetime 
AS 
DECLARE @SQL NVARCHAR(MAX) 
SET @SQL = N' 
    select date, sum (case when field1 = ''''xyz'''', ' + @ColumnName + ') as XYZ, 
    sum (case when field1 = ''''abc'''', ' + @ColumnName + ') as ABC 
    from table1 
    where date between ' + CONVERT(DateTime, @startdate, 101) + 
     ' and ' + CONVERT(DateTime, @enddate, 101) + 
    'group by date' 

EXEC(@SQL) 

您可以調用存儲過程以這種方式(從SQL):

EXEC procedureName 'qty1', '01/01/2012', '01/01/2011' 

注意

當我用CONVERT(DateTime, @startdate, 101),你將不得不改變風格值(目前爲101)以滿足您使用的日期格式的要求。詳情請參閱此頁面:MSDN CONVERT

+0

謝謝...請問我可以讓我知道如何使用參數執行程序?而且我修改了原始問題的數據參數... – user1449596 2012-08-16 08:21:06

+0

@ user1449596我已根據您的更改更新了我的答案。 – XN16 2012-08-16 09:45:11

+0

謝謝亞歷克斯 我需要寫exec(@sql)嗎?如果我不寫它會顯示「命令(s)成功完成」,但沒有輸出顯示。如果我編寫exec(@sql),它會顯示錯誤''xyz'附近的語法錯誤' – user1449596 2012-08-16 11:33:53

相關問題