Ambigious column reference PostgreSQL jsonb - sql-insert

I am trying to do an Upsert on JSONB Column, however, I get an SQL error:
SQL Error [42702]: ERROR: column reference "be_apis" is ambiguous
Position: 233
INSERT INTO "user"."permission"("name" , be_apis)
VALUES('PLAYERS_PLAYER-PROFILE_KYC','{"set":{"url": "/api/ews-files/agent/images/preview/private", "http_method": "GET"}}')
ON CONFLICT ("name", be_apis) DO update
SET be_apis = be_apis || '{"url": "/api/ews-files/agent/images/preview/private", "http_method": "GET"}'::jsonb
WHERE name = 'PLAYERS_PLAYER-PROFILE_KYC';
The exact position where I get this error is shown and bolded below:
SET be_apis = be_apis
If I run this:
UPDATE "user"."permission"
SET be_apis = be_apis || '{"url": "/api/ews-files/agent/images/preview/private", "http_method": "GET"}'::jsonb
WHERE name = 'PLAYERS_PLAYER-PROFILE_KYC';
It works. It appends the new url to the be_apis column.
Any ideas?

Related

In SQLite , How to SELECT a column only if it exists in the table

I am trying to write a query where the table will be generated dynamically for each job . And the columns will either exist or not based on input. In SQLite , i need to fetch the value of a column only if it exists otherwise null.
I tried with if & case statements using Pragma_table_info , but for negative scenario it is not working.
'''select
case when (select name from pragma_table_info('table_name') where name = col_name )is null
then error_message
else col_name'''
end
from table_name
This query is running if the mentioned col_name exists . But if not exists then it is throwing syntax error in else part.
Only in select query it should be done
Your code should work if the table's name, the column's name and the error message are properly quoted:
SELECT CASE
WHEN (SELECT name FROM pragma_table_info('table_name') WHERE name = 'col_name')
IS NULL THEN 'error_message'
ELSE 'col_name'
END
But you can do the same simpler with aggregation and COALESCE():
SELECT COALESCE(MAX(name), 'error_message')
FROM pragma_table_info('table_name')
WHERE name = 'col_name'
See the demo.

Handling Null Values in Linq in Select Block

I am facing any issue while selecting values in Linq as while selecting there may be null values but I cannot give the condition to remove that row if the value is null.
Below is the Linq query I am using. Can you please help me with this?
var value = (from data in dt.AsEnumerable()
select new{
Test1 = data.Field<decimal>("Test1"),Test2 = data.Field<decimal?>("Test2") --Value is null
,Test3 = data.Field<decimal?>("Test3") --Value is null
,Test4 = data.Field<decimal?>("Test4") --Value is null
,Test5 = data.Field<DateTime?>("Test5")--Value is null });
I need to handle null in select block and not in where condition.
I am getting below error while executing the query:
Cannot cast DBNull.Value to type 'System.DateTime'. Please use a
nullable type

Mariadb syntax error on Declare statement

I am creating a MariaDB stored procedure in phpmyAdmin and I keep getting a syntax error.
I have tried appending an # symbol for the variable but I am still getting the error.
Here is what my code currently looks like:
DECLARE #cNAME = VARCHAR(100);
SET #cNAME = "TEST";
SELECT * from tblUser where UserName = cNAME;
I expect that my stored procedure would get saved but it doesn't because of the syntax error.

Add column to a table if not exist

I have a problem to add a new column to a SQLITE table if this column is not already exist.
I have try with this code but i don’t know why it wont execute:
IF NOT EXISTS(SELECT * FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'mytable' AND COLUMN_NAME = 'mynewcolumn')
BEGIN
ALTER TABLE mytable ADD COLUMN mynewcolumn TEXT
END
I get an Exception error :
error: near "IF": syntax error
This is the solution i select :
1- I do PRAGMA table_info :
pragma table_info(MyTable)
This command gives all the informations about all the columns of the table. each row correspand to the information of a column.
This commande return an output table with 4 columns : cid, name, type, notnull, dft value, pk
2- I read all the rows from "PRAGMA table_info (MyTable)", and i compare the column "name" with the name of the column i want to check if exist.
3- If Column exist then i dont do anything
4- but if the column doen't exist then, here i add the column to my table using this commade :
ALTER TABLE MyTable ADD COLUMN NewColumn TEXT;
this is work for me, and do the job correctly.
To test whether a column exists, execute PRAGMA table_info and check if the column name appears in the result.

Modify a column to NULL - Oracle

I have a table named CUSTOMER, with few columns. One of them is Customer_ID.
Initially Customer_ID column WILL NOT accept NULL values.
I've made some changes from code level, so that Customer_ID column will accept NULL values by default.
Now my requirement is that, I need to again make this column to accept NULL values.
For this I've added executing the below query:
ALTER TABLE Customer MODIFY Customer_ID nvarchar2(20) NULL
I'm getting the following error:
ORA-01451 error, the column already allows null entries so
therefore cannot be modified
This is because already I've made the Customer_ID column to accept NULL values.
Is there a way to check if the column will accept NULL values before executing the above query...??
You can use the column NULLABLE in USER_TAB_COLUMNS. This tells you whether the column allows nulls using a binary Y/N flag.
If you wanted to put this in a script you could do something like:
declare
l_null user_tab_columns.nullable%type;
begin
select nullable into l_null
from user_tab_columns
where table_name = 'CUSTOMER'
and column_name = 'CUSTOMER_ID';
if l_null = 'N' then
execute immediate 'ALTER TABLE Customer
MODIFY (Customer_ID nvarchar2(20) NULL)';
end if;
end;
It's best not to use dynamic SQL in order to alter tables. Do it manually and be sure to double check everything first.
Or you can just ignore the error:
declare
already_null exception;
pragma exception_init (already_null , -01451);
begin
execute immediate 'alter table <TABLE> modify(<COLUMN> null)';
exception when already_null then null;
end;
/
You might encounter this error when you have previously provided a DEFAULT ON NULL value for the NOT NULL column.
If this is the case, to make the column nullable, you must also reset its default value to NULL when you modify its nullability constraint.
eg:
DEFINE table_name = your_table_name_here
DEFINE column_name = your_column_name_here;
ALTER TABLE &table_name
MODIFY (
&column_name
DEFAULT NULL
NULL
);
I did something like this, it worked fine.
Try to execute query, if any error occurs, catch SQLException.
try {
stmt.execute("ALTER TABLE Customer MODIFY Customer_ID nvarchar2(20) NULL");
} catch (SQLException sqe) {
Logger("Column to be modified to NULL is already NULL : " + sqe);
}
Is this correct way of doing?
To modify the constraints of an existing table
for example... add not null constraint to a column.
Then follow the given steps:
1) Select the table in which you want to modify changes.
2) Click on Actions.. ---> select column ----> add.
3) Now give the column name, datatype, size, etc. and click ok.
4) You will see that the column is added to the table.
5) Now click on Edit button lying on the left side of Actions button.
6) Then you will get various table modifying options.
7) Select the column from the list.
8) Select the particular column in which you want to give not null.
9) Select Cannot be null from column properties.
10) That's it.

Resources