| How to use pivot with parameters [message #582230] | 
			Tue, 16 April 2013 04:06   | 
		 
		
			
				
				
				
					
						
						cornwall
						 Messages: 36 Registered: June 2009 
						
					 | 
					Member  | 
					 | 
		 
		 
	 | 
 
	
		How can you use pivot with a parameter? 
 
If you attempt to use a parameter then an ORA-500: bind variable not supported is given 
 
Below is a simplified version of the sql to illustrate teh problem. 
Any help would be appreciated.. 
 
create table test_pivot  
( 
ord_no number, 
qty   number, 
loc   varchar2(10) 
): 
 
insert into test_pivot values (123, 3, 'LOC1'); 
insert into test_pivot values (123, 2, 'LOC1'); 
insert into test_pivot values (234, 4, 'LOC1'); 
insert into test_pivot values (234, 5, 'LOC1'); 
 
-- this one is fine 
Select * From  
( 
Select Ord_No, Qty, Loc  
From   Test_Pivot 
Where  Ord_No In ('123', '234') 
) 
Pivot (Sum(Qty) For Ord_No In ('123', '234')  ) 
 
-- how to use pivot with parameters 
Select * From  
( 
Select Ord_No, Qty, Loc  
From   Test_Pivot 
Where  Ord_No In (:Ord1, :Ord2) 
) 
Pivot (Sum(Qty) For Ord_No In (:Ord1, :Ord2)  )
		
		
		
 |  
	| 
		
	 | 
 
 
 | 
	| 
		
 | 
	| 
		
 | 
	
		
		
			| Re: How to use pivot with parameters [message #582298 is a reply to message #582230] | 
			Tue, 16 April 2013 14:17    | 
		 
		
			
				
				
				
					
						
						Solomon Yakobson
						 Messages: 3305 Registered: January 2010  Location: Connecticut, USA
						
					 | 
					Senior Member  | 
					 | 
		 
		 
	 | 
 
	
		If you don't care about column names: 
 
variable Ord1 number
variable Ord2 number
exec :Ord1 := 123;
exec :Ord2 := 234;
Select loc,"1","2" From 
(
Select Ord_No, Qty, Loc, dense_rank() over(order by Ord_No) rnk
From Test_Pivot
Where Ord_No In (:Ord1, :Ord2)
)
Pivot (Sum(Qty),max(Ord_No) x For rnk In (1, 2) )
/
LOC                 1          2
---------- ---------- ----------
LOC1                5          9
SQL> 
  
 
SY.
		
		
		
 |  
	| 
		
	 | 
 
 
 | 
	
		
		
			| Re: How to use pivot with parameters [message #582299 is a reply to message #582232] | 
			Tue, 16 April 2013 14:23   | 
		 
		
			
				
				
				
					
						
						Solomon Yakobson
						 Messages: 3305 Registered: January 2010  Location: Connecticut, USA
						
					 | 
					Senior Member  | 
					 | 
		 
		 
	 | 
 
	
		Littlefoot wrote on Tue, 16 April 2013 05:23Ampersand instead of colon? 
  
 
Actually it can be both in SQL*Plus: 
 
variable Ord1 number
variable Ord2 number
exec :Ord1 := 123;
exec :Ord2 := 234;
set verify off
column ord1 new_value ord1 noprint
column ord2 new_value ord2 noprint
select :Ord1 Ord1,:Ord2 Ord2 from dual;
Select * From 
(
Select Ord_No, Qty, Loc
From Test_Pivot
Where Ord_No In (:Ord1, :Ord2)
)
Pivot (Sum(Qty) For Ord_No In (&Ord1, &Ord2) )
/
LOC               123        234
---------- ---------- ----------
LOC1                5          9
SQL> 
  
 
SY.
		
		
		
 |  
	| 
		
	 | 
 
 
 |