Home > Back-end >  Converting Oracle Procedure into MSSQL
Converting Oracle Procedure into MSSQL

Time:01-20

I'm trying to convert this procedure from Oracle to Microsoft SQL. I used SQLINE without success. The code is supposed to insert up to rows into the table. It is working fine in Oracle and just want to do something similar in MSSQL.

Here is the Proc

 ```create or replace procedure insert_emp as 
    begin
for p in 0..1000 loop
insert into emp (empno, empname, salary, hiredate, dept, title) values(p||1,'Bob',30000,'02-FEB-2004','SALES','EMP');
insert into emp (empno, empname, salary, hiredate, dept, title) values(p||2,'Raj',80000,'02-MAR-2018','MRKTNG','EMP');
insert into emp (empno, empname, salary, hiredate, dept, title) values(p||3,'Kevin',90000,'05-APR-2010','ADMIN','EMP');
insert into emp (empno, empname, salary, hiredate, dept, title) values(p||4,'Laila',30000,'12-FEB-2004','PURCHASE','EMP');
insert into emp (empno, empname, salary, hiredate, dept, title) values(p||5,'Steve',100000,'15-JAN-2008','SALES','MGR');
insert into emp (empno, empname, salary, hiredate, dept, title) values(p||6,'Lucy',80000,'10-FEB-2014','MRKTNG','MGR');
insert into emp (empno, empname, salary, hiredate, dept, title) values(p||7,'Nathan',120000,'17-JUN-2012','ADMIN','MGR');
insert into emp (empno, empname, salary, hiredate, dept, title) values(p||8,'Jose',60000,'16-JUL-2015','DESIGN','EMP');
insert into emp (empno, empname, salary, hiredate, dept, title) values(p||9,'Andrew',80000,'18-AUG-2005','SALES','EMP');
insert into emp (empno, empname, salary, hiredate, dept, title) values(p||10,'Lata',90000,'02-FEB-1998','SALES','EMP');
end loop;
commit;
end ;```

And here is the converted proc in SQLINE but it does not run in MSSQL.

   ```begin
declare p cursor for 0..1000 open p;
 fetch p into;
 while @@fetch_status=0
 begin   
insert into emp (empno, empname, salary, hiredate, dept, title) values(isnull(p, '') 1,'bob',30000,'02-feb-2004','sales','emp');
insert into emp (empno, empname, salary, hiredate, dept, title) values(isnull(p, '') 2,'raj',80000,'02-mar-2018','mrktng','emp');
insert into emp (empno, empname, salary, hiredate, dept, title) values(isnull(p, '') 3,'kevin',90000,'05-apr-2010','admin','emp');
insert into emp (empno, empname, salary, hiredate, dept, title) values(isnull(p, '') 4,'laila',30000,'12-feb-2004','purchase','emp');
insert into emp (empno, empname, salary, hiredate, dept, title) values(isnull(p, '') 5,'steve',100000,'15-jan-2008','sales','mgr');
insert into emp (empno, empname, salary, hiredate, dept, title) values(isnull(p, '') 6,'lucy',80000,'10-feb-2014','mrktng','mgr');
insert into emp (empno, empname, salary, hiredate, dept, title) values(isnull(p, '') 7,'nathan',120000,'17-jun-2012','admin','mgr');
insert into emp (empno, empname, salary, hiredate, dept, title) values(isnull(p, '') 8,'jose',60000,'16-jul-2015','design','emp');
insert into emp (empno, empname, salary, hiredate, dept, title) values(isnull(p, '') 9,'andrew',80000,'18-aug-2005','sales','emp');
if p<>0 begin  
insert into emp (empno, empname, salary, hiredate, dept, title) values(isnull(p, '') 0,'lata',90000,'02-feb-1998','sales','emp');
end 
fetch p into;
end;
close p;
deallocate p;
commit;
end;```

Thanks!

CodePudding user response:

DECLARE @P INT = 0;
WHILE @P < 100
begin   
insert into emp (empno, empname, salary, hiredate, dept, title) values(@P 1,'bob',30000,'02-feb-2004','sales','emp');
insert into emp (empno, empname, salary, hiredate, dept, title) values(@P 2,'raj',80000,'02-mar-2018','mrktng','emp');
insert into emp (empno, empname, salary, hiredate, dept, title) values(@P 3,'kevin',90000,'05-apr-2010','admin','emp');
insert into emp (empno, empname, salary, hiredate, dept, title) values(@P 4,'laila',30000,'12-feb-2004','purchase','emp');
insert into emp (empno, empname, salary, hiredate, dept, title) values(@P 5,'steve',100000,'15-jan-2008','sales','mgr');
insert into emp (empno, empname, salary, hiredate, dept, title) values(@P 6,'lucy',80000,'10-feb-2014','mrktng','mgr');
insert into emp (empno, empname, salary, hiredate, dept, title) values(@P 7,'nathan',120000,'17-jun-2012','admin','mgr');
insert into emp (empno, empname, salary, hiredate, dept, title) values(@P 8,'jose',60000,'16-jul-2015','design','emp');
insert into emp (empno, empname, salary, hiredate, dept, title) values(@P 9,'andrew',80000,'18-aug-2005','sales','emp');
insert into emp (empno, empname, salary, hiredate, dept, title) values(@P 0,'lata',90000,'02-feb-1998','sales','emp');
SET @P =1;
end ;
  • Related