dbTalk Databases Forums  

Running a query inside a variable

comp.databases.sybase comp.databases.sybase


Discuss Running a query inside a variable in the comp.databases.sybase forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Rafael Cotta
 
Posts: n/a

Default Running a query inside a variable - 10-10-2003 , 09:49 AM






Hi all,

I am completely newbie at stored procedures, and I do need to do
something that may be simple, but I don't know how to do.

I am building a query inside a varchar variable, like this:

DECLARE @i INTEGER
DECLARE @s VARCHAR(255)

SELECT @i = 0

WHILE @i < 3
BEGIN
IF @i = 0
SELECT @s = @s + 'SELECT ' + CONVERT(VARCHAR, @i)
ELSE
SELECT @s = @s + ' UNION SELECT ' + CONVERT(VARCHAR, @i)

SELECT @i = @i + 1
END

And after that @s will be like...

SELECT 1 UNION SELECT 2 UNION SELECT 3

.... and I want to run the select that is the value of @s.

This could be something like

EXECUTE_QUERY @s

Does the command with this functionality really exists?

Thanks in advance,

Rafael Cotta

Reply With Quote
  #2  
Old   
Carl Kayser
 
Posts: n/a

Default Re: Running a query inside a variable - 10-10-2003 , 10:08 AM






Yes - if you are on ASE 12.0 or later. There are a few corrections that
have to be made, e.g., "execute (@s)" instead of "EXECUTE_QUERY @s".

"Rafael Cotta" <rcotta (AT) ig (DOT) com.br> wrote

Quote:
Hi all,

I am completely newbie at stored procedures, and I do need to do
something that may be simple, but I don't know how to do.

I am building a query inside a varchar variable, like this:

DECLARE @i INTEGER
DECLARE @s VARCHAR(255)

SELECT @i = 0

WHILE @i < 3
BEGIN
IF @i = 0
SELECT @s = @s + 'SELECT ' + CONVERT(VARCHAR, @i)
ELSE
SELECT @s = @s + ' UNION SELECT ' + CONVERT(VARCHAR, @i)

SELECT @i = @i + 1
END

And after that @s will be like...

SELECT 1 UNION SELECT 2 UNION SELECT 3

... and I want to run the select that is the value of @s.

This could be something like

EXECUTE_QUERY @s

Does the command with this functionality really exists?

Thanks in advance,

Rafael Cotta




Reply With Quote
  #3  
Old   
Stephen Simpson
 
Posts: n/a

Default Re: Running a query inside a variable - 02-13-2004 , 05:14 PM



On 10 Oct 2003 07:49:18 -0700, rcotta (AT) ig (DOT) com.br (Rafael Cotta) wrote:

It depends on your version of ASE. 12.x introduced the exec(@s)
fuctionality.

Steve
Quote:
Hi all,

I am completely newbie at stored procedures, and I do need to do
something that may be simple, but I don't know how to do.

I am building a query inside a varchar variable, like this:

DECLARE @i INTEGER
DECLARE @s VARCHAR(255)

SELECT @i = 0

WHILE @i < 3
BEGIN
IF @i = 0
SELECT @s = @s + 'SELECT ' + CONVERT(VARCHAR, @i)
ELSE
SELECT @s = @s + ' UNION SELECT ' + CONVERT(VARCHAR, @i)

SELECT @i = @i + 1
END

And after that @s will be like...

SELECT 1 UNION SELECT 2 UNION SELECT 3

... and I want to run the select that is the value of @s.

This could be something like

EXECUTE_QUERY @s

Does the command with this functionality really exists?

Thanks in advance,

Rafael Cotta


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.