Search code examples
sqlvariablesstored-proceduressybase

Sybase SQL - Stored Procedure


I’m trying to create a very simple stored procedure which would do the following:

 EXEC sp_help N’some.table’

 SELECT TOP 1000 * FROM some.table

So far I have the following

 CREATE PROCEDURE info_qry(@tbl)

 BEGIN

      DECLARE @tbl varchar(100)

      EXEC sp_help N’@tbl’

      SELECT TOP 1000 * FROM @tbl

END

I can’t figure it out how to pass the @tbl variable after the N’’ part so it is taken as a variable and not as a constant. How to do that?

Any help is appreciated.

Apologies about formatting I’m on mobile.


Solution

  • You can use dynamic SQL:

    declare @sql nvarchar(max);
    
    set @sql = 'sp_help ''' + @tbl + '''';
    
    exec(@sql);