Oracle SQL, SELECT clause using &input - sql

I have an quite simple query
SELECT id FROM table where ID in &data
When I run this SQL I am prompted to type in some values to &data.
I would like to be able to select several id's using this. i.e. &data = "11,12,13"
but then I get an error ORA-00933.
Ive tried with:
11,12,13
'11,12,13' -> ORA-01722
'11','12','13'
Any ideas?

Try to add ():
SELECT id FROM table where ID in (&data)
the input values should be '11,12,13'
or try to add (''):
SELECT id FROM table where ID in (&data)
the input values should be 11,12,13

Related

PostgreSQL subqueries as values

I am trying to use a postgreSQL INSERT query with a subquery as parameter value. This is to find the corresponding user_id from an accompanying auth_token in user_info tabel first and then create a new entry in a different table with the corresponding user_id.
My query looks something like this
INSERT INTO user_movies(user_id, date, time, movie, rating)
VALUES ((SELECT user_id FROM user_info where auth_token = $1),$2,$3,$4,$5)
RETURNING *
I know that a query such as this will work with a single value
INSERT INTO user_movies(user_id)
SELECT user_id FROM user_info where auth_token = $1
RETURNING *
but how do I allow for multiples input values. Is this even possible in postgreSQL.
I am also using nodejs to run this query -> therefore the $ as placeholders.
To expand on my comment (it is probably a solution, IIUC): Easiest in this case would be to make the inner query return all the values. So, assuming columns from the inner query have the right names, you could just
INSERT INTO user_movies(user_id, date, time, movie, rating)
SELECT user_id,$2,$3,$4,$5 FROM user_info where auth_token = $1
RETURNING *
Note this form is also without VALUES, it uses a query instead.
Edited 20220424: a_horse_with_no_name removed the useless brackets around SELECT ... that appeared in my original version; thanks!
YOu could try uising where IN clause
INSERT INTO user_movies(user_id)
SELECT user_id
FROM user_info
WHERE auth_token IN ($1,$2,$3,$4,$5)
RETURNING *

pgp_sym_decrypt using with select query

I want to select all data from a table (all data in table were encrypted) in postgresql database. But can't get all data.Query only works with condition. This query works. select pgp_sym_decrypt(name::bytea,'code'), pgp_sym_decrypt(surname::bytea,'code'), pgp_sym_decrypt(context::bytea,'code') from schema.table_name where id=1;
But I want to use this query : select pgp_sym_decrypt(name::bytea,'code'), pgp_sym_decrypt(surname::bytea,'code'), pgp_sym_decrypt(context::bytea,'code') from schema.table_name;
How can I get all data?

t-sql query returns undefined after using ORDER BY

I am currently working with a MS SQL database on Windows 2012 Server
I need to query only 1 column from a table that I only have access to read, not make any kind of changes.
Problem is that the name of the column is "Value"
My code is this:
SELECT 'Value' FROM table
If I add
`ORDER BY 'Value'`
The issue is that the query is returning an empty list of results.
Things I've tried already
I tried replacing ' with `"' but this didn't work either.
I also tried writing SELECT * instead of SELECT VALUE
Using the table name in the SELECT or ORDER clauses again didn't help
You are claiming that this query:
SELECT 'Value'
FROM table
ORDER BY 'Value'
Is returning no rows. That's not quite correct. It is returning an error because SQL Server does not allow constant expressions as keys for ORDER BY (or GROUP BY for that matter).
Do not use single quotes. In this case:
SELECT 'Value' as val
FROM table
ORDER BY val;
Or, if value is a column in the table:
SELECT t.Value
FROM table t
ORDER BY t.Value;
Value is not a reserved word in SQL Server, but if it were, you could escape it:
SELECT t.[Value]
FROM table t
ORDER BY t.[Value];
it looks like your table has null values. and because of the order by all null values come first.
try to add filter like this
select Value FROM table
where Value is not null and Value <> ''
order by Value

Insert into a specific row of a table

I am trying to select the row in a table where the id = user and once I have that row I want to insert into the docId column the value docId. To do this I have tried this:
INSERT INTO (SELECT * FROM users WHERE (id='"+user+"')); (docId) VALUES ('"+docId+"')
but this does not work
I think you want:
update users
set docId = ?
where id = ?
Do not munge the query string with parameter values. These only cause unexpected syntax errors and make the code vulnerable to SQL injection. Learn to use parameters.
Try INSERT INTO TableNmae(SELECT * FROM users WHERE (id='value') and (docId) = ('value1'))

sql server to delete a record and add sum of value on trigger

I have two tables in my database, bill_datail and bill_log. I want to delete one record from table bill_log and after that trigger an action to do something in table bill_detail. My code for delete is the following:
DELETE FROM [mydatabase].[dbo].[Bill_Log]
WHERE [mydatabase].[dbo].[Bill_Log].[CU_BILL_ID] in
(SELECT
FROM [mydatabase].[dbo].[Bill_Log],[mydatabase].[dbo].[Bill_Detail]
where [mydatabase].[dbo].[Bill_Log].bill_id=37
and [mydatabase].[dbo].[Bill_Log].bill_id=[mydatabase].[dbo].[CU_Bill_Detail].cu_bill_id
and [mydatabase].[dbo].[Bill_Detail].Pay_date>20130206
and [CL_Com_Rec_Description] like '%اoffpage%'
and [mydatabase].[dbo].[Bill_Log].amount<0
and [mydatabase].[dbo].[Bill_Log].[Com_Act_Date]='2013/02/07')
go
CREATE TRIGGER [mydatabase].[dbo].[Bill_Log]
ON [mydatabase].[dbo].[Bill_Log]]
AFTER Delete
AS
---
BEGIN
-- get 'amount' from deleted record and sum it to field 'amount' of bill detail
END
But in delete action I get the following error:
'Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.
And I don't know how to fix the error and do the second part.
You only need to get a list of CU_BILL_ID to search. So remove all other fields from inner query and just select CU_BILL_ID.
DELETE FROM [mydatabase].[dbo].[CU_Bill_Log]
WHERE [mydatabase].[dbo].[CU_Bill_Log].[CU_BILL_ID] in
(SELECT cu_bill_id
FROM [mydatabase].[dbo].[CU_Bill_Detail]
where Pay_date>13930206)
and [mydatabase].[dbo].[CU_Bill_Log].cu_bill_id=37
and [mydatabase].[dbo].[CU_Bill_Log].cu_bill_id=
and [mydatabase].[dbo].[CU_Bill_Detail].
and [CL_Com_Rec_Description] like '%اoffpage%'
and [mydatabase].[dbo].[CU_Bill_Log].amount<0
and [mydatabase].[dbo].[CU_Bill_Log].[CL_Com_Act_Date]='2013/02/07'
go
Try this please.
if you want use of "in" keyword in your main query ,
the subquery must return just one column as result
Select ID,F_Name,L_Name
From Clients
Where ID in(
Select ClientID
From Orders
Where OrderNo > 120
)