Search code examples
snowflake-cloud-data-platformdbt

Can we return a table with about 270 columns on snowflake through a stored procedure using sql? Syntax is below


Can we return a table in Snowflake using sql as language? What is the correct syntax if possible?

create or replace procedure sp()
returns table ()
language sql
as
    $$ 
    declare
    
    accountingMonth :=(select dateadd(month,case when day(current_timestamp())<=10 then -1 else 0 end,dateadd(month,0,date_trunc('month',current_date()))));
    endmonth  :=(select dateadd(month,1,dateadd(month,case when day(current_timestamp())<=10 then -1 else 0 end,dateadd(month,0,date_trunc('month',current_date())))));
    currmonth date default accountingMonth;

        
    
    begin
          
    create or replace temporary table sa as (
        select col1,col2, col3,datecol,....col270
        from table2 
where datecol = :accountingmonth
        );
    end;
      select *  from sa;
         
     $$

Solution

  • Yes, it is possible to return table from strored procedure using RESULTSET

    CREATE OR REPLACE PROCEDURE sp()
    RETURNS TABLE(col1 INTEGER, ...)
    LANGUAGE SQL
    AS
    
      BEGIN
         -- ...
         let RESULTSET DEFAULT (select * from sa);
        RETURN TABLE(res);
      END;