Question : Table name as a input parameter to stored procedure in Sybase (use in the join condition)

     I have a question, can we pass table name as input parameter to store procedure.
The answer is yes we can.
      However, can we use this table name in the join condition for example&.
I have two tables Product_data and Inventory. I want to pass Product_data as input parameter to store procedure and in WHERE clause join the product_data and Inventory table on id
(product_data.id = inventory.id ).

Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
Create procedure DeleteFromDataTables @product_name varchar(50)
as
 
DECLARE @table_name varchar(50)
select @table_name = @product_name + "_data"
 
declare @sqlstring varchar(2000)
select @sqlstring =  "DELETE from " 
                               + @table_name 
                               + "where @table_name.id = Inventory.id" +  
 
execute (@sqlstring)
Open in New Window Select All

Answer : Table name as a input parameter to stored procedure in Sybase (use in the join condition)

Try...

exec SelectFromDataTables 'Position'

Also, you have some syntax issues...

Make certain you have spaces every where you need them or you will get garbage as well.  For instance, there is no space between the WHERE and @table_name.  You are also missing a space between 10 and * in SELECT top 10*...  Also, I am not certain you are using the top 10 functionality correctly.  Usually you use TOP with an order by clause which you don't have.  The right way to do this is with the "set rowcount #" but since you are doing it with dynamic SQL, you can probably get away with it as long as you don't need the same rows back each time.

What I would do is put a SELECT @sqlstring just before the execute statement and perhaps comment out the execute.  You will be able to see syntax problems much more easily that way.  Also, you can grab the text as output by the SELECT, paste it into a command window on your favorite tool, and try running it.

Regards,
Bill
Random Solutions  
 
programming4us programming4us