SQL query vbscript vs TSQL - Posted (1877 Views)
Senior Member
StephenD
Posts: 1044
1044
I'm having a brain snap on this basic query:

SELECT FORUM_ID, F_SUBJECT FROM FORUM_FORUM WHERE FORUM_ID IN (1,2,3) ORDER BY F_SUBJECT DESC

If I run it in TSQl I get 3 rows with Forum_Subject returned.
If I put the same query in an .asp page eg:

strSql = "SELECT FORUM_ID, F_SUBJECT FROM FORUM_FORUM WHERE FORUM_ID IN (" & clients & ") ORDER BY F_SUBJECT DESC "

Set rs2 = Server.CreateObject("ADODB.Recordset")
rs2.Open strSql, my_Conn, adOpenForwardOnly, adLockReadOnly, adCmdText

if not rs2.EOF then
customer = trim(rs2("F_SUBJECT"))
end if
rs2.Close
Set rs2 = Nothing

My query returns a single row or last record only. I know I am doing something blindingly obviously wrong to everyone but I've been looking at this a while now and can't see it. Any ideas?
 Sort direction, for dates DESC means newest first  
 Page size 
Posted
Forum Admin
HuwR
Posts: 20611
20611
how do you know it returns a single row? you haven't even attempted to find out, this bit of code
if not rs2.EOF then
customer = trim(rs2("F_SUBJECT"))
end if
rs2.Close
Set rs2 = Nothing

will only ever return a single value, you haven't attempted to itterate through the results smile
Posted
Senior Member
StephenD
Posts: 1044
1044
Ah... the penny drops ... getrows then array etc... Doh! :)
Posted
Senior Member
StephenD
Posts: 1044
1044
I'm still struggling with this:

Code:
strSql = "SELECT F_SUBJECT FROM FORUM_FORUM WHERE FORUM_ID IN (" & clients & ") ORDER BY F_SUBJECT DESC "
Set rs2 = Server.CreateObject("ADODB.Recordset")
rs2.Open strSql, my_Conn, adOpenForwardOnly, adLockReadOnly, adCmdText

if not rs2.EOF then
customer = rs2.GetRows(adGetRowsRest)
For iRowLoop = 0 to UBound(customer, 2)
For iColLoop = 0 to UBound(customer, 1)

customerArray = ("'" & customer(iColLoop, iRowLoop) & "',")
response.Write customerArray

customerArray = left(customerArray,len(customerArray)-1)
response.Write customerArray
Next
Next

end if
rs2.Close
Set rs2 = Nothing

response.Write customerArray


I'm trying to get a string of Forum Subject Names available outside this bit of code as a variable in the following format eg. 'Forum_name1','Forum_name2','forum_Name3'

At my first response.write I get 'Forum_Name1','Forum_Name2','Forum_name3',
At my 2nd response.write I get 'Forum_Name1''Forum_Name2''Forum_name3'

At my 3rd response.write I get only 'Forum_Name1' ..single value

Which brings me back to where I started...
Where is my error please.
Posted
Forum Admin
HuwR
Posts: 20611
20611
you are continually overwriting customerArray rather than appending to it, try something like this
Code:

For iRowLoop = 0 to UBound(customer, 2)
For iColLoop = 0 to UBound(customer, 1)

customerArray = customerArray & ("'" & customer(iColLoop, iRowLoop) & "',")
response.Write customerArray

Next
Next
customerArray = left(customerArray,len(customerArray)-1)
Posted
Senior Member
StephenD
Posts: 1044
1044
Awesome thanks!
 
You Must enter a message