Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

sybase question: how to create a table with dynamic tablename

588 views
Skip to first unread message

Thomas Vogt

unread,
Nov 19, 2002, 9:57:35 AM11/19/02
to
Hi

I need to create a table within a stored procedure, whose dynamic name
consists of a string and a parameter, i.e. something like

create procedure Foo (@param1 varchar(30), @param2 varchar(30)) as ...
...
select @tablename = "ThisString" + @param1 create table @tablename (bar
varchar)

as the identifier of a tablename cannot be a varchar, it somehow should be
converted. my sybase version is 11.9.2.3

regards
Thomas Vogt

Tim Vernum

unread,
Dec 1, 2002, 9:24:29 PM12/1/02
to
Thomas Vogt <tho...@bsdunix.ch> wrote in message news:<pan.2002.11.19.14....@bsdunix.ch>...

> I need to create a table within a stored procedure, whose dynamic name
> consists of a string and a parameter

This is not directly supported.
Tablenames cannot be variables.

You will need to use Dynamic SQL.

> my sybase version is 11.9.2.3

Dynamic SQL is not directly supported in 11.9, you will need to "fake it"
See: http://www.isug.com/Sybase_FAQ/ASE/section6.2.html#6.2.10

rvennik

unread,
Dec 6, 2002, 3:03:39 AM12/6/02
to

Upgrade to v12++ and you can use "execute immediate" like this:

create proc mk_tab (@tab varchar(32) )
as
begin
declare @cmd varchar(16000)
select @cmd = 'create table '+@tab+ ' ( col1 int, col2 int NULL )'
exec (@cmd)
end

PS varchar(16000) is NOT possible in v12.0


Ramon


Originally posted by Thomas Vogt

--
Posted via http://dbforums.com

Bernd Dulfer

unread,
Dec 6, 2002, 6:01:41 AM12/6/02
to

> I need to create a table within a stored procedure, whose dynamic name
> consists of a string and a parameter, i.e. something like

Since ASE 11.9.2 doesn't support dynamic sql you have two options:

Use the sp_remotesql procedure to execute sql on the server. See the
documentation in the online docs or the description at Rob Verschoors
web site (www.sypron.nl).

Or create the table with a default name, that would never be used and
use sp_rename to give it the real name.
You have to do this in two separate procedures.
I think Rob has an example for this as well.

0 new messages