Solved

Dynamic SQL in stored procs

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
SQL Query 2 318
Crystal Reports VB6 11 1,020
Sybase IQ how to log personalized Users? 1 192
restore master and user database in sybase ase 2 18
Short answer to this question: there is no effective WiFi manager in iOS devices as seen in Windows WiFi or Macbook OSx WiFi management, but this article will try and provide some amicable solutions to better suite your needs.
What is Backup? Backup software creates one or more copies of the data on your digital devices in case your original data is lost or damaged. Different backup solutions protect different kinds of data and different combinations of devices. For e…
This video discusses moving either the default database or any database to a new volume.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

760 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

23 Experts available now in Live!

Get 1:1 Help Now