日期:2014-05-16  浏览次数:20438 次

mssql 将临时表中随机一条数据,并修改部分字段,写入到另一张表中
例:表A,查询出部分数据生成临时表#B,从临时表B中随机取一条记录,修改其部分字段(创建时间等),再插入表A,请教应该怎么写,我写出来老提示'关键字 order 附件有错误'     
------解决方案--------------------
insert into A
select top 1 ..... from #B order by newid()

你的语句贴出来啊,不然怎么知道哪有问题
------解决方案--------------------
drop table ##t
select top 100 * into ##t from busiorder where year(createtime)=2013 and companyid=@companyid  and [status]<5
select top 1 * from ##t order By NewID()//这里是可以查询到数据

INSERT INTO busiorder(orderno, busiorderno, subsysid, classid, goodsid, goodscount,customid
,[description],[type],goodsoutreason,serviceday,refusereason,phonenum,[address],recordpoint,
creator,createtime,updater,lastmodify,paymode,contacttime,callbackflag,processorid,companyid,
syncflag,accountflag,needbill,billtitle,billtype,customname,[status],proccontent,totalprice,
porderno,priority,areano,callid,billno,resultperiod,resultdesc,resultservice,resultname,sbegindate,
senddate,ticketcallback,callbackcontent,ticketstatus,assigner,allotmode,billdate
) SELECT top 1 @maxorderno+1, @maxbusiorderno+1, subsysid, classid, goodsid, goodscount,customid
,[description],[type],goodsoutreason,@xdate,refusereason,phonenum,[address],recordpoint,
creator,@xdate,updater,@xdate,paymode,contacttime,callbackflag,processorid,companyid,
syncflag,accountflag,needbill,billtitle,billtype,customname,[status],proccontent,totalprice,
porderno,priority,areano,callid,billno,resultperiod,resultdesc,resultservice,resultname,@xdate,
@xdate,ticketcallback,callbackcontent,ticketstatus,assigner,allotmode,billdate FROM ##t order By NewID()