2013-07-18 70 views
0

存在的錯誤,我想:SQL LIKE /地方使用INSERT語句

與上TBTRIGPMMCANC也TBPROPPAY所有id_payments的列表中創建一個臨時表。

從此列表中將它們從TBPROPPAY移動到TBPROPPAYH。

然後從TBPROPPAY中刪除它們。

這是我的sql我有一個插入語句問題。我想我需要像以前一樣的SQL將行移動到tbproppayh爲每個id_payment在臨時表中,但我不知道如何編寫它

錯誤是: E_US0AD8插入:目標列的數量必須等於目標 列出指定子查詢中的元素。 (星期四7月18日9時31分23秒2013)

----Clean up for IN:035466 

----Description : Create a temporary table with with a list of all id_payments that are on TBTRIGPMMCANC and also TBPROPPAY. 
---------------------------------------------------------------------------------------------------------------------------- 
DECLARE GLOBAL TEMPORARY TABLE session.set_values AS 
SELECT a.id_payment FROM TBTRIGPMMCANC a, TBPROPPAY b 
WHERE a.id_payment = b.id_payment 
ON COMMIT PRESERVE ROWS WITH NORECOVERY;\p\t\g 


--View temporary table 

SELECT * FROM session.set_values;\p\t\g 
-------------------------------------------------------------- 

-- Description: Move list from TBPROPPAY to TBPROPPAYH 
------------------------------------------------------------- 
SELECT id_payment FROM session.set_values 
WHERE EXISTS id_payment;\p\t\g 

-- Insert into tbproppayh 
------------------------------------------------------------- 
Insert into tbproppayh 
Select id_payment FROM TBTRIGPMMCANC a, TBPROPPAY b 
WHERE a.id_payment = b.id_payment;\p\t\g 
------------------------------------------------------------- 

-- Description: DELETE list FROM tbproppay 
------------------------------------------------------------- 
Delete FROM tbproppay b WHERE id_payment IN (Select a.id_payment FROM TBTRIGPMMCANC a, TBPROPPAY b 
WHERE a.id_payment = b.id_payment);\p\t\g 

------------------------------------------------------------- 
--Select from tbproppay & TBTRIGPMMCANC 
------------------------------------------------------------- 
SELECT a.id_payment FROM TBTRIGPMMCANC a, TBPROPPAY b 
WHERE a.id_payment = b.id_payment;\p\t\g  
------------------------------------------------------------- 

回答

0

如果你想要一個臨時表可能是最好使用視圖來代替。您可以獲取所需的所有詳細信息,然後從視圖中獲取。

稍後會發布代碼。

你得到的錯誤是相當自我解釋。您正在爲目標選擇太多列。

0

如果表tbproppayh有不止一個列,您必須對您的INSERT語句中指定的影響列:

Insert into tbproppayh (mycolumn) 
Select id_payment 

另一個建議,把別名表id_payment前場否則解決第一個錯誤後,你會得到另一個錯誤在ambiguos領域。

+0

插入到tbproppayh(id_payment)選擇a.id_payment從TBTRIGPMMCANC a,TBPROPPAY b WHERE a.id_payment = b.id_payment。仍然無法正常工作在插入錯誤 – louise

+0

上嘗試插入值時在目標表中的現有密鑰上添加條件:INSERT INTO ... SELECT .... AND NOT EXISTS(SELECT'X'FROM tbproppayh where condition關鍵) –