I'm attempting to retreive a resultset from a MS Access database using VBA for Excel. In the VBA code, I'm constructed a string equal to:
strSql = "SELECT * FROM Pricing WHERE Account In (''1234'', ''ABCD'') '; "
Note that there are 2 single quotes around the strings within the SQL statement. There is also a single quote before the semi-colon. If I'm not mistaken, this evaluates to:
SELECT * FROM Pricing WHERE Account In ('1234', 'ABCD') ;
This query works fine when run directly in MS Access. However, in Excel VBA, I keep getting the Run-time error:
Syntax error (missing operator) in query expression 'Account In (''1234'', ''ABCD'') '; '
Notice that this error actually cut off the first half of the SQL statement.
I've tried a few variations, using double-quotes, double double-quotes, no quotes, etc. etc.
Any advice?
Thanks.
In Excel VBA the string identifier is the " (double quote) character. You do not need to double the single quote characters for them to pass through to the database when enclosed by double quotes.
Try this:
strSql = "SELECT * FROM Pricing WHERE Account In ('1234', 'ABCD')"
Please try it with this:
strSql = "SELECT * FROM Pricing WHERE Account In ('1234', 'ABCD')"
Related
I have db structure like this :
"selfId": "cd29433e-f36b-1410-851b-009d805073d7",
"selfName" : "A",
"bookIds": [
"2f51bfd4",
"2f3a1010",
"090436c0",
"1078c3b2",
"b63b06e0"
]
I am working in C# and get bookId as a string.
I am writing query as :
string SQLquery = string.Format("select c.selfName from c where ARRAY_CONTAINS(c.bookIds,\""+bookId+"\"");
But When bookId contains special character, then query is giving error. For example if bookId = "AK"s" book" (please note id itself contains ") , then executing sqlQuery is giving error.
When a text contains quotes, slashes, special characters, you can use Escape Sequence
The quoted property operator [""] can also be used to access properties. for example, SELECT food.id and SELECT food["id"] are equal. This syntax can be used to escape a property with spaces, special characters, or a name that is the same as a SQL keyword or reserved term.
Example:
SELECT food["id"]
FROM food
WHERE food["foodGroup"] = "Snacks" and food["id"] = "19015"
Reference :- https://learn.microsoft.com/en-us/azure/cosmos-db/sql/sql-query-constants#bk_arguments
I am looking to use a string that takes in some parameters from my cells and use it as my sql statement like so:
" select * from table where " & data_from_cells & " group by ...;"
store it as sqlstring
let
mystring = sqlstring,
myQuery = Odbc.Query("driver={Oracle in etc etc etc", mystring)
and I run into this error
Formula.Firewall: Query '...' (step 'myQuery') references other queries or steps, so it may not directly access a data source. Please rebuild this data combination.
now apparently I can't combine external queries and another query -- but I am only using passed-on parameters from Excel for my sql string? I am hoping to use WITH keyword as well to make nested queries using parameters but it doesn't even let me combine values from excel with an sql statement..
to be clear, the data_from_cells was transformed and formatted as a string.
When queries that do stuff are called by other queries that do other stuff, sometimes you can get firewall issues.
The way to get around that is for everything to be done in a single query.
The way to get around that without ending up with horrible code is to change your called queries from returning the result to functions that return the result.
For sqlstring:
() => " select * from table where " & data_from_cells & " group by ...;" // returns a function that gets the query string when called
Then your "myQuery" query can be
let
mystring = sqlstring(), //note the parentheses!
myQuery = Odbc.Query("driver={Oracle in etc etc etc", mystring)
I'm generating a dynamic sql query based on some user input. Here is the code that prepares the query:
var preparedParamValues = paramValues.map(paramValue => `'${paramValue}'`).join(',');
var sql = `INSERT INTO [DB] (${paramNames}) VALUES (${preparedParamValues})`;
When I send the following string to the DB it throws the below error:
'They're forced to drive stupid cars.'
I get an error :
'Unclosed quotation mark after the character string \')\'.'
I'm trying to find a way to escape all those characters but I don't understand the error or at least the last part of it with all the symbols.
You have to use two single quotes when a single quote appears in the string:
'They''re forced to drive stupid cars.'
I am trying to populate a table variable in SSRS and call a SP subsequently to process the data in it:
DECLARE #Tbl1 TABLE
(
D01 float,
D02 float,
D03 float,
D04 float,
D05 float,
...
D96 float
)
To populate it I use a text parameter #LS. The input is comma delimited string with 96 elements:
0.635316969,0.756943899,0.890520142,1.028008362,1.166350106,1.30511861,1.444527254,1.580948571,1.578743639,1.575542931,1.573195746,1.571346448,1.571275321,1.56992391,1.568003484,1.567221089,1.556836567,1.543820351,1.53037, ...., ,0.514543561
In a dataset I tried to populate the table first (after table variable declaration):
insert into #Tbl1
VALUES (#LS)
But got this error at run-time: "Column name or number of supplied values does not match table definition."
I tried JOIN(SPLIT()) with comma without luck. Any ideas?
Thanks!
The problem is the the #LS parameter is a single-value text parameter so you can't use it like that - you'd need to use a multi-value parameter.
So let's try something different. You don't need to create a temporary table because you could build your column values to give a dataset that you want using Sql like this:
SELECT 0.635316969 AS D01, 0.756943899 AS D02, ... , 0.514543561 AS D96
Fortunately almost everything in SSRS is an expression, so we just need to build this Sql statement dynamically from the #LS parameter using an expression. Go to the Report menu then Report Properties... and click the Code tab. Enter the following code:
Function MakeSql(LS As String) As String
Dim Sql As String
Dim Values() As String
Dim i As Integer
Sql = "SELECT "
Values = Split(LS, ",")
For i = 0 To Values.Length - 1
Sql = Sql + Values(i) + " AS D" + Right("0" + CStr(i+1), 2) + ", "
Next i
Sql = Left(Sql, Len(Sql) - 2) ' Remove trailing comma
Return Sql
End Function
So what we are doing is splitting the string into an array of values which we loop through to create a Sql statement that aliases these values to the field names we want.
Right-click your dataset, choose Dataset Properties and press the fx button beside the query textbox. This allows us to enter a text expression for our Sql statement rather than an actual Sql statement. Here we need to call the custom code function we created above which will insert our custom built Sql expression:
=Code.MakeSql(Parameters!LS.Value)
Make sure your dataset has the fields D01 to D96 (you'll have to set these up manually because SSRS can't analyse the Sql expression to determine the field values) and you're done!
I have the following string expression in a PowerShell script:
"select count(*) cnt from ${schema}.${table} where ${col.column_name} is null"
The schema and table resolve to the values of $schema and $table, respectively. However, an empty string is supplied for ${col.column_name}. How can I dot into the member of a variable as part of a string substitution?
How about:
"select count(*) cnt from $schema.$table where $($col.column_name) is null"
I think the problem you're having is mainly syntax related. If you have a variable named $foo, ${foo} references the same variable. So, the ${table} and ${schema} references in your sql string work ok.
The issue is with ${col.column_name}. Your variable (I assume) is called $col, and has a member named column_name. As Robert and Steven both indicate in their answers, to refer to this, you should use $($col.column_name). In general, $(expression) will be replaced with the value of the expression.
The reason for allowing braces in variable names is so that variables can have unusual characters in their names. I would recommend not using the ${} syntax (unless you have a compelling reason), and replacing it with straight $var references for variables and $($var.member) for member references in strings.
One way would be:
"select count(*) cnt from $schema.$table where $($col.column_name) is null"
Another option would be
"select count(*) cnt from {0}.{1} where {2} is null" -f $schema, $table, $col.column_name