July 25th, 2008

How to rename a column in Sybase ASE

Today I needed to rename a column in a Sybase ASE 12.5.1 database, and couldn’t seem to find the right syntax. My first few search results gave me syntax that was not actually relevant for my database. Two different pages led me to believe that some variation of this might work:

ALTER TABLE [table_name] RENAME [old_column_name] TO [new_column_name]

But that did not work for me. Upon closer inspection, I noticed that those two pages were for Sybase ASA, not ASE. I refined my search terms a bit, and found this:

sp_rename ‘[table_name].[old_column_name]’,'[new_column_name]’

Thanks to Rob Verschoor for posting this on dbforums.com!

September 4th, 2007

Sybase UPDATEs taking forever/timing out? Time to dump tran!

I’m a programmer, not a DBA, but I do have to be able to perform basic database configuration and maintenance tasks. One thing that’s come up, a couple times, over the past few years, is a transaction log getting full.

There are a couple of reasons this can happen:

  • poorly written queries that run for a very long time
  • applications neglecting to end transactions, when they encounter error conditions

Here are a few useful commands I’ve found that deal with transaction logs (Note: I’m using Sybase ASE 12.5.1):

check for long-running transactions (found here):
use master;

SELECT * FROM syslogshold
WHERE dbid = db_id(’MyDbName’);

transaction log space free:
SELECT CONVERT(CHAR,
(lct_admin('logsegment_freepages', 4) -
lct_admin('reserved_for_rollbacks', 4)) * 2)

transaction log space total:
SELECT SUM(size)
FROM master..sysusages
WHERE dbid = db_id('MyDbName')
AND segmap & 4 = 4 -- logsegment

dump the transaction log (clears it out, so things can get moving again!):
***WARNING*** Use this command at your OWN RISK! Please do some additional reading before executing the following statement, so that you fully understand the implications. The transaction log is there for a reason, and, from what I understand, you should not tamper with it unless you know what you’re doing. On the occasions that I’ve used this, it was a last resort. It solved my problems, with no ill effects, but it might not solve yours!

dump tran myDbName with truncate_only;
commit;

For additional information/reference, Sybase’s online books sometimes come in handy. When trying to find out how to dump transaction logs, I referred to their dump transaction page.

Another site I’ve found useful*, on numerous occasions, is Sybase 101. I found a related tip, there, for tips, there, for Dealing With Server Failure, which addresses what to do when your log fills up completely, such that you can’t even restart the server.

*Beware: I just noticed that Sybase 101 has become less useful than it used to be, because useful pages are automatically redirecting to one main start page. If they were just trying to prevent deep linking from the outside world, I could understand this behavior a little more; but it’s even happening when I click other links from their own site! Navigation works fine with javascript disabled, though.

May 15th, 2007

Checking Sybase ASE version number

This is extremely simple, but it’s something that I always forget, and I’m always happy to find it in my text file of miscellaneous useful snippets:

SELECT @@version

June 20th, 2006

getting the current date in a particular format in Windows XP/2003

I needed to get the current date, in the format ‘yyyy-MM-dd’, and use that value multiple times within a batch file.

The command ‘DATE /T’ returns a date like this:
Tue 06/20/2006

But I needed:
2006-06-20

Now, this would have been extraordinarily easy, if I could use 4nt, cygwin, nclip, or any number of other tools. However, my options were limited on the machine that I needed to run this on, as it is a production web application server. So, I had to come up with a way to do this using the tools I had available to me:
- Windows 2003 Server (assumed to be a default installation, or more minimal than that)
- Sybase ASE 12.5.x
- Java

I probably could have just written a simple java application to do this, but where’s the fun in THAT? :)

So, I consulted with my good friend, Jeff Scanlon, as I usually do when I have an interesting tech challenge, and together we came up with this batch file [NOTE: you can download it here, if the code gets too munged with the blogger html/css]:

—— BEGIN BATCH FILE ——-

@echo off
REM generate SQL script to get date in correct format:
echo select ’set my_date=’ + str_replace(convert(char(20),getdate(),102),’.',’-') >%TEMP%\getDate_yyyy-MM-dd.sql
echo go >>%TEMP%\getDate_yyyy-MM-dd.sql

REM run the SQL script to generate a simple, temporary batch file:
isql -U username -P password -i %TEMP%\getDate_yyyy-MM-dd.sql | findstr set >%TEMP%\set_my_date.bat

REM run the temporary batch file, which will set an environment variable, ‘MY_DATE’, to contain the date:
call %TEMP%\set_my_date.bat

REM clean up
del /q %TEMP%\set_my_date.bat
del /q %TEMP%\getDate_yyyy-MM-dd.sql

REM this is where you’d actually put code that uses the date, like:
REM md c:\backup\%MY_DATE%
REM copy c:\mystuff\*.* c:\backup\%MY_DATE%

—— END BATCH FILE ——-

November 23rd, 2005

Simple column name list in sybase with sqsh

At my current job, we primarily use Sybase ASE for our database servers. For most of my querying needs, I use a nice little tool called sqsh, a handy replacement for the basic Sybase ‘isql’.

I hate the excessive amounts of output that sybase gives me when I do ‘sp_help [table_name]‘, because most of the time all I’m trying to do is get a list of column names for a particular table.

MySql’s ’show databases’, ’show tables’, ’show columns’, etc. seem so much more intuitive to me.

So, a couple days ago I chained together a few tools, and created an alias + function combo that let me simply type ‘show_columns [table_name]‘, and get nothing but a quick list of column names.

Here is what I added to my .sqshrc file to make this a permanent addition to my collection of useful commands

\func show_columns_func
\echo Column list for table: ${1}
sp_help ${1}; -m vert grep “Column_name:” \sed -e ’s/Column_name: *\([^ ]\)/\1/g’
\done
\alias show_columns=”\call show_columns_func ”