Verifying data is unreferenced in other tables

keb1965

Well-known member
Joined
Feb 9, 2009
Messages
103
Programming Experience
10+
Our database does not have all of the relationships created where there exists one (or rather one should exist), and we are looking for a way, in the meantime, to ensure any referenced data isn't deleted.

So, to that end, I have come up with this for deleting a database document record:

VB.NET:
CREATE PROCEDURE [dbo].[spDocuments_DeleteDocument]
	@UserID int,
	@DocID int
AS

DECLARE @Success bit = 0

--Are there referenced documents?
	IF (SELECT COUNT(*) FROM tblDocs d
		INNER JOIN tblUsers_DocTypes dt
		ON dt.intDocTypeID = d.intUserDocTypeID
		WHERE dt.intUserID = @UserID) = 0
	BEGIN
                          --Delete it if it isn't referenced
		DELETE FROM tblDocs WHERE intID = @DocID
		SET @Success = 1
	END

SELECT @Success AS [Success]

RETURN

Is this going to do what I think it will do?
 
Back
Top