Solved

Dynamic SQL in stored procs

Posted on 2001-06-14
2
797 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Citrix policies are the most efficient method to configure and tune XenDesktop environments, allowing organizations to control connection, security and bandwidth settings based on various combinations of users, devices or connection types.  Citrix …
What does UTC stand for?  “Coordinated Universal Time” – Think of this as the true time on Planet Earth that never changes with the exception of minor leap seconds here and there to account for the changes in the planet's rotation.   What does th…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
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.

948 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

21 Experts available now in Live!

Get 1:1 Help Now