Is it possible to use the sql UPDATE on a sub query? I am using Big
Query Standard SQL and have tried every permutation of the below that I can come up with:
WITH test AS (SELECT * FROM 'my.database.table'),
test2 AS (UPDATE test SET myField = 100 WHERE myField < 100)
SELECT * FROM test2
I always receive the error:
Syntax error: Expected "(" or keyword SELECT or keyword WITH but got
keyword UPDATE
Use below instead
with test as (
select * from `my.database.table`
), test2 as (
select * replace(greatest(myfield, 100) as myfield) from test
)
select * from test2
Related
I'm using SQL Server 2014 and I'm trying to execute a query with REPLACE method within a CONTAINS method like this:
SELECT *
FROM A
WHERE CONTAINS(Name, REPLACE('abcd', 'a', 'b'))
But the query returns an error
Incorrect syntax near 'REPLACE'.
How can I do it correctly?
You can write as below:
declare #param nvarchar(100)='abcd';
set #param= REPLACE(#param,'a','b');
SELECT *
FROM PersonAddress
WHERE CONTAINS(FullName, #param)
But first, You need to do this
Try with cte
with cte as
(
select
*
from A
where REPLACE('abcd', 'a', 'b') as col
)
select
*
from cte
where contains(col, name)
I'm currently building a query and apparently, it doesn't work. This is my current query (this is the full one)
IF NOT EXISTS(SELECT * FROM invite WHERE uid=$1::uuid)
BEGIN
INSERT INTO invite (uid, link) VALUES ($1::uuid, $2::text);
END
ELSE
BEGIN
SELECT * FROM invite WHERE uid=$1::uuid;
END
In my console, i'm getting syntax error at or near "IF"
Many errors in your code:
plpgsql code can return result only from function (not from DO)
IF clause must have THEN
you do not need BEGIN / END in each statement inside IF ... THEN ... ELSE ... END (not error also has no sense)
Probably is better to do this by pure SQL:
WITH q AS (
SELECT *
FROM invite
WHERE uid=$1::uuid
), i AS (
INSERT INTO invite (uid, link)
SELECT $1::uuid, $2::text
WHERE (SELECT count(*) = 0 FROM q)
RETURNING *
) SELECT * FROM q
UNION SELECT * FROM i
I need to select records that has ID = 10,23,30 so I wrote this SQL
Select * from mytable where position(id in '10,23,30') > 0
But the problem I get additional records where ID = 1 and 2
Any ideas how to select only what I need ?
No need for position, just do IN:
Select * from mytable
where id in (10,23,30)
Use IN operator:
Select * from mytable where id in (10,23,30)
The following query works in MS Access, but it does not work in MS SQL Server:
SELECT
tblSession.PatientID as PID,
max(tblSession.SessionAttend) -
min(tblSession.SessionAttend) + 1 as NumSA,
max(tblSession.SessionSched) -
min(tblSession.SessionSched) + 1 as NumSS FROM
(
SELECT top 100 percent
tblSession.PatientID,
tblSession.SessionNumber,
tblSession.SessionDate,
tblSession.SessionAttend,
tblSession.SessionSched FROM
tblPatient INNER JOIN tblSession ON
tblPatient.PatientID = tblSession.PatientID) WHERE
(tblSession.SessionDate >= '12/8/2010') AND
(tblSession.SessionDate <= '5/18/2011') AND
(tblSession.Status = '2') ORDER BY
tblSession.PatientID, tblSession.SessionNumber
) GROUP BY tblSession.PatientID
In SQL Server, it gives the error "Incorrect syntax near the keyword 'GROUP'." When I hover over the GROUP keyword, the tooltip displays "Incorrect syntax near 'GROUP'. Expecting AS, ID, or QUOTED_ID." I don't understand. Can anyone tell me how to make this query work?
The derived table in () needs an alias, and its column references in the SELECT list updated accordingly:
SELECT top 100 percent
ALIASNAME.PatientID as PID,
max(ALIASNAME.SessionAttend) -
min(ALIASNAME.SessionAttend) + 1 as NumSA,
max(ALIASNAME.SessionSched) -
min(ALIASNAME.SessionSched) + 1 as NumSS FROM
(
SELECT top 100 percent
tblSession.PatientID,
...
...
...
...
tblSession.PatientID, tblSession.SessionNumber
-- This derived table needs an alias
) ALIASNAME
GROUP BY ALIASNAME.PatientID
I have a sql query that I run against a sql server database eg.
SELECT * FROM MyTable WHERE Id = 2
This may return a number of records or may return none. If it returns none, I would like to alter my sql query to return a default record, is this possible and if so, how? If records are returned, the default record should not be returned. I cannot update the data so will need to alter the sql query for this.
Another way (you would get an empty initial rowset returned);
SELECT * FROM MyTable WHERE Id = 2
IF (##ROWCOUNT = 0)
SELECT ...
SELECT TOP 1 * FROM (
SELECT ID,1 as Flag FROM MyTable WHERE Id = 2
UNION ALL
SELECT 1,2
) qry
ORDER BY qry.Flag ASC
You can have a look to this post. It is similar to what you are asking
Return a value if no rows are found SQL
I hope that it can guide you to the correct path.
if not exists (SELECT top 1 * FROM mytable WHERE id = 2)
select * from mytable where id= 'whatever_the_default_id_is'
else
select * from mytable where id = 2
If you have to return whole rows of data (and not just a single column) and you have to create a single SQL query then do this:
Left join actual table to defaults single-row table
select
coalesce(a.col1, d.col1) as col1,
coalesce(a.col2, d.col2) as col2,
...
from (
-- your defaults record
select
default1 as col1,
default2 as col2,
...) as d
left join actual as a
on ((1 = 1) /* or any actual table "where" conditions */)
The query need to return the same number of fields, so you shouldn't do a SELECT * FROM but a SELECT value FROM if you want to return a default value.
With that in mind
SELECT value FROM MyTable WHERE Id = 2
UNION
SELECT CASE (SELECT count(*) FROM MyTable WHERE Id = 2)
WHEN 0 THEN 'defaultvalue'
END