|
| 1 | +--- |
| 2 | +title: SQL API |
| 3 | +description: Get started with SQL API in LocalStack for Snowflake |
| 4 | +tags: ["Base"] |
| 5 | +--- |
| 6 | + |
| 7 | +## Introduction |
| 8 | + |
| 9 | +The [Snowflake SQL API](https://docs.snowflake.com/en/developer-guide/sql-api/about-sql-api) allows you to submit SQL statements for execution over HTTP. LocalStack for Snowflake supports the SQL API, enabling you to execute single or multiple SQL statements locally. |
| 10 | + |
| 11 | +## Multi-Statement Execution |
| 12 | + |
| 13 | +The Snowflake emulator supports [submitting multiple SQL statements in a single request](https://docs.snowflake.com/en/developer-guide/sql-api/submitting-multiple-statements). Separate each statement with a semicolon (`;`) and specify the statement count using one of the following methods: |
| 14 | + |
| 15 | +### Session-level configuration |
| 16 | + |
| 17 | +Set `MULTI_STATEMENT_COUNT` in the session parameters. This setting is persistent for the entire session. When set to `0`, the emulator accepts any number of statements without requiring an exact count. If set to a non-zero value, you must specify exactly that many statements in each batch. |
| 18 | + |
| 19 | +```python showLineNumbers |
| 20 | +conn = snowflake.connector.connect( |
| 21 | + user="test", |
| 22 | + password="test", |
| 23 | + account="test", |
| 24 | + host="snowflake.localhost.localstack.cloud", |
| 25 | + session_parameters={"MULTI_STATEMENT_COUNT": 0} |
| 26 | +) |
| 27 | + |
| 28 | +with conn.cursor() as cur: |
| 29 | + cur.execute("SELECT 1; SELECT 2; SELECT 3") |
| 30 | + print(list(cur)) # First result |
| 31 | + cur.nextset() |
| 32 | + print(list(cur)) # Second result |
| 33 | +``` |
| 34 | + |
| 35 | +### Request-level configuration |
| 36 | + |
| 37 | +Specify `num_statements` per query using the Python connector's `execute()` method. |
| 38 | + |
| 39 | +```python showLineNumbers |
| 40 | +with conn.cursor() as cur: |
| 41 | + cur.execute("SELECT 1; SELECT 2; SELECT 3", num_statements=3) |
| 42 | + print(list(cur)) # First result |
| 43 | + cur.nextset() |
| 44 | + print(list(cur)) # Second result |
| 45 | +``` |
| 46 | + |
| 47 | +If `MULTI_STATEMENT_COUNT` does not match the actual number of statements, an error is returned: |
| 48 | + |
| 49 | +``` |
| 50 | +Actual statement count <actual_count> did not match the desired statement count <desired_count>. |
| 51 | +``` |
0 commit comments