Summing tables in a UDF

From: Andrew (anonymous_at_discussions.microsoft.com)
Date: 08/08/04


Date: Sun, 8 Aug 2004 16:31:59 -0700

I am having a problem translating a custom function
developed in Sybase into a SQL Server UDF.

I keep getting a 208 error - 'invalid object' when I try
to run call the function in Query Analyzer even though it
compiles without errors in the Enterprize Create Function
module.

The original function used a select statement so I'm
thinking I have to use an inline table function and not a
scalar one? I was having the same issue with a scalar
function and could only get it to work once I granted
execute permissions on the function. No such property is
available for the inline table function however.

Here is the abbreviated version of the original function
I'm trying to recreate.

--------------------------- Sybase version

ALTER function gpcb.frentrev_bdgt(in evt_id integer)
returns integer
begin
  declare rentrev integer;
  select SUM(if er101_acct_order_dtl.er101_db_cr_flag
= 'D' then
      er101_acct_order_dtl.er101_ext_chrg
    else-er101_acct_order_dtl.er101_ext_chrg
    endif) into rentrev from
ev200_event_master,er101_acct_order_dtl where
    ev200_event_master.ev200_org_code =
er101_acct_order_dtl.er101_org_code and
    ev200_event_master.ev200_evt_id =
er101_acct_order_dtl.er101_evt_id and
    ev200_event_master.ev200_org_code = '02' and
    ev200_event_master.ev200_evt_id = evt_id and
    er101_acct_order_dtl.er101_phase = 'R' and
    er101_acct_order_dtl.er101_dept = 'SALES' and
    er101_acct_order_dtl.er101_res_class <= '7' and
    er101_acct_order_dtl.er101_new_res_type <> '1GENRT' and
    er101_acct_order_dtl.er101_new_res_type <> '3F&BRT' and
    er101_acct_order_dtl.er101_new_res_type <> '3OTHRT' and
    er101_acct_order_dtl.er101_res_code <> 'A500' and
    er101_acct_order_dtl.er101_res_code <> 'A400'
    group by ev200_event_master.ev200_evt_id;
  if rentrev is null then set rentrev=0
  end if;
  return(rentrev)
end

-----------------------------------------------------

What UDF type do I need to recreate this in SQL Server?

The following code compiles correctly but I get the this
erroro when I try to run it in Query Analyzer...

Server: Msg 208, Level 16, State 1, Line 1
Invalid object name 'dbo.frentrevtest'.

---------------------- SQL Server version ------------

CREATE FUNCTION dbo.frentrevtest

 (@EvtID Integer)
  
RETURNS @Result Table

(OrdTot Integer)
AS
BEGIN
        INSERT INTO @Result
                (OrdTot)
                Select Sum(
er101_acct_order_dtl.er101_ext_chrg ) AS rentTot
           From ev200_event_master, er101_acct_order_dtl
           Where ev200_event_master.ev200_org_code =
er101_acct_order_dtl.er101_org_code AND
                 ev200_event_master.ev200_evt_id =
er101_acct_order_dtl.er101_evt_id AND
                 ev200_event_master.ev200_org_code = '02'
AND
                 ev200_event_master.ev200_evt_id = @EvtID
AND
                er101_acct_order_dtl.er101_phase = '1' AND
                er101_acct_order_dtl.er101_dept = 'SALES'
AND
                er101_acct_order_dtl.er101_res_class
<= '7' AND
                er101_acct_order_dtl.er101_new_res_type
<> '1GENRT' AND
                er101_acct_order_dtl.er101_new_res_type
<> '3F&BRT' AND
                er101_acct_order_dtl.er101_new_res_type
<> '3OTHRT' AND
                er101_acct_order_dtl.er101_res_code
<> 'A500' AND
                er101_acct_order_dtl.er101_res_code
<> 'A400'
        
        
        RETURN
END

------------------------------------

I just want to be able to do a select/sum for a specific
condition on a table join. Why won't this work?

Any help would be greatly appreciated.

thx



Relevant Pages

  • Re: SQL2005 sys tables
    ... Maybe you don't have the SQL Editor toolbar open. ... You can also change the database context by using the USE statement in the ... SQL Server Documentation Team ... I mean in sql2000 query analyzer I can do the select statement against ...
    (microsoft.public.sqlserver.security)
  • RE: select during transaction**
    ... > in query analyzer I want to update ... beacuse sql server decide on clustered index scan, therefore this statement must wait on releasing locks made by UPDATE ... because sql server chose clustered index seek and it didn't touch/need ... the data locking by UPDATE from first window. ...
    (microsoft.public.sqlserver.programming)
  • Re: Using Query Analyzer with SQL 2005
    ... to 2005) was undone for the release of SP4. ... What version of SQL Server 2000 Query Analyzer are you using? ... or will I always need to keep a copy of SQL Server 2000 to install ...
    (microsoft.public.sqlserver.tools)
  • Re: Query Analyzer Erorr: Resource is low. Some results are dropped
    ... What exact build are you running? ... font or display resolution have any effect)? ... >When I use Query Analyzer on my server to either query the local SQL Server or a remote SQL Server, ...
    (microsoft.public.sqlserver.server)
  • Re: SQL Server Project Template
    ... You can create a SQL Server UDF in Managed C++ however, I do not have the SQL Server Project template as mentioned. ... Maybe I should point out that I am using Visual Studio 2008 Pro Ed SP ...
    (microsoft.public.dotnet.framework.clr)