allow sql insert into table1 only if it does not exist in table2?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mnymoen
    New Member
    • May 2007
    • 3

    allow sql insert into table1 only if it does not exist in table2?

    I am using asp.net 2.0 /sql 2005 and have setup a basic form to store contact info.
    I use a basic insert statement in an ASP page to insert the form fields into table1.
    I would like to check to see if the PK which is made up of fname, lname, and addr1 exists in table2 before inserting.
    I can't figure out the best procedure for this, SQL statement, store proc, or something else .
    Any help with the code to handle this would be greatly appreciated.

    I am new to .net and SQL
  • shweta123
    Recognized Expert Contributor
    • Nov 2006
    • 692

    #2
    Hi,

    I think Stored procedure will be the better approach. In the procedure,You can hadle the error if PK is violated using
    RaiseError('Err orMessage',.... ...)

    This message you can capture in .Net code before inserting into Table1.
    OR
    In a stored procedure , check for each of the columns fname,lname etc. if they exists or not before Insertion.

    Comment

    • Nitinkcv
      New Member
      • Mar 2007
      • 65

      #3
      Originally posted by mnymoen
      I am using asp.net 2.0 /sql 2005 and have setup a basic form to store contact info.
      I use a basic insert statement in an ASP page to insert the form fields into table1.
      I would like to check to see if the PK which is made up of fname, lname, and addr1 exists in table2 before inserting.
      I can't figure out the best procedure for this, SQL statement, store proc, or something else .
      Any help with the code to handle this would be greatly appreciated.

      I am new to .net and SQL
      Hi,

      I think this should solve the problem.
      http://answers.yahoo.c om/question/index?qid=20070 327232957AAukyQ 4

      thanks

      Comment

      • dwadish
        New Member
        • Nov 2006
        • 129

        #4
        u can easily insert using a subquery with "not in"

        Comment

        Working...