To prevent duplicate inserts in the SearchViewNavigation table, we can use the same pattern as the example provided: wrapping the INSERT statement in an IF NOT EXISTS clause that checks for the existence of a row with the same key fields.
✅ Key Considerations
From the example, it's clear that the combination of svfdSrvwID and svfdFieldName is used to uniquely identify a row in the SearchViewFieldDescriptor table. Following this logic, the corresponding key fields in the SearchViewNavigation table would be:
These two fields together are likely used to uniquely identify a navigation field for a given search view. Therefore, we will use these two fields in the IF NOT EXISTS condition to prevent duplicate inserts.
🧠 Why Not Check All Fields?
While it's possible to include all fields (svedNavigatorField, svedNavigatorKey) in the IF NOT EXISTS check, doing so would make the condition too strict. The primary purpose of this check is to prevent duplicate configuration entries for the same search view and field, regardless of the navigator values. If you want to allow different navigator values for the same svedSrvwID and svedField, then including all fields would be appropriate. But based on the example, it's safe to assume that svedSrvwID and svedField are the primary keys.
✅ Final SQL Code
Here is the updated SQL code with the duplicate check:
IF NOT EXISTS (
SELECT 1
FROM SearchViewNavigation
WHERE svedSrvwID = 40
AND svedField = N'document_id'
)
BEGIN
INSERT INTO SearchViewNavigation (
svedSrvwID,
svedField,
svedNavigatorField,
svedNavigatorKey
)
VALUES (
40,
N'document_id',
N'Documentation validation',
N'MWCredentialDocumentValidation'
);
END;
📌 Notes
- The
N prefix before string literals is preserved for Unicode compatibility.
- This pattern ensures that the same
svedField is not inserted more than once for the same svedSrvwID.
- If your table has a unique constraint or index on a different combination of fields, you should adjust the
WHERE clause accordingly.