2015-01-13 11 views
1

POSTGRESQL에 문제가 있습니다.동시에 두 테이블을 업데이트하십시오. postgresql

두 테이블을 동시에 업데이트하려고합니다. 동일한 조건이므로 두 업데이트를 모두 적용 할 수 있습니다.

UPDATE standards.standards 
SET description = 'The student will demonstrate positive self esteem.' 
WHERE description LIKE 'The student will demonstrate positive his/her self esteem.' 
AND custom_code LIKE 'qwertyuiop'; 

UPDATE bank SET description = 'The student will demonstrate positive self esteem.' 
WHERE description LIKE 'The student will demonstrate positive his/her self esteem.' 
AND designation LIKE 'asdfghjkl'; 

난 그냥 하나의 SQL 문에 모두 업데이트 할 :

여기에 개별 업데이 트입니다. POSTGRESQL은 두 테이블을 동시에 업데이트 할 수 없기 때문에 내가 한 일이지만 오류가 발생했습니다.

UPDATE standards.standards ss, bank bb 
SET description = 'The student will demonstrate positive self esteem.' 
WHERE description LIKE 'The student will demonstrate positive his/her self esteem.' 
AND (ss.custom_code LIKE 'qwertyuiop' OR bb.designation LIKE 'asdfghjkl'); 

도와 주시겠습니까? 난 단지 하나의 SQL 문을 원한다. 감사!

+5

이유를 한 문장으로 왜 사용해야합니까? 단일 트랜잭션에서 두 개의 업데이트 만 실행할 수 있습니까? 하나의 성명서로 어떤 문제를 해결하려고합니까? –

+0

나는 결국 내 궁금해. 다른 대안은 무엇입니까? – asdasdas

+0

왜 Postgresql 질문 일 때 MySQL에 태그를 지정해야합니까? 아니요, 하나의 delete 문에서 두 테이블을 삭제할 수 없습니다 (어쨌든 명시 적으로 트리거는 할 수 있지만 다른 이야기입니다). – jarlh

답변

2

어쨌든 원자 적으로 발생시키지 않으므로 아무런 의미가 없습니다. 더 좋은 방법으로는 해결할 수없는 문제를 해결할 수 없습니다 (예 : 값을 두 번 보내지 않으려는 경우 매개 변수를 사용).

예를 들어보기를 사용하여 두 테이블을 동시에 업데이트 할 수 있습니다. 그러나 다시, 그것은 원자가 될 수 없습니다. 이 방법이 의도 한대로 작동하는지 확인하려면 적절한 트랜잭션 처리가 필요합니다.

1

당신의 현재 정보를 고려하여 FUCTION을 작성할 수 있습니다. 예를 들어

create table standards (description text,custom_code text); 
insert into standards 
values ('hai','AA'), 
     ('how are you ?','AA'), 
     ('The student will demonstrate positive his/her self esteem.','qwertyuiop'); 


create table bank (description text,designation text); 
insert into bank 
values ('Am','BB'), 
     ('Fine','BB'), 
     ('The student will demonstrate positive his/her self esteem.' ,'asdfghjkl'); 

을 설명하고이

create or replace function update_tables(_desc text, /*-- your value to update i.e The student will demonstrate positive self esteem.*/ 
             _WDesc text, /*-- your value to use in where clause i.e 'The student will demonstrate positive his/her self esteem.' */ 
             _custom_code text, /* this AND custom_code LIKE 'qwertyuiop'; goes here */ 
             _designation text /* this AND designation LIKE 'asdfghjkl'; goes here*/ 
             ) 
returns void as 
/* add two update queries inside this function */ 
/* 1 Updating table standards*/ 
'update standards 
set description = _desc 
where description like _WDesc and 
     custom_code like _custom_code;' 
/*End*/ 
/* 2 Updating table bank*/ 
'update bank 
set description = _desc 
where description like _WDesc and 
     designation like _designation;' 
/*End*/ 
language sql 

같은 Fucntion을 만들고 여기에 두 테이블

select update_tables ('The student will demonstrate positive self esteem.', 
         'The student will demonstrate positive his/her self esteem.', 
         'qwertyuiop', 
         'asdfghjkl') 

을 업데이트 하나 SQL 명령입니다 sqlfiddle

관련 문제