如何实现insert 语句嵌套select查询

如何实现insert 语句嵌套select查询,第1张

在VALUES子句中不能有子查询,这样就可以了: insert into VoteRecord(IP,TopicNum) select '" + ip + "',ID from Topic where [Content]='" + topic + "' 实际生成的语句应该这样: insert into VoteRecord(IP,TopicNum) select '192.168.1.1',ID from Topic where [Content]='123' 不过,为保证不发生错误,最好在子查询中加入TOP 1 子句或MAX()函数等,保证子查询记录是一条 insert into VoteRecord(IP,TopicNum) select '192.168.1.1',max(ID) from Topic where [Content]='123'

可以的。

例如:Insert into A Select * From B注意:这里要求A和B的表结构是一样的。如果不一样,则需要使用:

Insert into A(C1,C2,...) Select C1,C2,... From B;

这里C1、C2分别指A表与B表字段大小和类型都相同的列。

--id,name为从a表中查出来的值,1为单个值

insert into b(id,name,flag) select id,name,1 from a

--其中建表如下:

create table a(id number(10) not null,name varchar2(256))

create table b(id number(10) not null,name varchar2(256),flag int )

--插入语句如下:

insert into a(id,name) values(1,'aaa')

commit

insert into a(id,name) values(2,'bbb')

commit


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/bake/11904265.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-19
下一篇 2023-05-19

发表评论

登录后才能评论

评论列表(0条)

保存