I need to insert the selected data from tblA
to tblB
only if data selected does not exist in tblB
. I created a button that will execute this stored procedure.
I cannot figure out what I am doing wrong in my stored procedure, it is not inserting/showing non existing data into tblB
.
I am using SQL Server 2008 and ASP.NET.
CREATE PROCEDURE [dbo].[ADDATA]
@EmpFrom varchar(7)
,@EmpTo varchar(7)
AS
SET NOCOUNT ON;
DECLARE @affectedRows int
SET @affectedRows = 0;
BEGIN
IF NOT EXISTS (SELECT 1 FROM [dbo].[tblA] WHERE @EmpFrom = @EmpTo)
SET @affectedRows = @affectedRows + @@ROWCOUNT
BEGIN
INSERT INTO tblB
(EmpNum --- PK
,Last_First
,Title
,NTUserName)
select
@EmpTo
,a.emp_name_lfn
,a.job_title
,a.[user_id]
FROM tblA
END
END