Solved

Dynamic SQL in stored procs

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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Why do some people recommend buying business VoIP from an ISP? What are the benefits to my company? What are the costs?
After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

617 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