Click here to Skip to main content
15,902,299 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
SQL
insert into employees(employee_id,last_name,hire_date,job_id,email)
values(dep,'a',sysdate,'1','www')
select department_id as dep  from departments where manager_id=200;


i want to insert department_id value into employee id and rest columns as default... i am getting wrrr.. can u plz suggest
Posted
Updated 28-Feb-14 23:38pm
v2

Try this

SQL
insert into employees(employee_id,last_name,hire_date,job_id,email)
select department_id, 'a', sysdate, '1', 'www'  from departments where manager_id=200;
 
Share this answer
 
Put the same department id query in the location where you have put dept.
What you will basically be doing is using a subquery.
 
Share this answer
 
v2
There are few ways to achieve that:

  1. using variable
    SQL
    DECLARE @dep INT
    SELECT @dep = department_id 
    FROM departments 
    WHERE manager_id=200;
    INSERT INTO employees(dept_id, last_name, hire_date, job_id, email)
    VALUES(@dep,'a',sysdate,'1','www')

  2. using stored procedure[^]
    SQL
    CREATE PROCEDURE InsertNewEmployee
        @manid INT,
        @lname VARCHAR(50),
        @jobid INT,
        @email VARCHAR(50)
    AS
    BEGIN
        DECLARE @dept INT
        SELECT @dept = COALESCE(department_id,0)
        FROM departments
        WHERE manager_id = @manid
    
        IF @dept >0 
        BEGIN
            INSERT INTO employees(dept_id, last_name, hire_date, job_id, email)
            VALUES(@dept,@lname,sysdate(),@jobid,@email)
        ELSE
            PRINT 'Unknown manager!'
        END
    
    END

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900