Query - Error assigning value to variable in VB - sql

I have the following code to query in VB6:
SQL = " if object_id('tempdb..#MovSeq','U') is not null drop table #MovSeq;" & _
" declare #Data_Inicio datetime, #Data_Fim datetime; set dateformat dmy; set #Data_Inicio = DataInicio; set #Data_Fim = DataFinal; set #Data_Fim = DateAdd(day, +1, #Data_Fim); " & _
" with Mov as ( SELECT EI.Cod_Empresa, EI.Cod_Estoque, EI.Cod_Produto, 'E' as Tipo_Mov, E.Dta_Entrada as Data_Mov, EI.id_Doc as NF, EI.Qtde, EI.V_Unitario, EI.V_Total " & _
" from Entrada_Itens as EI inner join Entrada as E on EI.Cod_Empresa=E.Cod_Empresa and EI.id_Doc=E.id_Doc " & _
" where E.Dta_Entrada >= #Data_Inicio and EI.Cod_Empresa='" & Sys.Empresa & "' and EI.Cod_Estoque='" & dcEstoque.BoundText & "' and EI.Cod_Produto='" & dtProdutos.BoundText & "' " & _
" Union " & _
" SELECT SI.Cod_Empresa, SI.Cod_Estoque, SI.Cod_Produto, 'S', S.Dta_Entrada , SI.id_Doc, -SI.Qtde, SI.V_Unitario, SI.V_Total " & _
" from Saida_Itens as SI inner join Saida as S on SI.Cod_Empresa=S.Cod_Empresa and SI.id_Doc=S.id_Doc " & _
" where S.Dta_Entrada >= #Data_Inicio and SI.Cod_Empresa='" & Sys.Empresa & "' and SI.Cod_Estoque='" & dcEstoque.BoundText & "' and SI.Cod_Produto='" & dtProdutos.BoundText & "' " & _
" Union " & _
" SELECT Cod_Empresa, Cod_Estoque, Cod_Produto, 'A', #Data_Inicio, null, null, null, null " & _
" From Estoque " & _
" where Cod_Empresa='" & Sys.Empresa & "' and Cod_Estoque='" & dcEstoque.BoundText & "' and Cod_Produto='" & dtProdutos.BoundText & "') " & _
" SELECT *, Seq= row_number() over (partition by Cod_Empresa, Cod_Estoque, Cod_Produto order by Data_Mov desc, Tipo_Mov desc) into #MovSeq from Mov; " & _
" create unique clustered index IndMovSeq on #MovSeq (Cod_Empresa, Cod_Estoque, Cod_Produto, Seq); " & _
" SELECT M.Cod_Empresa, M.Cod_Estoque, M.Cod_Produto, P.Descricao," & _
" Estoque= case when M.Seq=1 then E.Qtde_Estoque else (E.Qtde_Estoque - (SELECT sum(Mi.Qtde) from #MovSeq as Mi " & _
" Where Mi.Cod_Empresa = M.Cod_Empresa And Mi.Cod_Estoque = M.Cod_Estoque And Mi.Cod_Produto = M.Cod_Produto and Mi.Seq < M.Seq)) end " & _
" from #MovSeq as M inner join " & _
" Estoque as E on M.Cod_Empresa=E.Cod_Empresa and M.Cod_Estoque=E.Cod_Estoque and M.Cod_Produto=E.Cod_Produto inner join " & _
" Produtos as P on M.Cod_Empresa=P.Cod_Empresa and M.Cod_Estoque=P.Cod_Estoque and M.Cod_Produto=P.Cod_Produto " & _
" where Data_Mov < #Data_Fim " & _
" order by M.Cod_Empresa, M.Cod_Estoque, M.Cod_Produto, Seq desc; " & _
" drop table #MovSeq;"
the error appears 'invalid columm name' when run showing DataInicio as a reason
I know the error is in the assignment of the variable. but how to solve.
Thanks for the help...

