Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Dynamic SQL in stored procs

Posted on 2001-06-14
2
Medium Priority
?
804 Views
Last Modified: 2008-02-26
Hi,


   I'm writing a stored proc to return a resultset on a Sybase DB. The complication is that one of
the parameters into the proc is a value which indicates which field on a table a second parameter belongs
to.

   E.G. Say MyTable has a number of fields, 3 of which are identifiers for a record, called Code1,
Code2, Code3. My proc will receive a first parameter with a value of 'LOG1' say. This is the value of
the key. A second parameterrepresents which Code type it belongs to, for example 'Code2'. This would
then mean a WHERE condition can be added to the SELECT statement saying

   WHERE MyTable.Code2 = @Parameter1

OR

   WHERE MyTable.Code2 = 'LOG1'

How would you implement something like this? Would you dynamically generate the SQL, or write a SELECT
for each possibility of 'Code' field and seperate them with an IF condition on @Parameter2. Or can you
embed an IF statement into a SELECT statement to say :

   IF @parameter2 = 'Code1'
       WHERE MyTable.Code1 = @parameter1
   ELSEIF @parameter2 = 'Code2'
       WHERE MyTable.Code2 = @parameter1
   ..
   ..

Help much appreciated...

Nik
0
Comment
Question by:NikWhitfield
2 Comments
 
LVL 10

Accepted Solution

by:
bret earned 0 total points
ID: 6192429
Hi Nik,  

You didn't specify *which* Sybase product you are writing for (ASA, ASE, ASIQ); I will assume ASE.

You can't imbed an if statement in the select as per your example.

Using dynamic sql is probably the easiest method if you are just trying to create a select statement,
but this is only available with ASE 12.0 and higher.  (from 11.5 up you can also hack the server to
be able to use sp_remotesql against the "local" server and accomplish much that same thing, but
the 12.0 EXECUTE solution is much cleaner).


declare @sqlstring varchar(255)
select @sqlstring = "select  * from mytable where "+@param2+ " = " + @param1
execute (@sqlstring).

See the "dynamicsql " section at http://www.sypron.nl for some additional pointers and examples.

-bret
0
 

Author Comment

by:NikWhitfield
ID: 6195497
Thanks Bret -

    I've actually redesigned things so dynamic SQL is not required. No doubt this will come in handly in the near future though...
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There can be many situations demanding the conversion of Outlook OST files to PST format and as such, there is no shortage of automated tools to perform this conversion. However, what makes Stellar OST to PST converter stand above the rest? Let us e…
The decision to migrate to the cloud is not a simple one—many factors, such a cost, ease of use, and ongoing maintenance come into play. The goal is always for cloud platforms to make storage and backups more seamless and effective. Here’s a look at…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

782 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