对于表的部分或全部字段的复制,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 (插入常量)