简体   繁体   English

无法使用 execBatch() 在 Hana 数据库中创建用户

[英]Unable to create users in Hana Database Using execBatch()

I have an XSA System and XSA Database User.我有一个 XSA 系统和 XSA 数据库用户。 This XSA DB user will be used to create new Hana database users in XSA.此 XSA 数据库用户将用于在 XSA 中创建新的 Hana 数据库用户。 I am trying to create a batch of users with the help of execBatch() functionality.我试图在 execBatch() 功能的帮助下创建一批用户。 I have a cap with node.js project.我有一个 node.js 项目的上限。

I am executing sql queries in my node.js file.我在 node.js 文件中执行 sql 查询。 Below is the code-下面是代码——

console.log("Targeting User Creation")
try{
       let createuserstatement = await xsaDbConn.preparePromisified("CREATE USER ? PASSWORD AbcYes NO FORCE_FIRST_PASSWORD_CHANGE")
       await createuserstatement.execBatch(createusersarr)
       console.log(`Users created.`) 
}catch(err){
       console.log(err.stack)
}

createusersarr is the array which contains the list of users to be created. createusersarr 是包含要创建的用户列表的数组。 The array looks like this -数组看起来像这样 -

[ [ 'SAC_XSA_HDB_USER_ABC1' ], [ 'SAC_XSA_HDB_USER_ABC2' ], 
  [ 'SAC_XSA_HDB_USER_ABC3' ], [ 'SAC_XSA_HDB_USER_ABC4' ], 
  [ 'SAC_XSA_HDB_USER_ABC5' ], [ 'SAC_XSA_HDB_USER_ABC6' ], 
  [ 'SAC_XSA_HDB_USER_ABC7' ], [ 'SAC_XSA_HDB_USER_ABC8' ], 
  [ 'SAC_XSA_HDB_USER_ABC9' ], [ 'SAC_XSA_HDB_USER_ABC10' ] ]

When I run my node app,当我运行我的节点应用程序时,

It doesnt create the users and the error shown is undefined as below -它不会创建用户,并且显示的错误未定义如下 -

ERROR - Targeting User Creation

undefined

I am not sure why this is failing?我不确定为什么这会失败? It runs fine when I use exec() instead of execBatch() and provide user name one by one in the create user query (basic loop mechanism).当我使用 exec() 而不是 execBatch() 并在创建用户查询(基本循环机制)中一一提供用户名时,它运行良好。

To figure out the problem you can use HANA Studio or XSA SQL console to see that statement CREATE USER ? PASSWORD "qwe"要找出问题,您可以使用 HANA Studio 或 XSA SQL 控制台查看该语句CREATE USER ? PASSWORD "qwe" CREATE USER ? PASSWORD "qwe" cannot use placeholder. CREATE USER ? PASSWORD "qwe"不能使用占位符。 User name here is an object, and placeholders are parameters only, they cannot substitute object name.这里的用户名是一个对象,占位符只是参数,不能代替对象名。

I didn't find the doc link (this is SAP, so not surprised), but it works this way in other databases.我没有找到文档链接(这是 SAP,所以并不感到惊讶),但它在其他数据库中以这种方式工作。

EDIT : By "object" I mean something that persist in database system tables: table, view, sequence, function, user, role, privilege etc. It is an entity which is part of your model or part of a system, which you can "use" (call, select from, create, alter etc).编辑:“对象”是指在数据库系统表中持久存在的东西:表、视图、序列、函数、用户、角色、权限等。它是一个实体,它是您的模型的一部分或系统的一部分,您可以“使用”(调用、选择、创建、更改等)。 It is part of a syntax (in documentation you'll find in place for it).它是语法的一部分(在文档中你会找到它)。 The data is the content which you can manipulate, and it can be substituted with placeholders.数据是您可以操作的内容,可以用占位符代替。 Of course, metadata is also stored somewhere, but it is an another level of abstraction.当然,元数据也存储在某个地方,但它是另一个抽象层次。

To make your user name a data you should write some procedure which takes a user name, creates SQL statement and issues it with EXECUTE IMMEDIATE (so it calls for SQL parser to parse the entire string as it is).要使您的用户名成为数据,您应该编写一些采用用户名的过程,创建 SQL 语句并使用EXECUTE IMMEDIATE发出它(因此它要求 SQL 解析器按原样解析整个字符串)。 Then you should call for that procedure with ?那么你应该用 ? as user name parameter and pass the dynamic user name to be created.作为用户名参数并传递要创建的动态用户名。

create procedure p_create_user( in p_uname nvarchar(20))
language sqlscript
sql security invoker
as
begin
  declare lv_sql nvarchar(1000);
  lv_sql := replace('create user <uname> password "QWEasd!23"', '<uname>', p_uname);
  execute immediate lv_sql;
end;

call p_create_user('demo');

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

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