Solved

Dynamic SQL in stored procs

Posted on 2001-06-14
2
800 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Check out this step-by-step guide for asking an anonymous question on Experts Exchange.
By reading this blog, MSPs will gain insight into how to improve communications with their clients as well as establish a more profitable business.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

752 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