My database has a table where the data is loaded from an Excel upload. Then, a stored procedure is run on the data to validate it and insert it to another table.
Currently, my stored procedure will run the validation on all the data again when it's executed.
How do I make it so that it runs the query on the newly loaded data only?
My current stored procedure has the following query:
INSERT INTO dbo.Data_Pass(
[ACC_NO]
,[ORGN]
,[DEST]
,[CTRY]
,[CRNCY]
,[PRDCT_NAME]
,[PRDCT_CODE]
,[DETAILS]
)
SELECT
[ACC_NO]
,[ORGN]
,[DEST]
,[CTRY]
,[CRNCY]
,[PRDCT_NAME]
,[PRDCT_CODE]
,[DETAILS]
FROM dbo.SOURCE_TBL AS ST
JOIN UPLOAD_DATA AS UD ON ST.ACC_NO = CAST(UD.ACC_NO AS nvarchar)
AND ST.CTRY = CAST(UD.CTRY AS nvarchar)
WHERE ST.[PRDCT_CODE] = '102'
AND CAST(UD.[PRDCT_CODE] AS nvarchar) LIKE '%%2'
AND ST.Val LIKE ('%' + CAST(UD.CorrectionVal AS nvarchar)+ '%');
UPDATE Data_Pass
SET REPORT_TY = 'Correction',
CHECK_DT = GETDATE(),
CHECK_SRC = 'Correction Report',
Data_Pass.STATUS = 'Validated'
WHERE REPORT_TY IS NULL AND CHECK_DT IS NULL AND CHECK_SRC IS NULL
I also have an id
field in Data_Pass
which is a primary key and is auto incremented.
The loaded data can be around 200,000 or sometimes more in one go, so I'm looking for the most efficient way to do this.
Edit: Added in my create table statement:
CREATE TABLE [dbo].[Data_Pass](
[id] [int] PRIMARY KEY IDENTITY(1,1) NOT NULL,
[ACC_NO] [nvarchar](50) NULL,
[ORGN] [nvarchar](50) NULL,
[DEST] [nvarchar](max) NULL,
[CTRY] [nvarchar](max) NULL,
[CRNCY] [nvarchar](50) NULL,
[PRDCT_NAME] [nvarchar](50) NULL,
[PRDCT_CODE] [nvarchar](50) NULL,
[DETAILS] [nvarchar](max) NULL,
[REPORT_TY] [nvarchar](max) NULL,
[CHECK_DT] [datetime2](7) NULL,
[CHECK_SRC] [nvarchar](max) NULL,
[STATUS] [nvarchar](50) NULL
)
| id | ACC_NO | ORGN | DEST | CTRY | CRNCY | PRDCT_NAME | PRDCT_CODE | DETAILS | REPORT_TY | CHECK_DT | CHECK_SRC | STATUS |
--------------------------------------------------------------------------------------------------------------------------------------------------------
| 979 | 900000000 | TYO | SIN | SG | SGD | NULL | A12 | NULL | Correction | 15:47.8 | Correction Report | Validated |
| 980 | 900000001 | TYO | SIN | SG | SGD | NULL | A12 | NULL | Correction | 15:47.8 | Correction Report | Validated |
| 981 | 900000002 | TYO | SIN | SG | SGD | NULL | A12 | NULL | Correction | 15:47.8 | Correction Report | Validated |
| 982 | 900000003 | TYO | SIN | SG | SGD | NULL | A12 | NULL | Correction | 15:47.8 | Correction Report | Validated |
| 983 | 900000004 | TYO | SIN | SG | SGD | NULL | A12 | NULL | Correction | 09:09.0 | Correction Report | Validated |