Solved

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

Posted on 2008-06-17
4
4,229 Views
Last Modified: 2010-04-21
     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

0
Comment
Question by:jeet_78
  • 2
  • 2
4 Comments
 
LVL 19

Expert Comment

by:grant300
Comment Utility
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
 

Author Comment

by:jeet_78
Comment Utility
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
 
LVL 19

Accepted Solution

by:
grant300 earned 500 total points
Comment Utility
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
 

Author Closing Comment

by:jeet_78
Comment Utility
Thank you
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

In this article, you will read about the trends across the human resources departments for the upcoming year. Some of them include improving employee experience, adopting new technologies, using HR software to its full extent, and integrating artifi…
Scam emails are a huge burden for many businesses. Spotting one is not always easy. Follow our tips to identify if an email you receive is a scam.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now