Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > SQL Language
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 14th, 2003, 10:18 PM
Registered User
 
Join Date: Oct 2003
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
 
Old October 15th, 2003, 06:00 AM
Friend of Wrox
 
Join Date: Jun 2003
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





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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.