'Fetch only first row from a result set in Snowflake stored procedure

I am writing a stored procedure in a snowflake that has to join 3 tables and output the first row in JSON format.

Is there a way to fetch only the first row??

Currently I am iterating the result set and breaking the loop after the first iteration.

   while(result.next()){
      id = result.getColumnValue(1);
      break;
    }


Solution 1:[1]

If you just call resultSet.next(); on its own (not in a WHILE statement) then it will make the first record in the resultset available e.g.

var resultSet = stmt.execute();
resultSet.next();
var my_sfDate = resultSet.getColumnValue(1);

Obviously, if you need a specific record (not just a random first record) you would need an ORDER BY clause in the statement you are executing

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 NickW