使用Groovy使用OUT参数调用MS SQL存储过程时遇到问题

时间:2022-10-14 16:39:52

When calling this MS-SQL stored procedure:

调用此MS-SQL存储过程时:

CREATE PROCEDURE [dbo].[AddUser] 

@networkID varchar(512), 
@id uniqueidentifier OUTPUT 
.... 

With this Groovy code:

使用这个Groovy代码:

networkAliasID = 'aschoewe_test' 
sql.call("{call AddUser(?, ${Sql.VARCHAR}) }", [networkAliasID]) { s -> 
   println "userID: ${s}" 
}

I receive this error:

我收到此错误:

java.sql.SQLException: Incorrect syntax near '.'. 

This is because it doesn't like the 'Sql.' part of the code code (even though all examples I've seen does it like this). If I change the code to:

这是因为它不喜欢'Sql'。代码的一部分(尽管我见过的所有例子都是这样的)。如果我将代码更改为:

networkAliasID = 'aschoewe_test' 
String userID = null 
sql.call("{call AddUser(?, ?) }", [networkAliasID, userID]) { s -> 
   println "userID: ${s}" 
   println "userID: ${userID}" 
} 

The code executes, but I can't get the new user ID. Both 's' and 'userID' are null when the code finished executing.

代码执行,但我无法获取新的用户ID。代码完成执行时,'s'和'userID'都为null。

Am I doing something systematically wrong? Is this an issue with MSSQL? Is this an issue with the return type being UNIQUEIDENTIFIER? I'm using the JTDS drivers and the documentation shows that it's JDBC equiavalent type is VARCHAR.

我是在做一些系统性的错误吗?这是MSSQL的问题吗?这是返回类型为UNIQUEIDENTIFIER的问题吗?我正在使用JTDS驱动程序,文档显示它的JDBC等价类型是VARCHAR。

Any help is appreciated,

任何帮助表示赞赏,

Andrew

1 个解决方案

#1


The type of the output parameter should go in the parameter list, not the query string, like so:

输出参数的类型应该放在参数列表中,而不是查询字符串,如下所示:

networkAliasID = 'aschoewe_test'
sql.call("{call AddUser(?, ?) }", [networkAliasID, Sql.VARCHAR]) { s -> 
    println "userID: ${s}"
}

#1


The type of the output parameter should go in the parameter list, not the query string, like so:

输出参数的类型应该放在参数列表中,而不是查询字符串,如下所示:

networkAliasID = 'aschoewe_test'
sql.call("{call AddUser(?, ?) }", [networkAliasID, Sql.VARCHAR]) { s -> 
    println "userID: ${s}"
}