Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to execute multiple statements in in a single API call? #33

Closed
MahesRam opened this issue May 14, 2018 · 35 comments
Closed

How to execute multiple statements in in a single API call? #33

MahesRam opened this issue May 14, 2018 · 35 comments
Assignees
Labels
enhancement The issue is a request for improvement or a new feature feature

Comments

@MahesRam
Copy link

My SQL has multiple SQL statements. While using execute reader method I am getting error {"Multiple SQL statements in a single API call are not supported; use one API call per statement instead."}

@howryu
Copy link
Contributor

howryu commented May 15, 2018

It is not supported by Snowflake right now (a.k.a backend did not support it either) and no other driver support multiple statements. However, this is in our roadmap.

@howryu howryu added the feature label May 18, 2018
@samrogers226
Copy link

@howryu any update on this?

@larfre
Copy link

larfre commented Mar 26, 2019

@howryu Our company is also interested in this being updated.

@relloyd
Copy link

relloyd commented Apr 18, 2019

+1 from me.

@howryu
Copy link
Contributor

howryu commented Apr 18, 2019

Update: server side support of multi statement is in progress and JDBC will be the first driver supporting it. Then will come to .NET driver.

@hkapre is our PM and he will have a better timeline or ETA

@smtakeda
Copy link
Contributor

For new feature requests, please reach out the support. We can discuss here but cannot commit it. Thank you.

@dinesh-procore
Copy link

@howryu Our company is interested in this as well.

@michaeldunnegan
Copy link

If you're interested in making multiple insert statements, this may help
https://docs.snowflake.net/manuals/sql-reference/sql/insert-multi-table.html

@sfc-gh-stakeda sfc-gh-stakeda added the enhancement The issue is a request for improvement or a new feature label Jan 8, 2020
@WATYF
Copy link

WATYF commented Nov 17, 2020

Update: server side support of multi statement is in progress and JDBC will be the first driver supporting it. Then will come to .NET driver.

@hkapre is our PM and he will have a better timeline or ETA

@howryu Is there any update on this. We need to run queries with variables using the .NET driver.

@michael135
Copy link

michael135 commented Nov 23, 2020

We are also interested in multiple SQL statements via python api.
For me in such case I can provide few short queries instead of providing a long one.
Which is much more convenient to use and also to maintain.

@bplaats
Copy link

bplaats commented Dec 6, 2020

+1 here too, especially in the native stored procedure api, I could really use it now.

For python the concurrent futures package is helpful, I used it once

@benryan2010
Copy link

benryan2010 commented Dec 28, 2020

+1 here as well - I'm trying to write a Django backend for snowflake and multiple queries would be a really nice feature since snowflake has decided to omit the RETURNING (ansi standard command) from the dialect. I have to make three separate calls to get the last inserted id when one would due (I'm using the python API).

@RichieRunner
Copy link

any progress update after 3 years? 😔

@ghost
Copy link

ghost commented Apr 15, 2021

@howryu is there any update on ODBC driver multiple query support. I am trying to run multiple queries in excel using ODBC snowflake driver connection.

@pranavq212
Copy link

pranavq212 commented May 14, 2021

Any updates on this, for .net core 3.* code works on local debugging & updates it. But on after deploying to AWS lamda it gives error :

"Message": "Multiple SQL statements in a single API call are not supported; use one API call per statement instead. , StackTrace: at Snowflake.Data.Core.SFStatement.BuildResultSet(QueryExecResponse response, CancellationToken cancellationToken)\n at Snowflake.Data.Core.SFStatement.Execute(Int32 timeout, String sql, Dictionary``2 bindings, Boolean describeOnly)\n at Snowflake.Data.Client.SnowflakeDbCommand.ExecuteInternal(Boolean describeOnly)\n at Snowflake.Data.Client.SnowflakeDbCommand.ExecuteNonQuery()"

@amine-mouffok
Copy link

@lukaszkoziara
Copy link

@amine-mouffok Yes, in our project we actively use execute_string, but that method has one disadvantage - lack of possibility to safely pass parameters to query.

@TiagoACS
Copy link

Hi,
Is there any update on this?
Execution of multiple SQL statements is supported by ODBC and JDBC drivers as well as in Snowflake SQL API. It would be very useful to have this feature in .Net driver too.

/Tiago Silva

@hans2520
Copy link

hans2520 commented Feb 7, 2022

+1

@trofimchuk-t
Copy link

trofimchuk-t commented Feb 17, 2022

I've found a workaround which is works for me. You just need to wrap multiple statements with the BEGIN and END commands:

BEGIN
    <statement_1>;
    <statement_2>;
END;

With these operators I was able to use two MERGE commands to upsert parent/child data in one request.

@WRJE
Copy link

WRJE commented Feb 18, 2022

I've found a workaround which is works for me. You just need to wrap multiple statements with the BEGIN and END commands:

BEGIN
    <statement_1>;
    <statement_2>;
END;

With these operators I was able to use two MERGE commands to upsert parent/child data in one request.

You got my hopes up for a minute there. That may work with statements, but not with variables, which is what I need.

Any reason why this still hasn't been implemented almost four years later?

@TiagoACS
Copy link

