tags:

views:

73

answers:

2

I write stored procedure for update.

create procedure sp_videos_updateviews (@videoid varchar(50),@ret int output)
as
   set @ret = update videos set [views]=[views]+1 
   where videoid = @videoid

   if(@ret>1)
   begin
     set @ret=1
   end
   else
   begin
     set @ret=0
   end

but it is giving error like this

Incorrect syntax near the keyword 'update'.

+6  A: 

You can't set a variable to an UPDATE statement. If you are trying to get the row count at the end of the update, try this:

UPDATE vidoes SET [views] = [views] + 1
WHERE videoid = @videoid
SET @ret = @@ROWCOUNT
David M
i not get you Mr.David M
Surya sasidhar
The first line of your procedure attempts to set the value of an `int` variable to be an update statement. I think what you want to set is the number of rows affected by the update statement. That is what the code I have given you does.
David M
ok then code will update videos set [views]=[views]+1 set @ret= (select count(*) from videoswhere videoid=@videoid) if(@ret>1)set @ret=1elseset@ret=0 am i right
Surya sasidhar
No, just replace the one incorrect line in your stored procedure with the two lines in my answer. The rest can stay as it is.
David M
Sorry, realised my snippet omitted the WHERE clause...
David M
Ya Mr.David I got it thank you for response.
Surya sasidhar
+2  A: 

Surya,

From what I gather you are looking to return from the proc a 1 or 0 depending on if any records were updated from your proc. I agree that @@ROWCOUNT is where you should go, try this code:

create procedure proc_videos_updateviews (@videoid varchar(50),@ret int output) 
as 

update videos set [views]=[views]+1
where videoid = @videoid

if(@@ROWCOUNT > 0)
begin
    set @ret = 1
end
else
begin
    set @ret = 0
end
Tj Kellie
Thank you Mr.Tj Kellie it is working fine
Surya sasidhar