insert语句嵌套select语句

这个总是提示错误insert into VoteRecord(IP,TopicNum) values ('" + ip + "',select ID from Topic where [Content]='" + topic + "')

在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'
温馨提示:答案为网友推荐,仅供参考
第1个回答  2018-07-31
在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'本回答被网友采纳
第2个回答  2015-09-17
在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'
第3个回答  2013-05-17
insert into VoteRecord(IP,TopicNum) select '" + ip + "',ID from Topic where [Content]='" + topic + "';
commit;
'" + ip + "'可以为常量 ,也可以是变量,常量就会在每次插入的数据中都插入相同的值
例如
insert into cd_wellbore@kftest_LINK(well_id,wellbore_id,WB_SERIAL)
select well_id,well_id,’00’ from well_master 这其中的'00'就是在WB_SERIAL中始终插入00
相似回答