RedRocket RedRocket - 2 months ago 5
SQL Question

How to copy record from one table to the same table

I have a insert select query in my stored procedure. In the query below, I inserted one record. What I want to do is to copy this record and insert to the same table but the salary will be negative. I am not sure what how to do it. Help will be appreciated

Here is part of my code

Insert into employee_name, age, Sum(salary)
Select employee_name, age, salary from employee
group by employee_name


Record

employee_name age salary
John 39 $30,000


What I want

employee_name age salary
John 39 $30,000
John 39 $-30,000

Answer

First of all your INSERT syntax is completely wrong. You could have easily looked it up in the docs: INSERT INTO table (columns) SELECT ....

Then you say you want to copy a record but make the salary negative. What does that have to do with row aggregation (SUM and GROUP BY)?

It seems you simply want:

insert into employee (employee_name, age, salary)
select employee_name, age, -salary from employee;
Comments