Your type of code is very prone to SQL Injection attacks...
Are you escaping the variable values correctly? A simple quote (') in one of the variables can change the SQL string completely...
You should consider using parameters to pass values instead of building SQL commands using string concatenation.

Related

Encountered error Syntax error in JOIN operation

OBJECTIVE: trying to extract data using union all with join.
CODE:
MYSQL = "SELECT trans_id, item_id, SUM(OrigQty) AS NewOrigQty, SUM(TempQty) AS NewTempQty, " & _
"itm_name, itm_category, itm_details, itm_um, itm_cost " & _
"FROM (" & _
"SELECT T1.trans_id, T1.item_id, T1.itm_qty AS OrigQty, 0 AS TempQty, " & _
"T3.itm_name, T3.itm_category, T3.itm_details, T3.itm_um, T3.itm_cost FROM INVENTORY AS T1 " & _
"LEFT JOIN ITEM AS T3 ON T1.item_id = T3.itm_id " & _
"WHERE T1.trans_id = '" & NewSearchList.lvSearchWindow.SelectedItem.Text & "' " & _
"AND T1.itm_movement = 'TRANSFER IN' AND T1.inv_temporig_status = 'ORIGINAL' " & _
"UNION ALL " & _
"SELECT T2.trans_id, T2.item_id, 0 AS OrigQty, T2.itm_qty AS TempQty, " & _
"t4.itm_name, t4.itm_category, t4.itm_details, t4.itm_um, t4.itm_cost FROM INVENTORY AS T2 " & _
"LEFT JOIN ITEM AS t4 ON T1.item_id = T4.itm_id " & _
"WHERE T2.trans_id = '" & NewSearchList.lvSearchWindow.SelectedItem.Text & "' " & _
"AND T2.itm_movement = 'TRANSFER IN' AND T2.inv_temporig_status = 'TEMPORARY' " & _
") x " & _
"GROUP BY trans_id, item_id, itm_name, itm_category, itm_details, itm_um, itm_cost"
Set rsInventory = dbInventory.Execute(MYSQL)
ERROR: Syntax error in JOIN operation

Cannot interpret a BASIC file querying SQL

So, I am trying to figure out where the TOTAL_CHG is coming from. Below is a snippet of where it is first used (not defined at all before).
...
sStrBalance = " (TOTAL_CHG - (ISNULL((SELECT SUM(isnull(t.amount, 0)) FROM transactions t " & _
" WHERE cp.contpolid = t.contpolid AND t.tran_date <= '" & GRepdate & "'), 0) + " & _
" ISNULL((SELECT SUM(isnull(dirpayamt, 0)) FROM bkrtrans bkr " & _
" WHERE cp.contpolid = bkr.contpolid), 0))) "
...
And then the next block of code that references it again is as follows:
...
strSQL = strSQL & vbCrLf & " SELECT cp.Contpolid, cp.CTYPE_ID, cp.C#, cp.REV, " & _
vbCrLf & "CASE WHEN cp.Div_Code = 'GM' THEN 'SLP' ELSE 'SCA' END, " & _
vbCrLf & "cp.Effective_ as EFFDATE, ADJUSTMENT, cp.terminatio, cp.TOTAL_CHG, " & _
vbCrLf & " ISNULL((SELECT SUM(isnull(t.amount, 0)) FROM transactions t " & _
vbCrLf & " WHERE cp.contpolid = t.contpolid AND t.tran_date <= '" & GRepdate & "'), 0) AS Payments, " & _
vbCrLf & " ISNULL((SELECT SUM(isnull(dirpayamt, 0)) FROM bkrtrans bkr " & _
vbCrLf & " WHERE cp.contpolid = bkr.contpolid), 0) AS BrkPayment, " & _
sStrBalance & " AS Balance, cast(0 as numeric(12,2)) as SixtyDays, cast(0 as numeric(12,2)) as NinetyDays , " & _
vbCrLf & " cast(0 as numeric(12,2)) as OverNinetyDays , CASE WHEN Register_D IS NULL THEN 'N' ELSE CASE WHEN Register_D < '" & GRepdate & "' THEN 'Y' ELSE 'N' END END "
strSQL = strSQL & vbCrLf & " FROM CONTRACTS_POLICIES cp (NOLOCK) /*JOIN Salesmen s (NOLOCK) ON s.salesmenid = cp.sales1*/ " & _
vbCrLf & " Where cp.CTYPE_ID = 1 And cp.Cancel_dat Is Null and CP.Sales1 is Not NULL " & _
vbCrLf & " AND ((CP.Effective_ <= '" & GRepdate & "' AND CP.Rev = 0) OR (CP.Adjustment <= '" & GRepdate & "' AND CP.Rev > 0)) " & _
vbCrLf & " AND NOT((" & sStrBalance & " > 0 AND cp.Effective_ <= '01/01/1996') OR (" & sStrBalance & "< 0 AND cp.Effective_ <= '01/01/1998')) " & _
vbCrLf & " And " & sStrBalance & " <> 0 "
...
What table is it even coming from? It seems like it is defined in the first block of code but isn't really a value yet.

SQL ExecuteQuery 3061 error message

I've got this:
sql = "INSERT INTO instroom ( " & _
"team_id, " & _
"proces_id, " & _
"datum, " & _
"aantal_instroom, " & _
"ctime, " & _
"cuser, " & _
"mtime, " & _
"muser " & _
") "
sql = sql & "SELECT " & _
"team_id, " & _
"proces_id, " & _
"datum, " & _
"SUM(aantal_instroom), " & _
"#" & Format(MTime, "yyyy-mm-dd hh:mm:ss") & "#, " & _
"" & mod_global.RealUserID & ", " & _
"#" & Format(MTime, "yyyy-mm-dd hh:mm:ss") & "#, " & _
"" & mod_global.RealUserID & " " & _
"FROM tmp_import_instroom " & _
"WHERE userid = '" & EscapeString(LCase(mod_global.RealUser)) & "' " & _
"AND team_id <> 0 " & _
"AND proces_id <> 0 "
sql = sql & "GROUP BY team_id, proces_id, datum " & _
"HAVING SUM(aantal_cases) > 0 "
When it goes through:
-- Execute Query
Private Function executeSQL(ByVal sql As String, Optional ByVal autoCommit As Boolean = False) As Boolean
On Error GoTo executeSQLError
executeSQL = False
If mod_global.DevStart Then QueryNum = QueryNum + 1
If mod_global.DevStart Then Call saveQueryToFile(sql)
' Check if database is open
If Not testConn Then
Call openDB
End If
If startTrans Then
db.Execute sql
executeSQL = True
If autoCommit Then
executeSQL = commitDB
End If
End If
DoEvents
Exit Function
executeSQLError:
Debug.Print ("executeSQL - " & Err.Number & " : " & Err.Description)
Call writeToLog("executeSQL - " & Err.Number & " : " & Err.Description)
End Function
I get the error message
"Runtime Error 3061: Too few parameters. Expected 1.".
What am I missing? I did debug.print and still can't find something wrong.
Here are my column names from tmp_import_instroom:
results of debug.print
insert into instroom (
team_id
, proces_id
, datum
, aantal_instroom
, ctime
, cuser
, mtime
, muser
)
select
team_id
, proces_id
, datum
, SUM(aantal_instroom)
, #2017-02-23 20:22:33#
, 310
, #2017-02-23 20:22:33#
, 310
from tmp_import_instroom
where userid = 'xg30222'
and team_id <> 0
and proces_id <> 0
group by team_id
, proces_id
, datum
having SUM(aantal_cases) > 0
Unless it's not showing, there's no ctime, cuser, mtime or muser in your table. Therefore, you need to alias your calculated fields.
sql = sql & "SELECT " & _
"team_id, " & _
"proces_id, " & _
"datum, " & _
"SUM(aantal_instroom) as aantal_instroom, " & _
"#" & Format(MTime, "yyyy-mm-dd hh:mm:ss") & "# as ctime, " & _
"" & mod_global.RealUserID & " as cuser, " & _
"#" & Format(MTime, "yyyy-mm-dd hh:mm:ss") & "# as mtime, " & _
"" & mod_global.RealUserID & " as muser " & _
"FROM tmp_import_instroom " & _
"WHERE userid = '" & EscapeString(LCase(mod_global.RealUser)) & "' " & _
"AND team_id <> 0 " & _
"AND proces_id <> 0 "

WHERE clause makes LEFT JOIN work like an INNER JOIN

I am trying to left join three tables with a where clause. In the first example
the query results in an inner join. If I take out the where clause it results in a left join, but in includes records outside the desired date range.
I'm using Microsoft Access 2010 and Visual Basic 2010.
strQry = " SELECT tblUnits.UnitNumber, TenantName, SchedRent, SchedCAM, sum(AMOUNT) as SUMAMOUNT " _
& " FROM ((tblUnits LEFT JOIN tblTenants ON tblTenants.Unitptr = tblUnits.ID) " _
& " LEFT JOIN tblTrans ON (tblTenants.ID = tblTrans.id) ) " _
& " WHERE (tblTrans.PostDate BETWEEN #" & txtStartDate.Text & "# AND #" & txtEndDate.Text & "# ) " _
& " GROUP BY tblUnits.UnitNumber, TenantName, SchedRent, SchedCAM " _
& " ORDER BY tblUnits.UnitNumber "
In the second example it works perfectly, but only joins two tables
strQry = " SELECT U.UnitNumber, sum(AMOUNT) as sumamount " _
& " FROM tblUnits AS U " _
& " LEFT JOIN " _
& " ( " _
& " SELECT * " _
& " FROM tblTrans " _
& " WHERE (tblTrans.PostDate BETWEEN #" & txtStartDate.Text & "# AND #" & txtEndDate.Text & "# ) " _
& " ) as X " _
& " ON U.ID = X.ID " _
& " GROUP BY U.UnitNumber "
I can't get the syntax correct when I try to join the third table
Try WHERE tblTrans.PostDate IS NULL OR ...
As it stands, your LEFT JOIN includes tblUnits rows for which there is no matching tblTrans row. Then your WHERE clause eliminates these rows.
The following works
strQry = " SELECT U.UnitNumber, T.TenantName, T.SchedRent, T.SchedCAM, sum(AMOUNT) as SUMAMOUNT " _
& " FROM ((tblUnits AS U " _
& " LEFT JOIN tblTenants as T ON U.ID = T.UnitPtr) " _
& " LEFT JOIN " _
& " ( " _
& " SELECT * " _
& " FROM tblTrans " _
& " WHERE (tblTrans.PostDate BETWEEN #" & txtStartDate.Text & "# AND #" & txtEndDate.Text & "# ) " _
& " ) as X " _
& " ON U.ID = X.ID) " _
& " GROUP BY U.UnitNumber, TenantName, SchedRent, SchedCAM " _
& " ORDER BY U.UnitNumber "

SQL JOIN Master group to Result group

I am using Excel VBA and connecting to PGSQL using OleDB. I have 2 lines of SQL code that work fine on there own, but I now need to merge them.
The 1st query is the master group that finds the people I need for the 2nd query
Const sqlconnection = "Provider=oledb;"
Dim conn As New Connection
conn.ConnectionString = sqlconnection
conn.Open
Dim rs As Recordset
Sheets("Sheet1").Select
Range("A1").Select
Dim GRP As String
GRP = "SELECT h.master_id, p.surname, p.forename1, h.eventdate, h.code " _
& "FROM hist h INNER JOIN person p ON h.master_id=p.entity_id " _
& "AND code LIKE 'C10%' " _
& "ORDER BY h.master_id "
The 2nd query needs to show the results based on the 1st group of people it found.
Dim DATA As String
DATA = "SELECT latest.master_id, p.surname, p.forename1, " _
& "SUBSTRING(latestAP,1,10) eventdate, " _
& "SUBSTRING(latestAP,12,3) TX1, " _
& "SUBSTRING(latestAP,16,3) TX2 " _
& "FROM ( " _
& "SELECT master_id, " _
& "MAX(CAST(eventdate AS VARCHAR(10)) + '.' + RIGHT('00' + TEXT1,3)+ '.' + RIGHT('00' + TEXT2,3)) as latestAP " _
& "FROM ap " _
& "GROUP BY master_id) AS latest " _
& "LEFT JOIN person p ON latest.master_id = p.entity_id " _
& "ORDER BY master_id "
Set rs = conn.Execute(DATA)
With ActiveSheet.QueryTables.Add(Connection:=rs, Destination:=Range("A1"))
.Refresh
End With
rs.Close
Consider adding first query as an additional derived table to second query:
Dim DATA As String
DATA = "SELECT latest.master_id, p.surname, p.forename1, " _
& " SUBSTRING(latestAP,1,10) eventdate, " _
& " SUBSTRING(latestAP,12,3) TX1, " _
& " SUBSTRING(latestAP,16,3) TX2 " _
& "FROM ( " _
& " SELECT master_id, " _
& " MAX(CAST(eventdate AS VARCHAR(10)) + '.' + " _
& " RIGHT('00' + TEXT1,3) + '.' + " _
& " RIGHT('00' + TEXT2,3)) as latestAP " _
& " FROM ap " _
& " GROUP BY master_id) AS latest " _
& "INNER JOIN ( " _
& " SELECT h.master_id, p.surname, p.forename1, "_
& " h.eventdate, h.code " _
& " FROM hist h " _
& " INNER JOIN person p ON h.master_id=p.entity_id " _
& " AND code LIKE 'C10%') AS grp "
& "ON grp.master_id = latest.master_id"
& "LEFT JOIN person p ON latest.master_id = p.entity_id " _
& "ORDER BY master_id "