SQL Error in VBA - sql

I have the following code in SQL (2005) which calculates the avarage user logins on a systm:
with
user_total as
(
select COUNT(distinct ID) as counter
FROM [dbo].[LOG]
where [LOG].DESCRIPTION='Login success.'
AND
Convert(datetime,convert(char(10),[LOG].CREATED_ON,101)) BETWEEN '2009-01- 01' AND '2009-12-31'
),
USER_avg as
(
select counter/365 as Avarage_Daily_Logins
from user_total
)
select *
from USER_avg
Now the problem is when i put this in a VBA macro in excel to get the result in a spcific cell in strSQL = "QUERY SHOWN ABOVE HERE" argument i get the error in excel
incorrect sysntax near the keyword with
Its worth mentioning that i dont break the code in VBA in multiple lines..i have it all in one line.

Use ;WITH ...
WITH usage for a CTE must have ; after the previous statement. To ensure this is the cases, prefix with ;

Check all of your spacings, if there is an error further down the compiler may interpret it as a problem with the with statement.

Related

SQLite - problem with "near « ) » : syntax error"

I try here since i didn't find anything anywhere else.
I have a query which has been check by my colleagues and "teachers", but the error remains:
" near « ) » : syntax error".
The (last) query is the following one:
WITH valeur_par_ville AS
(
SELECT dep_code, com_code, AVG(valeur_fonciere) as valeur
FROM vente
JOIN bien ON bien.bien_id = vente.bien_id
JOIN commune ON commune.com_id = bien.com_id
WHERE dep_code IN (6,13,33,59,69)
GROUP BY dep_code, com_id
)
SELECT dep_code AS "Département", com_id AS "Commune", round(valeur,1) AS "Prix moyen"
FROM(
SELECT dep_code, com_nom, valeur_fonciere,
rank() OVER (PARTITION BY dep_code ORDER BY valeur_fonciere DESC) AS
rang
FROM valeur_par_ville) AS result
WHERE rang <= 3
I hope you can help me on this, because it becomes "black magic" to me!
Thanks.
I tried different ways to reach a result, but the same thing keeps going... I Checked all my data tables, updated SQLite (so PARTITION BY works on it now), reboot, checked the query little by little... No idea where it comes from.
Others colleagues (we are kind of students) managed to have a valid result with their own queries.
But for me:
" near « ) » : syntax error".
The error is coming from a different part of the code. I added CREATE TABLE statements to be able to run your code and it's failing differently.
CREATE TABLE vente(dep_code, com_code, valeur_fonciere, bien_id);
CREATE TABLE bien(bien_id, com_id);
CREATE TABLE commune(com_id);
-- your code here
The error:
Parse error near line 4: ambiguous column name: com_id
RE dep_code IN (6,13,33,59,69) GROUP BY dep_code, com_id ) SELECT dep_code AS
error here ---^
After prefixing it with a table name, there's another bunch of errors, e.g.
arse error near line 4: no such column: com_nom
(valeur,1) AS "Prix moyen" FROM( SELECT dep_code, com_nom, valeur_fonciere, ra
error here ---^
Parse error near line 4: no such column: valeur_fonciere
) AS "Prix moyen" FROM( SELECT dep_code, com_nom, valeur_fonciere, rank() OVER
error here ---^
etc.

SQL Sub-query parameters from Excel

I have SQL query with a sub-query where I want to assign sub query parameters from a cell in an Excel sheet, as this query cant be represented graphically Excel keep throwing error on this:
[Microsoft][ODBC SQL Server Driver] Invalid paramter number and
[Microsoft][ODBC SQL Server Driver] Invalid Descriptor Index
I have already tried solution as mentioned here to trick excel without success Using Parameters in SQL query with sub-query
;WITH dataset_bl as (
SELECT
--rank() over (partition by date_of_AC, ac_room order by into_theatre) as OrderNumber
--,datetime_of_AC as booking_datetime
date_of_AC
,AC_room
,specialty
,OAMRN
,substring(Subject_NAME,1,CHARINDEX(' ',Subject_NAME,1)) as 'surname'
,substring(Subject_NAME,CHARINDEX(' ',Subject_NAME,1)+2,500) as 'forename'
,convert(date,[dttm_of_birth],103) as DOB
,PRIN_SO as 'surg'
,'"'+ASST_SO_1+' '+ASST_SO_2+'"' as 'assistant_SO'
,substring(CONSULT_NAME,1,abs(CHARINDEX(',',CONSULT_NAME,1)-1)) as 'consult_surname'
,substring(CONSULT_NAME,CHARINDEX(' ',CONSULT_NAME,1)+1,500) as 'consult_first_name'
,'"'+actual_AC+'"' as AC_details
,'"'+PLANNED_PROD+'"' as booked_details
,'Carried Out' as 'TT_outcome'
, 'NULL' as 'unP_return_flag'
,OP_type
--,LOS
,case when CL_PRIORITY ='' then 'Not listed' else 'isted' end as islisted
,CLI_PRIORITY as listed_priority
,case when EM_PRIORITY like '<=%' then 'Submitted' else 'No Greensheet' end as isGreensheet
,em_priority as a, case when EM_PRIORITY like '<=%' and em_priority <> '<=72hrsCannot charge before prod'then LEFT(EM_PRIORITY, abs(charindex('-',EM_PRIORITY)-1))
when em_priority = '<=72hrsCannot diso before prod' then '<=72hrs' else 'NULL' end as em_priority
FROM sample.dbo.tb_Fnl_Sur_th4 WITH (NOLOCK)
**WHERE main_ident = 'A224' and convert(date,into_Start, 103) >= '2019-07-01' and convert(date,into_Start, 103) <= '2019-07-31'** --i am trying to get these parameters from excel cell value
)
SELECT * FROM dataset_bl
WHERE specialty like 'abc%'
or (consult_surname like '%abc%' and consult_first_name like '%def%' )
or surg in ('cde,fghi',
'jkl,'mnop,
'qrs,Tuv')
order by convert(date,date_of_procedure,103), operating_room--, into_theatre
Here's some suggestion.
First is to check the formatting of your date columns.
and build your parameter to be like
Cast(into_Start as smalldatetime) between ? and ?
I've been on that same issue and seem to find the solution for me. In fact the parameter [?] use in MSQUERY in Excel will normally work when you have a direct Query
Ex:
Select [YourTable].[Column]
From [YourTable]
Where [YourTable].[Column]= ?
The Excel parameter will prompt and trigger where the windows that ask you which cell or Data you want that parameter have to be filled with.
In the other hand if you use the a Subquery and you add a Where condition with the Parameter ? will be stuck with the parameter [Microsoft][ODBC SQL Server Driver] Invalid paramter number and [Microsoft][ODBC SQL Server Driver] Invalid Descriptor Index
If you download the ODBC Driver for SQL Server here -->
https://www.microsoft.com/fr-fr/download/details.aspx?id=56567
Install it and make sure that the ODBC use for your MSQUERY in Excel use that connection.
ODBC Data Source to use
After this I was able to use a MSQuery in Excel using Parameter like this one
Select [RenamedTable].*
From (Select [YourTable].[Column1],
[YourTable].[Column2],
[YourTable].[Column3]
From [YourTable]) as RenamedTable
Where [RenamedTable].[Column1] between ? and ?
Hope this will work for you as for me because I had similar problem with between date Parameter

MS Access query produces invalid procedure call

My query in access works fine if i only use the below query with a select statement. As soon as it becomes an append query it produces an "invalid procedure call" error.
I have narrowed down the offending columns as being "Publ" and "PublLong". Both are long text strings. If I remove these two columns the query updates without an error.
Here is a sample data point found in the [Bezeichung] Field:
publications.bank.com/publ-dl-ch/pdf/WhatsUp_20181113_en.pdf
I checked the table that it is being inserted to and the data types are the same nor did i see any other setting that would block the insertion.
How can i get it to work?
INSERT INTO tbl_MatomoRaw ( DownloadDate, IntExt, Publ, PublLong,
PublDate, [Language], Download_Visits, PublMonth )
SELECT
Date() AS DownloadDate,
Left([Bezeichnung],InStr([Bezeichnung],".")-1) AS IntExt,
Nz(Mid([Bezeichnung],InStrRev([Bezeichnung],"/")+1,InStr([Bezeichnung],"_")-
InStrRev([Bezeichnung],"/")-1),"") AS Publ,
Mid([Bezeichnung],InStrRev([Bezeichnung],"/")+1,InStrRev([Bezeichnung],"_")-
InStrRev([Bezeichnung],"/")-1) AS PublLong,
Mid([Bezeichnung],InStr([Bezeichnung],"_")+1,8) AS PublDate,
Mid([Bezeichnung],Len([Bezeichnung])-5,2) AS [Language],
xlsx_Output.[Eindeutige Downloads] AS Download_Visits,
Mid([Bezeichnung],InStr([Bezeichnung],"_")+1,6) AS PublMonth
FROM xlsx_Output
WHERE
(((Nz(Mid([Bezeichnung],InStrRev([Bezeichnung],"/")+1,InStr([Bezeichnung],"_")-
InStrRev([Bezeichnung],"/")-1),"")) Not Like "#Func!"));
#Func! indicates one of your functions is causing an error.
Your query uses multiple functions that run into trouble when your input doesn't meet that format, pre-filter instead of filtering on an error, since you can't filter on an error when appending:
INSERT INTO tbl_MatomoRaw ( DownloadDate, IntExt, Publ, PublLong,
PublDate, [Language], Download_Visits, PublMonth )
SELECT
Date() AS DownloadDate,
Left([Bezeichnung],InStr([Bezeichnung],".")-1) AS IntExt,
Nz(Mid([Bezeichnung],InStrRev([Bezeichnung],"/")+1,InStr([Bezeichnung],"_")-
InStrRev([Bezeichnung],"/")-1),"") AS Publ,
Mid([Bezeichnung],InStrRev([Bezeichnung],"/")+1,InStrRev([Bezeichnung],"_")-
InStrRev([Bezeichnung],"/")-1) AS PublLong,
Mid([Bezeichnung],InStr([Bezeichnung],"_")+1,8) AS PublDate,
Mid([Bezeichnung],Len([Bezeichnung])-5,2) AS [Language],
[Eindeutige Downloads] AS Download_Visits,
Mid([Bezeichnung],InStr([Bezeichnung],"_")+1,6) AS PublMonth
FROM (SELECT * FROM xlsx_Output WHERE Len(Bezeichnung) > 5 AND Bezeichnung LIKE "*?.?*" AND Bezeichnung LIKE "*_????????*" AND Bezeichnung LIKE "*?\?*")
WHERE
(((Nz(Mid([Bezeichnung],InStrRev([Bezeichnung],"/")+1,InStr([Bezeichnung],"_")-
InStrRev([Bezeichnung],"/")-1),"")) Not Like "#Func!"));
Since I don't know exactly where the errors occur, I can't write up a proper filter to identify them, but judging by your query they should include a slash and a symbol after that slash, an underscore and at least 8 symbols after that underscore, and a dot with at least one symbol before and after the dot.

SQL Hive subquery error

I have the query below
set hive.cli.print.header=true;
set hive.query.max.partition=1000;
set hive.mapred.mode=unstrict;
SELECT
dim_lookup("accounts",name,"account_id") = '28016' as company,
dim_lookup("campaigns",name,"campaign_id") in (117649,112311,112319,112313,107799,110743,112559,112557,105191,105231,107377,108675,106587,107325,110671,107329,107181,106565,105123,106569,106579,110835,105127,105243,107185,105211,105215) as campaign_name,
case when is_click_through=0 then "PV" else "PC" end as conv_type,
(SELECT COUNT(1) FROM impressions WHERE ad_info[2] in (117649,112311,112319,112313,107799,110743,112559,112557,105191,105231,107377,108675,106587,107325,110671,107329,107181,106565,105123,106569,106579,110835,105127,105243,107185,105211,105215)) AS impressions
FROM actions
WHERE
data_date>='20170101'
AND data_date<='20171231'
AND conversion_action_id in (20769223,20769214,20769219,20764929,20764932,20764935,20769215,20769216,20764919,20769218,20769217,20769220,20769222)
GROUP BY conv_type
When I execute it I get an error
ERROR ql.Driver: FAILED: ParseException line 8:1 cannot recognize input near 'SELECT' 'COUNT' '(' in expression specification
I am trying to fetch each count of impression for a specified conversion_action_id. What could be the error in my query? Thanks for the help.
FYI: ad_info[2] and campaign_id are the same.
The problem is quite clear, you have a subquery inside your SELECT.
That is not how this works.
Unfortunately the exact solution is not that clear, as it I am not completely sure what you want, but here is some general advice:
Write your subquery, test it and make sure it is ok
Rather than putting it in your SELECT part, put it in your FROM part, and (as always) SELECt from the FROM
Just think of your subquery output as an other table that can be used in the from statement, and which needs to be combined (JOIN, UNION?) with other tables in the from statement.

Pervasive Control Center SQL INTO Error

I am getting an error in PCC which doesn't make a lot of sense. I have two statements inside a user defined function that are nearly exactly the same and one runs fine while the other is returning an error:
'INTO': Syntax error
end and start are parameters being passed to the function.
The Error is being thrown on the second INTO statement
SELECT count(*) INTO :divModelTot1
FROM "table1"."info" i
WHERE i.compldate <:end
AND (i.agree is null OR i.agree>:start)
UNION ALL
SELECT count(*) INTO :divModelTot2
FROM "table2"."info" i
WHERE i.compldate <:end
AND (i.agree is null or i.agree>:start);
Any help or suggestions would be appreciate.
Thanks!
SELECT INTO must be the first query in a statement containing a UNION.
SELECT count(*) INTO :divModelTot1
FROM "table1"."info" i
WHERE i.compldate <:end
AND (i.agree is null OR i.agree>:start)
UNION ALL
SELECT count(*)
FROM "table2"."info" i
WHERE i.compldate <:end
AND (i.agree is null or i.agree>:start);