BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / database / #1247同步于 2007/6/22
该镜像源已超过 30 天没有更新,可能在源站已被删除。
Database机器人发帖

[求助]关于SQL中的insert语句

wrc2872
2007/6/22镜像同步9 回复
若是在insert语句中需要使用嵌套的select,该如何写 特别当select的结果为char型时 比如INSERT INTO CourseInfo (CourseID, CourseName, CourseTerm) VALUES ('000004', select CourseName from CourseInfo where CourseID ='000005', 'next') 在本例中的CourseID, CourseName, CourseTerm都是char型的,在VALUES 中写上select CourseName where CourseID ='000005'是会报错的,应该如何修改。 select CourseName from CourseInfo where CourseID ='000005'这句返回一个char型 的CourseName
订阅后,新回复会通过你的通知中心匿名送达。
9 条回复
hong机器人#1 · 2007/6/23
【 在 wrc2872 的大作中提到: 】 : 若是在insert语句中需要使用嵌套的select,该如何写 : 特别当select的结果为char型时 : 比如INSERT INTO CourseInfo : ................... 个人觉得。使用参数应该可以解决吧。 仅供参考下。 比如: declare @name varchar(30) set @name=(select [Name] from CourseInfo where [ID]='000004') insert into CourseInfo values ('000005',@name,'next') 其中参数的类型跟表中定义的一样就是了
hisashi机器人#2 · 2007/6/24
这个问题在mysql里可以这样解决 insert into CourseInfo(CourseID, CourseName, CourseTerm) select '000004',CourseName, 'next' from CourseInfo1 where CourseID ='000005' insert ... select应该在其他一些数据库里也能用吧 2楼的做法可能会出错,家里没数据库验证不了,但是感觉第二行的赋值=右边的内容实际上不是一个值,是一个集合,没法保证只有一项的 注意,后面出现的表是CourseInfo1,mysql目前不能在插入的同时从同一个表中选择,建一个临时表就可以了
hong机器人#3 · 2007/6/25
【 在 hisashi 的大作中提到: 】 : 这个问题在mysql里可以这样解决 : insert into CourseInfo(CourseID, CourseName, CourseTerm) select '000004',CourseName, 'next' from CourseInfo1 where CourseID ='000005' : insert ... select应该在其他一些数据库里也能用吧 : ................... 测试的时候也想过。 当时用的是select top 1 限定。 看到楼主表里有ID属性。就让楼主自己尝试了^^
hisashi机器人#4 · 2007/6/27
【 在 Butp 的大作中提到: 】 : 本来就可以 : 楼主好像是因为没写from 子句, : 你看他说的报错还没有改“在VALUES 中写上select CourseName where CourseID ='000005'是会报错的” : ................... 说这么插入可以的实际试一下,把能通过的数据库的名字说出来,加了from也一样是错的,应该是一个值的地方却是一个集合,就算集合里只有一个值,也是集合,语法本身就不对
wrc2872机器人#5 · 2007/6/27
非常感谢大家的回复 最近忙着考试也没有看 但是那个加上from,还是有问题。不信可以试一下,真正的问题是出在单引号上,这可能算是最常见的问题了
hisashi机器人#6 · 2007/6/27
【 在 wrc2872 的大作中提到: 】 : 非常感谢大家的回复 : 最近忙着考试也没有看 : 但是那个加上from,还是有问题。不信可以试一下,真正的问题是出在单引号上,这可能算是最常见的问题了 和单引号没有关系,都换成整数这么写也一样不行的
uuuu机器人#7 · 2007/6/27
把这句强行转换一下:select CourseName from CourseInfo where CourseID ='000005' ------〉cast((select CourseName from CourseInfo where CourseID ='000005') as varchar(20))
uuuu机器人#8 · 2007/6/28
刚在SQL SEVER 上试过了,如下写就可以了。 declare @tmp varchar(20) set @tmp=(select CourseName from CourseInfo where CourseID ='000005') INSERT INTO CourseInfo (CourseID, CourseName, CourseTerm) VALUES('000004',@tmp , 'next') =后面的那个括号不能省
wrc2872机器人#9 · 2007/6/28
谢谢了,我先试试 【 在 uuuu 的大作中提到: 】 : 刚在SQL SEVER 上试过了,如下写就可以了。 : declare @tmp varchar(20) : set @tmp=(select CourseName from CourseInfo where CourseID ='000005') : ...................