如何使用表数据调用存储过程?

时间:2013-12-15 15:06:56

标签: mysql sql function stored-procedures

我想使用以下要求创建存储过程。

我尝试并编写了一个存储过程,它可以正常运行静态值。

存储过程如何与动态值一起使用。

请在此处找到我的要求。

 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

我的写作是:

++++++++++++++++++++++++++++++++++++++++++++++++++++++
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 |凯尔|     + ------ + -------- + -------- +     设置27行(0.00秒)

我的存储过程可以正常使用静态值。如何使用表数据动态调用存储的过程。 请帮我编写一个存储过程来调用表数据。

1 个答案:

答案 0 :(得分:0)

如果您需要在某个数据表的select语句中调用此存储过程,则无法使用。 您有两种选择: 1-将您的程序转移到功能,然后您可以从select语句中轻松调用它。 2-编写plsql代码来调用此过程,您可以查看以下关于此点的链接 oracle call stored procedure inside select