tiberius icon indicating copy to clipboard operation
tiberius copied to clipboard

Cannot execute multi-statement query

Open jonhoo opened this issue 6 years ago • 4 comments

It's currently not possible to use simple_exec to execute multiple ;-separated statements. The following code fails with "ExecResult received more than 1 resultset":

let mut lp = Core::new().unwrap();
let conn = "server=tcp:127.0.0.1,1433".to_owned();
let future = tiberius::SqlConnection::connect(lp.handle(), conn.as_str());
let mut c1 = lp.run(future).unwrap();

lp.run(c1.simple_exec("\
     SET NUMERIC_ROUNDABORT OFF; \
     SET ANSI_PADDING, ANSI_WARNINGS ON;")).unwrap();

jonhoo avatar Feb 27 '18 19:02 jonhoo

(this is on master)

jonhoo avatar Feb 27 '18 19:02 jonhoo

Yeah you can do something like simple_exec(...).into_stream().and_then(|f| f).collect() to handle multiple resultsets.

The original intent of this is that you do not unintentionally execute multiple statements and handle the wrong resultset.

Alternatively simple_exec/exec could take the first result and silently ignore others, but I'm not sure that behavior is desirable, since you won't easily find when something unintentionally goes wrong (e.g. a statement returns a empty result first, which you handle and then the results you wanted to handle and now you assume that they do not exist). So I think this is still the best approach, but maybe currently not documented enough.

steffengy avatar Feb 27 '18 21:02 steffengy

Yeah, those are good points. Documentation would help, so would modifying the error message to point you to the fact that only a single statement can be executed at a time.

jonhoo avatar Feb 27 '18 22:02 jonhoo

I believe this won't be a problem anymore with 0.4

steffengy avatar Oct 14 '19 20:10 steffengy