Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sybase, execute string as sql query

In Sybase SQL, I would like to execute a String containing SQL.

I would expect something like this to work

declare @exec_str char(100)
select @exec_str = "select 1"
execute @exec_str
go

from the documentation of the exec command

execute | exec

is used to execute a stored procedure or an extended stored

procedure (ESP). This keyword is necessary if there are multiple statements in the batch.

execute is also used to execute a string containing Transact-SQL.

However my above example gives an error. Am I doing something wrong?

like image 792
Mike Avatar asked Sep 22 '10 13:09

Mike


People also ask

What is EXEC SQL command?

The EXEC command is used to execute a stored procedure, or a SQL string passed to it. You can also use full command EXECUTE which is the same as EXEC.

Can we use EXEC in SQL function?

Exec is not allowed in functions, but it is allowed in stored procedures, so you can just rewrite the function as a stored procedure which retuns a resultset.

What is EXEC in Sybase?

You can either use the into clause of the exec statement or declare a cursor for the procedure. If you use the into clause, the stored procedure must not return more than one row of data, unless the host variables that you specify are arrays. The value param _ value can be a host variable or literal value.


1 Answers

You need bracketing:

execute ( @exec_str )
like image 159
martin clayton Avatar answered Oct 15 '22 14:10

martin clayton