Avatar of ali678
ali678
 asked on

SQL query If field exists update else insert

Ok i want to write a query to check if a field exists in a database if it does than i want to UPDATE and if it doesnt than i want to insert .

I'm using Visual Studio 2008 and my database is Gymbo.mdf






INSERT INTO Members
                      (UserID, FirstName, LastName, DOB, Height, Weight)
VALUES     (@UserID,@FirstName,@LastName,@DOB,@Height,@Weight)

Open in new window

.NET ProgrammingSQL

Avatar of undefined
Last Comment
ali678

8/22/2022 - Mon
basvanommen

You can execute an UPDATE query and find out if it was succesfull. If no success execute an INSERT query.
Guy Hengel [angelIII / a3]

yes, do a UPDATE first, then INSERT if @@rowcount is 0.
best would be to put that into a stored proc...
CREATE PROC add_member
( @userID int, @FirstName varchar(100), @lAstname varchar(100), dob datetime, @height decimal(10,2), @weigth(10,2))
AS
BEGIN
UPDATE Members
  SET Firstname = @FirstName
    , lastName = @lastName
    , dob = @dob
    , height = @height
    , weight = @weight
WHERE UserID = @UserID 
IF @@ROWCOUNT = 0 
  INSERT INTO Members
  (UserID, FirstName, LastName, DOB, Height, Weight)
   VALUES (@UserID,@FirstName,@LastName,@DOB,@Height,@Weight) 
END

Open in new window

ali678

ASKER
im not sure how to create PROC's in the query builder of visual studio :/
Your help has saved me hundreds of hours of internet surfing.
fblack61
Guy Hengel [angelIII / a3]

you don't, you just have to "run" the CREATE PROC statement.
ali678

ASKER
im kinda confused with how to do it, ive sent a screen shot of what im trying to use for it.

I've tried doing this without the PROC statement

BEGIN UPDATE Members
    SET FirstName = @FirstName
WHERE UserID = @UserID
IF @@ROWCOUNT = 0
INSERT INTO Members (FirstName) VALUES (@FirstName)
END

But i get this error :

SQL Execution Error.
....
Error Source :.Net SQLClient Data Provider
Error Message :Must declare the scalar variable "@FirstName".


Im sorry if im annoying you im very new to this, and its soo annoying :P, thanks for the help
screenshot.JPG
Guy Hengel [angelIII / a3]

sorry, I definitively DONT use the visual tools... they take too much control :)

I use the query windows of the sql management to build/modify procedures...
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ali678

ASKER
i just tired the following: and no luck either :( this thing is annoying me:(
Table :  UserID FirstName LastName DOB Height Weight

IF EXSTS ( SELECT * FROM members WHERE userID = @userID)
BEGIN
     UPDATE members SET FirstName = 'testupdateinput' WHERE userID = @userID
END
ELSE
BEGIN
     INSERT INTO members (FirstName) VALUES  ('Test')
END
ASKER CERTIFIED SOLUTION
basvanommen

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
ali678

ASKER
thanks