Question : MS SQL Output producing duplicate data

I'm having difficulty with an MS SQL 2005 query in a classic ASP VBScript environment.  I'm trying to create an XML data feed for a real estate company.  There are 3 tables that are relevant.  A properties table, a staffbios table (the agents) and a table called "Staff_Property_XRef" that is used to reference property ids and agent ids and bring them together to display all on one page.

Below is my code.  I'm not used to vbscript, so I'm sure I'm doing this the long and hard way.  I'm only outputting a fraction of the data that will go into the final feed, because I'm still in testing mode for now.

The problem I'm having is when there are two or more agents assigned to a property, the query returns duplicate property output, but with the different agent.  I want just one block of XML per property, with one or both agents in the agent's tag.  
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
SQL="SELECT * FROM Properties, StaffBios, Staff_Property_XRef WHERE Staff_Property_XRef.PropertyID=Properties.PropertyID AND Staff_Property_XRef.StaffBioID=StaffBios.StaffBioID AND  Properties.IsVisible='True'"

set rstemp=dbConn.execute(SQL)

'detects if data is empty
If  rstemp.eof then
   response.write "No records matched"
   connection.close
   set connection=nothing
   response.end

end if

' puts fieldnames into column headings
for each whatever in rstemp.fields
next

' grabs all the records

DO  UNTIL rstemp.eof

' put fields into variables
pid=rstemp("PropertyID")
fname=rstemp("FirstName")
lname=rstemp("LastName")
price=rstemp("price")
headline=rstemp("Headline")
city=rstemp("City")

tfile.WriteLine("")
tfile.WriteLine("" & cellstart & pid & "")
tfile.WriteLine("" & cellstart & fname & lname & "")
tfile.WriteLine("" & cellstart & price & "")
tfile.WriteLine("
" & cellstart & headline & "
") tfile.WriteLine("" & cellstart & city & "") tfile.WriteLine("
") rstemp.movenext LOOP tfile.WriteLine("") tfile.close set tfile=nothing set fs=nothing response.write "File Created!" ' Now close and dispose of resourcesrstemp.close set rstemp=nothing dbConn.close set dbConn=nothing %> Here's an example of the problem xml output: 180 Jane Doe 350000
1234 Main St.
Small Town
180 John Smith 350000
1234 Main St.
Small Town

Answer : MS SQL Output producing duplicate data

You have to put a second Loop inside the first, something like this:

Dim StrAgent
StrAgent = rstemp("TheIdOfYourAget"), or fname & lname if unique

DO  UNTIL rstemp.eof

' put fields into variables
pid=rstemp("PropertyID")
fname=rstemp("FirstName")
lname=rstemp("LastName")
price=rstemp("price")
headline=rstemp("Headline")
city=rstemp("City")

tfile.WriteLine("")
tfile.WriteLine("" & cellstart & pid & "")

Do UNTIL StrAgent = rstemp("TheIdOfYourAget"), or fname & lname if unique
  tfile.WriteLine("" & cellstart & fname & lname & "")
  StrAgent = rstemp("TheIdOfYourAget"), or fname & lname if unique
  rstemp.movenext
  If rstemp.eof Then exit loop
LOOP

tfile.WriteLine("" & cellstart & price & "")
tfile.WriteLine("
" & cellstart & headline & "
")
tfile.WriteLine("" & cellstart & city & "")
tfile.WriteLine("
")
LOOP

or

Dim StrAgent
StrAgent = rstemp("TheIdOfYourAget"), or fname & lname if unique

DO  UNTIL rstemp.eof

' put fields into variables
pid=rstemp("PropertyID")
fname=rstemp("FirstName")
lname=rstemp("LastName")
price=rstemp("price")
headline=rstemp("Headline")
city=rstemp("City")

tfile.WriteLine("")
tfile.WriteLine("" & cellstart & pid & "")
tfile.Write("" & cellstart)
Do UNTIL StrAgent = rstemp("TheIdOfYourAget"), or fname & lname if unique
  tfile.Write(fname & lname)
  tfile.Write("; ") 'Or the separated value you want
  StrAgent = rstemp("TheIdOfYourAget"), or fname & lname if unique
  rstemp.movenext
  If rstemp.eof Then exit loop
LOOP

tfile.WriteLine("
")
tfile.WriteLine("" & cellstart & price & "")
tfile.WriteLine("
" & cellstart & headline & "
")
tfile.WriteLine("" & cellstart & city & "")
tfile.WriteLine("
")
LOOP


or you could use a var inside loop to create
Name
Name
Name

I hope this will help.
Bye.
Random Solutions  
 
programming4us programming4us