sql server 的insert语句中使用子查询

-- 以省市表为例,主键自增 

CREATE TABLE [nis_provinces] (

[id] [int] IDENTITY (1, 1) NOT NULL ,
[provinces] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL ,
[fatherid] [int] NOT NULL ,
CONSTRAINT [PK_nis_provinces] PRIMARY KEY  CLUSTERED 
(
[id]
)  ON [PRIMARY] 
) ON [PRIMARY]
GO 

 insert into nis_provinces values('北京市',0);

想插入北京各区数据,在oracle中,可以这样写:

insert into  nis_provinces values('东城区',select id from nis_provinces where provinces='北京市') ;

但在sql server的insert语句中,不能使用子查询,要写成这样: 

insert into nis_provinces select '东城区',id from nis_provinces where provinces='北京市';
insert into nis_provinces select '西城区',id from nis_provinces where provinces='北京市';

posted on 2011-06-17 11:34  izumi  阅读(4563)  评论(1编辑  收藏  举报

导航