如何使用表数据调用存储过程? [英] how to call a stored procedure using table data?

查看:131
本文介绍了如何使用表数据调用存储过程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我试着编写了一个存储过程,它可以很好地处理静态值。



存储过程如何与动态值一起工作。



请在这里找到我的要求。

 创建一个存储过程skillsparse,它接受一串文本并将其分解为1,2,3个单词短语。 

a。例如:我爱Java,因为它很有趣,应该有这15个短语

i。 I
ii。我爱
iii。我爱java
iv。爱
诉爱java
vi。爱Java因为
vii。 Java
viii。 Java因为
ix。 Java因为它是
x。因为
xi。因为它是
xii。因为它很有趣
xiii。这是
xiv。这很有趣
xv。有趣的

3.将这些短语存储在一个名为github_skills_phrases的新表中:字段:ID,userid,skills_id(来自github_skills_source)和skills_phrase

4.创建一个storedproc, skill_phrases对技能表(ref表)并将这些值存储到每个用户的github_skills表中。如果可能,请保持技能来源(repodesc,repolang,starred,bio)

5.注意:除了Kishore创建的新表中的信息外,您还需要针对Skillsparse过程运行github_users.bio字段。因为github_users.bio已经存在并且有数据,所以你可以先启动它(用于测试逻辑等)。



我们不需要为那些尚未经过技能处理的用户进行此操作

我写的是:

  ++++++++ ++++++++++++++++++++++++++++++++++++++++++++++ 
DELIMITER $$
CREATE过程测试(IN id varchar(20),IN usr_id varchar(20),IN str varchar(200))
begin
DECLARE wordCnt varchar(20);
DECLARE wordCnt1 varchar(20);
DECLARE idx INT DEFAULT 1;
DECLARE splt varchar(200);
declare strng varchar(200);


创建临时表tmp.hello1(id varchar(200),usr_id varchar(200),st varchar(200));
set strng = str;
set wordCnt = LENGTH(trim(strng)) - LENGTH(REPLACE(trim(strng),'',''))+ 1;
set wordCnt1 = LENGTH(trim(strng)) - LENGTH(REPLACE(trim(strng),'',''))+ 1;

myloop:WHILE idx< = wordCnt DO
set splt = substring_index(trim(strng),'',idx);
插入tmp.hello1值(id,usr_id,splt);

set idx = idx + 1;
IF idx = 4 THEN
set strng = substring(trim(strng),length(substring_index(trim(strng),'',1))+ 1);
set idx = 1;
set wordCnt = wordCnt -1;
END IF;
结束;

insert into tmp.hello1 values(id,usr_id,trim(substring(trim(str),length(substring_index(trim(str),'',wordCnt1-1))+ 1))) ;
end $$

输出:

  mysql>通话测试('10','200','我是我很好的kks hhh nanj kell'); 
查询OK,1行受影响(0.77秒)

mysql>选择* from hello1;
+ ------ + -------- + --------------- +
| id | usr_id | st |
+ ------ + -------- + --------------- +
| 10 | 200 |我|
| 10 | 200 |我是|
| 10 | 200 |我是|
| 10 | 200 | am |
| 10 | 200 |是|
| 10 | 200 |是我的|
| 10 | 200 | |
| 10 | 200 |我的|
| 10 | 200 |我很好|

........
......... 。
| 10 | 200 |凯尔|
+ ------ + -------- + --------------- +
集合中有27行(0.00秒)



我的存储过程与静态值一起工作正常。如何使用表数据动态调用存储过程。
请帮我写一个存储过程来调用表数据。

解决方案

如果你的意思是你需要在某些数据表的select语句内调用这个存储过程,这是不可用的。
您有两种选择:
1-将您的过程转换为函数,然后您可以从select语句内轻松调用它。
2-编写plsql代码来调用这个过程,你可以检查下面的链接关于这一点
oracle调用存储过程内部选择


i want to create a stored procedure by using below requirement.

i tried and written a stored procedure it is working fine with the static values.

how a stored procedure will work with the dynamic values.

Please find my requirement here.

 Create a stored proc "skillsparse" that accepts a string of text and and breaks it up into 1,2,3 word phrases. 

    a.       For example: I love java because it’s fun should have these 15 phrases

    i.      I
   ii.      I love
   iii.      I love java
   iv.      Love
   v.      Love java
   vi.      Love java because
   vii.      Java
   viii.      Java because
   ix.      Java because it’s
   x.      Because
   xi.      Because it’s
   xii.      Because it’s fun
   xiii.      It’s
   xiv.      It’s fun
   xv.      fun

    3.       Store these phrases in a new table called: github_skills_phrases with fields:  ID, userid, skills_id (from github_skills_source) and skills_phrase

    4.       Create a storedproc that compares the skills_phrases against the skills table (ref Table) and store the values into the github_skills table for each user. If possible, please maintain the source of where the skills came from (repodesc, repolang, starred, bio)

    5.       NOTE: Aside from the info in the new table Kishore is creating, you will also need to run the github_users.bio field against the Skillsparse procedure.  You can start this first (for testing logic, etc) since the github_users.bio already exists and has data.



    We don’t need to go this for users who have not yet been processed for skills

How i written is:

++++++++++++++++++++++++++++++++++++++++++++++++++++++
DELIMITER $$
CREATE procedure testing(IN id varchar(20),IN usr_id varchar(20),IN str varchar(200))
begin
DECLARE wordCnt varchar(20);
DECLARE wordCnt1 varchar(20);
DECLARE idx INT DEFAULT 1;
DECLARE splt varchar(200);
declare strng varchar(200);


create temporary table tmp.hello1(id varchar(200),usr_id varchar(200),st varchar(200));
set strng = str;
set wordCnt = LENGTH(trim(strng)) - LENGTH(REPLACE(trim(strng), ' ', ''))+1;
set wordCnt1 = LENGTH(trim(strng)) - LENGTH(REPLACE(trim(strng), ' ', ''))+1;

 myloop: WHILE idx <= wordCnt DO
 set splt = substring_index(trim(strng),' ',idx);
 insert into tmp.hello1 values (id,usr_id,splt);

 set idx=idx+1;
 IF idx = 4 THEN
 set strng = substring(trim(strng),length(substring_index(trim(strng),' ',1))+1);
 set idx = 1;
 set wordCnt = wordCnt -1;
 END IF;
 end while ;

 insert into tmp.hello1 values (id,usr_id,trim(substring(trim(str),length(substring_index(trim(str),' ',wordCnt1-1))+1)));
 end $$

Out put ::

 mysql> call testing('10','200','I am the my fine the kks hhh nanj kell');
Query OK, 1 row affected (0.77 sec)

mysql> select * from hello1;
+------+--------+---------------+
| id   | usr_id | st            |
+------+--------+---------------+
| 10   | 200    | I             |
| 10   | 200    | I am          |
| 10   | 200    | I am the      |
| 10   | 200    | am            |
| 10   | 200    | am the        |
| 10   | 200    | am the my     |
| 10   | 200    | the           |
| 10   | 200    | the my        |
| 10   | 200    | the my fine   |

........ .......... | 10 | 200 | kell | +------+--------+---------------+ 27 rows in set (0.00 sec)

my stored procedure is working fine with static values .how to call dynamically a stored procdure by using table data. Please help me to write a stored procedure to calling with the table data.

解决方案

If you means you need to call this stored procedure inside select statement on certain data table, this is not available. You have two options: 1- transfer your procedure to function and then you can call it easily from inside the select statement. 2- write plsql code to call this procedure and you can check the below link about this point oracle call stored procedure inside select

这篇关于如何使用表数据调用存储过程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