Bulk copy out of text and image data

From SybaseWiki
Revision as of 19:24, 29 January 2012 by Psap (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

When you want to export a database with bcp, you need to check if there is any text or image data (blobs) in the database, check the maximum size of these blobs and use this to specify the value for the -T argument in bcp command. Here are some usefull queries.

To determine the tables and columns in the database that use text/image data:

select user_name(o.uid) as "owner",
       o.name as "table",
       c.name as "column"
       from   sysobjects o,
              syscolumns c
       where  o.type = "U"
       and    o.id = c.id
       and    c.type in (34,35)
       order  by 1,2,3

Once you know which columns are used to store blobs, run the following query to select the maximum length of a blob in bytes:

select max(datalength(<column_name>)) from <table_name>

Here is a stored procedure to check a database. You need to use ASE 12.0 or later for this.

create proc sp_blobsize
as

set nocount on
set flushmessage on

declare myCursor cursor for
  select user_name(o.uid),o.name,c.name
  from sysobjects o,syscolumns c
  where o.type = "U" and o.id = c.id and c.type in (34,35,174)
  order by 1,2,3

declare @user varchar(30),@table varchar(255),@column varchar(255),@cmd varchar(1024),@first tinyint,@db varchar(30)

select @first = 0,@db = db_name()

print "This utility looks for text and image data in tables and"
print "calculates the maximum size in bytes."
print "Checking database %1!",@db
print ""

open myCursor
if @@error != 0 return  1

create table #output
(user_name      varchar(30)     not null,
table_name      varchar(255)    not null,
column_name     varchar(255)    not null,
max_size        int             not null)

fetch myCursor into @user, @table, @column

while @@sqlstatus = 0
begin
  select @first = 1

  print "Calculating %1!.%2!.%3!",
        @user,@table,@column

  select @cmd   = "insert into #output select '" + @user + "','" + @table + "','"
                + @column + "',isnull(max(datalength(" + rtrim(@column)
                + ")),0) from " + rtrim(@user) + "." + rtrim(@table)
  exec (@cmd)

  if  @@error != 0 break

  fetch myCursor into @user, @table, @column
end

close myCursor
deallocate cursor myCursor

if @first = 0
begin
  print "No text/image data found in this database."
end
else
begin
  set nocount off
  select convert(varchar(70),rtrim(user_name) + "." + rtrim(table_name) + "." + rtrim(column_name)),max_size
    from #output
    where max_size > 0
    order by max_size
  if @@rowcount = 0
  begin
    print "Maximum size is 0 bytes"
  end
end

drop table #output

return  0
go