Converting Oracle Procedure into MSSQL - sql-server

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!

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

Balance is not calculating correctly, when date get change in transection

Below is data in which two different date are coming,01/19/2021 and 01/18/2021 ,
Date 01/19/2021 transection amount is not getting minus (Credit ) From Balance ,but 01/18/2021 amount is getting Less from balance.
Secondly,i want ,Order by trans_date asc
Create table #tbl_COA (Level_Four_ID int,Level_Four_Name varchar(50),Opening decimal(10,2))
Create table #tbl_trans_type (Trans_Type_ID int,trans_type_name varchar(50))
Create table #tbl_transection (Trans_ID int,Level_Four_ID_C int,Level_Four_ID_D int,Trans_Amount Decimal(10,2),Trans_date date,Trans_Type_ID int)
INSERT INTO #tbl_COA VALUES(1231,'Abdul Rauf',60000)
INSERT INTO #tbl_COA VALUES(1222,'Cheque In Hand',45000)
INSERT INTO #tbl_COA VALUES(1215,'MBL 833968',0)
insert into #tbl_trans_type VALUES(1,'Online')
insert into #tbl_trans_type VALUES(2,'Cheque')
insert into #tbl_trans_type VALUES(3,'Deposite')
insert into #tbl_trans_type VALUES(4,'Tranfer')
insert into #tbl_trans_type VALUES(5,'Return')
INSERT INTO #tbl_transection VALUES(1,1231,1222,55000,'2021-01-18',2)
INSERT INTO #tbl_transection VALUES(2,1231,1222,55000,'2021-01-18',2)
INSERT INTO #tbl_transection VALUES(3,1222,1215,44444,'2021-01-18',3)
INSERT INTO #tbl_transection VALUES(4,1215,1222,44444,'2021-01-18',5)
INSERT INTO #tbl_transection VALUES(5,1222,1231,44444,'2021-01-19',2)
;WITH cte
as(
SELECT NULL Trans_ID,NULL Trans_Type,NULL TransDate,'Opening' Trans_Remarks,NULL Code,NULL Head,null Debit,null Credit,isnull(opening,0) Balance
from #tbl_COA
where Level_Four_ID=1222
UNION ALL
SELECT T.Trans_ID,ty.trans_type_name as Trans_Type,Convert(varchar, T.Trans_Date ,101)as TransDate,
(CONCAT(COA.Level_Four_Name ,' ','Online Receipt C/O',' ',COAc.Level_Four_Name,' ', 'Rs. ',T.Trans_Amount)) as Trans_Remarks,
T.Level_Four_ID_C as Code ,COAc.Level_Four_Name as Head, T.Trans_Amount as Debit, CAST(0 AS decimal(18,2)) as Credit,T.Trans_Amount- CAST(0 AS decimal(18,2)) Balance
FROM #tbl_transection T
inner join #tbl_COA COA on COA.Level_Four_ID=T.Level_Four_ID_D
inner join #tbl_COA COAc on COAc.Level_Four_ID=T.Level_Four_ID_C
inner join #tbl_trans_type ty on ty.trans_type_ID=T.Trans_Type_ID
where COA.Level_Four_ID=1222
UNION ALL
SELECT T.Trans_ID,ty.trans_type_name as Trans_Type, Convert(varchar, T.Trans_Date ,101)as TransDate,
(CONCAT(COA.Level_Four_Name ,'Online Receipt C/O',' ',COA.Level_Four_Name, '',T.Trans_Amount)) as Trans_Remarks,
T.Level_Four_ID_D as Code,COA.Level_Four_Name as Head, CAST(0 AS decimal(18,2)) as Debit, Trans_Amount as Credit,CAST(0 AS decimal(18,2))-Trans_Amount Balance
FROM #tbl_transection T
inner join #tbl_COA COA on COA.Level_Four_ID=T.Level_Four_ID_D
inner join #tbl_trans_type ty on ty.trans_type_ID=T.Trans_Type_ID
where T.Level_Four_ID_c=1222
),cte1 as (
SELECT Trans_ID,Trans_Type,TransDate,Trans_Remarks, Code,Head,Debit,Credit,sum(Balance) over (order by (select null) ROWS UNBOUNDED PRECEDING) Balance
FROM cte)
select * from cte1
UNION ALL
SELECT NULL ,NULL ,NULL , null,null, 'Total',sum(Debit),sum(Credit),null
from cte1 order by Head,TransDate

Select subset of duplicate records in SQL Server

I need to select a subset of duplicate records in SQL Server 2016. Below is the data set and the code used. I need to select only duplicates highlighted in red. Basically I need only those duplicate records that have matching LName, FName, dateOfBirth, StreetAddress values and in the Source the nave NUll. At the same time, I need only those records that also match in the abovementioned fields and have Source value of "Company XYZ"
IF OBJECT_ID('tempdb..#Dataset') IS NOT NULL DROP TABLE #Dataset
GO
create table #Dataset
(
ID int not null,
LName varchar(50) null,
Fname varchar(50) null,
DateOfBirth varchar(50) null,
StreetAddress varchar(50) null,
Source varchar(50) null,
)
insert into #Dataset (ID, LName, Fname, DateOfBirth, StreetAddress, Source)
values
('1', 'John', 'Ganske', '37171', ' 1223 Sunrise St', 'Company XYZ'),
('2', 'John', 'Ganske', '37171', ' 1233 Sunrise St', 'Company XYZ'),
('4', 'Brent', 'Paine', '20723', ' 5443 Fox Dr', Null),
('3', 'Brent', 'Paine', '20723', ' 5443 Fox Dr', 'Company XYZ'),
('5', 'Adam', 'Smith', '22805', ' 1254 Lake Ridge Ct', Null),
('6', 'Adam', 'Smith', '22805', ' 1254 Lake Ridge Ct', Null),
('7', 'Adam', 'Smith', '22805', ' 1254 Lake Ridge Ct', 'Company XYZ'),
('8', 'Timothy', 'Johnson', '36165', ' 1278 Lee H-W', Null),
('9', 'Timothy', 'Johnson', '36165', ' 1278 Lee H-W', Null),
('10', 'Judy', 'Wilson', '32579', ' 5678 Dotties Dr', 'Company XYZ'),
('12', 'Peter', 'Pan', '37507', NULL, Null),
('11', 'Peter', 'Pan', '37507', NULL, 'Company XYZ');
--select * from #Dataset
select d.ID, d.LName, d.Fname, d.DateOfBirth, d.StreetAddress, d.Source
from #Dataset d
inner join (select
LName, Fname, DateOfBirth, StreetAddress
from #Dataset
--where Source is not null
group by
LName, Fname, DateOfBirth, StreetAddress
having count(*) > 1 ) b
on d.LName = b.LName
and
d.Fname = b.Fname
and
d.DateOfBirth = b.DateOfBirth
and
d.StreetAddress = b.StreetAddress
left outer join (select min(ID) as ID from #Dataset
group by LName, Fname, DateOfBirth, StreetAddress
having count(*) > 1 ) c
on d.ID = c.ID
My output looks like this one below:
You could use ROW_NUMBER:
WITH cte AS (
SELECT *,ROW_NUMBER() OVER(PARTITION BY LName,Fname,DateOfBirth,StreetAddress
ORDER BY ID DESC) rn
FROM #Dataset
)
SELECT *
FROM cte
WHERE rn > 1
ORDER BY ID;
db<>fiddle demo
EDIT:
WITH cte AS (
SELECT *,
ROW_NUMBER() OVER(PARTITION BY LName, Fname, DateOfBirth, StreetAddress
ORDER BY ID DESC) rn,
SUM(CASE WHEN Source = 'Company XYZ' THEN 1 ELSE 0 END)
OVER(PARTITION BY LName, Fname, DateOfBirth, StreetAddress) AS cnt
FROM #Dataset
)
SELECT *
FROM cte
WHERE rn > 1
AND cnt > 0
AND [Source] IS NULL
ORDER BY ID;
db<>fiddle demo2
EDIT 2:
WITH cte AS (
SELECT *,
SUM(CASE WHEN Source IS NULL THEN 1 ELSE 0 END) OVER(PARTITION BY LName, Fname, DateOfBirth, StreetAddress) c1,
SUM(CASE WHEN Source = 'Company XYZ' THEN 1 ELSE 0 END) OVER(PARTITION BY LName, Fname, DateOfBirth, StreetAddress) AS c2,
COUNT(*) OVER(PARTITION BY LName, Fname, DateOfBirth, StreetAddress) c3
FROM #Dataset
)
SELECT *
FROM cte
WHERE c1 > 0
AND c2 > 0
AND c3 > 1
AND Source IS NULL
ORDER BY ID;
db<>fiddle demo3

Removing Subqueries

I have 2 tables tab1 and tab2.
tab1:
id name monthid salary inflow
-----------------------------------------
1 mohan 1 2000 1000
1 mohan 3 3000 1000
1 mohan 4 4500 1600
1 mohan 2 2500 1200
in tab2 I want this output:
id name salary inflow
--------------------------
1 mohan 12000 1600
In tab2, salary column is the sum of salary of tab1 and inflow is the inflow of highest month.
I tried this query:
Insert into tab2(id, name, salary)
select id, name, sum(salary)
from tab1
update tab2
set inflow = (select inflow
from tab1
where monthid = max(monthid))
But I know this is not the correct method.
Can anyone help me to correct this query? And I also want to remove the subqueries.
You can use row_number as below
Insert into tab2(id, [name], [salary], inflow)
Select id, [name], Salary, inflow from (
Select id, [name], sum(salary) over(partition by id) as Salary,
inflow, RowN = Row_number() over (partition by id order by monthid desc) from tab1 ) a
Where a.RowN = 1
Without subquery you can use top(1) with ties as below
Insert into tab2(id, [name], [salary], inflow)
Select top (1) with ties id, [name], sum(salary) over(partition by id) as salary, inflow
from tab1
order by Row_number() over (partition by id order by monthid desc)
DECLARE #tab1 table(id int,name varchar(100),monthid int, salary int,inflow int)
INSERT INTO #tab1
SELECT 1,'Mohan',1,2000,1000
UNION ALL
SELECT 1,'Mohan',3,3000,1000
UNION ALL
SELECT 1,'Mohan',4,4500,1600
UNION ALL
SELECT 1,'Mohan',2,2500,1200
SELECT top 1
id, name,SUM(salary) OVER(PARTITION BY id) as salary,MAX(inflow) OVER(PARTITION BY id) as inflow
FROM #tab1
OR
SELECT DISTINCT
id, name,SUM(salary) OVER(PARTITION BY id) as salary,MAX(inflow) OVER(PARTITION BY id) as inflow
FROM #tab1

SQL identify duplicate and update

i need help in below issue.i have a customer table CustA which is having columns custid, first name , surname, phone1, phone2,lastupdateddate. This table has duplicate records.a record is considered duplicate in CustA table when
first name & surname & (phone1 or phone2) is duplicated
custid firstname surname phone1 phone2 lastupdateddate
1000 Sam Son 334566 NULL 1-jan-2016
1001 sam son NULL 334566 1-feb-2016
i have used cte for this scenario to Partition by firstname, lastname, phone1, phone2 based on rownumber. But the OR condition is remaining as challenge for phone1 or phone2 in CTE query. Please share your thoughts. Appreciate it.
Trick here is COALESCE
With cte as
(
select Count()over(partition by firstname, lastname, coalesce(phone1, phone2)) as cnt,*
From yourtable
)
Select * from CTE
WHere cnt > 1
Though if it isn't the case that one is always null You can use a CASE expression to ensure that the values are presented in a consistent order.
WITH cte
AS (SELECT COUNT(*)
OVER(
partition BY firstname,
lastname,
CASE WHEN phone1 < phone2 THEN phone1 ELSE phone2 END,
CASE WHEN phone1 < phone2 THEN phone2 ELSE phone1 END) AS cnt,
*
FROM yourtable)
SELECT *
FROM CTE
WHERE cnt > 1
This one will also give you the list of dupes (optional custid<>A.custid)
Declare #Yourtable table (custid int,firstname varchar(50),surname varchar(50),phone1 varchar(25),phone2 varchar(25),lastupdate date)
Insert into #Yourtable values
(1000,'Sam','Son' ,'334566',NULL ,'1-jan-2016'),
(1001,'sam','son' ,NULL ,'334566','1-feb-2016'),
(1003,'sam','son' ,NULL ,NULL ,'2-feb-2016'),
(1002,'Not','ADupe',NULL ,NULL ,'1-feb-2016')
Select A.*
,B.Dupes
From #YourTable A
Cross Apply (Select Dupes=(Select Stuff((Select Distinct ',' + cast(custid as varchar(25))
From #YourTable
Where custid<>A.custid
and firstname=A.firstname
and surname =A.surname
and (IsNull(A.phone1,'') in (IsNull(phone1,''),IsNull(phone2,'')) or IsNull(A.phone2,'') in (IsNull(phone1,''),IsNull(phone2,'')) )
For XML Path ('')),1,1,'')
)
) B
Where Dupes is not null
Returns
custid firstname surname phone1 phone2 lastupdate Dupes
1000 Sam Son 334566 NULL 2016-01-01 1001,1003
1001 sam son NULL 334566 2016-02-01 1000,1003
1003 sam son NULL NULL 2016-02-02 1000,1001

