How to Delete using INNER JOIN with SQL Server?

后端 未结 16 1722
刺人心
刺人心 2020-11-22 10:50

I want to delete using INNER JOIN in SQL Server 2008.

But I get this error:

Msg 156, Level 15, State 1, Line 15<

相关标签:
16条回答
  • 2020-11-22 11:16

    Another way using CTE.

    ;WITH cte 
         AS (SELECT * 
             FROM   workrecord2 w 
             WHERE  EXISTS (SELECT 1 
                            FROM   employee e 
                            WHERE  employeerun = employeeno 
                                   AND company = '1' 
                                   AND date = '2013-05-06')) 
    DELETE FROM cte 
    

    Note : We cannot use JOIN inside CTE when you want to delete.

    0 讨论(0)
  • 2020-11-22 11:17

    Possible this be helpful for you -

    DELETE FROM dbo.WorkRecord2 
    WHERE EmployeeRun IN (
        SELECT e.EmployeeNo
        FROM dbo.Employee e
        WHERE ...
    )
    

    Or try this -

    DELETE FROM dbo.WorkRecord2 
    WHERE EXISTS(
        SELECT 1
        FROM dbo.Employee e
        WHERE EmployeeRun = e.EmployeeNo
            AND ....
    )
    
    0 讨论(0)
  • 2020-11-22 11:17

    Here is my SQL Server version

    DECLARE @ProfileId table(Id bigint)
    
    DELETE FROM AspNetUsers
    OUTPUT deleted.ProfileId INTO @ProfileId
    WHERE Email = @email
    
    DELETE FROM UserProfiles    
    WHERE Id = (Select Id FROM @ProfileId)
    
    0 讨论(0)
  • 2020-11-22 11:18

    Try this query :

    DELETE WorkRecord2, Employee 
    FROM WorkRecord2 
    INNER JOIN Employee ON (tbl_name.EmployeeRun=tbl_name.EmployeeNo)
    WHERE tbl_name.Company = '1' 
    AND tbl_name.Date = '2013-05-06';
    
    0 讨论(0)
  • 2020-11-22 11:18

    Try this, it might help

     DELETE WorkRecord2 
              FROM WorkRecord2 
        INNER JOIN Employee 
                ON EmployeeRun=EmployeeNo
             WHERE Company = '1' 
               AND Date = '2013-05-06';
    
    0 讨论(0)
  • 2020-11-22 11:20

    This is a simple query to delete the records from two table at a time.

    DELETE table1.* ,
           table2.* 
    FROM table1 
    INNER JOIN table2 ON table1.id= table2.id where table1.id ='given_id'
    
    0 讨论(0)
提交回复
热议问题