mysql 将查询结果拼接输出到本地文件

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xukang11111/article/details/50947339
--①第一种
create procedure proc(in stuid int,in stuname VARCHAR(255),in stuage VARCHAR(255))
begin
    declare cnt int ;
    declare  str VARCHAR(255);
    declare  temp VARCHAR(255);
    declare  i int;
    set i = 0;
        set temp = '';
    select count(1) INTO cnt from tb_student;
    IF (cnt < 5) THEN
        insert into tb_student(stuid,stuname,stuage) values(sid,sname,sage);
    ELSE
        WHILE i < 5 DO
            select stuname into str from tb_student LIMIT i,1;
            select concat(temp,str) into str from DUAL ;
            set temp = str ;
            set i = i + 1;
        END WHILE ;
        select str into outfile "C:\\job\\job.txt";
    END IF;
end

--②第二种
begin
    declare cnt int ;
    declare  str VARCHAR(255);

    select count(1) INTO cnt from tb_student;
    IF (cnt < 5) THEN
        insert into tb_student(stuid,stuname,stuage) values(stuid,stuname,stuage);
    ELSE
        select group_concat(stuname) into str from tb_student;
        select str into outfile "mysqlTest.txt" FIELDS TERMINATED BY ',';
    END IF;
end
--③第三种
repeat  ...... until  i>=5 end repeat ;

展开阅读全文

没有更多推荐了,返回首页