0

How to display the sql query output to HTML table format output. am having shell script code to display output as html table format. and input is taken form sql query but am not getting the proper output. am getting the heading and data with unstructured it means it is taking the first column value only heading other displaying one by one format not displaying the table format. anyone help out to resolve this issue. Thanks in advance. This is my code : # SQL query to retrieve data from the custom table

#!/bin/bash

output_file="output.html"
temp_file="temp.txt"
delimiter="|"

sql_query="SELECT 'REQUEST_ID' AS \"REQUEST_ID\",
    'REQUESTED_BY' AS \"REQUEST_BY\",
    'USER_NAME' AS \"USER_NAME\",
    'REQUEST_DATE' AS \"REQUEST_DATE\",
    'CON_PRG_NAME' AS \"CON_PRG_NAME\",
    'OUTFILE_NAME' AS \"OUTFILE_NAME\",
    'FILE_MOVED_STATUS' AS \"FILE_MOVED_STATUS\",
    'CON_STATUS_CODE' AS \"CON_STATUS_CODE\",
    'ORG_ID' AS \"ORG_ID\"
    FROM DUAL
    UNION ALL
    SELECT TO_CHAR(request_id),
        TO_CHAR(requested_by),
        user_name,
        TO_CHAR(request_date),
        concurrent_program_name,
        outfile_name,
        file_moved_status,
        status_code,
        TO_CHAR(org_id)
    FROM xx_concurrent_stg
    WHERE concurrent_program_name = '$concurrent_program_name'
    AND request_date BETWEEN TO_DATE('$from_date', 'DD-MON-YY') AND TO_DATE('$to_date', 'DD-MON-YY');"

# Run the SQL query and save the output to a temporary file
sqlplus -S apps/apps@EBSDB <<EOF > $temp_file
SET PAGESIZE 0
SET FEEDBACK OFF
SET HEADING OFF
SET COLSEP "$delimiter"
SET TRIMSPOOL ON

$sql_query

EXIT;
EOF

# Generate the HTML table from the temporary file
awk -v delimiter="$delimiter" '
BEGIN {
    print "<table border=1 cellspacing=2 cellpadding=2>"
    FS = delimiter
}
NR == 1 {
    print "<tr>"
    for (i = 1; i <= NF; i++) {
        printf "<th>%s</th>", $i
    }
    print "</tr>"
}
NR > 1 {
    print "<tr>"
    for (i = 1; i <= NF; i++) {
        printf "<td>%s</td>", $i
    }
    print "</tr>"
}
END {
    print "</table>"
}' $temp_file > $output_file
5
  • 2
    Ok, but why? The database engines - also the oracle - can generate the html output. dba-oracle.com/oracle_tips_html_sql_plus.htm Commented May 25, 2023 at 11:38
  • Please use something that can run sql queries and fill html templates. E.g. python + psycopg + jinja. Commented May 25, 2023 at 11:43
  • 1) I am not sure the union is needed here. 2) I would work by selecting a single column (or a couple) and try to awk it. 3) I don't have sqlplus at hand, but there should be an HTML output option. in mysql I used XML ans xsltproc to format. Commented May 25, 2023 at 11:56
  • Please edit your question and give us an example input and the output you would want from that example. We don't have access to your database, so we cannot run your code. Commented May 25, 2023 at 12:59
  • Is the problem that the awk script does not create the expected output from the input file temp.txt? Or that the SQL query does not create the expected result in temp.txt? Please edit your question to clarify this. In the first case the SQL command might be informational only. Please show example contents of temp.txt, the actual output in output.html and the expected output. In the second case also show or describe (relevant parts of) the database tables. Commented May 25, 2023 at 14:10

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.