What is the correct syntax for Prepared Statement at Couchbase? - prepared-statement

I'm trying to create Prepared Statement with named parameter at Couchbase 7.1 as follows:
PREPARE index_1 AS SELECT DISTINCT $column FROM bucket_name;
EXECUTE index_1 USING {'column': 'Name'};
I would like to receive the JSON array with distinct values for a given column name which is achievable using SQL syntax e.g.: SELECT DISTINCT Name FROM bucket_name;
Instead of this, I receive only given column name.
How to do it correctly?

Named parameters or positional parameters can be used for values only.
Not for field names or identifiers.
Above statements and results are correct.
If you must use the following if you want distinct Name field values.
PREPARE index_1 AS SELECT DISTINCT Name FROM bucket_name;
EXECUTE index_1;
OR
PREPARE p1 AS SELECT DISTINCT RAW t.[$column] FROM bucket_name AS t;
EXECUTE p1 USING {'column': 'Name'};
see N1QL query to select a dynamic key in Couchbae explanation of dynamic fields

Related

How to pass a parameter in a SQL query in MS SQL Server from an Excel sheet

Let's say I have this query in SQL Server:
select *
from Table1
where Column1 IN ('01061120192T')
I want to pass the values for Column1 from a column in an Excel sheet.
I'm able to pass a single value to the query as mentioned here. But parenthesis of IN can accept multiple values so I want to pass 1000 values in the parenthesis from a column in an Excel sheet.
Tried to pass multiple values to the parameter as below, but that's not working.
=Sheet1!$G$1:$G$5
You pass variables in SSIS as a ?, and then define your variables in your parameter mapping pane.
select *
from Table1
where Column1 = ?
Then, you need to go to your Parameter mapping pane and click Add. Select your variable's name in the Variable name Column. Then Input as the direction. Leave Paramter Size as -1.

SQL Server LIKE operator not matching values containing square brackets

I have a table in my database where I inserted all table names in my database.
For example I have table name [test].[TestTable] and when I run this query
select * from Tables where Name like N'%[test].[TestTable]%'
it doesn't return any value but this query works:
select * from Tables where Name like N'%[TestTable]%'
Can someone explain me why?
You have to escape the left square bracket:
SELECT * FROM Tables WHERE Name LIKE N'%[[]test].[[]TestTable]%'
-----------------------------------------^ ^
--------------------------------------------------+
when u use the operator LIKE and in the condition add [], it has another meaning. LIKE "reads" it as any single character within the specified range ([a-f]) or set ([abcdef]).
try using: like N'%TestTable%'

ORA-14108: illegal partition-extended table name syntax

I have a requirement where I need to run a update script over multiple partitions of a table . I have written a script for it as below:
but it gives
ORA-14108: illegal partition-extended table name syntax
Cause: Partition to be accessed may only be specified using its name. User attempted to use a partition number or a bind variable.
Action: Modify statement to refer to a partition using its name
Any idea how can I circumvent this error?
DECLARE
TYPE partition_names IS varray(1) OF varchar2(20);
curr_partition partition_names;
LENGTH integer;
BEGIN
curr_partition :=partition_names('SM_20090731');
LENGTH := curr_partition.count;
FOR i IN 1 .. LENGTH LOOP
dbms_output.put_line('Current Partition name is: '||curr_partition(i));
UPDATE TABLE_Y PARTITION (curr_partition(i))
SET PARTITION_KEY=TO_DATE('2017-08-21','YYYY-MM-DD')
WHERE ORDER_ID IN
(SELECT ORDER_ID
FROM TABLE_X);
END LOOP;
END;
/
You will have to concatenate the partition name in and use dynamic SQL, i.e.
EXECUTE IMMEDIATE
'UPDATE TABLE_Y PARTITION (' || curr_partition(i) || ')
SET PARTITION_KEY=TO_DATE(''2017-08-21'',''YYYY-MM-DD'')
WHERE ORDER_ID IN
(SELECT ORDER_ID
FROM TABLE_X)';
Whenever you run a SQL SELECT query or an INSERT, UPDATE or DELETE statement from PL/SQL, bind variables are used to pass into the SQL engine the values of any PL/SQL expressions. In particular, a bind parameter will be used for curr_partition(i). However, it seems the PARTITION clause of such queries and statements doesn't support bind parameters. I guess that this is because Oracle tries to create an execution plan for the query or statement before it has the bind parameter values, but if the query or statement specifies a partition, that information is a critical part of the plan and hence cannot be provided in a bind parameter.

How can I extract multiple values from an XML column, using a function similar to value()?

I'm trying to extract xml metadata from from SQL Server's Report Server Database. My current query is listed below:
SELECT
a.Name as 'ReportName'
,a.ReportXML
,Parameter = x.value('(Parameter/Name)[1]','VARCHAR(250)')
FROM (SELECT C.Name,c.itemID,CONVERT(XML,CONVERT(VARCHAR(MAX),C.Parameter)) AS reportXML
FROM ReportServer.dbo.Catalog C
WHERE C.Content is not null
and c.Type = 2 -- Report only
) a
cross apply reportXML.nodes('/Parameters') r (x)
WHERE 1=1
and name ='ReportName'
My objective is to return all parameters associated with a report. The x.value method will only return 1 value at most. (It currently returns the first parameter of the report because 1 is hard coded in the string literal.) I know there are 5 parameters for the report I'm looking at.
Is there another function with similar syntax that will allow me to return all the values? Or is there a wildcard that I can use in place of the number? I've tried multiple functions on msdn with no luck.
Your query is using nodes('/Parameters'). This will return a resultset with one row for each Parameters element - but there is only one probably.
With Parameter = x.value('(Parameter/Name)[1]','VARCHAR(250)') you are reading the first name of the first Parameter. If there are more Parameter elements nested within Parameters you will read only the first...
Without an example of your actual XML it is not easy to answer but:
Try to add one nesting level by adding this below your cross apply
outer apply r.x.nodes('Parameter') AS p (y)
Then change the column to
,Parameter = y.value('Name[1]','VARCHAR(250)')
This should read the first Name element of each Parameter element
If you need further help, please poste an example of your XML.
Btw: I do not understand this:
CONVERT(XML,CONVERT(VARCHAR(MAX),C.Parameter)
What is the initial type of C.Parameter, that you have to cast it to VARCHAR(MAX) and then to XML? If you are still using the deprecated TEXT, NTEXT or IMAGE you should consider to change this!

How to write select query comparing value in string (which contains array)

I am using SQL Server. The select query returns me this:
folder_name field contains array. I need to check if it contains s:1:"1". In other words, how can I get output like below?
You can use LIKE operator:
SELECT *
FROM yourtable
WHERE [folder_name] like '%s:1:"1"%'

Resources