I am trying to utilize a TRY/CATCH/THROW statement with SQL but I am unfamiliar with exactly how to input everything for the most efficient code, as well as what code would be paired with each of those methods.
My current code is
BEGIN TRY
Insert Into ENT_ITEM_MASTER
from Sheet1$
WHERE ITEM_CODE = ENT_ITEM_MASTER
END TRY
BEGIN CATCH
If ITEM_CODE <> ENT_ITEM_MASTER
THROW;
END CATCH;
What I want it to do is that if ITEM_CODE <> ENT_ITEM_MASTER
then it will skip that item code and go to the next one.
If someone could explain how to correctly throw that exception and add a little detail of what exactly CATCH
and THROW
do so I could better understand what exactly they are used for, that would be incredibly helpful.
I have a linked server to an Excel file with 16,000 rows of information. My code inserts that info into an item list. It links with another program so that, if I search for an item code, all of the info on it will come up.
EDIT to address answer. Trial code:
Insert Into ENT_ITEM_MASTER
from Sheet1$
WHERE ITEM_CODE = ENT_ITEM_MASTER
If ITEM_CODE <> ENT_ITEM_MASTER
Insert Into ENT_ITEM_MASTER
from Sheet1$
WHERE ITEM_CODE = ENT_ITEM_MASTER
Else
Are you sure a try/catch/throw is necessary? It appears you could do a for loop that loops for each row with a nested if statement inside to check If ITEM_CODE <> ENT_ITEM_MASTER. If the condition is met then Insert Into ENT_ITEM_MASTER from Sheet1$ WHERE ITEM_CODE = ENT_ITEM_MASTER Else ( ). Does that make sense?