1

I'm trying to INSERT a value in a column, the result of a SELECT this sentence works fine:

insert into tempdepa (Trabajadores) (select count (DepartmentName) from DimEmployee where DepartmentName = @d) 

But I want to insert that SELECT into a specific column:

insert into tempdepa (Trabajadores) (select count (DepartmentName) from DimEmployee where DepartmentName = @d) 

where id = @cont

This last where id = @cont is from the first table (tempdepa)... I tried

insert into tempdepa (Trabajadores where id = @cont) (select count (DepartmentName) from DimEmployee where DepartmentName = @d) 

but it doesn't work. How can I do it?

2
  • 1
    Not clear at all,but I think you need a JOIN. Commented Aug 12, 2014 at 15:41
  • You probably need to use an UPDATE instead of an INSERT if you want to change the value of an existing row! The INSERT cannot be "parametrized" with a WHERE clause - it just inserts a new row. Commented Aug 12, 2014 at 15:50

1 Answer 1

5

If you need to insert new row, you need to do:

insert into tempdepa (Trabajadores, id) select count(DepartmentName), @cont from DimEmployee where DepartmentName = @d 

if you want to update existing one:

update tempdepa set Trabajadores=(select count (DepartmentName) from DimEmployee where DepartmentName = @d) where id = @cont 
Sign up to request clarification or add additional context in comments.

2 Comments

Lashane's got it. An INSERT is only used when you are creating a whole new row. You need to use an UPDATE if you are changing a field on an existing row.
WOW THANK YOU ALL! YOU WERE RIGHT @LASHANE I DIDNT REALIZED THAT I WAS INSERTIN NEW ROWS... BUT I NEEDED TO UPDATE :D THANKS!!!!!!!!!!!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.