Home I cant get this conditional update or insert statement to work in SQL Server
Reply: 0

I cant get this conditional update or insert statement to work in SQL Server

user1020
1#
user1020 Published in June 22, 2018, 11:32 am

This code is supposed to update a record if the name of the person in table 1 is the same as the name of the person in table 2 and if it is not then just insert the name and age of the person as a new record into table 1.

 SELECT
    CASE 
    WHEN
    [Name] = @Name 
    THEN
    UPDATE
    dbo.EmployeeInfo
    SET
    dbo.EmployeeInfo.Age = V.Age 
    FROM dbo.EmployeeInfo I
    INNER JOIN
    dbo.UpdateEmployeeInfo V
    ON
    V.[Name] = I.[Name]

    ELSE

    SELECT * INTO dbo.EmployeeInfo
    FROM dbo.UpdateEmployeeInfo
    WHERE [Name] = @Name

    END
    FROM dbo.EmployeeInfo

I have edited the code just to use IF conditions yet it still does not work

IF
dbo.EmployeeInfo.[Name] = @Name 
THEN
UPDATE
dbo.EmployeeInfo
SET
dbo.EmployeeInfo.Age = V.Age 
FROM dbo.EmployeeInfo I
INNER JOIN
dbo.UpdateEmployeeInfo V
ON
V.[Name] = I.[Name]

ELSE

SELECT * INTO dbo.EmployeeInfo
FROM dbo.UpdateEmployeeInfo
WHERE [Name] = @Name
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.32698 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO