如何实现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 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'
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询