简体   繁体   English

下一篇:jdbc SQL服务器如何允许多条语句

[英]next:jdbc How to allow multiple statements for SQL Server

I want to run multiple statements on a single execution against SQL Server.我想针对 SQL 服务器在一次执行中运行多个语句。 I do it with Node.js but I cannot run the same query using next.jdbc我使用 Node.js 执行此操作,但无法使用 next.jdbc 运行相同的查询

For example, if I run this:例如,如果我运行这个:

(def db {:jdbcUrl "jdbc:jtds:sqlserver://localhost:1433/TESTDB;user=sa;password=passwd"})
(def ds (jdbc/get-datasource db))
(jdbc/execute! ds ["select * from EMPLOYEE;select FIRST_NAME from EMPLOYEE;"])

I have also tried to wrap the statement within a transaction with the same result我还尝试将语句包装在具有相同结果的事务中

(jdbc/execute! ds ["BEGIN TRANSACTION select * from EMPLOYEE;select FIRST_NAME from EMPLOYEE; COMMIT"])

I always get the first query.我总是得到第一个查询。

I have tried Microsoft's JDBC driver also.我也试过微软的 JDBC 驱动程序。 Sean Corfield says that if the database supports it, then next.jdbc should support it. Sean Corfield 说如果数据库支持,那么 next.jdbc 应该支持。 next-jdbc: execute multiple statements? next-jdbc:执行多条语句?

But I cannot make it work但我不能让它工作


Solution解决方案

As indicated by Sean Corfield正如肖恩·科菲尔德所指出的

(jdbc/execute! ds ["BEGIN select * from EMPLOYEE;select FIRST_NAME from EMPLOYEE; END"] {:multi-rs true})

Yes, you can run multiple statements and multiple result sets back but you have to tell next.jdbc that's the behavior you want.是的,您可以运行多个语句和多个结果集,但您必须告诉next.jdbc这就是您想要的行为。

Take a look at the tests for MS SQL Server running multiple statements: https://github.com/seancorfield/next-jdbc/blob/develop/test/next/jdbc_test.clj#L560-L572查看运行多个语句的 MS SQL 服务器的测试: https://github.com/seancorfield/next-jdbc/blob/develop/test/next/jdbc_test.clj#L560-L572

This is mentioned (briefly) in the Getting Started guide: "If you pass the :multi-rs true option to execute! , you will get back a vector of results sets, instead of just one result set: a vector of zero or more vectors."入门指南中(简要地)提到了这一点:“如果您通过:multi-rs true选项来execute! ,您将返回一个结果集向量,而不仅仅是一个结果集:一个零个或多个向量载体。”

As far as I know, SQL Server's JDBC driver does not support multiple statements.据我所知,SQL Server的JDBC驱动支持多语句。 But, even if it did, you should probably not be using it, as it opens a potential security hole for injection type attacks.但是,即使这样做了,您也可能不应该使用它,因为它为注入型攻击打开了一个潜在的安全漏洞。 Instead, if you really need to execute multiple SQL statements, either refactor your current SQL into a single statement, or else use multiple statements wrapped in a single transaction.相反,如果您确实需要执行多个 SQL 语句,请将当前的 SQL 重构为单个语句,或者使用包装在单个事务中的多个语句。

For reference, some other JDBC drivers, such as MySQL, might support multiple statements.作为参考,其他一些 JDBC 驱动程序,例如 MySQL,可能支持多个语句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM