mysql - Update query is not working in my sql procedure.why? -


create procedure sp_iu_group(     gid int,     groupname nvarchar(200),     userid int,     status int  ) begin if gid=0     insert tblgroup (groupname,userid,status)             values (groupname,userid,status); else         update tblgroup set groupname=groupname,userid=userid,status=status gid=gid; end if; end 

this query:

update tblgroup set groupname=groupname,userid=userid,status=status gid=gid 

will update every record in table... to itself. matches every record, because always true:

where gid=gid 

and updates value to itself:

groupname=groupname 

the problem you're using the same names multiple things. give things different names. simple this:

create procedure sp_iu_group(     gidnew int,     groupnamenew nvarchar(200),     useridnew int,     statusnew int ) 

(or use other standard want distinguish variables database objects, such prepending them special character @.)

then query can tell difference:

update tblgroup set groupname=groupnamenew,userid=useridnew,status=statusnew gid=gidnew 

(modify rest of stored procedure new variable names accordingly, of course.)

basically, general rule of thumb, never rely on code "know meant". explicit , unambiguous.


Comments

Popular posts from this blog

serialization - Convert Any type in scala to Array[Byte] and back -

matplotlib support failed in PyCharm on OSX -

python - Matplotlib: TypeError: 'AxesSubplot' object is not callable -