sql server - SQL UPDATE saving expression NOT updating -
i have should simple statement in stored procedure increment value +10. however, if run sp more 1 time value not alter after first call. when modifiy increment value +20 resulting table values +20 original values set in table.
use [db] go set ansi_nulls on go set quoted_identifier on go alter procedure [dbo].[spupdatecareerrankfrominterviewbyid]( -- add together parameters stored procedure here @studentid int ) begin delete studentcategoryranktable studentid = @studentid insert studentcategoryranktable (studentid, occupationalcategoryid, categoryweightedtotal) select studentid, occupationalcategoryid, sum(weightedresponse) weightedresponsetotal dbo.weightedinterviewresponsesview grouping studentid, occupationalcategoryid having (studentid = @studentid) order weightedresponsetotal desc -- determine whether pupil wants pursue declare @certificateonlyquestionoptionid integer = 250 declare @highmathquestionoptionid integer = 43 declare @highreadingquestionoptionid integer = 47 declare @highwritingquestionoptionid integer = 51 declare @certificateonlyoptionanswered integer = null declare @highmathquestionoptionanswered integer = null declare @highreadingquestionoptionanswered integer = null declare @highwritingquestionoptionanswered integer = null -- returns 0 if exec @certificateonlyoptionanswered = dbo.chosequestionoptionid @studentid = @studentid, @questionoptionid = @certificateonlyquestionoptionid exec @highmathquestionoptionanswered = dbo.chosequestionoptionid @studentid = @studentid, @questionoptionid = @highmathquestionoptionid exec @highreadingquestionoptionanswered = dbo.chosequestionoptionid @studentid = @studentid, @questionoptionid = @highreadingquestionoptionid exec @highwritingquestionoptionanswered = dbo.chosequestionoptionid @studentid = @studentid, @questionoptionid = @highwritingquestionoptionid -- determine if pupil wants certificate / non-degree if @certificateonlyoptionanswered != 0 begin insert dbo.testertable (testcolumn) values ('certificate true') -- high math, add together weight scores -- 3: business -- 5: computer technologies -- 7: engineering science technology -- 18: sciences , math if @highmathquestionoptionanswered != 0 begin insert dbo.testertable (testcolumn) values ('high math') update dbo.studentcategoryranktable set categoryweightedtotal += 10 studentid = @studentid , occupationalcategoryid in (3, 5, 7, 18) end -- high reading/writing, add together weight scores -- 4: communications -- 10: humanities -- 19: social , behavioral sciences if @highreadingquestionoptionanswered != 0 or @highwritingquestionoptionanswered != 0 begin insert dbo.testertable (testcolumn) values ('high reading/writing') update dbo.studentcategoryranktable set categoryweightedtotal += 10 studentid = @studentid , (occupationalcategoryid = 4 or occupationalcategoryid = 10 or occupationalcategoryid = 19) end end else begin insert dbo.testertable (testcolumn) values ('failed certificate only') end end
the procedure posted first removes entries in studentcategoryranktable
inserts new ones. new records updated. if run procedure again, delete
updated records , started.
sql sql-server
No comments:
Post a Comment