cwolgamott
Programmer
Hello. I am trying to check if a record already exists before I insert it into a table in SQL using ASP. Here is the code that I added:
SQL = SQL + " IF EXISTS(SELECT 'True' From department WHERE DeptName = '" & inputDeptName & "')"
SQL = sQL + " BEGIN "
SQL = SQL + "SELECT 'This record already exists' "
SQL = SQL + "END ELSE BEGIN "
SQL = SQL + "SELECT 'Profile Added' "
SQL = SQL + "INSERT INTO department (department.DeptID, department.DeptName, department.Location, department.Communication, department.BestTime, department.WorstTime, department.Strength, department.Weakness, department.Responsibility1, department.Responsibility2, department.Responsibility3, department.Responsibility4, department.Responsibility5, department.Suggestion1, department.Suggestion2, department.Suggestion3, department.Suggestion4, department.Suggestion5) "
SQL = SQL + "VALUES (" + CStr(depID) + ", " + "'" + inputDeptName + "', " + "'" + inputLocation + "', " + "'" + inputCommunication + "', " + "'" + inputBestTime + "', " + "'" + inputWorstTime + "', " + "'" + inputBestThing + "', " + "'" + inputBiggestThing + "'," + "'" + inputResponsibility1 + "', " + "'" + inputResponsibility2 + "', " + "'" + inputResponsibility3 + "', " + "'" + inputResponsibility4 + "', " + "'" + inputResponsibility5 + "', " + "'" + inputSuggestion1 + "', " + "'" + inputSuggestion2 + "', " + "'" + inputSuggestion3 + "', " + "'" + inputSuggestion4 + "', " + "'" + inputSuggestion5 + "')"
SQL = SQL + " END"
Set rs=conn.Execute(SQL)
If rs(0) = "This record already exists" then
errorArea = errorArea + "The profile you entered already exists. Please press the back button and try again."
Else
addArea = addArea + "Your profile has been added."
End If
It will not add a record if one exists, however, it brings back the same message every time I add one, even if it does not add one. It bring back the :
Your profile has been added
message everytime. I would greatly appreciate any suggestions or tips on why it might possibly be doing this. Thank you.
SQL = SQL + " IF EXISTS(SELECT 'True' From department WHERE DeptName = '" & inputDeptName & "')"
SQL = sQL + " BEGIN "
SQL = SQL + "SELECT 'This record already exists' "
SQL = SQL + "END ELSE BEGIN "
SQL = SQL + "SELECT 'Profile Added' "
SQL = SQL + "INSERT INTO department (department.DeptID, department.DeptName, department.Location, department.Communication, department.BestTime, department.WorstTime, department.Strength, department.Weakness, department.Responsibility1, department.Responsibility2, department.Responsibility3, department.Responsibility4, department.Responsibility5, department.Suggestion1, department.Suggestion2, department.Suggestion3, department.Suggestion4, department.Suggestion5) "
SQL = SQL + "VALUES (" + CStr(depID) + ", " + "'" + inputDeptName + "', " + "'" + inputLocation + "', " + "'" + inputCommunication + "', " + "'" + inputBestTime + "', " + "'" + inputWorstTime + "', " + "'" + inputBestThing + "', " + "'" + inputBiggestThing + "'," + "'" + inputResponsibility1 + "', " + "'" + inputResponsibility2 + "', " + "'" + inputResponsibility3 + "', " + "'" + inputResponsibility4 + "', " + "'" + inputResponsibility5 + "', " + "'" + inputSuggestion1 + "', " + "'" + inputSuggestion2 + "', " + "'" + inputSuggestion3 + "', " + "'" + inputSuggestion4 + "', " + "'" + inputSuggestion5 + "')"
SQL = SQL + " END"
Set rs=conn.Execute(SQL)
If rs(0) = "This record already exists" then
errorArea = errorArea + "The profile you entered already exists. Please press the back button and try again."
Else
addArea = addArea + "Your profile has been added."
End If
It will not add a record if one exists, however, it brings back the same message every time I add one, even if it does not add one. It bring back the :
Your profile has been added
message everytime. I would greatly appreciate any suggestions or tips on why it might possibly be doing this. Thank you.