Question

I was wondering why this simple sql function won't work?

CREATE FUNCTION dbo.getTableCounts (@STREAM_ID nvarchar(10))
RETURNS table
AS RETURN ( select 1 as 'one', 2 as 'two');

When I run it in qwerybuilder I get two error messages:

Incorrect syntax near the keyword 'table' (for line 2)

and

Incorrect syntax near ',' (for line 3)

I don't understand how this is incorrect syntax. Does anyone see why this is an error?

Was it helpful?

Solution

According to ASE's Reference manual You can only return a scalar expression:

create function [ owner_name. ] function_name 
    [ ( @parameter_name [as] parameter_datatype [ = default ]
        [ ,...n ] ) ] 
    returns return_datatype
    [ with recompile]
    as 
    [begin]
    function_body 
    return scalar_expression
    [end]

So you can't use table as data type.

OTHER TIPS

Unlike Microsoft SQL Server, in Sybase if you need table as a result type you don't use a function but a stored procedure. In your case the definition of the procedure (written in Watcom-SQL) will look like:

CREATE PROCEDURE getTableCounts(@STREAM_ID NVARCHAR(10))
RESULT(one INT, two INT)
BEGIN
  SELECT 1 AS one, 2 AS two;
END;

However this option is not supported by Transact-SQL dialect in Sybase.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top