How delete the rows where comlete record is null.

  • Hi,

         Here is my query....

         I created a table with diff data types and inserted some values into that table as following

         create table tab_del_nulls

    (int_no int,

    var_name varchar(1000),

    int_marks int,

    var_desc varchar(1000),

    dt_date datetime)

    insert into tab_del_nulls

    values(1,'aregaddan',240,'pass',getdate())

    insert into tab_del_nulls

    values(null,null,null,null,null)

    insert into tab_del_nulls

    values(2,'nani',140,'fail',null)

    insert into tab_del_nulls

    values(null,null,null,null,null)

    Select * from tab_del_nulls

    Int_no   var_marks    int_marks var_desc dt_date

    -----    ---------     --------  ------   -------

    1 aregaddan 240 pass 2006-02-22 11:25:40.667

    NULL NULL  NULL NULL NULL

    2 nani  140 fail NULL

    NULL NULL  NULL NULL NULL

         Now i want to delete the rows where the complete row is null i.e for the above table i want to delete 2nd and 4th rows.

    Can any one give me a suggestion?

     

    Thanks,

    Rao Aregaddan.

  • delete from tab_del_nulls

    where Int_no is null and

    var_marks is null and

    int_marks is null and

    var_desc dt_date is null ;

    U need to check each column for null

  • I know this type of query.but i want to use the query for global use.In this case we have only 4 cols but i want to use this for a table which is having 400 cols.Is there any other way like by using cursor or anything...

     

    Thanks,

    Rao Aregaddan.

  • Excuse for the type mistake,not comlete it is complete....

  • ok. then u need to create a dynamic query

    here it is ...

    I HAVE TRRIED.

    SHOULD WORK FOR ANY TABLE:

    DECLARE @TABLE_NAME SYSNAME

    declare @is_null_columns varchar(8000)

    set @is_null_columns =''

    SET @TABLE_NAME = 'STORES'

    select @is_null_columns = @is_null_columns + [name] + ' IS NULL AND '+CHAR(13) FROM SYSCOLUMNS

    WHERE ID = OBJECT_ID(@TABLE_NAME )

    SET @IS_NULL_COLUMNS = LEFT(@IS_NULL_COLUMNS ,LEN(@IS_NULL_COLUMNS )-5)

    PRINT 'DELETE FROM '+@TABLE_NAME +' WHERE ' + @is_null_columns

    EXECUTE ('DELETE FROM '+@TABLE_NAME +' WHERE ' + @is_null_columns )

  • Thanks Veeresh....Really Thanks allot.....

         It is working....

        

     

    Regards,

    Rao Aregaddan.

  • We have had the same problem not long ago...

    http://www.sqlservercentral.com/forums/shwmessage.aspx?forumid=8&messageid=257275

  •  

    Thanks Jesper ,For the reply.

     

    Regards,

    Rao Aregaddan.

  • Try this

    DELETE TableA

    WHERE  COALESCE(Col1,Col2,Col3,'') = ''

    Ram Ramamoorthy

     

     

     

  • Or COALESCE(Col1, Col2, Col3) is null

    It may however fail. You will get a conversion error if you run it on the following table:

    create table #test(a uniqueidentifier null, b int null)

    But in many cases, it's a possible solution...

     

  • You could try this

    SELECT *

    FROM #Test2

    WHERE BINARY_CHECKSUM(*) = CHECKSUM(*)

    this should give you all the rows in the table that have NULLs for all columns

  • This seems not to work. I get one row with the example below:

     

    create table #test(a int null, b varchar(10) null)

    go

    insert #test select 1, ''

    SELECT *

    FROM #Test

    WHERE BINARY_CHECKSUM(*) = CHECKSUM(*)

    drop table #test

    go

  • Ok...Thanks for the reply..

      But  i need the query for  dynomic purpose  ......as per my requirement we cant expect that with how many cols the table will  be created in the sp....

     

    Regards,

    Rao Aregaddan.

     

     

     

     

     

  • Ohh........Finally i got the solution..Excuese me for I did not read the complete mail which is next page....

     

     

     

     

     

     

  • the BINARY_CHECKSUM and checksum solution is great, but know their downsides (text/image/...)

    It's all in Bol

    Johan

    Learn to play, play to learn !

    Dont drive faster than your guardian angel can fly ...
    but keeping both feet on the ground wont get you anywhere :w00t:

    - How to post Performance Problems
    - How to post data/code to get the best help[/url]

    - How to prevent a sore throat after hours of presenting ppt

    press F1 for solution, press shift+F1 for urgent solution 😀

    Need a bit of Powershell? How about this

    Who am I ? Sometimes this is me but most of the time this is me

Viewing 15 posts - 1 through 14 (of 14 total)

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