Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SQL query and csv output

Status
Not open for further replies.

jositox

Technical User
Jul 13, 2009
1
I am making a query and I need to output is to a csv.
I need only to retrieve the products that dont have an image or cataloge associated so I am making a couple of loops, but I am not sure if I am doing it well.
I got a time out so I added a Server.ScriptTimeout=200 but I am sure the time out is just because an error in the code.
Can you help me please?
I am don't know too much ASP.
I am sure there is a better method to do this.

Thank you very much in advance.
Just in case you want to take a look I have this code:
Code:
<%@ Language=VBScript%>
<!--#include file="lib/adovbs.inc" -->
<!--#include file="Connections/connDBseta.asp" -->
<!--#include file="response_log.asp" -->
<%
Server.ScriptTimeout=200
response.write(Server.ScriptTimeout)
dim cn, rsProducts, fdsTotal, bAlternate

	lCategoryID = CLng (Request.QueryString ("sCategoryID"))
	lIndustryID = CLng (Request.QueryString ("sIndustryID"))
	lPageSize = 100
	lStartPage = CLng (Request.QueryString ("sStartPage"))
	lSubCategoryID = CLng (Request.QueryString ("sSubCategoryID"))
	lfuelMethodID = CLng (Request.QueryString ("sfuelMethodID"))
	sAccessories = cleanString (Request.QueryString("sAccessories")& " ")
	sCode = cleanString (Request.QueryString ("sCode"))
	sExact = cleanString (Request.QueryString("sExact"))
	sKeyword = cleanString (Request.QueryString ("sKeyword"))
	sMethod = cleanString (Request.QueryString ("sMethod"))
	sMethodID = cleanString (Request.QueryString ("sMethodID"))
	sFuelID = cleanString (Request.QueryString ("sFuelID"))
	
	sWhere = "WHERE (ProductGroup<>'FGSD') "
	If lCategoryID > 0 Then
		sWhere = sWhere & "AND "
		If LCase (sAccessories) = "true" Then
			sWhere = sWhere & "Category.CategoryID = " & lCategoryID & " "
		Else
			sWhere = sWhere & "(Category.CategoryID = " & lCategoryID & " AND Part.SubCategoryID > 0) "
		End If
	End If
		
	If lSubCategoryID > 0 Then        'i.e. from category browse
		sWhere = sWhere & "AND "
		sWhere = sWhere & "Part.SubCategoryID = " & lSubCategoryID & " "
	End If
	
	If sCode <> "" Then
		sWhere = sWhere & "AND "
		If LCase (sAccessories) = "true" Then
			sWhere = sWhere & "PartNumber LIKE '" & sCode & "%' "
		Else
			sWhere = sWhere & "(PartNumber LIKE '" & sCode & "%' AND Part.SubCategoryID > 0) "
		End if
	End If
	
	If sKeyword <> "" Then
		sWhere = sWhere & "AND "
		If LCase (sAccessories) = "true" Then
				sWhere = sWhere & "keywords LIKE '%" & sKeyword & "%' "
			Else
				sWhere = sWhere & "(keywords LIKE '%" & sKeyword & "%' AND Part.SubCategoryID > 0) "
		End if
	End If
	
	If sMethod <> "" Then
		sWhere = sWhere & "AND "
		If LCase (sExact) = "true" then 'All methods in this field are terminated by a , - even the last one
				If LCase (sAccessories) = "true" Then
					sWhere = sWhere & "(Methods LIKE '%" & sMethod & ",%' OR Methods LIKE '%" & sMethod & ";%') "
				Else
					sWhere = sWhere & "(Methods LIKE '%" & sMethod & ",%' OR Methods LIKE '%" & sMethod & ";%') AND (Part.SubCategoryID > 0) "
				End if
			else
				If LCase (sAccessories) = "true" Then
					sWhere = sWhere & "Methods LIKE '%" & sMethod & "%' "
				Else
					sWhere = sWhere & "(Methods LIKE '%" & sMethod & "%' AND Part.SubCategoryID > 0) "
				End if
		End if
	End If
	
	' it searching fuel
	If sFuelID <> "" Then
		If sFuelID > 0 Then
			sWhere = "INNER JOIN (select max(PartToFuelSpecID) PartToFuelSpecID,PartID,FuelSpecID,max(MethodID) MethodID from PartToFuelSpec group by PartID,FuelSpecID) AS PartToFuelSpec ON PartToFuelSpec.PartID = Part.PartID  " & sWhere 
			sWhere = sWhere & "AND "
			
			If LCase (sAccessories) = "true" Then
				sWhere = sWhere & "PartToFuelSpec.FuelSpecID = " & sFuelID & " "
			Else
				sWhere = sWhere & "(PartToFuelSpec.FuelSpecID = " & sFuelID & " AND Part.SubCategoryID > 0) "
			End If
		
		End If
	End if 
	
	If lIndustryID > 0 Then 'from Industry browse
		sWhere = sWhere & "AND Industry.IndustryID = " & lIndustryID & " "
	End If
	
	'FROM Clause
	If lIndustryID > 0 Then
		sFrom = "FROM ((Industry RIGHT OUTER JOIN PartIndustryApplication ON Industry.IndustryID = PartIndustryApplication.IndustryID) RIGHT OUTER JOIN Part ON PartIndustryApplication.PartID = Part.PartID) LEFT OUTER JOIN (Category RIGHT OUTER JOIN SubCategory ON Category.CategoryID = SubCategory.CategoryID) ON Part.SubCategoryID = SubCategory.SubCategoryID "
	Else
		sFrom = "FROM Part LEFT OUTER JOIN (Category RIGHT OUTER JOIN SubCategory ON Category.CategoryID = SubCategory.CategoryID) ON Part.SubCategoryID = SubCategory.SubCategoryID "
	End If
	
	' this is the code for the results if browsing for fuel 
	If lfuelMethodID > 0 Then
			sFrom = "FROM Part JOIN (select b.PartID from PartToFuelSpec As a join PartToFuelSpec As b on a.MethodID=b.MethodID and a.FuelSpecID=b.FuelSpecID where  a.PartToFuelSpecID = '" & lfuelMethodID & "') PartToFuelSpec on Part.PartID = PartToFuelSpec.PartID "
			sWhere = ""
	End If
	
	'ORDER BY Clause
	sOrder = "ORDER BY MainPartYN DESC, PartNumber;"

	set cn=Server.CreateObject("ADODB.Connection")
  	cn.Open g_CONNECTION
  
