从 Excel 工作表生成 Oracle PL/SQL 脚本

发布于 2024-09-10 02:01:24 字数 575 浏览 3 评论 0原文

我需要从 Excel 工作表生成插入脚本文件。我在生成脚本文件方面部分成功。但我遇到了一个情况,我需要任何人的帮助。

我的逻辑是这样的,读取第一个单元格,检查单元格中的值是否已存在于数据库中。如果不存在,则生成一个插入脚本,如下所示

declare 
    PK1 integer;
begin
    select tablename_seq.currval into PK1 from dual;
    insert into TableName valuestablename_seq_seq.nextval,'Blagh',1);
end;

我将 PK1 存储在哈希表中,数据具有 KEY 。这样,如果出现相同的数据在接下来的行中,使用哈希表搜索,我将获取相应数据键的哈希表值并将其参数传递给另一个插入脚本。但是每次我生成像 PK1、Pk2... 等新变量时,我都会在声明后保留“BEGIN”关键字,并在每次插入后添加“END”关键字,如果我这样做,变量的范围就会超出范围。我可能在另一个插入语句中使用那些声明的变量有一个参数。是否有机会保存 PK1,Pk2..... 具有用于脚本执行的会话/全局变量。因此它们在整个脚本执行时间内都可用。

I'm having requirement to generate a insert script file from excel sheet. I'm partly successfully in generating script file. But i got struck in a situation,I need help from any1.

My logic is some thing like this, Read first cell,check if the value in the cell already exists in DB.If not, generate an insert script as follow

declare 
    PK1 integer;
begin
    select tablename_seq.currval into PK1 from dual;
    insert into TableName valuestablename_seq_seq.nextval,'Blagh',1);
end;

Im storing PK1 in hashtable with data has KEY .so that if the same data appears in the next rows,using Hashtable search, I will get the hashtable value for corresponding data key and pass it has parameter to another insert script. But every time i generate new variable like PK1,Pk2...etc.I have keep 'BEGIN' key word after Declare and also add 'END' key word after every insert,If i do so scope of variable goes out off scope.I may be using those declared variables in another insert statements has a parameter. Is there any chance of saving PK1,Pk2..... has session/Global variables for the script execution. So they wil become avialable for entire script execution time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

清醇 2024-09-17 02:01:25

我的倾向是,电子表格的每一行都应该创建一个类似于 insert into TableName values (tablename_seq_seq.nextval,'Blagh',1) returned ID into PK1; 的语句,然后将整个内容包装起来在单个 DECLARE-BEGIN-END 块中定义了适当的变量,例如:

declare
   pk1 integer;
   pk2 integer;
begin
   insert into TableName 
       values (tablename_seq_seq.nextval,'Blagh',1) 
       returning ID into PK1;
   insert into TableName  
       values (tablename_seq_seq.nextval,'Urgh',2)  
       returning ID into PK2;
   [...]
end;

您甚至可以在一列中创建变量声明列表,在另一列中创建 SQL,然后将它们复制并粘贴到块中的正确位置。

My inclination is to say that each line of your spreadsheet should just be creating a statement like insert into TableName values (tablename_seq_seq.nextval,'Blagh',1) returning ID into PK1;, then wrap the whole thing in a single DECLARE-BEGIN-END block with the appropriate variables defined, something like:

declare
   pk1 integer;
   pk2 integer;
begin
   insert into TableName 
       values (tablename_seq_seq.nextval,'Blagh',1) 
       returning ID into PK1;
   insert into TableName  
       values (tablename_seq_seq.nextval,'Urgh',2)  
       returning ID into PK2;
   [...]
end;

You could even create the list of variable declarations in one column and the SQL in another, then copy and paste them into the right place in the block.

等风来 2024-09-17 02:01:25

我会从 然后

DECLARE
  PROCEDURE action (p_val IN INTEGER) IS
  ...
  END action;
BEGIN

让电子表格中的每一行调用该过程,以便电子表格条目 1 变成

  action (1);

然后你最终会得到类似的

DECLARE
  PROCEDURE action (p_val IN INTEGER) IS
  ...
  END action;
BEGIN
  action (1);
  action (8);
  action (23);
  action (1);
  action (1);
END;

结果 操作过程可以像你喜欢的那样复杂,将信息存储在表/数组等等。

I'd start off with a

DECLARE
  PROCEDURE action (p_val IN INTEGER) IS
  ...
  END action;
BEGIN

Then have each line in the spreadsheet just do a call to the procedure so that a spreadsheet entry of 1 becomes

  action (1);

Then you end up with something like

DECLARE
  PROCEDURE action (p_val IN INTEGER) IS
  ...
  END action;
BEGIN
  action (1);
  action (8);
  action (23);
  action (1);
  action (1);
END;

The action procedure can be as complicated as you like, storing information in tables/arrays whatever.

山人契 2024-09-17 02:01:25

问题是从电子表格更新数据库的最佳方法,还是从电子表格生成脚本的最佳方法?

我建议将电子表格数据加载到临时表中,然后使用简单的 INSERT/SELECT 语句,除非您担心唯一性冲突,在这种情况下我会使用 MERGE 语句。这比尝试为每个插入语句生成包含逻辑的脚本要容易得多。

Is the question about the best way to update a database from a spreadsheet, or the best way to generate a script from a spreadsheet?

I would recommend loading the spreadsheet data into a temporary table and then using a simple INSERT/SELECT statement, unless you're worried about uniqueness collisions in which case I would use a MERGE statement instead. This is much easier than trying to generate a script with logic for each insert statement.

雨巷深深 2024-09-17 02:01:25

考虑每个单元格,而不是每行。每个单元格都会生成插入脚本到相应的单元格中。我以同样的方式创建,问题是如果我想在第 10 行、第 10 列(单元格值)插入脚本中的某个位置使用 PK1 变量,因为我们在 Begin 块之后立即结束“结束”,PK1 的范围始终保留在 Begin 块中。为此,我创建了一个插入的 Begin,然后创建另一个插入另一个的 Begin,依此类推。@ 最后我添加 end;end; 但上述方法的问题是,我正在尝试插入 200 行 X 200 列 = 400 个单元格插入脚本。在此流程中,当我尝试运行脚本时,它会抛出运行时错误“Stack Overflow”

Instead of each Line, Consider as each cell. Each cell will generate insert script into corresponding cell. I have created in the same way, problem is If i want to use PK1 variable some where in the row 10 ,column 10 (cell value) insert script,because we are ending 'end' immediately after Begin block, the scope of PK1 always be remain in Begin block.For this i have created Begin with one insert and then create another Begin with another insert and so on.and @ the end im adding end;end;But the problem with above method is,I'm trrying to insert 200 row X 200 columns = 400 cells insert scripts. in this flow when i try to run script, it throws an runtime error ' Stack Overflow'

复古式 2024-09-17 02:01:25

如果您使用 Oracle E-Business,您可能会对 webadi 感兴趣。

该工具创建一个要填充的 Excel 文件,然后通过过程加载到数据库中。然后您可以验证您的数据。

创建自定义 Web ADI 集成器
WebADI - 使用自定义集成器

If you use Oracle E-Business, you might be interested by webadi.

This tool creates a excel files to be populated and then loaded into database via a procedure. You can then validate your data.

Creating custom Web ADI Integrators
WebADI - Using a Custom Integrator

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文