Showing posts with label Sybase. Show all posts
Showing posts with label Sybase. Show all posts

Tuesday, August 27, 2013

How do you select random rows in Sybase?

SELECT TOP 10 *
FROM <TableName>
ORDER BY NEWID();

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.

Tuesday, August 7, 2012

How do you find tables with particular column by name in Sybase?

SELECT Object_name(id)
FROM syscolumns
WHERE name = '<Coulmn Name>'


or

SELECT sysobjects.name
FROM sysobjects, syscolumns
WHERE sysobjects.id = syscolumns.id

AND syscolumns.name = '<Column Name>'

Monday, July 16, 2012

What are the modules included in SybPerl?

Sybperl (Sybase extensions to Perl) includes the following four modules:

1. Sybase::DBlib
2. Sybase::CTlib
3. Sybase::BCP
4. Sybase::Sybperl

Friday, September 30, 2011

Write script to create a report of attributes of table of Sybase.

#!/bin/sh

Today=`date '+%d-%m-%y'`
report="report.$Today.txt"

print_header()
{
 awk 'BEGIN{for(c=0;c<50;c++) printf "-"; printf "\n"}'
 echo $0 started on $Today at `date '+%r'`
 awk 'BEGIN{for(c=0;c<50;c++) printf "-"; printf "\n"}'
}
print_footer()
{
 awk 'BEGIN{for(c=0;c<50;c++) printf "-"; printf "\n"}'
 echo $0 ended on $Today at `date '+%r'`
 awk 'BEGIN{for(c=0;c<50;c++) printf "-"; printf "\n"}'
}

print_header >> $report

isql -U<UserName> -P<Password> -S<ServerName> -D<DBName> <<EOF >>$report
set nocount on
select * from <TableName> where 1=2
go
exit
EOF
print_footer >> $report
echo $report has been successfully created!

Friday, September 23, 2011

Write code to connect Sybase and C.

#include <sybfront.h>
#include <syberror.h>
#include <sybdb.h>
#include <stdlib.h>
#include <stdio.h>

#define DB_SERVER       "******"
#define DB_USER            "******"
#define DB_PWD              "******"
#define DB_SOURCE      "******"
#define DB_TABLE           "******"

int main()
{
        DBPROCESS    *dbproc;
        LOGINREC     *login;
        DBINT        attachment_id;
        DBINT        row_number = 0;
        RETCODE      return_code;

        if (dbinit() == FAIL)
                exit(ERREXIT);

        login = dblogin ();

        DBSETLUSER (login, DB_USER);
        DBSETLPWD (login, DB_PWD);
        dbproc = dbopen(login, DB_SERVER);

        dbuse(dbproc, DB_SOURCE);
        dbcmd(dbproc, "select top 10 attachment_id from DB_TABLE");
        dbsqlexec(dbproc);

        while ((return_code = dbresults(dbproc)) != NO_MORE_RESULTS)
        {
                if (return_code == SUCCEED)
                {
                        dbbind(dbproc, 1, INTBIND, (DBINT) 0, (BYTE *) &attachment_id);

                        while (dbnextrow(dbproc) != NO_MORE_ROWS)
                        {
                                printf ("%d\n", attachment_id);
                        }
            }
        }

        dbclose (dbproc);
        dbexit();
        return 0;
}

Wednesday, September 21, 2011

Write code in Perl to connect to Sybase (SybPerl - CTlib) database and fetch resultset.

#!/usr/bin/perl

use Sybase::CTlib;

my $DB_SERVER_NAME = "******";
my $DB_USER_NAME = "******";
my $DB_USER_PSWD = "******";
my $DB_NAME = "******";
my $DB_TABLE_NAME = "******";

$dbh = new Sybase::CTlib $DB_USER_NAME, $DB_USER_PSWD, $DB_SERVER_NAME;

$dbh->ct_sql("use $DB_NAME");

$dbh->ct_execute("set rowcount 2\select * from $DB_TABLE_NAME");

while($dbh->ct_results($restype) == CS_SUCCEED)
{
    next unless $dbh->ct_fetchable($restype);

    while(@data = $dbh->ct_fetch)
    {
        print "@data\n";
    }
}

Saturday, September 17, 2011

Write code in Perl to connect to Sybase (DBlib) database and fetch resultset.

#!/usr/bin/perl

use Sybase::DBlib;

$DB_SERVER_NAME="******"; #Server Name
$DB_USER_NAME="******";#User Name
$DB_USER_PSWD="******";#Password
$DB_NAME="******";#Database Name
$DB_TABLE_NAME="******";#Table Name
my $DB_CMD="******";#Query to run

my $dbh = new Sybase::DBlib $DB_USER_NAME, $DB_USER_PSWD, $DB_SERVER_NAME;
$dbh->dbuse($DB_NAME);
$dbh->dbcmd($DB_CMD);
$dbh->dbsqlexec;

while($dbh->dbresults != NO_MORE_RESULTS)
{
while(@data = $dbh->dbnextrow)
{
print "@data\n";
}
}
$dbh->dbclose();

Tuesday, September 13, 2011

How do get/put bulk data from/to a Sybase database in UNIX?


#get data
bcp <Database Name>..<Table Name> out /home/nayakr/out.csv -S<Server Name> -U<User Id> -P<Passowrd> -c

#put data
bcp <Database Name>..<Table Name> in /home/nayakr/in.csv -S<Server Name> -U<User Id> -P<Passowrd> -c