SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Delete statement


Delete statement

Author
Message
khushbu
khushbu
SSC-Addicted
SSC-Addicted (447 reputation)SSC-Addicted (447 reputation)SSC-Addicted (447 reputation)SSC-Addicted (447 reputation)SSC-Addicted (447 reputation)SSC-Addicted (447 reputation)SSC-Addicted (447 reputation)SSC-Addicted (447 reputation)

Group: General Forum Members
Points: 447 Visits: 752
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


Eirikur Eiriksson
Eirikur Eiriksson
SSCoach
SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)

Group: General Forum Members
Points: 15164 Visits: 18600
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?
Cool
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

Phil Parkin
Phil Parkin
SSCoach
SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)

Group: General Forum Members
Points: 18561 Visits: 20441
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.


Help us to help you. For better, quicker and more-focused answers to your questions, consider following the advice in this link.

If the answer to your question can be found with a brief Google search, please perform the search yourself, rather than expecting one of the SSC members to do it for you.

Please surround any code or links you post with the appropriate IFCode formatting tags. It helps readability a lot.
TheSQLGuru
TheSQLGuru
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12495 Visits: 8554
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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search