2011-07-27 7 views
1

두 개의 테이블이 있지만 열의 이름이 약간 다릅니다. 1 개의 테이블에서 정보를 가져 와서 다른 테이블에 넣고 싶습니다. 테이블 1의 "정보 필드"가 null이 아닌 경우에만 테이블 1의 정보를 테이블 2에 넣어야합니다. 표 2는 무언가가 생성 될 때마다 고유 한 ID를 가지므로 삽입 된 모든 것이 다음 사용 가능한 ID 번호를 가져와야합니다.한 테이블에서 다른 테이블로 데이터를 삽입하십시오.

표 1

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

표 2

*need a unique id assigned 
client_last_name 
client_first_name 
taskDescription 
category 
+1

다음이 게시물을 읽을. http://stackoverflow.com/questions/25969/sql-insert-into-values-select-from – adatapost

+1

@ 존 나는 똑같은 제안을하려했다. OP, 넌 이미 뭐 해봤 니? – tom502

답변

9

이 작동한다. 표 2의 식별 필드에 대해 걱정할 필요가 없습니다. 당신은 문법 관련 문제에 직면하는 경우

INSERT INTO Table2 
(client_last_name, client_first_name, taskDescription, category) 
(SELECT clientLastName, clientFirstName, incidentDescription, category 
    FROM Table1 
    WHERE info_field IS NOT NULL) 
+0

이름의 차이는 중요하지 않습니다. – Narnian

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) 
관련 문제