Monday, August 27, 2012

How do you create temp tables in Sybase?

You can create Sybase temp tables in 2 ways :

1. CREATE TABLE #TableName
2. CREATE TABLE tempdb..TableName

Temp table being created with "#" are accessible by the current session or procedure and the table gets  automatically dropped when the session/procedure ends or is dropped manually.

Table being created using "tempdb..TableName" resides in tempdb database and remains as long as not dropped in manually. It is shareable among all sessions.

** If a table is created in any database other than tempdb, it won't be a temp table.

How could you determine the directory a shell script is in?

#!/usr/bin/sh -f

_DIR=`dirname "$(readlink -f $0)"`
echo ${_DIR}

Write a self-terminating shell script which doesn't hang up indefinately.

#!/usr/bin/sh

( sleep 5 ; echo Warning $0 hanging - aborted ; kill -9 $$ ) &
TIMEOUT_PID=$!

Your_Line1 ...
Your_Line2 ...
Your_Line3 ...
Your_Line4 ...
Your_Line5 ... 

# kill supervision sub-process
kill -9 $TIMEOUT_PID

How do you get 2nd, 3rd column of 2nd, 3rd row of CSV file?


head -3  <FileName> | tail -2 | cut -d "," -f 2-3
or
cat <FileName> | sed -n '2,3p' | cut -d "," -f 2-3
or
cat <FileName> | awk 'NR==2,NR==3' | cut -d "," -f 2-3

How could you read a file line by line in UNIX?

#!/usr/bin/sh

for line in $(cat <FileName>)
do
    echo "$line"
done

or

#!/bin/sh

file="<FileName>"

while read line
do
    echo $line
done < $file

Wednesday, August 22, 2012

How could you retrieve the manager's name of an employee within the EMPLOYEE table?

SELECT E.EMP_ID   AS EMPLOYEE_ID,
       E.EMP_NAME AS EMPLOYEE_NM,
       M.EMP_NAME AS MNGR_NM
FROM   EMP_MNGR E
       LEFT OUTER JOIN EMP_MNGR M
                    ON E.MNGR_ID = M.EMP_ID
ORDER  BY E.EMP_ID;


And the EMPLOYEE table goes as below :
EMP_ID    EMP_NAME    MNGR_ID
1001    Roger Scott   
2500    Mike Larsen    1001
2538    Marry Gartner  2500
2567    David Rice     2538
2570    Ben Tenesion   2567
2590    Joseph Robert  2570
3000    Adam McNally   1001

Tuesday, August 21, 2012