CREATE TABLE #a ( NAME VARCHAR(20), Age INT, ) CREATE TABLE #b ( NAME VARCHAR(20), Age INT, )INSERT INTO #a SELECT 'aa',20 UNION ALL SELECT 'bb',30 UNION ALL SELECT 'cc',20 UNION ALL SELECT 'dd',20 --插入到b表中 INSERT INTO #b(name,Age) SELECT * FROM #a WHERE Age=20 SELECT * FROM #b ------------------------------ NAME Age -------------------- ----------- aa 20 cc 20 dd 20(3 row(s) affected)
insert into B select * from A where Value='关健字'注:有标识列(自增列)时需要用SET IDENTITY_INSERT [ database_name . [ schema_name ] . ] table { ON | OFF }
insert into B(col1,col2...) select col1,col2... from A where (条件表达式)select语句的列数与B表的列数要一样多,数据类型也要一致。一般要排除掉自动递增列不插入
insert into B
select *
from A
where ...
CREATE TABLE #a
(
NAME VARCHAR(20),
Age INT,
)
CREATE TABLE #b
(
NAME VARCHAR(20),
Age INT,
)INSERT INTO #a
SELECT 'aa',20 UNION ALL
SELECT 'bb',30 UNION ALL
SELECT 'cc',20 UNION ALL
SELECT 'dd',20 --插入到b表中
INSERT INTO #b(name,Age)
SELECT * FROM #a WHERE Age=20
SELECT * FROM #b
------------------------------
NAME Age
-------------------- -----------
aa 20
cc 20
dd 20(3 row(s) affected)
select * from A where Value='关健字'注:有标识列(自增列)时需要用SET IDENTITY_INSERT [ database_name . [ schema_name ] . ] table { ON | OFF }
select col1,col2...
from A
where (条件表达式)select语句的列数与B表的列数要一样多,数据类型也要一致。一般要排除掉自动递增列不插入