April 6, 2010 at 6:49 am
Hello,
I want to run the update statement. And if the update statement is successful, then I want to run another set of statements.
So, does execute(update_statement) returns anything like in mysql??
I would like to get help if there are any other options as well..
Thanks in advance
April 6, 2010 at 6:54 am
bndgyawali (4/6/2010)
Hello,I want to run the update statement. And if the update statement is successful, then I want to run another set of statements.
So, does execute(update_statement) returns anything like in mysql??
I would like to get help if there are any other options as well..
Thanks in advance
Can't speak for MySQL, but in SQL Server - 😛 - you would use a transaction and probably a TRY...CATCH construct. Post an example if you want more detail.
April 6, 2010 at 7:09 am
What do you mean by "update statement is successful".
Updates 1 row? >= 1 row? Doesn't throw an error?
April 6, 2010 at 7:22 am
I mean like
if
update tbl1 set col1='Y' where col2='xyz'
updates one or more rows, then i need to execute one set of statements. if this doesn't update any rows, then i need to execute other set of statements. how can this be achieved?
April 6, 2010 at 7:30 am
bndgyawali (4/6/2010)
I mean likeif
update tbl1 set col1='Y' where col2='xyz'
updates one or more rows, then i need to execute one set of statements. if this doesn't update any rows, then i need to execute other set of statements. how can this be achieved?
Use the OUTPUT Clause:
DECLARE @Example
TABLE (col1 INTEGER NOT NULL);
INSERT @Example (col1) VALUES (1);
DECLARE @UpdatedRows
TABLE (col1 INTEGER NOT NULL);
BEGIN TRY
BEGIN TRANSACTION;
UPDATE @Example
SET col1 = col1 + 1
OUTPUT inserted.col1
INTO @UpdatedRows (col1)
WHERE col1 = 1;
IF EXISTS
(
SELECT *
FROM @UpdatedRows
)
BEGIN
-- Do other stuff
PRINT 'Update succeeded, doing other stuff...'
END;
COMMIT TRANSACTION;
END TRY
BEGIN CATCH
IF XACT_STATE() <> 0 ROLLBACK TRANSACTION;
PRINT 'Error updating. Details follow:'
PRINT ERROR_MESSAGE();
END CATCH;
April 6, 2010 at 1:14 pm
You can also use @@ROWCOUNT UPDATE MyTable
SET Col1 = Val1 ...
IF @@ROWCOUNT > 0
BEGIN
-- DO other updates
ENDIf you have triggers on your table thay could affect the value of @@ROWCOUNT, so just be ware of that.
April 6, 2010 at 1:42 pm
Lamprey13 (4/6/2010)
If you have triggers on your table thay could affect the value of @@ROWCOUNT, so just be aware of that.
There are a number of scenarios where @@ROWCOUNT may produce unexpected results (MERGE being another example). The trigger issue is why I posted the OUTPUT clause solution - you never know when someone might add a trigger...
April 6, 2010 at 10:11 pm
Thanks everybody for your warm suggestions. I used output inserted.col1 for my task.
I tried to use @@rowcount but it returned 0 for update statement. So, i couldn't use this.
Thanks again to all.
April 6, 2010 at 10:17 pm
Thanks for the feedback.
Viewing 9 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply