I am passing datatable as input parameter to stored procedure.Datatable contain id, Name,Lname,Mobileno,EmpId. Employee tbl contain [Name],[Lname],[mobno],[Did] as columns. When user login that user's Id come as DId.There are more than 1000 records.Instead of passing that id to datatable.I have created separete parameter to sp.'I want add record into Employee which are not already exist.If combination of mobileno and Did is already exist don't insert into Emp else insert.'Datatable contain now of record which may be duplicate.So I don't want to include that record.I want select only distinct record and only that want to add.'I am interested in mobile no.If there are 10 records having same moble no,I am fetching record which come first. Following code is right or wrong.According to my knowledge,first from clause then inner join then where then select execute.Record get fetched from datatable then inner join happens generate result,from that result not from datatable it will check record. So it will give me proper output.
Create Procedure Proc_InsertEmpDetails
@tblEmp EmpType READONLY,
@DId int
as
begin
INSERT INTO Employee
([Name],[Lname],[mobno],[Did])
SELECT [Name],[Lname],[mobno] @DId
FROM @tblEmp A
Inner join (
select min(Id) as minID, mobno from @tblEmp group by mobno
) MinIDTbl
on MinIDTbl.minID = A.ExcelId
WHERE NOT EXISTS (SELECT 1
FROM Employee B
WHERE B.[mobno] = A.[mobno]
AND B.[Did] = @DId )
end
or does I need to change like this
INSERT INTO Employee
([Name],[Lname],[mobno],[Did])
SELECT C.[Name],C.[Lname],C.[mobno], C.D_Id
from
(SELECT [Name],[Lname],[mobno] @DId as D_Id
FROM @tblEmp A
Inner join (
select min(Id) as minID, mobno from @tblEmp group by mobno
) MinIDTbl
on MinIDTbl.minID = A.ExcelId
)C
WHERE NOT EXISTS (SELECT 1
FROM Employee B
WHERE B.[mobno] = C.[mobno]
AND B.[Did] = @DId )