Delete statement

  • Can someone help me in writing this query. Doing some silly mistake but not able to recognize.
    for testing purpose commented actual delete clause

    if DB_name () = N'MyDB'
    begin

    IF( EXISTS( SELECT 1 FROM sch1.table1 WHERE guid= N'05172A47' and IntId=12 ))

    print 'im deleting 1';

    --DELETE FROM sch1.table1 where Guid = N'05172A47' and IntId=12


    print 'im deleting 2';

    IF( EXISTS( SELECT 1 FROM sch1.table1 WHERE Guid = N'3DFB856F' and IntId=13 ))

    --DELETE FROM sch1.table1 where Guid = N'3DFB856F' and IntId=13

    end
    ELSE PRINT 'Nothing deleted'

    end

  • khushbu - Friday, April 21, 2017 3:22 AM

    Can someone help me in writing this query. Doing some silly mistake but not able to recognize.
    for testing purpose commented actual delete clause

    if DB_name () = N'MyDB'
    begin

    IF( EXISTS( SELECT 1 FROM sch1.table1 WHERE guid= N'05172A47' and IntId=12 ))

    print 'im deleting 1';

    --DELETE FROM sch1.table1 where Guid = N'05172A47' and IntId=12


    print 'im deleting 2';

    IF( EXISTS( SELECT 1 FROM sch1.table1 WHERE Guid = N'3DFB856F' and IntId=13 ))

    --DELETE FROM sch1.table1 where Guid = N'3DFB856F' and IntId=13

    end
    ELSE PRINT 'Nothing deleted'

    end

    Something like this?
    😎
    IF DB_name () = N'MyDB'
    BEGIN
      IF( EXISTS( SELECT 1 FROM sch1.table1 WHERE guid= N'05172A47' and IntId=12 ))
      BEGIN
       print 'im deleting 1';
       --DELETE FROM sch1.table1 where Guid = N'05172A47' and IntId=12
      END

      IF( EXISTS( SELECT 1 FROM sch1.table1 WHERE Guid = N'3DFB856F' and IntId=13 ))
      BEGIN
       print 'im deleting 2';
       --DELETE FROM sch1.table1 where Guid = N'3DFB856F' and IntId=13
      END
    END
    ELSE
    BEGIN 
      PRINT 'Nothing deleted'
    END

  • A more efficient way of coding this is to perform the deletion and then check @@ROWCOUNT afterwards, thus hitting the table once per deletion rather than twice.

    The absence of evidence is not evidence of absence
    - Martin Rees
    The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
    - Phil Parkin

  • khushbu - Friday, April 21, 2017 3:22 AM

    Can someone help me in writing this query. Doing some silly mistake but not able to recognize.
    for testing purpose commented actual delete clause

    if DB_name () = N'MyDB'
    begin

    IF( EXISTS( SELECT 1 FROM sch1.table1 WHERE guid= N'05172A47' and IntId=12 ))

    print 'im deleting 1';

    --DELETE FROM sch1.table1 where Guid = N'05172A47' and IntId=12


    print 'im deleting 2';

    IF( EXISTS( SELECT 1 FROM sch1.table1 WHERE Guid = N'3DFB856F' and IntId=13 ))

    --DELETE FROM sch1.table1 where Guid = N'3DFB856F' and IntId=13

    end
    ELSE PRINT 'Nothing deleted'

    end

    Not sure if your GUID column is actually a uniqueidentifier or not, but if so I don't think either of your strings are valid. Also, I don't see where you stated what the actual problem/error is.

    Best,
    Kevin G. Boles
    SQL Server Consultant
    SQL MVP 2007-2012
    TheSQLGuru on googles mail service

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

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