TSQL JOIN Clarification

Suppose i have two tables
declare #emp table
(
EmpID int,
EmpName varchar(10)
)
declare #Remu table
(
EmpID int,
Sal Decimal(10,2),
PaidYear varchar(10)
)
I want maximum salary grouped on PaidYear (With Ties)
Expected OUTPUT
EmpID EmpName PaidYear Sal
1 Jon 2001 2000
2 Smith 2001 2000
3 Nash 2003 4000
4 Hoge 2005 5000
5 Peter 2005 5000
I have an issue when using Join
select e.EmpID,e.EmpName,r.Sal,r.PaidYear from #emp e
inner join
(select max(Sal) as Sal,PaidYear from #Remu group by PaidYear)r
on e.EmpID=???
when i select EmpID in
select max(Sal) as Sal,PaidYear from #Remu group by PaidYear
i have to Group by PaidYear and EmpID,which won't give the desired result as i expected.
How to solve this.I want a query which should be compatible with SQL Server 2000.
select e.EmpID,e.EmpName,r.Sal,r.PaidYear
from #emp e inner join #Remu r on e.EmpId = r.EmpId
where r.sal in (select max(sal) from #remu group by paidyear)
Each year needs to determine a single max salary specific to that year.
select e.EmpID
, e.EmpName
, r.Sal
, r.PaidYear
from #emp as e
inner join #Remu as r
on e.EmpId = r.EmpId
where r.sal = (select max(sal)
from #remu
where paidyear = r.PaidYear ' makes it year specific
)
Data To Test:
declare #emp table
(
EmpID int,
EmpName varchar(10)
)
declare #Remu table
(
EmpID int,
Sal Decimal(10,2),
PaidYear varchar(10)
)
insert into #emp (EmpID, EmpName)
values(1, 'Jon')
insert into #emp (EmpID, EmpName)
values(2, 'Smith')
insert into #emp (EmpID, EmpName)
values(3, 'Nash')
insert into #emp (EmpID, EmpName)
values(4, 'Hoge')
insert into #emp (EmpID, EmpName)
values(5, 'Peter')
Insert into #Remu (EmpID, Sal, PaidYear)
values(1, 2000, '2001')
Insert into #Remu (EmpID, Sal, PaidYear)
values(2, 4999, '2001')
Insert into #Remu (EmpID, Sal, PaidYear)
values(2, 8000, '2003')
Insert into #Remu (EmpID, Sal, PaidYear)
values(3,4000, '2003')
Insert into #Remu (EmpID, Sal, PaidYear)
values(4, 5000, '2005')
Insert into #Remu (EmpID, Sal, PaidYear)
values(5, 4999, '2005')
Results:
EmpID EmpName Sal PaidYear
4 Hoge 5000.00 2005
2 Smith 8000.00 2003
2 Smith 4999.00 2001

Resources