How to resolve Unexpected 'select' error in Snowflake - snowflake-cloud-data-platform

Code:
Delete from Table1
Where(A,B,C)
IN
(Select A,B,C From Table2);
If there is matched record then it should delete the records from Table1.
When I executed the above code, I got the error like unexpected select and unexpected From error

I think you are trying to do something like this:
delete from table1 using table2
where
table1.a = table2.a and
table1.b = table2.b and
table1.c = table2.c

Related

Combining two tables with multiple common columns into one in SQL

I am trying to create a table in SQL server which has the same output as the following:
Select *
FROM Table1
LEFT JOIN Table2
ON
Table1.Key1 = Table2.Key1
AND Table1.Key2 = Table2.Key2
The result of the above query is exactly what I need, but as a new table.
The problem is, there are multiple columns that are common between the two tables. I have executed the following code:
Select *
INTO NewTable
FROM Table1
LEFT JOIN Table2
ON
Table1.Key1 = Table2.Key1
AND Table1.Key2 = Table2.Key2
The following error appears:
Msg 2705, Level 16, State 3, Line 1
Column names in each table must be unique. Column name 'Key1' in table 'NewTable' is specified more than once.
Could someone please help? I would highly appreciate it after a long day of searching the internet without any solution.
Thank you so much in advance!
This will help you identify what records you need to get a unique list.
select ',' + Column_Name
from INFORMATION_SCHEMA.COLUMNS c2
where column_Name not in (
select COLUMN_NAME
from INFORMATION_SCHEMA.COLUMNS
where TABLE_NAME = 'table1')
and table_Name = 'Table2'
So you can safely say:
Select table1.*
<<Paste in your results from above here>>
INTO NewTable
FROM Table1
LEFT JOIN Table2
ON
Table1.Key1 = Table2.Key1
AND Table1.Key2 = Table2.Key2
When you write select into table query then it dynamically create the table and at the time of creating a table the name of column should be unique.
In your case when you join then name of column can be in both table.
So replace * and write it as shown below
Select Column1, Column2, ... etc
INTO NewTable
FROM Table1
LEFT JOIN Table2
ON
Table1.Key1 = Table2.Key1
AND Table1.Key2 = Table2.Key2
Your easiest option would be to change your select-into statement into something like this, where you give unique names to the fields with the same name:
Select Table1.Key1 as [Key1a], Table2.Key1 as [Key1b], etc.
INTO NewTable
FROM Table1
LEFT JOIN Table2
ON
Table1.Key1 = Table2.Key1
AND Table1.Key2 = Table2.Key2
If you are using SSMS, you could highlight your query, right-click on it and select "Design in Query Editor" and it will show you the select statement as "select [all of the fields]" rather than "select *", which will probably be useful to you.

SQL Server Merge statement causing error

