Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Stored Procedure across database and servers? 1

Status
Not open for further replies.

coders4hire

IS-IT--Management
Dec 31, 2003
22
US
We have a HISTORY table on a backup server. I've never written a stored procedure in Sybase. I would like to open the backup server, and export the HISTORY table to the PRODUCTION server into a temp table where I can then do a join and export.
1. Can that be done?
2. Could you give me some sample code?

Thanks,
Doug

---
doug@coders4hire.com
 
I don't think that Sybase supports a
Code:
server..database..table
mechanism, but what I've done when moving code from one server to another is written a make_bcp shell script that basically takes a query as input (so you can specify where-clause criteria), generates a pipe-delimited file, and prompts you for the destination server/table/etc.
Code:
#!/bin/sh

err_flg=0
progname=`basename $0`

#
# set command-line argument defaults
#

SVR=<some server>
DBN=<some dbname>
UID=<some uid>
OUF=&quot;&quot;
END=&quot;.tmp&quot;
SKIP=&quot;N&quot;

#
# Get command-line arguments
#

while getopts D:d:O:o:P:p:S:s:U:u:Xx argument
do
  case $argument in

      D|d) DBN=$OPTARG;;
      O|o) OUF=$OPTARG;;
      P|p) PWD=$OPTARG;;
      S|s) SVR=$OPTARG;;
      U|u) UID=$OPTARG;;
      X|x) SKIP=&quot;Y&quot;;;
      ?) err_flg=1;;
  esac
done

shift `expr $OPTIND - 1`
SCR=$1

if [ &quot;${SKIP}&quot; = &quot;Y&quot; ]
then
    echo &quot;&quot;
    echo &quot;WILL SKIP ASKING TO LOAD TO ANOTHER TABLE&quot;
fi

#
# Exit if there were argument errors
#
if [ $err_flg -eq 1 ]
then
  echo &quot;&quot;
  echo &quot;USAGE:  $progname [-d database] [-h] [-o out_file] -p password [-s server] [-u user] query_file&quot;
  echo &quot;&quot;
  exit 2
fi

if [ -f &quot;$SCR&quot; ]
then
  continue
else
  echo &quot;&quot;
  echo File \&quot;$SCR\&quot; does not exist in this directory.
  echo &quot;&quot;
  exit 1
fi

if [ &quot;$OUF&quot; = &quot;&quot; ]
then
    OUF=${SCR}.dat
fi

if [ -f ${OUF} ]
then
  rm ${OUF}
fi

END=&quot;.tmp&quot;

if [ -f $OUF ]
then
  echo &quot;Deleting $OUF from previous run...&quot;
  /usr/bin/rm $OUF
fi

echo &quot;&quot;
echo &quot;Running isql query......&quot;
isql -U $UID -P $PWD  -S $SVR -D $DBN -i $SCR -o $OUF -w 9000 -s \| -h

if [ ! -s $OUF ]
then
    echo &quot;&quot;
    echo &quot;Query <$SCR> failed or returned no rows.  Result file <$OUF> not created.&quot;
    rm $OUF
    echo &quot;&quot;
    exit 1
fi

echo &quot;Done&quot;
echo &quot;&quot;
echo &quot;Formatting BCP file.....&quot;

#
# initialize variables
#
FILE_NAME=$OUF
END=&quot;.tmp&quot;
FILE_NAME2=$FILE_NAME$END

#
# Format the file
#

echo &quot;1...&quot;
sed 's/ *$//' $FILE_NAME > $FILE_NAME2
mv $FILE_NAME2 $FILE_NAME

echo &quot;2...&quot;
sed 's/^\| *//' $FILE_NAME > $FILE_NAME2
mv $FILE_NAME2 $FILE_NAME

echo &quot;3...&quot;
sed 's/^\|//' $FILE_NAME > $FILE_NAME2
mv $FILE_NAME2 $FILE_NAME

echo &quot;4...&quot;
sed 's/\| */\|/g' $FILE_NAME > $FILE_NAME2
mv $FILE_NAME2 $FILE_NAME

echo &quot;5...&quot;
sed 's/ *\|/\|/g' $FILE_NAME > $FILE_NAME2
mv $FILE_NAME2 $FILE_NAME

echo &quot;6...&quot;
sed 's/\|$//' $FILE_NAME > $FILE_NAME2
mv $FILE_NAME2 $FILE_NAME

echo &quot;7...&quot;
sed 's/\|\|/\| \|/g' $FILE_NAME > $FILE_NAME2
mv $FILE_NAME2 $FILE_NAME

echo &quot;8...&quot;
sed 's/\|NULL/\|/g' $FILE_NAME > $FILE_NAME2
mv $FILE_NAME2 $FILE_NAME

echo &quot;Done&quot;
echo &quot;&quot;

if [ &quot;${SKIP}&quot; = &quot;Y&quot; ]
then
    return 0
fi

#
# If we've been successful, maybe the user wants to bcp the file now?
#

echo &quot;Do you want to bcp <$OUF> into another database table [y/n]? \c&quot;
read run_bcp

case $run_bcp in
    Y|y) echo &quot;Please provide the following:&quot;
         echo &quot;Target Server: \c&quot;
	 read TSVR
	 echo &quot;Target Database: \c&quot;
	 read TDBN
	 echo &quot;User on <$TDBN>: \c&quot;
	 read TUID
	 echo &quot;Password for <$TUID>: \c&quot;
	 read TPWD
         echo &quot;Target Table: \c&quot;
         read TTAB
         echo &quot;Batch Size: \c&quot;
         read BSZ
         echo &quot;&quot;
         echo &quot;Errors will be in <bcp.err>.\n&quot;
         echo &quot;Loading.....&quot;
         echo &quot;bcp $TDBN..$TTAB in $OUF -b$BSZ -c -t \| -U$TUID -P$TPWD -S$TSVR -e bcp.err -m 99999&quot;
         bcp $TDBN..$TTAB in $OUF -b$BSZ -c -t \| -U$TUID -P$TPWD -S$TSVR -e bcp.err -m 99999
	 ;;
  
  *) echo &quot;&quot;
     echo &quot;Data in bcp-format can be found in <$OUF>.&quot;
     echo &quot;To load database, the command is:&quot;
     echo &quot;  bcp $DBN..<tablename> in $OUF -b500 -c -t \| -U$UID -P$PWD -S$SVR -e bcp.err -m 99999&quot;
     echo &quot;&quot;
     ;;
esac

exit 0
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top