SQL - 根据列值删除所有表中的行

时间:2015-01-29 10:14:34

标签: mysql sql sql-delete

我有一个包含数百个表的MySQL数据库。

我需要根据名为' companyId'的公共列删除所有表中的记录。 - 此列存在于数据库的所有表中。

如何删除所有表中columnId = somenumber?

的所有记录

提前谢谢!

Ivano C。

@SocratesG我尝试了你的存储过程,但是我在下面创建了SP:

错误1064:您的SQL语法出错;检查与MySQL服务器版本对应的手册,以便在@TableName VARCHAR(128)附近使用正确的语法;

DECLARE @MyColumn VARCHAR(128);

SET @MyColumn ='公司'在第4行

SQL语句:

CREATE PROCEDURE deletePortalInstance()

BEGIN

DECLARE @TableName VARCHAR(128);

DECLARE @MyColumn VARCHAR(128);

SET @MyColumn =' companyId'

宣布MyCursor CURSOR

(SELECT OBJECT_NAME(c.id)as ObjectName

          FROM dbo.syscolumns c

          WHERE 

            OBJECTPROPERTY(c.id,'ISTABLE') = 1 --Search for tables only

            AND c.name = @MyColumn)

OPEN MyCursor

FETCH NEXT从MyCursor进入@TableName

WHILE @@ FETCH_STATUS = 0

BEGIN

EXEC

        (

             'DELETE ' + @MyColumn

            +' FROM ' + @TableName  

            +' WHERE ' + @MyColumn + ' = 906454'

        )

FETCH NEXT FROM MyCursor into @TableName

END

关闭MyCursor

DEALLOCATE MyCursor

END

错误1064:您的SQL语法出错;检查与MySQL服务器版本对应的手册,以便在@TableName VARCHAR(128)附近使用正确的语法;

DECLARE @MyColumn VARCHAR(128);

SET @MyColumn ='公司'在第4行

SQL语句:

CREATE PROCEDURE deletePortalInstance()

BEGIN

DECLARE @TableName VARCHAR(128);

DECLARE @MyColumn VARCHAR(128);

SET @MyColumn =' companyId'

宣布MyCursor CURSOR

(SELECT OBJECT_NAME(c.id)as ObjectName

          FROM dbo.syscolumns c

          WHERE 

            OBJECTPROPERTY(c.id,'ISTABLE') = 1 --Search for tables only

            AND c.name = @MyColumn)

OPEN MyCursor

FETCH NEXT从MyCursor进入@TableName

WHILE @@ FETCH_STATUS = 0

BEGIN

EXEC

        (

             'DELETE ' + @MyColumn

            +' FROM ' + @TableName  

            +' WHERE ' + @MyColumn + ' = 906454'

        )

FETCH NEXT FROM MyCursor into @TableName

END

关闭MyCursor

DEALLOCATE MyCursor

END

4 个答案:

答案 0 :(得分:0)

DELETE FROM <tblName> where columnId = <Your Column Id val>

但我认为您使用外键使用参照完整性约束并使用删除级联

根据您的新信息,

更新:

DELETE t1, t2
      FROM <table1> t1
      JOIN <table2> t2 ON t1.columnId = t2.columnId 
     WHERE t1.columnId  = <Your Column Value>

说明:

  1. 这里我们要删除多个表中的记录,因此我们使用DELETE FROM table1,table2

  2. 由于所有表中都有一个同名的列,我们创建了JOIN,因此可以一次删除100个表中的所有行。

  3. 3.最后我们有条件WHERE t1.columnId = 1,表示在table1中,检查columnId = 1,所以当我们加入时,它选择100表中的所有这些行,然后删除它。

答案 1 :(得分:0)

你可以试试这个

DECLARE @TableName VARCHAR(128);
DECLARE @MyColumn VARCHAR(128);
SET @MyColumn = 'MyColumnName'
DECLARE MyCursor CURSOR FOR  
(SELECT OBJECT_NAME(c.id) as ObjectName
              FROM dbo.syscolumns c
              WHERE 
                OBJECTPROPERTY(c.id,'ISTABLE') = 1 --Search for tables only
                AND c.name = @MyColumn)

OPEN MyCursor 
FETCH NEXT FROM MyCursor into @TableName
WHILE @@FETCH_STATUS = 0  
BEGIN
    EXEC
            (
                 'DELETE ' + @MyColumn
                +' FROM ' + @TableName  
                +' WHERE ' + @MyColumn + ' <= 1000'
            )
    FETCH NEXT FROM MyCursor into @TableName
END
CLOSE MyCursor   
DEALLOCATE MyCursor

答案 2 :(得分:-1)

DELETE FROM companyId where columnId = 1

答案 3 :(得分:-1)

DELETE FROM table_Name where columnn_name = value ;

示例

DELETE fom students where rll_num = 5 ;