Column returns error when called in Select statement, but returns values when * is used - tsql

Values retrieved for a table column are incorrect unless I use SELECT * FROM table
I'm sure this is something basic that just isn't clicking with me.
Accurate values for the User column are returned when wildcard * is ran against the table, but my SQL Server username replaces the values in the column when it is called in a select statement. If I try to call the column in the SELECT statement with a table alias, I get "Incorrect syntax near the keyword 'User'."
User column is nvarchar(12), Action column is nvarchar(50)
This query returns accurate data in the User column:
SELECT *
FROM TransHistory as th
This query returns my SQL server username in the User column instead of the actual values:
SELECT Action, User
FROM TransHistory
This query, using a table alias, results in "incorrect syntax near the keyword 'User'.":
SELECT th.Action, th.User
FROM TransHistory as th
I expect to get the accurate User ID from the table in all 3 queries.

Related

Supabase PostgreSQL query error: Failed to run sql query: column __ does not exist

Every solution I see has to do with people having case sensitive column names.
The query select * from users works, but when I say select * from users where username=maxspiri, I get error column maxspiri doesn't exist. Here is my table:
Since maxspiri is a string, you need to wrap it in single quotes.
select * from users where username='maxspiri'

How can I insert into another table selecting from another ignoring the data type

I am running the query below and I am getting the error shown
core=# INSERT INTO transactionp SELECT * FROM transaction WHERE posting_date>'2021-08-17';
ERROR: column "balance" is of type u_money but expression is of type u_datetime
LINE 1: INSERT INTO transactionp SELECT * FROM transaction WHERE pos...
^
HINT: You will need to rewrite or cast the expression.
It's most likely because of the dreaded select * and not specifying target columns for the insert. Both are considered "code smell" and should be avoided.
The error message suggests that the order of columns is different in the source and target tables (they are matched by position not by name)
To fix this, specify column names in both parts: the INSERT and the SELECT statement:
INSERT INTO transactionp (balance, some_column, another_column, ...)
SELECT some_balance, column_one, column_two, ...
FROM transaction
WHERE posting_date>'2021-08-17';

Can any find the solution for this Error in IBM DB2?

%sql select Name_of_School, Safety_Score from SCHOOLS where \
Safety_Score= (select MAX(Safety_Score) from SCHOOLS)
i am trying to execute this query i got the message.
ibm_db_sa://rbm44299:***#dashdb-txn-sbox-yp-lon02-04.services.eu-gb.bluemix.net:50000/BLUDB
(ibm_db_dbi.ProgrammingError) ibm_db_dbi::ProgrammingError: SQLNumResultCols failed: [IBM][CLI Driver][DB2/LINUXX8664] SQL0206N "SAFETY_SCORE" is not valid in the context where it is used. SQLSTATE=42703 SQLCODE=-206
[SQL: select Name_of_School, Safety_Score from SCHOOLS where Safety_Score= (select MAX(Safety_Score) from SCHOOLS)]
(Background on this error at: http://sqlalche.me/e/f405)
SQL0206N is this error message https://www.ibm.com/support/knowledgecenter/SSEPGG_11.5.0/com.ibm.db2.luw.messages.sql.doc/com.ibm.db2.luw.messages.sql.doc-gentopic1.html#sql0206n
SQL0206N name is not valid in the context where it is used.
This error can occur in the following cases:
For an INSERT or UPDATE statement, the specified column is not a column of the table, or view that was specified as the object of the insert or update.
For a SELECT or DELETE statement, the specified column is not a column of any of the tables or views identified in a FROM clause in the statement.
among other cases.
I.e. Column SAFETY_SCORE does not exist in your table. Maybe the column is "Safety_Score" or "Safety Score" or some other name.
If the column name is not in UPPER CASE in your table, you will need to surround it in double quotes.
I could fix by using the %%sql structure and the double quotes :
%%sql
select MAX("Safety Score") AS MAX_SAFETY_SCORE from Chicago_SCHOOLS;

PostgreSQL use Select return in function

I am trying to run a query like
select id from table where id = pseudo_encrypt(select last_value from id_seq)
..
select last_value from id_seq
returns 5 which is the correct result. My question is how can I use it as function argument
The story is I am storing id of the table as encrypted value of id_seq.
So I need to get the result where id matches with encrypted one.
select last_value from id_seq
this query returns
and the result I need is
I guess I need to write a function but I am not good at Postgresql.
I found the solution.
The query must have be like
select * from table where id = pseudo_encrypt(currval('id_seq'::regclass))
Thanks for your helps

Create function/variable/parameter in pgAdmin with specific values

Not sure whether this is possible and what's the best way to do it, but I have a list of values (say it is 'ACBC', 'ADFC', 'AGGD' etc.) which grows over time. I want to use these values in pgADmin as a sort of variable/parameter for SQL statements; e.g:
Codes = {'ACBC', 'ADFC', 'AGGD'}
SQL: Statement => SELECT * FROM xxx WHERE SUBSTRING IN (Codes)
Is that somehow realizable, either with a variable, parameter, function or anyhing else?
I can think of the following options:
1) Create separate table
create table qry_params(prm_value varchar);
insert into qry_params values
('xxx'),
('yyy'),
('zzz');
select * from xxx where substring in (select prm_value from qry_params)
Whenever you have new parameter, you just need to add it to the table.
2) CTE at the top of query
Use query like:
with params (prm_value) as (select values ('xxx'), ('yyy'), ('zzz'))
select * from xxx where substring in (select prm_value from qry_params)
Whenever you have new parameter, you just need to add it to the CTE.