|
tedeansiii
Starting Member
2 Posts |
Posted - 2012-07-05 : 11:58:21
|
| I am a new Database Administrator but i have a some mediocre knowledge of ASP, VBScript programming an have dabbled a little bit with SQL, mysql and powershell, So some things I do know. Triggers are not one of them. Our company is changing our main system for everything and we needed an old RPG program which my boss wrote to be converted to SQL. I got the task. I have finished the script but the catch is it needs to be in the form of a trigger. Basically we have a table which has custom columns, two of which one is a sequence number which is a default of a 1 for everyone, the second is a barcode number which we create from their user id and the sequence number. we need the trigger to update the barcode number anytime the sequence number changes.Can anyone help me with this? I have read about triggers and tried to get it to work as a trigger with no luck.Below is the script:Can someone help me with turning this into a trigger? I have never written a trigger so this will be my first one. Thanks--------- Begin Code -----------Platform: Microsoft SQL Server Script--Purpose Written: Calculate Barcode for Aramark Systems ID Cards.--Written by: Thomas E. Deans III--Date Written: 6-10-2012declare @seq_num as int-- Pull the newly inserted/updated sequence number from the-- udf table it is being-- updated/inserted into when triggered by -- -- change. Update the barcode via this triggerset @seq_num = ((select inserted.BCN_BAR_CDE_SEQ from TmsEPrd..NAME_MASTER_UDF nmu join inserted i on nmu.BCN_BAR_CDE = i.BCN_BAR_CDE) * 10) -- Set variable equal to base numberdeclare @14_long as bigintset @14_long = 20000000000000 -- Pull id num from current row being updated by triggerdeclare @id_num as bigintset @id_num = ((select inserted.ID_NUM from tmseprd..NAME_MASTER_UDF nmu join inserted i on nmu.ID_NUM = i.id_num where len(ID_NUM) >= 6 and LEN(id_num) < 7) * 10000) -- Add up for the base number = base number + calculated id num + calculated sequence numberdeclare @temp as bigintdeclare @temp_2 as bigintset @temp = (@14_long + @id_num + @seq_num)set @temp_2 = @temp -- Declare variables for digits 02, 04, 06, 08, 10 & 12 of the 14 digit @temp number, -- and store them in variables.declare @var_pos_02 as intdeclare @var_pos_04 as intdeclare @var_pos_06 as intdeclare @var_pos_08 as intdeclare @var_pos_10 as intdeclare @var_pos_12 as intdeclare @even_total as int -- Setting values for declared variables.set @var_pos_02 = right(left(@temp_2,2),1)set @var_pos_04 = right(left(@temp_2,4),1)set @var_pos_06 = right(left(@temp_2,6),1)set @var_pos_08 = right(left(@temp_2,8),1)set @var_pos_10 = right(left(@temp_2,10),1)set @var_pos_12 = right(left(@temp_2,12),1)set @even_total = (@var_pos_02 + @var_pos_04 + @var_pos_06 + @var_pos_08 + @var_pos_10 + @var_pos_12) -- Declare variables for digits 01, 03, 05, 07, 09, 11, & 13 of the 14 digit @temp number, -- and store them in variables.declare @var_pos_01 as intdeclare @var_pos_03 as intdeclare @var_pos_05 as intdeclare @var_pos_07 as intdeclare @var_pos_09 as intdeclare @var_pos_11 as intdeclare @var_pos_13 as int -- Setting values for declared variables.set @var_pos_01 = right(left(@temp_2,1),1)set @var_pos_03 = right(left(@temp_2,3),1)set @var_pos_05 = right(left(@temp_2,5),1)set @var_pos_07 = right(left(@temp_2,7),1)set @var_pos_09 = right(left(@temp_2,9),1)set @var_pos_11 = right(left(@temp_2,11),1)set @var_pos_13 = right(left(@temp_2,13),1) -- Declare variables to work with individual odd digits.declare @odd_register_01 as intdeclare @odd_register_03 as intdeclare @odd_register_05 as intdeclare @odd_register_07 as intdeclare @odd_register_09 as intdeclare @odd_register_11 as intdeclare @odd_register_13 as int -- Set values for all odd variable digits.set @odd_register_01 = (@var_pos_01 * 2)set @odd_register_03 = (@var_pos_03 * 2)set @odd_register_05 = (@var_pos_05 * 2)set @odd_register_07 = (@var_pos_07 * 2)set @odd_register_09 = (@var_pos_09 * 2)set @odd_register_11 = (@var_pos_11 * 2)set @odd_register_13 = (@var_pos_13 * 2) -- Declare variables to work with left and right digits of odd digits in barcode in case statements two sections down.declare @odd_register_01_tmp as intdeclare @odd_register_03_tmp as intdeclare @odd_register_05_tmp as intdeclare @odd_register_07_tmp as intdeclare @odd_register_09_tmp as intdeclare @odd_register_11_tmp as intdeclare @odd_register_13_tmp as int -- Declare variables to total left and right didit of odd digits in barcode number.declare @odd_register_01_tot as intdeclare @odd_register_03_tot as intdeclare @odd_register_05_tot as intdeclare @odd_register_07_tot as intdeclare @odd_register_09_tot as intdeclare @odd_register_11_tot as intdeclare @odd_register_13_tot as int -- Set the odd tmp variable equal to the odd digit of the barcode.-- Example of what we're doing here: @odd_register_01 might be a 9, (9*2) = 18. We are adding the number in the first digit to the -- number in the second digit for calculating a check digit. The first digit is 1, the second is 8, -- since the whole number in @odd_register_01 can never be above 18 we check the first digits number for a 1, anything else will-- always be an even number because the number you start with is multiplied by 2. A single digit in any mathematical placeholder -- can never be larger than a 9, the lowest number we can have is 0, if there is not a 1 in the first digit it could contain a -- 2, 4, 6, or 8, and the highest we can have is 18. (example 9 multiplied by 2). If the first digit contains a 1 we want to add -- the first digit to the second, if it's not a 1 then we want to just keep the value in the first digits placeholder.-- ------------------------------------------------------------------------------------------------------------------------------- Case Statement: In @odd_register_01 give me the left digit, check for 1, if 1 then add the first digit plus second digit, else -- give me first digit-- required the use of a (cast as int) because even though we are working with numbers left() and right() are varchar functions.-- trying to add the left digit and right digit without cast as int returned 44 for an @odd_register_ of 4 instead of 8 and 18 instead of 9.set @odd_register_01_tmp = @odd_register_01set @odd_register_01_tot = case when cast(left(@odd_register_01,1) as int) = 1 then ((cast(left(@odd_register_01,1) as int)) + (cast(RIGHT(left(@odd_register_01,2),1) as int))) else cast(left(@odd_register_01,1) as int) endset @odd_register_03_tmp = @odd_register_03set @odd_register_03_tot = case when cast(left(@odd_register_03,1) as int) = 1 then ((cast(left(@odd_register_03,1) as int)) + (cast(RIGHT(left(@odd_register_03,2),1) as int))) else cast(left(@odd_register_03,1) as int) endset @odd_register_05_tmp = @odd_register_05set @odd_register_05_tot = case when cast(left(@odd_register_05,1) as int) = 1 then ((cast(left(@odd_register_05,1) as int)) + (cast(RIGHT(left(@odd_register_05,2),1) as int))) else cast(left(@odd_register_05,1) as int) endset @odd_register_07_tmp = @odd_register_07set @odd_register_07_tot = case when cast(left(@odd_register_07,1) as int) = 1 then ((cast(left(@odd_register_07,1) as int)) + (cast(RIGHT(left(@odd_register_07,2),1) as int))) else cast(left(@odd_register_07,1) as int) endset @odd_register_09_tmp = @odd_register_09set @odd_register_09_tot = case when cast(left(@odd_register_09,1) as int) = 1 then ((cast(left(@odd_register_09,1) as int)) + (cast(RIGHT(left(@odd_register_09,2),1) as int))) else cast(left(@odd_register_09,1) as int) endset @odd_register_11_tmp = @odd_register_11set @odd_register_11_tot = case when cast(left(@odd_register_11,1) as int) = 1 then ((cast(left(@odd_register_11,1) as int)) + (cast(RIGHT(left(@odd_register_11,2),1) as int))) else cast(left(@odd_register_11,1) as int) endset @odd_register_13_tmp = @odd_register_13set @odd_register_13_tot = case when cast(left(@odd_register_13,1) as int) = 1 then ((cast(left(@odd_register_13,1) as int)) + (cast(RIGHT(left(@odd_register_13,2),1) as int))) else cast(left(@odd_register_13,1) as int) end -- Declare calculated variables.declare @total as bigintdeclare @processed_odd_total as bigintdeclare @total_2 as bigintdeclare @final_total as bigint -- Add processed odd variables together for a total odd digit sum number.set @processed_odd_total = (@odd_register_01_tot + @odd_register_03_tot + @odd_register_05_tot + @odd_register_07_tot + @odd_register_09_tot + @odd_register_11_tot + @odd_register_13_tot)-- Add total for sum of odd and sum of evenset @total = (@even_total + @processed_odd_total)-- The total for sum of odd + sum of even can be a 3 digit number,-- so we want the right most digit 1 digit to the left in 3 digit number.set @total_2 = right(left(@total,3),1)-- Set the final calculated sequence number to be 10 minus the last number we calculated.set @final_total = (10 - @total_2)-- Set the final calculated id_num to be equal to the last number plus the original @temp.set @temp = (@temp_2 + @final_total)Print ' 'print 'Final Barcode Number'-- Update the field in the bcn_name_master_udf for id_num.update TmsEPrd..NAME_MASTER_UDF nmu insert @temp into bcn_bar_cde join inserted i on nmu.id_num = i.id_num--------- End Code ---------I know that some of the lines that deal with setting a variable aren't correct, i wrote them pseudo so you could understand what I was trying to do.The subtle is often profound. |
|