I have 2 tables Table1 and Table2 that have the following identical schema:
Table1 (id int, lastModifiedDate datetime)
Table2 (id int, lastModifiedDate datetime)
Table1 is the source table, Table2 is the target table.
I need in only one query to execute the following scenario:
if Table1.id = Table2.id and Table1.lastModifiedDate = Table2.lastModifiedDate then don't do anything.
elseif Table1.id = Table2.id and Table1.lastModifiedDate <> Table2.lastModifiedDate then we have to delete from Table2 all row with this id and then we have to insert into it the matching row
I need also to insert into Table2 id values present in Table1 and not present in Table2
To summarize I need to do something like the following query (which has an incorrect syntax):
MERGE INTO Table2 AS target
USING (SELECT id, lastModifiedDate FROM Table1) AS source ON Source.id = target.id
WHEN NOT MATCHED BY TARGET
THEN INSERT(id,lastModifiedDate) VALUES(source.id,source.lastModifiedDate)
WHEN MATCHED AND Source.lastModifiedDate <> target.lastModifiedDate
THEN Delete
WHEN MATCHED AND Source.lastModifiedDate <> target.lastModifiedDate
THEN
INSERT(idlastModifiedDate)
VALUES(source.id, source.lastModifiedDate)
OUTPUT $action, inserted.*, deleted.*;
The generated error is that we can not have an insert in the when matched case.
Can anyone has an idea how can we do this?
here is an example of scenarios:
scenario 1:
table 1 an table2 contain the following row:
Table1 (1,2011-10-05 14:55:00.403)
Table2 is empty
after execution, since the Table1.id is not present in Table2, we should insert this row into Table2
So Table2 should contain (1,2011-10-05 14:55:00.403)
Scenario 2:
Table1 (1,2011-10-05 14:55:00.403),(2,2011-10-05 14:55:00.403)
Table2 (1,2011-10-05 14:55:00.403)
after execution, because the first row already exist in Table2, we don't touch it, however the 2nd row don't exist yet so we have to insert it .
So Table2 should contain (1,2011-10-05 14:55:00.403),(2,2011-10-05 14:55:00.403)
Scenario 2:
Table1 (1,2011-10-05 14:55:00.403),(2,2011-10-05 00:00:00.403)
after execution, because the 2nd row has the same id but another lastModifiedDate, we have to delete from table2 the row having this id and then insert the 2nd row of table1
So after execution, Table2 should contain (1,2011-10-05 14:55:00.403),(2,2011-10-05 00:00:00.403)
Thanks in advance
No, you cannot have an INSERT in matched (https://msdn.microsoft.com/en-us/library/bb510625.aspx) But are you sure your disallowed INSERT shouldn't just be an UPDATE? Also seeing that you try to insert two columns into one column? (source.id, source.lastModifiedDate)==> idlastModifiedDate and the two matched conditions are the same?
So more like:
MERGE INTO Table2 AS target
USING (SELECT id, lastModifiedDate FROM Table1) AS source ON Source.id = target.id
WHEN NOT MATCHED BY TARGET
THEN INSERT(id,lastModifiedDate) VALUES(source.id,source.lastModifiedDate)
WHEN MATCHED THEN
UPDATE set lastModifiedDate = Source.lastModifiedDate

Subquery returned more than 1 value error after executing update query

I am using sql-server 2012 and i have a strange problem in updating a table.
My select query returns tree rows and is like below:
select * from
TAble1 p join
(select ProductId=max(ProductId) from Table2 s group by s.ProductId) pin on p.id=pin.ProductId
where p.categoryid=238
and the returned row is:
Now, When i run this update query:
update TAble1 set sizing=0 from
TAble1 p join
(select ProductId=max(ProductId) from TAble2 s group by s.ProductId) pin on p.id=pin.ProductId
where p.categoryid=238
I got this error:
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
The statement has been terminated.
Where is the problem in my query?
Looks like the problem that generates an exception is somewhere else (inside of a trigger for example).
This line could be the reason why there is more than one row updated
(select ProductId=max(ProductId) from TAble2 s group by s.ProductId)
If you want to obtain max ProductID (a single value) - remove it from GROUP BY clause. Currently you are requesting server to return maximum from a single value - which is absurd. It simply returns a list of all ProductID values from Table2. Which is the same as
select distinct ProductID from Table2
This select ProductId=max(ProductId) from Table2 s group by s.ProductId
will give you DISTINCT ProductId's, but not MAX. And You dont have link with TAble1 In fact your query will update all TAble1.sizing column.
Try this:
UPDATE TAble1
SET sizing = 0
FROM TAble1 p
JOIN
(SELECT max(s.ProductId) AS ProductId
FROM TAble2) pin
ON p.id = pin.ProductId AND p.categoryid=238
WHERE p.categoryid = categoryid
I think a better question is what isn't the problem in your query. In your SELECT what exactly are you supposed to be joining? And what index are you joining it to? You're using a GROUP BY but not including the GROUP BY as a column in your SELECT. You don't need to alias the 'TAble2 s' in the subquery. TAble p doesn't have a categoryid column based on what you've shown. You shouldn't need the FROM clause in the UPDATE query in most cases, especially since you're just setting a column to a static value.
But to answer your question: the subquery: "select ProductId=max(ProductId) from TAble2 s group by s.ProductId" returns all ProductId rows, so it fails when you're trying to join.
Since you're not using info from Table2 why not just simply update like this:
update TAble1 set sizing=0 where categoryid=238

SQL DELETE query with NOT IN clause not working

i have a simple delete query i have two tables table1 and table2
the query is
DELETE FROM table1
WHERE end_time NOT IN (SELECT end_time
FROM table2
WHERE comp_name = 'component')
AND id = 'id'
the query does not delete the record what am i missing here i tried to change the first condition with second but it deletes without checking the condition 2
what are the alternatives here?
i am wondering what is the actual problem here the query runs without errors but it is not correct.
May be alias should fix your problem since the both the tables have same column name end_time. Outer query column can be referred inside the subquery that might be the problem.
DELETE FROM table1
WHERE end_time NOT IN (SELECT b.end_time
FROM table2 B
WHERE b.comp_name = 'component')
AND id = 'id'
Update : Try this delete because you may have null values in your sub query where NOT IN will fail. Check this post link
DELETE a
FROM table1 a
WHERE NOT EXISTS (SELECT 1
FROM table2 B
WHERE b.comp_name = 'component'
AND a.end_time = b.end_time)
AND id = 'id'

SQL error when trying to copy value from column based on condition

with dummytable as
(
Select table2.number, table2.paymentto
from table2
inner join table1 on table1.number = table2.number and table2.number='1'
and table2.paymentto is not null and table2.paymentto !='B'
)
update table1
set ProvCOBAdjustments = (Select TotCOBAdjustmentAmt
from table1, dummytable
where dummytable.paymentto='P'and dummytable.number = table1.number)
where table1.paymentto is null;
ERROR: syntax error at or near "update"
Not sure if it's what is causing the error, but your final where clause looks wrong:
where table.paymentto is null
Shouldn't it read:
where table1.paymentto is null
instead?
There is no syntax error in this code. There might be something with your original table names or something in the code before or after what you have posted here.
I think you want this:
update table1
set ProvCOBAdjustments = TotCOBAdjustmentAmt
from table1, dummytable
where dummytable.paymentto='P'and dummytable.number = table1.number
and table1.paymentto is null;
Otherwise you're trying to do some kind of correlated subquery, and it all goes quite pearshaped.
Also, comma style joining tends to be frowned upon these days - I'd re-write the join as:
update t1
set
ProvCOBAdjustments = TotCOBAdjustmentAmt
from
table1 t1
inner join
dummytable dt
on
dt.number = t1.number
where
dt.paymentto='P' and
t1.paymentto is null;
What is the version of SQL Server?
If it is SQL Server 2000 or earlier, then you cannot run this query in its present form.
This query uses the so called CTE, Common Table Expression (the with dummytable as (...) part). CTE support was introduced in SQL Server 2005.
SQL Server 2008 R2, installed on my somputer, does not complain about syntax in this query.

Resources