'Response.Write sFrom
'Response.Write sWhere
'Response.End
  	Set rsProducts = Server.CreateObject("ADODB.Recordset")
  	rsProducts.open  "SELECT * " & sFrom & sWhere & sOrder,cn,,,adCmdText

	Set fdsTotal = Server.CreateObject("ADODB.Recordset")
  	fdsTotal.Open "SELECT COUNT(Part.PartID) " & sFrom & sWhere & ";",cn,,,adCmdText
	lTotal = fdsTotal.fields.item(0)
								If lPageSize <= 0 Then
										lPageSize = 100
									End If
									lPages = Int (lTotal / lPageSize)
									If lPages < (lTotal / lPageSize) Then
										lPages = lPages + 1
									End If
									lStart = lStartPage * lPageSize
									If lStart > lTotal Or lStart < 0 Then
										lStart = 0
										lStartPage = 0
									End If
									
									lCount = 0
									bAlternate = false
									rsProducts.MoveFirst
									rsProducts.Move lStart 
									Do While Not rsProducts.EOF And lCount < lPageSize
PartNumber 		= "" & rsProducts.Fields ("PartNumber").Value
ProductImage 	= GetProductImage( PartNumber, "_" )																
If Len(ProductImage)=0 Then ProductImage = PartNumber & ".jpg"
strPicname 		= Server.MapPath("images/products/" & ProductImage)
ProductPDF 		= GetPDF( PartNumber, "_" )		
If IsFileExists(strPicName)=false or Len(ProductPDF)=0 then
    csvText = rsProducts.Fields ("PartNumber") & "," & rsProducts.Fields ("Prod_Title")
End If

rsProducts.MoveNext

Loop

    cn.close: set cn = nothing 
 
    set fso = CreateObject("Scripting.FileSystemObject") 
    set fs = fso.CreateTextFile(Server.MapPath("csv.csv"), true) 
    fs.writeline(csvText) 
    fs.close: set fs = nothing 
    set fso = nothing 
 
    response.write "Right-click to download: " & _ 
        "<a href=csv.csv>csv.csv</a>" 
%>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top