Search code examples
sqlsybase

SYBASE query with parameter in a store procedure


In a SQL Server stored procedure, I can write like this:

begin
    declare 
    @sSQL       varchar(max),
    @cod1       varchar(10)

    set @sSQL='update myTable set col_name=''ERR'' where col_id=''' + @cod1 + '''';
    exec (@sSQL);
end;

How can I do the same with a Sybase stored procedure? I need to execute a SQL command with variables.

When I try to write the same code in SYBASE, I got a syntax error on exec (@sSQL);


Solution

  • You could try something like this.

    SET @sSQL = 'UPDATE myTable SET col_name = ''ERR'' WHERE col_id = ''' + @cod1 + ''''
    
    EXECUTE IMMEDIATE @sSQL
    

    As exec() for this purpose not exist in sybase, you can use EXECUTE IMMEDIATE as a fast solution.