Update my data based on passed ID

Hi,

what I am trying to do is "if [id] found update the Defect_Type else insert as a new record"

Select id, Defect_Type from CRReport

output: 

id                  Defect_Type
en58648      2 change
en95865      4 change
en69544      6 change
en92451      2 change


ALTER PROCEDURE CQuestReport 

AS
BEGIN

	SET NOCOUNT ON;

   select id,Defect_Type from CRReport

-- Here I want to see if id exist than update the record else insert a new one
-- Maybe like create a temp table 

END

Open in new window


Thank you,
Lulu
lulu50Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Vitor MontalvãoMSSQL Senior EngineerCommented:
IF EXISTS(SELECT 1 FROM CRReport WHERE id = @MyID)
        UPDATE CRReport
        SET ColName = @NewValue
        WHERE id = @MyID
ELSE
        INSERT INTO CRReport (Col1, Col2, ..., ColN)
        VALUES (....)

Open in new window

0
lulu50Author Commented:
Vitor,

what is @MyID, @NewValue?
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
Are only variables. Since you are using a SP, I don't know if you are sending these values by parameter or it will be hardcoded. You just need to replace those variables with the correct values.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

sameer2010Commented:
Try this.
ALTER PROCEDURE CQuestReport 

AS
BEGIN

	SET NOCOUNT ON;

--   select id,Defect_Type from CRReport

-- Here I want to see if id exist than update the record else insert a new one
-- Maybe like create a temp table 
Update CRReport Set Defect_Type =@Input_Defect_Type Where id = @Input_ID
If @@ROWCOUNT = 0
INSERT INTO CRReport(ID, Defect_Type) Values(@Input_ID, @Input_Defect_Type)

END

Open in new window

0
lulu50Author Commented:
I'm confused

Let me give you the exact problem

I have two tables

ClearQuestReport_Import
ClearQuestReport

I import data from an excel to this table ClearQuestReport_Import

I want to say if the id from ClearQuestReport_Import exist in ClearQuestReport than update the record else insert.

 IF EXISTS(SELECT 1 FROM ClearQuestReport WHERE id = (select id from ClearQuestReport_Import))
        UPDATE ClearQuestReport
        SET Defect_Type= @NewValue
        WHERE id = @MyID
--ELSE
       -- INSERT INTO CRReport (Col1, Col2, ..., ColN)
       -- VALUES (....)


END


Something is wrong but don't know what?
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
Oh, there's a second table. That's why all information should be supplied at first time.
Things are a little bit different then:
--This will update all matching records
UPDATE ClearQuestReport
SET Defect_Type= ClearQuestReport_Import.Defect_Type
FROM ClearQuestReport
    INNER JOIN ClearQuestReport_Import ON ClearQuestReport.id = ClearQuestReport_Import.id

--This will insert all non matching records
INSERT INTO ClearQuestReport(Col1, Col2, ..., ColN)
SELECT Col1, Col2, ..., ColN
FROM ClearQuestReport_Import
WHERE NOT EXISTS (SELECT 1 
                 FROM ClearQuestReport
                 WHERE ClearQuestReport.Id = ClearQuestReport_Import.ClearQuestReport.Id)

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
lulu50Author Commented:
hmmm it did not do it

this is what I have

ClearQuestReport_Import
id     Defect_Type
1        test 1
2        test 2


ClearQuestReport
id        Defect_Type
1          old data

the correct output should be like this: 
ClearQuestReport
id         Defect_Type
1           test 1
2           test 2



It should update Id 1 and insert Id 2 into ClearQuestReport table 


UPDATE ClearQuestReport
SET Defect_Type= ClearQuestReport_Import.Defect_Type
FROM ClearQuestReport
    INNER JOIN ClearQuestReport_Import ON ClearQuestReport.id = ClearQuestReport_Import.id

--This will insert all non matching records
INSERT INTO ClearQuestReport(id, Defect_Type)
SELECT id, Defect_Type
FROM ClearQuestReport_Import
WHERE NOT EXISTS (SELECT 1 
                 FROM ClearQuestReport
                 WHERE ClearQuestReport.Id = ClearQuestReport_Import.Id)

Open in new window

0
Vitor MontalvãoMSSQL Senior EngineerCommented:
So, what it did instead?
0
lulu50Author Commented:
Vitor,

I am sorry it works fine :-)

I don't know why it didn't work when I tested last time.

GREAT!!!!!!!

THANK YOU SO MUCH FOR ALL YOUR HELP.

THIS IS GREAT!!!!!!

THANK YOU ONE MORE TIME LOL LOL

LULU
0
lulu50Author Commented:
A++++++

Thank you
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
Great. I've just tested your code and worked fine for me as well :)
Cheers.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.