Insert/Update SP

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • geo039
    New Member
    • Nov 2006
    • 47

    Insert/Update SP

    Background:
    I'm a beginner developer learning when to use the app and when to use the DB to execute what I need. I have two tables in separate databases with employee data. I need to set my asp.net application variable to today so a sp would run based on that.

    Question:
    The sp needs to check for empid in Table B. If exists update based on info in Table A if not found insert from Table A.

    I have this update statement
    Code:
    UPDATE  dbo.employee 
    SET full_name = t.full_name, cost_center = t.cdcstcntr,
    	cost_center_desc=t.desccstcntr, job_title=t.job_title,
    	supervisor=t.supervisor_name
    FROM dbo.v_WQTimeEmployees as t RIGHT OUTER JOIN 
    	dbo.employee ON t.empUserID = dbo.employee.NTUserID
    WHERE t.empUserID = dbo.employee.NTUserID 
    	AND (dbo.employee.manual IS NULL or dbo.employee.manual=0)
    I just don't know how to say in SQL, if empuserid found update if not insert.

    Thanks for your assistance.
  • bwestover
    New Member
    • Jul 2007
    • 39

    #2
    Originally posted by geo039
    I just don't know how to say in SQL, if empuserid found update if not insert.

    Thanks for your assistance.
    Try this
    Code:
    If Exists (Select empuserid From .....)
         Begin
              Update dbo.employee
              ...
              ...
         End
    
    Else 
         Begin
             Insert .....
             ....
             ....
         End

    Comment

    Working...