Can you please provide an example @WRJE ?

@WRJE
Copy link

WRJE commented Feb 18, 2022

Can you please provide an example @WRJE ?

Any use of a var in a query, which I can do in the Snowflake console but cannot do via the .NET driver.

This example is from Snowflake's documentation:

set (min, max)=(40, 70);
select $min;

It runs fine in the console, but via the .NET driver it produces the "Multiple SQL Statements not supported" error. If I use BEGIN/END, it says "$MIN does not exist".

@TiagoACS
Copy link

Can you please provide an example @WRJE ?

Any use of a var in a query, which I can do in the Snowflake console but cannot do via the .NET driver.

This example is from Snowflake's documentation:

set (min, max)=(40, 70); select $min;

It runs fine in the console, but via the .NET driver it produces the "Multiple SQL Statements not supported" error. If I use BEGIN/END, it says "$MIN does not exist".

That's because you are using session variables. It would work with "regular" variables:
begin
let min := 40;
select :min;
--return :min;
end;
https://docs.snowflake.com/en/developer-guide/snowflake-scripting/variables.html#declaring-a-variable

Or if you set the session variable before the scripting block:
set (min, max)=(40, 70);
begin
set min = 10;
select $min; -- value 10
end;

@WRJE
Copy link

WRJE commented Feb 21, 2022

That's because you are using session variables. It would work with "regular" variables: begin let min := 40; select :min; --return :min; end; https://docs.snowflake.com/en/developer-guide/snowflake-scripting/variables.html#declaring-a-variable

Or if you set the session variable before the scripting block: set (min, max)=(40, 70); begin set min = 10; select $min; -- value 10 end;

OK, using the link you provided I was able to work up a sample of querying a value to use as a variable (which is what I needed this for):

BEGIN
LET cust VARCHAR := (SELECT TOP 1 Customer FROM Customers ORDER BY Sales DESC);
RETURN cust;
END;

So it looks like switching to "regular" variables (not sure the difference there either) allows me to at least setup a simple var and RETURN it. The problem is that I need these vars to be used in other SELECT statements. For example:

BEGIN
LET cust VARCHAR := (SELECT TOP 1 Customer FROM Customers ORDER BY Sales DESC);
SELECT * FROM Orders WHERE Customer = :cust;
END;

This runs without error in the .NET driver, but returns an empty anonymous block. No data. Even something as simple as 'SELECT :cust;" does the same. Can you use these vars in queries, or is that only for session variables?

@TiagoACS
Copy link

Yes, you can.

begin
let min := 40;
create or replace table MY_TABLE(MY_COL number) as select :min;
end;

SELECT * FROM MY_TABLE;

@WRJE
Copy link

WRJE commented Feb 22, 2022

Yes, you can.

begin let min := 40; create or replace table MY_TABLE(MY_COL number) as select :min; end;

SELECT * FROM MY_TABLE;

That isn't working for me. As-is, it produces the "multiple API calls" error because it sees the begin/end and SELECT as separate calls. If I put the SELECT inside the begin/end, it produces the same empty anonymous block, presumably because the SELECT in the CREATE statement is generating the same output as a stand-alone SELECT statement (which is an empty block).

@bcikili
Copy link

bcikili commented Jun 14, 2022

our company is also having issues and interested in any computation-wise solutions for this! hope to see some progress

@salvadorplj
Copy link

Hard to believe that this functionality doesn't exist at this point. A very basic capability.

@brettpostin
Copy link

I'm unable to get the BEGIN/END workaround to work with parameters.

The following results in a "SQL compilation error: error line 4 at position 42 invalid identifier 'GROUPCODE'":

var sql = $@"
BEGIN
    USE DATABASE {targetDatabase}; 
    CALL PIPELINES.ADD_CLIENTS_TO_ONBOARD(:groupCode, :clientCode, ',');
END";

Removing the BEGIN/END results in the original "Multiple SQL statements in a single API call are not supported; use one API call per statement instead." error:

var sql = $@"
    USE DATABASE {targetDatabase}; 
    CALL PIPELINES.ADD_CLIENTS_TO_ONBOARD(:groupCode, :clientCode, ',');
";

This line on it's own works fine:

var sql = $@"
    CALL PIPELINES.ADD_CLIENTS_TO_ONBOARD(:groupCode, :clientCode, ',');
";

Any ideas or suggestions?

@sfc-gh-hchaturvedi
Copy link
Collaborator

Multi Statement support for .NET has been addressed in the .NET release in November 2022 - For more details please refer to .NET release notes - https://community.snowflake.com/s/article/Dot-NET-Driver-Release-Notes

@sfc-gh-ext-simba-dl
Copy link
Contributor

Add parameter "MULTI_STATEMENT_COUNT" to 3, which means the query contains 3 statements. set to 0 is no limit (but not recommended, because sql injection).

@dss010101
Copy link

does this work for python as well? seeing same frustrating issue with multi-statement queries and using set varaibles

@dss010101
Copy link

were you able to figure this out..the SELECT part?

@paulverbeke
Copy link

multi statement (batch) support has been added but only without bind parameters ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement The issue is a request for improvement or a new feature feature
Projects
None yet
Development

No branches or pull requests