SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 SQL WHERE Clause
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

GaryEL
Starting Member

USA
5 Posts

Posted - 07/29/2013 :  07:28:03  Show Profile  Reply with Quote
The below code connects to a Teradata table and downloads the record set into my excel spreadsheet. No problem. The issue is: using a WHERE clause in the SQL statement….
I have a form with three variables on the form, (one is a date field and requires a range; (1) DateFrom to DateTo), (2) StatusX and (3) ErrorTypeX. I need to use any combination of these variables to retrieve the desired record set. i.e. variables 1 and 2, or 2 and 3, or 1 and 3 etc. or none of the variables which would return all records.

I’m stuck on how to set up the variables in the SQL statement…..???



Dim conn As ADODB.Connection
Set conn = New ADODB.Connection
Dim recset As ADODB.Recordset
Set recset = New ADODB.Recordset
Dim cmdSQLData As ADODB.Command
Set cmdSQLData = New ADODB.Command
Dim RowCnt, FieldCnt As Integer

conn.Open "DSN=OneView; Persist Security Info=True; User ID=fraudjedi; Password=Welcome3; Session Mode=ANSI;"
Set cmdSQLData.ActiveConnection = conn

Query = "SEL SRN_ACCT_NUM, QUEUE_NAME, ERROR_TYPE, SUB_ERROR_TYPE, DATE_WORKED, MONTH_WORKED, DATE_APPLICATION_RECEIVED, ASSOC_WORKED, ACCT_ID, STATUS, COMMENTS, REVIEWED_IND, REVIEWED_AGENT, LOAD_DT " & _
 "FROM UD402.JD_MCP_MASTER  WHERE " & _
 "DATE_WORKED >= DateworkedF & <= DateWorkedT ;"

    cmdSQLData.CommandText = Query
    cmdSQLData.CommandType = adCmdText
    cmdSQLData.CommandTimeout = 0
    Set recset = cmdSQLData.Execute()

   RowCnt = 5
   For FieldCnt = 0 To recset.Fields.Count - 1
      Cells(RowCnt, FieldCnt + 1).Value = _
      recset.Fields(FieldCnt).Name
      Rows(1).Font.Bold = True
   Next FieldCnt
   RowCnt = 6
   While Not recset.EOF
      For FieldCnt = 0 To recset.Fields.Count - 1
         Cells(RowCnt, FieldCnt + 1).Value = _
         recset.Fields(FieldCnt).Value
      Next FieldCnt
      recset.MoveNext
      RowCnt = RowCnt + 1
   Wend
 
conn.Close
Set conn = Nothing
Set recset = Nothing
Set cmdSQLData = Nothing

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 07/29/2013 :  07:35:20  Show Profile  Reply with Quote
Set a default value for each of the parameters when user doesnt pass a value and then make query like

...
Query = "SEL SRN_ACCT_NUM, QUEUE_NAME, ERROR_TYPE, SUB_ERROR_TYPE, DATE_WORKED, MONTH_WORKED, DATE_APPLICATION_RECEIVED, ASSOC_WORKED, ACCT_ID, STATUS, COMMENTS, REVIEWED_IND, REVIEWED_AGENT, LOAD_DT " & _
 "FROM UD402.JD_MCP_MASTER  WHERE " & _
 "(DATE_WORKED >= DateworkedF OR DateworkedF IS NULL)" & _
 "AND (DATE_WORKED <= DateWorkedT OR DateworkedT IS NULL)" & _
 "AND (STATUS  = StatusX OR StatusX IS NULL)" & _
 "AND (ERRORTYPE = ErrorTypeX OR ErrorTypeX IS NULL);"


I'm assuming column names above so make sure you use correct column names instead. Also make default values to be passed as NULL from application code

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

GaryEL
Starting Member

USA
5 Posts

Posted - 07/29/2013 :  07:49:10  Show Profile  Reply with Quote
Good morning! And thanks for the help... But i still have a problem:
Run-time error ‘-2147467259(80004005)
…Column DateworkedF not found in UD402.JD_MCP_MASTER

The field DateworkedF and DateworkedT are on the form but it appears the query thinks they should be in the table???

Go to Top of Page

James K
Flowing Fount of Yak Knowledge

3712 Posts

Posted - 07/29/2013 :  08:55:32  Show Profile  Reply with Quote
You can fix that problem by using the values from the text boxes on the form like shown below. HOWEVER, doing it this way is vulnerable to SQL injection attacks. So I would recommend creating a stored procedure and passing the values of DateWorkedF and DateWorkedT as parameters to that stored proc
Query = "SEL SRN_ACCT_NUM, QUEUE_NAME, ERROR_TYPE, SUB_ERROR_TYPE, DATE_WORKED, MONTH_WORKED, DATE_APPLICATION_RECEIVED, ASSOC_WORKED, ACCT_ID, STATUS, COMMENTS, REVIEWED_IND, REVIEWED_AGENT, LOAD_DT " & _
 "FROM UD402.JD_MCP_MASTER  WHERE " & _
 "(DATE_WORKED >= " & DateworkedF.Text & " OR " & DateworkedF.Text & " = '')" & _
 "AND (DATE_WORKED <= " & DateWorkedT.Text & " OR " DateworkedT.Text & " = '' )" & _
 "AND (STATUS  = StatusX OR StatusX IS NULL)" & _
 "AND (ERRORTYPE = ErrorTypeX OR ErrorTypeX IS NULL);"
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 07/30/2013 :  01:26:59  Show Profile  Reply with Quote
quote:
Originally posted by GaryEL

Good morning! And thanks for the help... But i still have a problem:
Run-time error ‘-2147467259(80004005)
…Column DateworkedF not found in UD402.JD_MCP_MASTER

The field DateworkedF and DateworkedT are on the form but it appears the query thinks they should be in the table???




Is DateworkedF a field in table? I think thats a parameter. If yes, it should be as per the last sugestion.
If there are lots of parameters you may be better off using dynamic sql as the usage of above method may cause bad plans

see below

http://sqlinthewild.co.za/index.php/2009/03/19/catch-all-queries/

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000