Find a column from all the tables and delete

  • find a column from all the tables of a database and delete the row of that table where ever it finds that column in tables of a database

    vijay verma


    vijay verma

  • This makes very little sense as it is written. Can you clarify?

    Steve Jones

    steve@dkranch.net

  • Here i am giving the details of the question.

    My question was :

    i wanted to delete the rows from all the tables of a database where a particular colums occurs in the selected tables of a database in one query stored procedure and using one query both.

    for ex. if dept_id column occures in six table. then i want to delete the rows from all the six table where ever it finds that dept_id column exist in the table.

    vijay verma


    vijay verma

  • Hi,

    declare @strTable varchar(100)

    declare @strsql varchar(1000)

    declare cur_del cursor

    read_only

    for select

    O.[name] as tablename

    from syscolumns C inner join sysobjects O on C.id=O.id

    and O.type='U'

    where C.[name]='ida'

    open cur_del

    fetch next from cur_del into @strTable

    while @@fetch_status = 0

    begin

    set @strsql = ' delete from ' + @strtable

    exec(@strsql)

    fetch next from cur_del into @strtable

    end

    close cur_del

    deallocate cur_del

  • the code Klas gave might work .but am not too sure , how it will delete in the required order . coz, if any references do occur . this will generate a error.

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply