簡體   English   中英

將結果從MySQL過程傳遞到Linux命令行

[英]Pass results from a MySQL procedure to the Linux command line

我有一個運行以下選擇的過程:

select distinct concat( 'php /home/rudyerd-systems/frontend/ClientStatementGenerator.php ', left(user(), locate('@',user())-1), space(1), ca.client_id, space(1), date_format( @pTradingPeriodMonth, '%y%m' ), space(1), date_format( @pTradingPeriodMonth, '%y%m' ),
                    ' > /home/rudyerd-systems.sco/frontend/bash/new_statement_test.html' )
from   ca_client_account ca,
       ca_tranche tr
where  @pTradingPeriodMonth    between tr.dt_value and ifnull( tr.dt_withdrawal, @pTradingPeriodMonth ) and
       ca.client_account_id   = tr.client_account_id
order  by ca.client_id;

並產生以下結果:

| php /home/rudyerd-systems/frontend/ClientStatementGenerator.php scorbet ATP 1205 1205 > /home/rudyerd-systems.sco/frontend/bash/new_statement_test.html                                                                                                          |
| php /home/rudyerd-systems/frontend/ClientStatementGenerator.php scorbet BLB 1205 1205 > /home/rudyerd-systems.sco/frontend/bash/new_statement_test.html                                                                                                          |
| php /home/rudyerd-systems/frontend/ClientStatementGenerator.php scorbet CAR 1205 1205 > /home/rudyerd-systems.sco/frontend/bash/new_statement_test.html                                                                                                          |
| php /home/rudyerd-systems/frontend/ClientStatementGenerator.php scorbet CDR 1205 1205 > /home/rudyerd-systems.sco/frontend/bash/new_statement_test.html                                                                                                          |
| php /home/rudyerd-systems/frontend/ClientStatementGenerator.php scorbet CSP 1205 1205 > /home/rudyerd-systems.sco/frontend/bash/new_statement_test.html                                                                                                          |
| php /home/rudyerd-systems/frontend/ClientStatementGenerator.php scorbet DGC 1205 1205 > /home/rudyerd-systems.sco/frontend/bash/new_statement_test.html                                                                                                          |
| php /home/rudyerd-systems/frontend/ClientStatementGenerator.php scorbet FDR 1205 1205 > /home/rudyerd-systems.sco/frontend/bash/new_statement_test.html                                                                                                          |
| php /home/rudyerd-systems/frontend/ClientStatementGenerator.php scorbet FVN 1205 1205 > /home/rudyerd-systems.sco/frontend/bash/new_statement_test.html                                                                                                          |
| php /home/rudyerd-systems/frontend/ClientStatementGenerator.php scorbet JLM 1205 1205 > /home/rudyerd-systems.sco/frontend/bash/new_statement_test.html                                                                                                          |
| php /home/rudyerd-systems/frontend/ClientStatementGenerator.php scorbet JRA 1205 1205 > /home/rudyerd-systems.sco/frontend/bash/new_statement_test.html                                                                                                          |
| php /home/rudyerd-systems/frontend/ClientStatementGenerator.php scorbet JRP 1205 1205 > /home/rudyerd-systems.sco/frontend/bash/new_statement_test.html                                                                                                          |
| php /home/rudyerd-systems/frontend/ClientStatementGenerator.php scorbet MJO 1205 1205 > /home/rudyerd-systems.sco/frontend/bash/new_statement_test.html                                                                                                          |
| php /home/rudyerd-systems/frontend/ClientStatementGenerator.php scorbet MPW 1205 1205 > /home/rudyerd-systems.sco/frontend/bash/new_statement_test.html                                                                                                          |
| php /home/rudyerd-systems/frontend/ClientStatementGenerator.php scorbet NTC 1205 1205 > /home/rudyerd-systems.sco/frontend/bash/new_statement_test.html                                                                                                          |
| php /home/rudyerd-systems/frontend/ClientStatementGenerator.php scorbet RJA 1205 1205 > /home/rudyerd-systems.sco/frontend/bash/new_statement_test.html                                                                                                          |
| php /home/rudyerd-systems/frontend/ClientStatementGenerator.php scorbet RMM 1205 1205 > /home/rudyerd-systems.sco/frontend/bash/new_statement_test.html                                                                                                          |
| php /home/rudyerd-systems/frontend/ClientStatementGenerator.php scorbet SJC 1205 1205 > /home/rudyerd-systems.sco/frontend/bash/new_statement_test.html                                                                                                          |
| php /home/rudyerd-systems/frontend/ClientStatementGenerator.php scorbet UDB 1205 1205 > /home/rudyerd-systems.sco/frontend/bash/new_statement_test.html 

我想將過程的結果直接輸出到Linux命令行,以便創建包含每個變量的HTML頁面。 該過程將從我要在其中創建HTML文件的文件夾中的bash腳本運行。

為了完整性,這是完整的過程。

-- ---------------------------------------------------------------------------------
-- pUIGetCliStmtGenList
--
-- This procedure returns the Clients who statements need to be generated for
--
-- ---------------------------------------------------------------------------------

drop procedure if exists pUiGetCliStmtGenList;
delimiter //

create procedure pUiGetCliStmtGenList(
  IN  pTradingPeriodMonth      DATE,
  IN  pUsername                VARCHAR,
  IN  pPassword                VARCHAR,
  OUT pResult                  INT
)
MODIFIES SQL DATA
COMMENT 'Gathers the list of people a statement needs to be created for'
begin
 --
  declare vClient_id           VARCHAR(3);
  declare vAmtTradedSystemCcy  DECIMAL(13,2);
  declare vCount               INT;
  --
  declare EXIT handler for SQLWARNING, SQLEXCEPTION call pRdHandleError( 10000, 'pUiGetCliStmtGenList', vHint );
  --  ***********************************************************************************************************
  set vHint = pTradingPeriodMonth;
  set pResult = 0;

  SELECT DISTINCT CONCAT( 'php /home/rudyerd-systems/frontend/ClientStatementGenerator.php ', left(user(), locate('@',user())-1), space(1), ca.client_id, space(1), date_format( @pTradingPeriod, '%y%m' ), space(1), date_format( @pTradingPeriod, '%y%m' ),
                    ' > /home/rudyerd-systems.sco/frontend/bash/'ca.client_id''@pTradingPeriod'statement.html' )
  FROM   ca_client_account ca,
         ca_tranche tr
  INTO     
  WHERE  @pTradingPeriodMonth    between tr.dt_value and ifnull( tr.dt_withdrawal, @pTradingPeriodMonth ) and
         ca.client_account_id   = tr.client_account_id
  ORDER BY ca.client_id;
  --
  -- If there are no clients
    if vCount <> 0 then
     call pRdHandleError( 1002, 'pUiGetCliStmtGenFlag', vHint );
    end if; 
  --
  --   
  ELSE
  -- Output to Linux Command Line and Generate the HTML pages

  -- Then Convert the pages to pdf
  -- 
  set pResult = 1;
  --
end;
//

delimiter ;

假設輸出顯示旨在成為可運行的腳本,並且您將>更改為>>或在輸出文件名中添加字段值之一,以便它們是唯一的,則可能是這樣:

mysql -B -u username -p dbname < script.sql | sed 's/|//' | sh

sed命令刪除管道字符(如果存在)。 通過sh傳遞整個內容,將使用給定的參數執行運行php每一行,並將輸出重定向到給定的文件。 您可能需要更改mysql參數以適合您的需求。

如果您有權訪問服務器,從而可以創建文件,則可以將輸出導出為CSV文件,例如:

SELECT field1,field2 FROM table1
INTO OUTFILE '/home/output.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'

然后,您可以編寫解析器或使用其他腳本語言/工具來修改條目,以滿足您的需求...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM