Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
GaryEL
Starting Member
5 Posts |
Posted - 2013-07-29 : 07:28:03
|
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.ConnectionSet conn = New ADODB.ConnectionDim recset As ADODB.RecordsetSet recset = New ADODB.RecordsetDim cmdSQLData As ADODB.CommandSet cmdSQLData = New ADODB.CommandDim RowCnt, FieldCnt As Integerconn.Open "DSN=OneView; Persist Security Info=True; User ID=fraudjedi; Password=Welcome3; Session Mode=ANSI;"Set cmdSQLData.ActiveConnection = connQuery = "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.CloseSet conn = NothingSet recset = NothingSet cmdSQLData = Nothing |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-07-29 : 07:35:20
|
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 MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
GaryEL
Starting Member
5 Posts |
Posted - 2013-07-29 : 07:49:10
|
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_MASTERThe field DateworkedF and DateworkedT are on the form but it appears the query thinks they should be in the table??? |
|
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2013-07-29 : 08:55:32
|
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 procQuery = "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);" |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-07-30 : 01:26:59
|
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_MASTERThe 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 belowhttp://sqlinthewild.co.za/index.php/2009/03/19/catch-all-queries/------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
|
|
|
|
|