Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Database > SQL Language
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old October 14th, 2003, 10:18 PM
Registered User
 
Join Date: Oct 2003
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Passing variables into the SELECT of Stored Proc

I already posted this in the SQL Server section but now I realize it may be better suited for the SQL Section. So here it is again... Passing variables into the SELECT clause? I want to be able to dynamically select which field to pull from the table. Here is the code:


CREATE proc select_student_flight_hotel_prices
@passengerID int ,
@flightID int ,
@hotelID int ,
@fldPriceNumber varchar(15)
As

Select (f.fldAirOnlyPrice - f.fldDiscwHotel) As FlightPrice,
@fldPriceNumber As HotelPrice , f.fldAirportTax, hr.fldpartyExtravaganza
FROM tblFlights f, tblpassengers p, tblHotelRooms hr
WHERE f.fldFlightID=@FlightID AND f.fldflightID = p.fldflightID
 AND p.fldhotelRoomID = hr.fldhotelRoomID AND hr.fldHotelRoomID=@HotelID
AND p.fldPassengerID =@passengerID

GO



ASP code which executes fine...

rs.Open "Exec select_student_flight_hotel_prices " &passengerID&","&FlightID&","& HotelID&",'"&fldPriceNumber&"' ", conn

Note: @fldpriceNumber is a string which contains the text for a field name in a table
So I am really submitting…
rs.Open "Exec select_student_flight_hotel_prices " 1, 212, 57, ‘fldPrice3’, conn

You’ll notice that fldPriceNumber is a variable in the SELECT clause. The query executes fine all the correct values are returned except the rs(“HotelPrice”) is equal to the name of the field that I enter for @fldPriceNumber (so it returns ‘fldPrice3’) and not the actual value contained in the table. So the HotelPrice should return a numeric value but it is only returning the text value that I submitted it as in the parameter. Hope that make sense and hope you have a second to look at it.

Thanks,
Jon
Reply With Quote
  #2 (permalink)  
Old October 15th, 2003, 06:00 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Hudson, MA, USA.
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

You can do what you want by using dynamic SQL, that is, building a string containing the desired SQL SELECT statement, then EXECuting it. (Basically, what you did in ASP).

There are issues with dynamic SQL. See http://p2p.wrox.com/topic.asp?TOPIC_ID=4534 for some of them.

Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
A select and then an insert in a stored proc smacks SQL Server 2005 3 July 21st, 2007 07:31 PM
Calling an insert stored proc from a select stored dzitam SQL Language 10 April 2nd, 2007 12:39 PM
How to get value from stored proc busybee ASP.NET 1.0 and 1.1 Basics 4 April 2nd, 2006 01:06 AM
Passing variables into the SELECT of Stored Proc kerrj SQL Server 2000 0 October 14th, 2003 10:16 PM
Passing Parameters to Stored Proc mdillard Classic ASP Professional 6 June 10th, 2003 10:35 PM



All times are GMT -4. The time now is 11:20 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.