plsqlselectinto
A. Oracle中insert into select和select into的区别
Oracle中insert into select和select into的区别:(select into 就相当于赋值语句,insert into是复制语句),在Oracle中,将一张表的数据复制到另外一个对象中。
通常会有这两种方法:insert into select 和 select into from。前者可以将select 出来的N行(0到任意数)结果集复制一个新表中,后者只能将"一行"结果复制到一个变量中。这样说吧,select into是PL/sql language 的赋值语句。而前者是标准的SQL语句。
做一个简单测试,我们就可以很容易地看出两者的差别。
1、首先,我们创建两个表,一个作为源表,一个作为目标表;
create table t_source(
id number primary key,
testname varchar2(20),
createtime date,
flag varchar2(10)
);
create table t_target(
id number primary key,
testname varchar2(20),
createtime date,
flag varchar2(10)
);
2、接着,插入测试数据;
insert into t_source values(1,'测试数据1....1',sysdate-2,'N');
insert into t_source values(2,'测试数据1....2',sysdate-2,'N');
insert into t_source values(3,'测试数据1....3',sysdate-2,'N');
commit;
测试insert into select 操作
insert into test2 select * from t_source where id=1;
commit;
测试select into 操作:
因为select into是一个plsql语言中的复制语句,和:=实现的目标一样。
create or replace procere sp_sync_test is
aa varchar2(100);
v_record t_source%rowtype;
begin
select t1.testname into aa from t_source t1 where id = 1;
dbms_output.put_line('普通变量 t1.testname= ' || aa);
select t1.* into v_record from t_source t1 where id = 1;
dbms_output.put_line('记录变量 t1.testname= ' || v_record.testname);
end;
3、这里增加了原始类型的变量和记录类型的变量,便于大家理解。
B. 小白初学PL/SQL 中关于SELECT INTO的问题如何解答
这就是基本的语法啊,select 。。。into。。。from 就是一个语法格式,没什么特别的。
其实你可以这样考虑。在存储过程中使用select的目的本身就是查询数据,既然要查询出来,那么肯定是要使用的,要想在过程中使用,就需要借助载体来获取到select查出的结果。这就是【变量】。通常有几种,单纯类型的变量,比如varchar2,number等等,还有就是集合,比如record,索引表等等。
殊途同归,不论使用什么样的变量来获取值,都需要通过select into从数据库中把想要的只查询出来,直接赋值或者循环赋值。
至于你说的sql server中没有这种语法,其实是不对的,sql server不是没有,只不过是写法不同罢了。sql server中的语法格式是:select @变量 = 列名 from 表名,其实和oracle的select into都是一个道理,只是写法形式上不同。
希望对你有帮助。
C. PLSQL中复制表select * into b from a where 11这句话中where 11在这里怎么理解
首先oracle里不适合你这个语法oracle里复制表的语法是create b as select * from a where 11这里11 代表只复制表结构,而不复制里边的数据因为11是个false,也就是为假,所以就不复制数据只复制表结构了如果后边改成1=1...