Snowflake - Stored Procedures using JavaScript - Working Session

Поделиться
HTML-код
  • Опубликовано: 28 дек 2024
  • You can get all snowflake Videos, PPTs, Queries, Interview questions and Practice files in my Udemy course for very less price.. I will be updating this content and will be uploading all new videos in this course.
    My Snowflake Udemy Course:
    www.udemy.com/...
    I can be reachable on jana.snowflake2@gmail.com.
    ------------------------------------------------------------
    CREATE OR REPLACE TABLE MYOWN_DB.PUBLIC.DAILY_TABLE_COUNTS
    (
    LOAD_TIME TIMESTAMP,
    DATABASE_NAME VARCHAR(30),
    SCHEMA_NAME VARCHAR(30),
    TABLE_NAME VARCHAR(40),
    TABLE_COUNT INT
    );
    CREATE OR REPLACE TABLE MYOWN_DB.PUBLIC.PROC_ERROR_LOG
    (
    PROC_NAME VARCHAR(50),
    RUN_TIME TIMESTAMP,
    ERROD_CD VARCHAR(20),
    ERROR_MSG VARCHAR(1000),
    ERROR_LINE VARCHAR(100)
    );
    // Create procedure
    CREATE OR REPLACE PROCEDURE MYPROCS.PROC_LOAD_TABLE_COUNTS(DB VARCHAR, SCH VARCHAR)
    RETURNS VARCHAR
    LANGUAGE JAVASCRIPT
    //EXECUTE AS CALLER
    AS
    $$
    var err = '';
    var procName = Object.keys(this)[0];
    var procResult = 'SUCCESSFULLY COMPLETED';
    try
    {
    var dbase = DB;
    var table_list = `SELECT TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME FROM `+dbase+`.INFORMATION_SCHEMA.TABLES WHERE TABLE_CATALOG = ? and TABLE_SCHEMA = ?`;
    var table_list_stmt = snowflake.createStatement({sqlText: table_list, binds : [DB, SCH]});
    var exe_table_list_stmt = table_list_stmt.execute();
    while (exe_table_list_stmt.next())
    {
    var dbname = exe_table_list_stmt.getColumnValue('TABLE_CATALOG');
    var schname = exe_table_list_stmt.getColumnValue('TABLE_SCHEMA');
    var tblname = exe_table_list_stmt.getColumnValue('TABLE_NAME');
    var row_count_qry = `SELECT COUNT(1) as CNT FROM `+dbname+`.`+schname+`.`+tblname+`;`;
    var row_count_stmt = snowflake.createStatement({sqlText: row_count_qry});
    var exe_row_count_stmt = row_count_stmt.execute();
    exe_row_count_stmt.next();
    var row_count = exe_row_count_stmt.getColumnValue('CNT');
    var insrt_qry = `INSERT INTO MYOWN_DB.PUBLIC.DAILY_TABLE_COUNTS(LOAD_TIME, DATABASE_NAME, SCHEMA_NAME, TABLE_NAME, TABLE_COUNT) VALUES(CURRENT_TIMESTAMP,'`+dbname+`','`+schname+`','`+tblname+`',`+row_count+`);`;
    var insrt_qry_stmt = snowflake.createStatement({sqlText: insrt_qry});
    var exe_insrt_qry_stmt = insrt_qry_stmt.execute();
    }
    }
    catch(err)
    {
    var error_query = `INSERT INTO MYOWN_DB.PUBLIC.PROC_ERROR_LOG(PROC_NAME, RUN_TIME, ERROD_CD, ERROR_MSG, ERROR_LINE) VALUES('`+procName+`', CURRENT_TIMESTAMP,'`+err.code+`','`+err.message.replace(/\'/g, "")+`','`+err.stackTraceTxt+`');`;
    var error_query_stmt = snowflake.createStatement({sqlText: error_query});
    var exe_error_query_stmt = error_query_stmt.execute();
    procResult = `FAILED, CHECK PROC_ERROR_LOG TABLE FOR ERROR DETAILS`;
    }
    return procResult;
    $$
    ;
    // Calling Procedure
    CALL MYOWN_DB.MYPROCS.PROC_LOAD_TABLE_COUNTS('SNOWFLAKE_SAMPLE_DATA', 'TPCH_SF1');
    CALL MYOWN_DB.MYPROCS.PROC_LOAD_TABLE_COUNTS('MYOWN_DB', 'PUBLIC');
    // Validating Data
    select * from MYOWN_DB.PUBLIC.DAILY_TABLE_COUNTS;
    select * from MYOWN_DB.PUBLIC.PROC_ERROR_LOG;
    // Scheduling the procedure to run daily at 7.30 AM UTC
    CREATE OR REPLACE TASK MYTASKS.TASK_TABLE_COUNTS
    WAREHOUSE = COMPUTE_WH
    SCHEDULE = 'USING CRON 30 7 * * * UTC'
    AS
    CALL MYOWN_DB.MYPROCS.PROC_LOAD_TABLE_COUNTS('SNOWFLAKE_SAMPLE_DATA', 'TPCH_SF1');

Комментарии • 32

  • @raghunaidukalvapalli
    @raghunaidukalvapalli Год назад +2

    Perfect!🎉❤...
    I was searching for the same kind of video for so long but luckily got this video.
    Really appreciated!

  • @referralhelper
    @referralhelper 2 года назад +2

    Just started working a new job which is primarily writing stored procedures and I’ve watched a lot of videos on RUclips but this is by far the best one. Great job.

  • @jimitmehta5985
    @jimitmehta5985 Год назад +1

    Excellent video.... I was not able to find a better video than this.... Can you make similar video in python in snowflake??

  • @klayanbabu
    @klayanbabu Год назад +1

    anna please explain about star and snowflake schema

  • @prakashshiva2296
    @prakashshiva2296 7 месяцев назад

    It was wonderful explanations.
    Are you taking any online live courses on snowflake. Please let me know.

    • @mrjana520
      @mrjana520  7 месяцев назад

      No, I am not taking any classes

  • @shailu1243
    @shailu1243 2 года назад

    can we also write a procedure for mailing internally from the component it self by importing relevant library in the proceedure?

    • @mrjana520
      @mrjana520  2 года назад +1

      as per my knowledge that is not available yet

  • @sravaniram-t7m
    @sravaniram-t7m 6 месяцев назад

    sir plz do playlist on snowpark

    • @mrjana520
      @mrjana520  5 месяцев назад

      I am not experienced on Snowpark

    • @sravaniram-t7m
      @sravaniram-t7m 5 месяцев назад

      @@mrjana520 😥😥

  • @imunnoticeable5757
    @imunnoticeable5757 6 месяцев назад

    Thank u for explanation,but when exactly I followed ur code ,how ever even if after some modification sp succeed .out is not getting inserted.Post some of debug ,seems ,binding has issue .can u recheck the code ..

    • @mrjana520
      @mrjana520  6 месяцев назад

      I checked and the code is perfectly working

  • @muktiarahamed890
    @muktiarahamed890 2 года назад

    could u please make a video on snowflake real time project implementation

  • @reduxCVA
    @reduxCVA 2 года назад +1

    Hi, Can we use python to create stored procs ?

    • @mrjana520
      @mrjana520  2 года назад +1

      Yes but you have to use snowpark

    • @reduxCVA
      @reduxCVA 2 года назад

      @@mrjana520 oh ok thank you

  • @spoorthihoney8512
    @spoorthihoney8512 2 года назад

    Please make a video to ingest data from aws to snowflake using matillion

  • @maneendrareddy1791
    @maneendrareddy1791 2 года назад

    please upload videos on snowflake end to end project explanation.

    • @mrjana520
      @mrjana520  2 года назад

      I can be reachable on jana.snowflake2@gmail.com

  • @NamrataSharma-cx4sw
    @NamrataSharma-cx4sw 11 месяцев назад

    how to optimize stored procedure?

    • @mrjana520
      @mrjana520  11 месяцев назад

      That is not 1 line answer, if you can optimize the sql queries inside the procedure, automatically the procedure will be optimized. I have explained about query optimization in my SQL playlist.

  • @sql-world5536
    @sql-world5536 2 года назад +1

    Hi sir
    Do one video Data modeling

    • @mrjana520
      @mrjana520  2 года назад

      I am not expert in that Bro

  • @ashokkumarreddy8396
    @ashokkumarreddy8396 Год назад

    bro is it mandatory to learn stores procedures using javascript for working as a snowflake developer

    • @mrjana520
      @mrjana520  Год назад

      yes mandatory to learn writing stored procs,
      but the language is of your choice(Java script, SQL, python, Scala)

  • @lavanyaadiseshu7489
    @lavanyaadiseshu7489 2 года назад

    Hi bro ur giving snowflake training