loop through all tables and delete records

后端 未结 4 1557
借酒劲吻你
借酒劲吻你 2021-01-23 06:20

I\'m new to MsSql and I\'m not sure if this can be done but I figured I\'d ask before I want on my way with the current process..

I need to create a script that loops th

4条回答
  •  情歌与酒
    2021-01-23 07:10

    sp_MSForEachTable is an undocumented stored procedure which will run a command for each table in the database:

    USE MyDatabase
    
    DECLARE @CorporationId VARCHAR(50)
    SET @CorporationId = '52D3AEFE-8EBD-4669-8096-4596FE83BB36'
    
    DECLARE @Sql VARCHAR(MAX)
    SET @Sql = '
    IF COL_LENGTH(''?'',''CorporationId'') IS NOT NULL
    BEGIN
        DELETE FROM Web.?
        WHERE CorporationId = ''' + @CorporationId + '''
    END
    '
    
    EXEC sp_MSForEachTable @Sql
    

提交回复
热议问题