Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

ASP.NET Forum


You are currently viewing our ASP.NET Forum as a guest. Please register to participate.
Login



Reply
correct use of a function
Old 08-18-2005, 05:20 AM correct use of a function
numbenator's Avatar
Webmaster Talker

Posts: 516
Location: London
Trades: 0
Hi There,
Im doing a lot of access to a db and retrieval of all informaiton in numerous tables. Now i know that should be using stored procedures bu i am trying to get my head into functions and thus have the below question.

Ive created a function shown below called getALL. I want it to return a recordset so as I can then process the information retrieved.
In my sub, how do i use this function and continue processing after the retrieval of the recordset. I start erroring when i attempt to code use and though sumone could look over.

cheers

my function which is defined in a SSI
Conn (the connection) is defined in another SSI.

Code:
function getAll(tableName) 
    
                Set RS = Server.CreateObject("ADODB.Recordset") 
	strSQL = "SELECT * FROM "&dbName
	RS.Open strSQL, Conn 
                getAll=RS
	
end function
Code:
sub getSelectTable(tableName)  

                tabN=tableName
	function call here i assume
	RS.MoveFirst 

                'process recordset here
__________________

Please login or register to view this content. Registration is FREE
numbenator is offline
Reply With Quote
View Public Profile Visit numbenator's homepage!
 
 
Register now for full access!
Old 08-18-2005, 06:04 PM
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,517
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Pass the contents of the rs in an array, it you try to pass the rs it will error due to the variable scope. RS is a local to the function therefore is only valid inside the function.
This way you can ensure that RS is closed and destroyed after each use.

Also you should not use * with ADO to select all columns, use a fieldlist even if all columns are requiredd. The use of * makes the DB call less efficient, as the SQL server has to parse the table twice, once to retrieve the column names and a second time to retrieve the data.

Code:
function getAll(tableName) 
    
                Set RS = Server.CreateObject("ADODB.Recordset") 
	strSQL = "SELECT fieldlist FROM " & tableName
	RS.Open strSQL, Conn 
                getAll=RS.GetRows()
                RS.close
                set rs = nothing
	
end function
DevGuru GetRows() reference
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 08-19-2005, 05:26 AM
numbenator's Avatar
Webmaster Talker

Posts: 516
Location: London
Trades: 0
HI thanks for that although I am confused then what I do with the array. Do i process like a recordSet or transfer array to the record set?

Steve
__________________

Please login or register to view this content. Registration is FREE
numbenator is offline
Reply With Quote
View Public Profile Visit numbenator's homepage!
 
Old 08-19-2005, 05:58 AM
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,517
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
The data is returned in a two dimension array the first dimension is the columns, the second the rows. So it's simply processed as any other array.
number of columns = ubound(data_array,1)
number of rows (recordcount) = ubound(data_array,2)

To recover the last record (for instance) it would be
Code:
for i = 0 to ubound(data_array,1)
response.write data_array(i,ubound(data_array,2))
next
the first record would be
Code:
for i = 0 to ubound(data_array,1)
response.write data_array(i,lbound(data_array,2))
next
This is also where using an explicit fieldlist is important because the columns will always be returned in the same order (the fieldlist) even if the table gets restructured.

It can be a little daunting at first and you'll probably code it the wrong way around a few times, but it is worth persevering and learning as the speed increase in queries can be noticeable on a busy database.
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 08-23-2005, 05:59 AM
numbenator's Avatar
Webmaster Talker

Posts: 516
Location: London
Trades: 0
wicked. thanks very much
__________________

Please login or register to view this content. Registration is FREE
numbenator is offline
Reply With Quote
View Public Profile Visit numbenator's homepage!
 
Reply     « Reply to correct use of a function
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.41516 seconds with 12 queries