0

Need help to call variable inside snowflake stored procedure. It throws a "Missing column specification" - how to access variable allcols inside dynamic SQL in snowflake?

CREATE OR REPLACE PROCEDURE SP_TES()
RETURNS VARCHAR NOT NULL
LANGUAGE SQL
EXECUTE AS CALLER
AS 
$$
DECLARE 
    
    allcols VARCHAR(16777216);
 
    sql_text_test VARCHAR(16777216);
    
BEGIN

           SELECT LISTAGG( '''' || NAME || '''', ',') WITHIN GROUP (ORDER BY NAME) 
            INTO :allcols
            FROM(SELECT DISTINCT NAME FROM PARSEDNUM UNION SELECT DISTINCT NAME FROM PARSEDCHAR) 
           AS NAMES;

         sql_text_test := ' 
                CREATE OR REPLACE TEMP TABLE TEST as 
                    select
                        a.DATA_KEY_ as DATA_KEY_, 
                         ' || allcols || '
                        
                    from transposednum AS a 
                    left join transposedchar AS b
                    on a.DATA_KEY_ = b.DATA_KEY_
                
            ';
            EXECUTE IMMEDIATE sql_text_test;```
          
    RETURN 'Table  created successfully';

END;
$$;
2
  • Temporarily change your SP to return sql_text_test (and don't execute it) and then you'll be able to see the actual statement being run and debug it Commented Jul 23, 2024 at 18:33
  • Still gave same error and I tried set allcols and calling them in sql, SET allcols = ( SELECT LISTAGG('''' || NAME || '''', ',') WITHIN GROUP (ORDER BY NAME) FROM ( SELECT DISTINCT NAME FROM PARSEDNUM UNION SELECT DISTINCT NAME FROM PARSEDCHAR ) AS NAMES ); Now this throws below error Uncaught exception of type 'STATEMENT_ERROR' on line 28 at position 12 : Assignment to 'ALLCOLS' not done because value exceeds size limit for variables. Its size is 1,768; Commented Jul 23, 2024 at 19:13

1 Answer 1

0

i tried this and was able to execute, let me know if this is what you wanted.

    create table PARSEDNUM(name char(2000)) ;
create table PARSEDCHAR(name char(2000));
insert into PARSEDNUM values('PARSEDNUM');
insert into PARSEDNUM values('PARSEDCHAR');

CREATE OR REPLACE PROCEDURE SP_TES()
RETURNS VARCHAR NOT NULL
LANGUAGE SQL
EXECUTE AS CALLER
AS 
$$
DECLARE 
    
    allcols VARCHAR(16777216);
 
    sql_text_test VARCHAR(16777216);
    
BEGIN

           SELECT LISTAGG( '''' || NAME || '''', ',') WITHIN GROUP (ORDER BY NAME) 
            INTO :allcols
            FROM(SELECT DISTINCT NAME FROM PARSEDNUM UNION SELECT DISTINCT NAME FROM PARSEDCHAR) 
           AS NAMES;

         sql_text_test := ' 
                CREATE OR REPLACE TEMP TABLE TEST as 
                    select
                        a.DATA_KEY_ as DATA_KEY_, 
                         ' || :allcols || '
                        
                    from transposednum AS a 
                    left join transposedchar AS b
                    on a.DATA_KEY_ = b.DATA_KEY_
                
            ';
      --      EXECUTE IMMEDIATE sql_text_test;```
          
    RETURN sql_text_test;

END;
$$;

 call SP_TES();

The above execution returns the following statement, CREATE OR REPLACE TEMP TABLE TEST as select a.DATA_KEY_ as DATA_KEY_, 'PARSEDCHAR','PARSEDNUM'

                    from transposednum AS a 
                    left join transposedchar AS b
                    on a.DATA_KEY_ = b.DATA_KEY;

I created a table as follows, create table transposednum (DATA_KEY_ char(200)); create or replace table transposedchar (DATA_KEY char(200));

It returns error on the "'PARSEDCHAR','PARSEDNUM'" , are they the new columns that you will create or they are existing in the table transposednum or transposedchar ?

If you want to create table on the basis of columns in the PARSEDCHAR or PARSEDNUM table then you can use the column_name from information_schema.columns views.

Sign up to request clarification or add additional context in comments.

4 Comments

This still throws same missing column specification
Please check the edited answer and what are the values in the PARSEDNUM and PARSEDCHAR tables ?
If possible, can you post the table structure and sample data in the tables used in the procedure.
Thanks Himanshu, I am able to retrieve column headers from the temp table, when trying to select column I am not getting all columns with values.I believe , I am missing a portion of the code.Let me post as new comment .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.