SYBASE的select into与insert into使用和区别

本文介绍了SYBASE数据库中复制表数据的两种方法:select into和insert into。select into用于创建新表并填充数据,要求目标表不存在,且不能直接插入常量;而insert into则向已存在的表中插入数据,允许插入常量。通过多个例子展示了它们的不同用法和注意事项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

对于表的部分或全部字段的复制,Sybase数据库提供了两种方式:select into和insert into。

select into:

语法:select  value1, value2, value3 ...into table2 from table1.

注意:

①被插入的表table2必须是不存在的,在执行语句的时候创建table2表。如果已经存在,执行时会报错。

②插入的value1,value2,value3等不能是常量

(2016/09/05日修正:虽然不能直接写一个常量,但是插入常量也是可以的,只需要给这个插入的常量命名作为列名。请参考下面的例子③和例子④---罪过啊,现在才知道,不知道有没有坑看过这篇博客的朋友,罪过。。。)

例子:

①select  id, name, age into tempdb..testtable1 from  student    (复制部分字段)

②select * into tempdb..testtable2 from student    (复制所有字段)

③select '10:00' hour, 'name' name, 28 age into #temp_table_one   (为每一个插入的常量命名作为列名,就可以插入常量了)

④select *, 2 into #temp_table_two from #temp_table_one (如果只插入常量不命名,则会报错,报错信息如下所示)

插入常量时报错:

------------------------ Execute ------------------------
SELECT INTO failed because column 4 in table '#temp_table_two00005230001896877' has a null column name. Null column names are not allowed.
----------------- Done ( 1 errors ) ------------------

insert into:

语法:Insert into Table2(value1,value2,...) select value1,value2,... from Table1

注意:

①table2表必须已经存在。

②因为table2表已经存在,所以可以向其中插入常量。

例子:

①insert into tempdb..testtable4(id, name,age) select id, name, age from student 

②insert into tempdb..testtable4 select * from student 

③insert into tempdb..testtable4 select id, name, age from student

④insert into tempdb.. testtable4(id, name, age) select id, name, 27 from student    (插入常量)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值