mssql的写法:insert into a(codename,oper,addtime) values((select top 1 codename from b where code=10),'admin',getdate())可是oracle不支持top,怎么写啊?

解决方案 »

  1.   

    用 insert into a(codename,oper,addtime) values((select codename from b where code=10 and rownum=1),'admin',getdate())
      

  2.   

    --oracle
    insert into a(codename,oper,addtime) select codename 'admin',sysdate from b where code=10 and rownum = 1另:你的mssql写法也是错的.应该为:
    insert into a(codename,oper,addtime) select top 1 codename ,'admin',getdate() from b where code=10
      

  3.   

    --oracle(3楼我少了个逗号)
    insert into a(codename,oper,addtime) select codename, 'admin',sysdate from b where code=10 and rownum = 1另:你的mssql写法也是错的.应该为:
    insert into a(codename,oper,addtime) select top 1 codename ,'admin',getdate() from b where code=10
     
     
      

  4.   

    不好意思.突然想到,你是只取一条记录,你的MSSQL写法应该也对.不过,我喜欢我的那种写法.
      

  5.   

    没用过mssql,不过我想insert values 和insert select 两种写法可能会返回不同的结果。当没有符合条件的记录时,前者插入colname为空的一条记录,后者不插入
      

  6.   

    那个rownum 冒似不支持等于号的吧?这个可以用游标的
      

  7.   

    declare
    cursor myc is select * from b;
    remyc myc%rowtype;
    begin
    open myc;
    fetch myc into remyc;
    insert into a(codename,oper,addtime) values(remyc.codename,'admin',sysdate);
    close myc;
    end;
    因为游标第一次是放在表中的首记录,也就是 top 1 
      

  8.   

    在oracle中没有与mssql的top对应的语法,但可以使用伪列rownum来达到,例取t表按begindate升序后的第一条记录:
    select * from (select t.*,rownum rn from t order by t.begindate) where rn<2;
    注意:如果在排序之后取,一定要象上面这样写子查询的
    oracle在WEB上分页也是使用上述方式来实现
      

  9.   

    --oracle
    insert into a(codename,oper,addtime) select codename 'admin',sysdate from b where code=10 and rownum < 2;貌似这样   ROWNUM 好象不支持=号
      

  10.   

    楼上的那样, 不同的数据库是不一样的, MSSQL, IQ是top, mysql是limit, oracle 是rownum