DEV Community

Query Filter
Query Filter

Posted on

syb

#!/usr/bin/env bash

SYBASE_SERVER=MY_ASE
DB_NAME=mydb
USER=myuser
PASS=mypass

INPUT_FILE=tables.txt
REPORT=report.csv

echo "table,status,max_datetime,updated_last_2_days" > "$REPORT"

while read -r TABLE COL; do
  [ -z "$TABLE" ] && continue

  RESULT=$(isql -S "$SYBASE_SERVER" -U "$USER" -P "$PASS" -D "$DB_NAME" -b -s"|" <<EOF
set nocount on

if object_id('$TABLE') is null
begin
    select 'NOT_FOUND' as status, null as max_dt, 'NO' as recent
end
else
begin
    if not exists (select 1 from $TABLE)
    begin
        select 'EMPTY' as status, null as max_dt, 'NO' as recent
    end
    else
    begin
        select
            'OK' as status,
            convert(varchar(30), max($COL), 120) as max_dt,
            case
                when max($COL) >= dateadd(day, -2, getdate()) then 'YES'
                else 'NO'
            end as recent
        from $TABLE
    end
end
go
EOF
)

  LINE=$(echo "$RESULT" | tail -1)
  STATUS=$(echo "$LINE" | cut -d"|" -f1 | xargs)
  MAX_DT=$(echo "$LINE" | cut -d"|" -f2 | xargs)
  RECENT=$(echo "$LINE" | cut -d"|" -f3 | xargs)

  echo "$TABLE,$STATUS,$MAX_DT,$RECENT" >> "$REPORT"

done < "$INPUT_FILE"

Enter fullscreen mode Exit fullscreen mode

Top comments (0)