当前位置: 代码迷 >> Sql Server >> SQL 函数的语法有关问题
  详细解决方案

SQL 函数的语法有关问题

热度:43   发布时间:2016-04-24 10:29:05.0
SQL 函数的语法问题
create   function    check_unit_price(@p_org_id  int,
      @p_item_no  int,
      @p_order_number  int,
      @p_line_number  int,
      @p_cust_number  varchar(50)) returns decimal(18,8)
AS 
   begin 
   
   declare   @v_selling_price decimal(18,8) 
   declare   @v_cust          varchar(50)  
   
   declare  c1 cursor
   for  select ola.unit_selling_price *  dbo.get_day_rate(oha.creation_date,oha.transactional_curr_code,'hkd')
from oe_order_headers_all oha,
     oe_order_lines_all   ola
where  oha.header_id = ola.header_id
and oha.booked_flag = 'y'
and oha.cancelled_flag = 'n'
and ola.cancelled_flag = 'n'
and ola.unit_selling_price > 0
and  ola.org_id = @p_org_id
and inventory_item_id=@p_item_no
and pricing_quantity_uom='ctn'
and ola.unit_selling_price *  dbo.get_day_rate(oha.creation_date,oha.transactional_curr_code,'hkd')>0
order by ola.unit_selling_price *  dbo.get_day_rate(oha.creation_date,oha.transactional_curr_code,'hkd')
   
    declare c2 cursor 
for    select  ola.unit_selling_price*  dbo.get_day_rate(oha.creation_date,oha.transactional_curr_code,'hkd')
from oe_order_headers_all oha,
 oe_order_lines_all   ola
where  oha.header_id = ola.header_id
and oha.booked_flag = 'y'
and oha.cancelled_flag = 'n'
and ola.cancelled_flag = 'n'
and ola.unit_selling_price > 0
and  ola.org_id = @p_org_id
and order_number=@p_order_number
and line_number=@p_line_number
and ola.unit_selling_price *  dbo.get_day_rate(oha.creation_date,oha.transactional_curr_code,'hkd')>0
  
  
  declare c_cust cursor 
       for select  segment2
from      mtl_system_items_kfv
where    organization_id=@p_org_id
and      inventory_item_id=@p_item_no

IF (isnull(@p_order_number,0) = 0)
         open c_cust
                fetch next from c_cust into @v_cust
while (@@fetch_status <> -1)
begin
fetch next from c_cust into @v_cust
end 
close c_cust
deallocate c_cust 
    else
       set @v_cust = @p_cust_number 
        
    IF (( @v_cust in ( 'h06700', 'p01911', 's14005' )) or (isnull(@p_cust_number,0) =0 )) 
         open c1
fetch next from c1 into @v_selling_price
while (@@fetch_status <> -1)
begin
fetch next from c1 into @v_selling_price
end 
close c1
deallocate c1 
  else
         open c2
fetch next from c2 into @v_selling_price
while (@@fetch_status <> -1)
begin
fetch next from c2 into @v_selling_price
end 
close c2
deallocate c2

        return(@v_selling_price)
END 


错误提示:Incorrect syntax near the keyword 'else'.

刚刚开始接触SQL  对语法不熟,请帮我看看 谢谢
------解决方案--------------------
试试:
CREATE 	 FUNCTION check_unit_price
    (
      @p_org_id INT ,
      @p_item_no INT ,
      @p_order_number INT ,
      @p_line_number INT ,
      @p_cust_number VARCHAR(50)
    )
RETURNS DECIMAL(18, 8)
AS
    BEGIN 
   
        DECLARE @v_selling_price DECIMAL(18, 8) 
        DECLARE @v_cust VARCHAR(50)  
   
        DECLARE c1 CURSOR
        FOR
            SELECT  ola.unit_selling_price
  相关解决方案