存储过程求助

somerenren 2012-07-12 02:32:39
表table有一个number字段的a
存储过程test有一个变量x。\

如果table表a字段有大于x的纪录y个,那么向A表插入y条a字段等于x的纪录
如果a字段没有大于x的纪录,则a字段全部加100。


求助。。。刚好书本没存储过程

有oracle更好
...全文
115 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
WSZHAO_SELECT 2014-03-26
  • 打赏
  • 举报
回复

 create or replace procedure pro_test1(in_key in number)
  as 
  x number;
  begin
  x:=in_key;
       for i in (select test1.a from t_test1 test1)
         loop
         if(i.a>x) then
         insert into t_test1(a) values(x);
         dbms_output.put_line(x);
         elsif(i.a<x) then
         update t_test1 set a=a+100 where a <x;
         dbms_output.put_line(x);
         end if;
         end loop;
          commit;
  end pro_test1;
勿勿 2012-07-12
  • 打赏
  • 举报
回复
上面的有点错误 不好意思改后的测试对了如下
 
SQL> select * from t;

A
----------
1382.4
1920
1500
2975
1500
2850
2940
5000
1800
1320
1368
1560

已选择12行。

SQL> create or replace procedure emp_test
2 (
3 x number
4 )
5 is
6 v_a employee.salary%type;
7 v_count number;
8 i number;
9 begin
10 i:=0;
11 select count(a) into v_count from t where a>x;
12 if v_count >0 then
13 for i in 1..v_count loop
14 insert into t (a) values (x);
15 end loop ;
16 else
17 update t set a=a+100 ;
18 end if;
19 end;
20 /

过程已创建。

SQL> exec emp_test(9000)

PL/SQL 过程已成功完成。

SQL> select * from t;

A
----------
1482.4
2020
1600
3075
1600
2950
3040
5100
1900
1420
1468
1660

已选择12行。

SQL> exec emp_test(3000)

PL/SQL 过程已成功完成。

SQL> select * from t;

A
----------
1482.4
2020
1600
3075
1600
2950
3040
5100
1900
1420
1468
1660
3000
3000
3000

已选择15行。
勿勿 2012-07-12
  • 打赏
  • 举报
回复
SQL> select * from tb;

NAME A
---------- ----------
SMITH 1382.4
ALLEN 1920
WARD 1500
JONES 2975
MARTIN 1500
BLAKE 2850
CLARK 2940
SCOTT 3000
KING 5000
TURNER 1800
ADAMS 1320
JAMES 1368
FORD 3000
MILLER 1560

已选择14行。

SQL> create table t(a) select a from tb;
create table t(a) select a from tb
*
第 1 行出现错误:
ORA-00922: 选项缺失或无效


SQL> create table t(a) as select a from tb;

表已创建。

SQL> select * from t
2 ;

A
----------
1382.4
1920
1500
2975
1500
2850
2940
3000
5000
1800
1320
1368
3000
1560

已选择14行。

SQL> create or replace procedure emp_test
2 (
3 x number
4 )
5 is
6
7 v_a employee.salary%type;
8 v_count number;
9 i number;
10
11 begin
12 i:=0;
13 select count(a) int v_count from t;
14 if v_count >0 then
15 for i in 1..v_count loop
16 insert into t (a) values (x);
17 end loop ;
18 else
19
20 update t set a=t+100 ;
21
22 end if;
23 end;
24 /

警告: 创建的过程带有编译错误。

SQL> show error;
PROCEDURE EMP_TEST 出现错误:

LINE/COL ERROR
-------- -----------------------------------------------------------------
13/1 PL/SQL: SQL Statement ignored
13/21 PL/SQL: ORA-00923: 未找到要求的 FROM 关键字
SQL> create or replace procedure emp_test
2 (
3 x number
4 )
5 is
6 v_a employee.salary%type;
7 v_count number;
8 i number;
9 begin
10 i:=0;
11 select count(a) int v_count from t;
12 if v_count >0 then
13 for i in 1..v_count loop
14 insert into t (a) values (x);
15 end loop ;
16 else
17 update t set a=t+100 ;
18 end if;
19 end;
20 /

警告: 创建的过程带有编译错误。


SQL> show error
PROCEDURE EMP_TEST 出现错误:

LINE/COL ERROR
-------- -----------------------------------------------------------------
17/2 PL/SQL: SQL Statement ignored
17/17 PL/SQL: ORA-00904: "T": 标识符无效
SQL> :
SP2-0042: 未知命令 ":" - 其余行忽略。
SQL> create or replace procedure emp_test
2 (
3 x number
4 )
5 is
6 v_a employee.salary%type;
7 v_count number;
8 i number;
9 begin
10 i:=0;
11 select count(a) into v_count from t;
12 if v_count >0 then
13 for i in 1..v_count loop
14 insert into t (a) values (x);
15 end loop ;
16 else
17 update t set a=a+100 ;
18 end if;
19 end;
20 /

过程已创建。

SQL> select * from t;

A
----------
1382.4
1920
1500
2975
1500
2850
2940
3000
5000
1800
1320
1368
3000
1560

已选择14行。

SQL> exec emp_test(3000)

PL/SQL 过程已成功完成。

SQL> select * from t;

A
----------
1382.4
1920
1500
2975
1500
2850
2940
3000
5000
1800
1320
1368
3000
1560
3000
3000
3000
3000
3000
3000
3000
3000
3000
3000
3000
3000
3000
3000

已选择28行。

SQL> set spool off;
SP2-0158: 未知的 SET 选项 "spool"
SQL> spool off;
天-笑 2012-07-12
  • 打赏
  • 举报
回复


create procedure pr_Test
@x numeric ,
@y int
as
begin

declare @c int,
@i int
select @c = count(1) from [table] where a > @x
if (@c >= 1)
begin
set @i = 1
while (@i <=@c)
begin
insert into [table](a)
select @x
set @i = @i + 1
end
end
else
begin
update [table] set a = a + 100

end
end

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