Mysql

從一個數據庫呼叫儲存過程以在另一個數據庫中給我一個結果

  • September 15, 2015
CREATE DEFINER=root@localhost
PROCEDURE AccountReqs( IN IsNewAccountStart VARCHAR (255)
                , In AccountName varchar (255)
                , IN AccountNumber varchar(255)
                , In PaymentTerm varchar(255)
                , IN Batch varchar (255)
                , In AdditionalEmailAddresses varchar (500)
                , In WorkEmail varchar(500)
                , In PersonalEmail varchar (500) )`
BEGIN
   SELECT * FROM ACCOUNTS
   WHERE IsNewAccountStart <>'TRUE' AND IsNewAccountStart <>' ' 
   Union all 
   SELECT * FROM ACCOUNTS WHERE AccountName=' ' 
   Union all 
   SELECT * FROM ACCOUNTS WHERE AccountNumber=' ' 
   Union all 
   SELECT * FROM ACCOUNTS 
   WHERE PaymentTerm <> 'Net 30' 
   AND PaymentTerm <> 'Net 45' 
   AND PaymentTerm <> 'Net 60' 
   AND PaymentTerm<> 'Due Upon Receipt' 
   Union All 
   SELECT * FROM ACCOUNTS WHERE Batch <> 'Batch1';

   SELECT * FROM ACCOUNTS
   WHERE AdditionalEmailAddresses <> ' '
   OR WorkEmail <> ' '
   OR PersonalEmail <> ' ';

END

建議 #1

您需要在呼叫儲存過程之前顯式設置數據庫

USE targetDB
CALL Shared.AccountReqs(...);

建議 #2(可選)

由於所有這些UNION子句都針對同一個表,因此請嘗試將所有WHERE子句組合在一個SELECT. 我在您的原始問題中看到了兩個 SELECT。因此,儲存過程應該看起來更像這樣:

CREATE DEFINER=`root`@`localhost` PROCEDURE `AccountReqs`(
 IN IsNewAccountStart VARCHAR (255),
 In AccountName varchar (255),
 IN AccountNumber varchar(255),
 In PaymentTerm varchar(255),
 IN Batch varchar (255),
 In AdditionalEmailAddresses varchar (500),
 In WorkEmail varchar(500),
 In PersonalEmail varchar (500)
)
BEGIN
   SELECT * FROM ACCOUNTS 
   WHERE (IsNewAccountStart <>'TRUE' AND IsNewAccountStart <>' ')
   OR    (AccountName=' ') OR (AccountNumber=' ')
   OR    (PaymentTerm NOT IN ('Net 30','Net 45','Net 60','Due Upon Receipt'))
   OR    Batch<> 'Batch1';

   SELECT * FROM ACCOUNTS
   where AdditionalEmailAddresses<>' '
   Or  WorkEmail<> ' '
   Or  PersonalEmail<> ' ';
END

引用自:https://dba.stackexchange.com/questions/115093