0

I want to delete rows who specific column exists in select statement like:

delete from [ProjectCustomer] pcc 
where ProjectKey in (
  select p.ProjectKey  
  FROM [Project] AS [p] inner JOIN [ProjectDesign] AS [pd] ON [p].[ProjectKey] = [pd].[ProjectKey]
  inner JOIN [Design] AS [d] ON [pd].[DesignKey] = [d].[DesignKey]
  inner JOIN [ProjectCustomer] AS [pc] ON [pc].[ProjectKey] = [p].[ProjectKey]
  inner join Customer as c on pc.CustomerKey = c.CustomerKey
  where d.Folio = 3014)

But in where clause of delete statement I get

Incorrect syntax near where clause

What am I doing wrong? Regards

3
  • Could you please add enough of the database schema for the above query to refer to existing table and column names? It might help me diagnose your issue. Commented Oct 19, 2018 at 23:34
  • Should be delete pcc from [ProjectCustomer] pcc Commented Oct 19, 2018 at 23:34
  • try removing pcc alias as you are not using it. Commented Oct 19, 2018 at 23:35

3 Answers 3

1

DELETE statement with table alias has different syntax, so either you can remove the alias as it's unused or change the delete statement like @Martin commented.

delete pcc from [ProjectCustomer] pcc

I would preper to revise the query using INNER JOIN instead of IN

delete pcc from [ProjectCustomer] AS pcc 
  inner join [Project] AS [p] ON [p].ProjectKey = [pcc].ProjectKey
  inner JOIN [ProjectDesign] AS [pd] ON [p].[ProjectKey] = [pd].[ProjectKey]
  inner JOIN [Design] AS [d] ON [pd].[DesignKey] = [d].[DesignKey]
  inner JOIN [ProjectCustomer] AS [pc] ON [pc].[ProjectKey] = [p].[ProjectKey]
  inner join Customer as c on pc.CustomerKey = c.CustomerKey
  where d.Folio = 3014)
Sign up to request clarification or add additional context in comments.

Comments

0

I think the error message is

Incorrect syntax near 'pcc'

instead of

Incorrect syntax near where clause

You wil get the first error message cause you are using the alias wrong, so while you are not joining the target table, you may want

delete from [ProjectCustomer]
where ProjectKey in (
  select p.ProjectKey  
  FROM [Project] AS [p] inner JOIN [ProjectDesign] AS [pd] ON [p].[ProjectKey] = [pd].[ProjectKey]
  inner JOIN [Design] AS [d] ON [pd].[DesignKey] = [d].[DesignKey]
  inner JOIN [ProjectCustomer] AS [pc] ON [pc].[ProjectKey] = [p].[ProjectKey]
  inner join Customer as c on pc.CustomerKey = c.CustomerKey
  where d.Folio = 3014);

or even if you still want to use an alias

delete pcc from [ProjectCustomer] pcc
where pcc.ProjectKey in ...

Comments

0

You can probably simplify your query a little. I think that you don't need a subquery, you can specify the table to delete from in the first FROM clause (without an alias) and join the tables in a second (using aliases as you like):

DELETE FROM ProjectCustomer
FROM ProjectCustomer pc
  INNER JOIN ProjectDesign pd ON pc.ProjectKey = pd.ProjectKey
    INNER JOIN Design d ON pd.DesignKey = d.DesignKey
WHERE d.Folio = 3014;

The Customers and Products table should not be neccessary, as long as you have set up referential integrity and pc.CustomerKey isn't nullable.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.