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 ).


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

jeet_78Asked:
Who is Participating?
 
grant300Connect With a Mentor Commented:
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
0
 
grant300Commented:
You really have two questions here:
 - How do I pass a table name to a piece of dynamic SQL in a stored procedure?, and
 - How do I use a JOIN in a DELETE statement?

You are on the right track passing the name in but your code got lazy toward the end.  You cannot do "where @table_name.id = Inventory.id" because you have quoted @table_name as a literal.  You need something like:
"where " + @table_name + ".id = Inventory.id"

Now, as far as the syntax for doing a Join in a DELETE (or UPDATE for that matter goes, I use the ANSI SQL syntax because it is clearer and less ambiguous, particularly when you start doing outer joins.  The syntax without all the dynamic table name substitution is:

DELETE FROM Garments_data gd JOIN Inventory iv ON gd.id = iv.id

In some cases, it is better/easier/necessary to write the join in the opposite order which would confuse the DELETE and wind up deleting rows from Inventory.  The full syntax is...

DELETE Garments_data
  FROM Inventory iv
    JOIN Garments_data gd
      ON gd.id = iv.id

BTW, it is usually desirable to use table name aliases in SQL statements involving joins as it allows you an easier to read short-hand for the table names.  This is, of course, a necessity when you have to join a table to itself.

Regards,
Bill
0
 
jeet_78Author Commented:
While executing the following procedure I am getting the error&


create procedure SelectFromDataTables
@product_name varchar(50)
as
DECLARE @table_name varchar(50)
select @table_name = @product_name
declare @sqlstring varchar(2000)
select @sqlstring = "SELECT top 10* from " 
                               + @table_name
                               + "where" + @table_name + ".pt_sp_type_c=PTypeDesc.pt_type_c"
                               
execute (@sqlstring)



Execute the store procedure
exec SelectFromDataTables
@product_name = Position

Error
Server Message:  Number  102, Severity  15
(This error occurs when Adaptive Server detects a syntax error in a Transact-
SQL command or query.)
Server 'IDEV', Line 1:
Incorrect syntax near '='.
(1 row affected)
(return status = 0)

Please advice ......

0
 
jeet_78Author Commented:
Thank you
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.