1

I have 2 different tables but the columns are named slightly differently. I want to take information from 1 table and put it into the other table. I need the info from table 1 put into table 2 only when the "info field" in table 1 is not null. Table 2 has a unique id anytime something is created, so anything inserted needs to get the next available id number.

Table 1

category
clientLastName
clientFirstName
incidentDescription
info field is not null then insert all fields into table 2

Table 2

*need a unique id assigned
client_last_name
client_first_name
taskDescription
category
KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
lisa
  • 91
  • 1
  • 4
  • 12
  • 1
    If you are facing syntax related problem then read this post. http://stackoverflow.com/questions/25969/sql-insert-into-values-select-from – KV Prajapati Jul 27 '11 at 15:03
  • 1
    @Jon I was about to suggest the same thing. OP, what have you tried already? – tom502 Jul 27 '11 at 15:05

2 Answers2

9

This should work. You don't need to worry about the identify field in Table2.

INSERT INTO Table2
 (client_last_name, client_first_name, taskDescription, category)
 (SELECT clientLastName, clientFirstName, incidentDescription, category
  FROM Table1
  WHERE info_field IS NOT NULL)
Narnian
  • 3,858
  • 1
  • 26
  • 29
0
Member_ID nvarchar(255) primary key,
Name nvarchar(255),
Address nvarchar(255)
)
insert into Member(Member_ID,Name,Address) (select m.Member_Id,m.Name,m.Address from library_Member m WHERE Member_Id IS NOT NULL)
Nazik
  • 8,696
  • 27
  • 77
  • 123
Ashan
  • 1