waqar ahmed somra waqar ahmed somra - 13 days ago 5
SQL Question

insert selected columns from one table to selected columns of another table in sql server

I am new to sql thing and want to find a solution.
I have two tables, table employees and table new_employees.

Table employees has columns:

id(pk) | username | phonenumber | createdby | deptid |date


Table new_employees has columns:

id(pk) | name, p_number | dept_id | ext_no | salary


I want to insert all the records from table new_employees with selected columns
name
,
p_number
,
dept_id'
to columns
username
,
phonenumber
,
deptid
of table employees.

But in
createdby
and
date
columns of Table employees i want to put two values 'John' and
getDate();


Please help me to write query for this scenario.

Answer

If he confusion is about mixing values from a table and literal values then you can use syntax of this form:

INSERT INTO Table (...)
SELECT col1, ... 'John', GetDate()
  FROM AnotherTable;
Comments