| 1 | use gringlobal;
|
|---|
| 2 |
|
|---|
| 3 | /* QUESTIONS
|
|---|
| 4 | * Relink note?
|
|---|
| 5 | * AL cooperator?
|
|---|
| 6 | */
|
|---|
| 7 |
|
|---|
| 8 | begin transaction
|
|---|
| 9 |
|
|---|
| 10 | begin try
|
|---|
| 11 |
|
|---|
| 12 | print('')
|
|---|
| 13 | print('copying system inventory names to physical inventory')
|
|---|
| 14 |
|
|---|
| 15 | insert into accession_inv_name
|
|---|
| 16 | (inventory_id, category_code, plant_name, plant_name_rank, owned_by, owned_date, created_by, created_date)
|
|---|
| 17 | select
|
|---|
| 18 | pi.inventory_id,
|
|---|
| 19 | an.category_code,
|
|---|
| 20 | an.plant_name,
|
|---|
| 21 | an.plant_name_rank,
|
|---|
| 22 | 184963, --DM
|
|---|
| 23 | GETDATE(),
|
|---|
| 24 | 184963, --DM
|
|---|
| 25 | GETDATE()
|
|---|
| 26 | from accession_inv_group_map as gm
|
|---|
| 27 | join accession_inv_group as g
|
|---|
| 28 | on g.accession_inv_group_id = gm.accession_inv_group_id
|
|---|
| 29 | join inventory as si
|
|---|
| 30 | on si.inventory_id = gm.inventory_id
|
|---|
| 31 | join inventory as pi
|
|---|
| 32 | on pi.accession_id = si.accession_id
|
|---|
| 33 | and pi.inventory_id != si.inventory_id
|
|---|
| 34 | join accession_inv_name as an
|
|---|
| 35 | on an.inventory_id = si.inventory_id
|
|---|
| 36 | join cooperator as c
|
|---|
| 37 | on c.cooperator_id = pi.owned_by
|
|---|
| 38 | where g.group_name = 'irri'
|
|---|
| 39 | and si.accession_id != 2142808
|
|---|
| 40 | and si.form_type_code = '**'
|
|---|
| 41 | and c.site_id = 42
|
|---|
| 42 |
|
|---|
| 43 | print('')
|
|---|
| 44 | print('copying accession taxonomy to inventory names')
|
|---|
| 45 |
|
|---|
| 46 | insert into accession_inv_name
|
|---|
| 47 | (inventory_id, category_code, note, plant_name, owned_by, owned_date, created_by, created_date)
|
|---|
| 48 | select
|
|---|
| 49 | i.inventory_id,
|
|---|
| 50 | 'OTHER',
|
|---|
| 51 | 'Requested taxonomy',
|
|---|
| 52 | ts.name,
|
|---|
| 53 | 184963, --DM
|
|---|
| 54 | GETDATE(),
|
|---|
| 55 | 184963, --DM
|
|---|
| 56 | GETDATE()
|
|---|
| 57 | from (
|
|---|
| 58 | select distinct i.*
|
|---|
| 59 | from inventory as si
|
|---|
| 60 | join accession_inv_group_map as gm
|
|---|
| 61 | on gm.inventory_id = si.inventory_id
|
|---|
| 62 | join accession_inv_group as g
|
|---|
| 63 | on g.accession_inv_group_id = gm.accession_inv_group_id
|
|---|
| 64 | join inventory as i
|
|---|
| 65 | on i.accession_id = si.accession_id
|
|---|
| 66 | join cooperator as c
|
|---|
| 67 | on c.cooperator_id = i.owned_by
|
|---|
| 68 | where g.group_name = 'irri'
|
|---|
| 69 | and si.form_type_code = '**'
|
|---|
| 70 | and si.accession_id != 2142808
|
|---|
| 71 | and i.inventory_id != si.inventory_id
|
|---|
| 72 | and c.site_id = 42
|
|---|
| 73 | ) as i
|
|---|
| 74 | join accession as a
|
|---|
| 75 | on a.accession_id = i.accession_id
|
|---|
| 76 | join taxonomy_species as ts
|
|---|
| 77 | on ts.taxonomy_species_id = a.taxonomy_species_id
|
|---|
| 78 |
|
|---|
| 79 | print('')
|
|---|
| 80 | print('adding relink actions to physical inventory')
|
|---|
| 81 |
|
|---|
| 82 | insert into inventory_action
|
|---|
| 83 | (inventory_id, action_name_code, note, cooperator_id, completed_date, owned_by, owned_date, created_by, created_date)
|
|---|
| 84 | select
|
|---|
| 85 | i.inventory_id,
|
|---|
| 86 | 'RELINKED',
|
|---|
| 87 | 'Relinked from '
|
|---|
| 88 | + a.accession_number_part1
|
|---|
| 89 | + coalesce(' ' + convert(nvarchar, a.accession_number_part2), '')
|
|---|
| 90 | + coalesce(' ' + a.accession_number_part3, '')
|
|---|
| 91 | + ' to BLACKBOX 15655',
|
|---|
| 92 | 177589, --CLM
|
|---|
| 93 | GETDATE(),
|
|---|
| 94 | 184963, --DM
|
|---|
| 95 | GETDATE(),
|
|---|
| 96 | 184963, --DM
|
|---|
| 97 | GETDATE()
|
|---|
| 98 | from (
|
|---|
| 99 | select distinct i.*
|
|---|
| 100 | from inventory as si
|
|---|
| 101 | join accession_inv_group_map as gm
|
|---|
| 102 | on gm.inventory_id = si.inventory_id
|
|---|
| 103 | join accession_inv_group as g
|
|---|
| 104 | on g.accession_inv_group_id = gm.accession_inv_group_id
|
|---|
| 105 | join inventory as i
|
|---|
| 106 | on i.accession_id = si.accession_id
|
|---|
| 107 | join cooperator as c
|
|---|
| 108 | on c.cooperator_id = i.owned_by
|
|---|
| 109 | where g.group_name = 'irri'
|
|---|
| 110 | and si.form_type_code = '**'
|
|---|
| 111 | and si.accession_id != 2142808
|
|---|
| 112 | and i.inventory_id != si.inventory_id
|
|---|
| 113 | and c.site_id = 42
|
|---|
| 114 | ) as i
|
|---|
| 115 | join accession as a
|
|---|
| 116 | on a.accession_id = i.accession_id
|
|---|
| 117 |
|
|---|
| 118 | print('')
|
|---|
| 119 | print('inactivating old accessions')
|
|---|
| 120 |
|
|---|
| 121 | update accession set
|
|---|
| 122 | status_code = 'INACTIVE',
|
|---|
| 123 | modified_by = 177589, --CLM
|
|---|
| 124 | modified_date = GETDATE()
|
|---|
| 125 | from (
|
|---|
| 126 | select distinct i.accession_id
|
|---|
| 127 | from accession_inv_group_map as gm
|
|---|
| 128 | join accession_inv_group as g
|
|---|
| 129 | on g.accession_inv_group_id = gm.accession_inv_group_id
|
|---|
| 130 | join inventory as i
|
|---|
| 131 | on i.inventory_id = gm.inventory_id
|
|---|
| 132 | join cooperator as c
|
|---|
| 133 | on c.cooperator_id = i.owned_by
|
|---|
| 134 | where g.group_name = 'irri'
|
|---|
| 135 | and i.accession_id != 2142808
|
|---|
| 136 | and i.form_type_code = '**'
|
|---|
| 137 | and c.site_id = 42
|
|---|
| 138 | ) as targets
|
|---|
| 139 | where accession.accession_id = targets.accession_id
|
|---|
| 140 |
|
|---|
| 141 | print('')
|
|---|
| 142 | print('adding blackbox group')
|
|---|
| 143 |
|
|---|
| 144 | insert into accession_inv_group_map
|
|---|
| 145 | (inventory_id, accession_inv_group_id, owned_by, owned_date, created_by, created_date)
|
|---|
| 146 | select
|
|---|
| 147 | i.inventory_id,
|
|---|
| 148 | gi.accession_inv_group_id,
|
|---|
| 149 | 184963, --DM
|
|---|
| 150 | GETDATE(),
|
|---|
| 151 | 184963, --DM
|
|---|
| 152 | GETDATE()
|
|---|
| 153 | from (
|
|---|
| 154 | select distinct i.*
|
|---|
| 155 | from inventory as si
|
|---|
| 156 | join accession_inv_group_map as gm
|
|---|
| 157 | on gm.inventory_id = si.inventory_id
|
|---|
| 158 | join accession_inv_group as g
|
|---|
| 159 | on g.accession_inv_group_id = gm.accession_inv_group_id
|
|---|
| 160 | join inventory as i
|
|---|
| 161 | on i.accession_id = si.accession_id
|
|---|
| 162 | join cooperator as c
|
|---|
| 163 | on c.cooperator_id = i.owned_by
|
|---|
| 164 | where g.group_name = 'irri'
|
|---|
| 165 | and si.form_type_code = '**'
|
|---|
| 166 | and si.accession_id != 2142808
|
|---|
| 167 | and i.inventory_id != si.inventory_id
|
|---|
| 168 | and c.site_id = 42
|
|---|
| 169 | ) as i
|
|---|
| 170 | join accession_inv_group as gi
|
|---|
| 171 | on gi.group_name = 'blackbox'
|
|---|
| 172 | where i.inventory_id not in (
|
|---|
| 173 | select gm_.inventory_id
|
|---|
| 174 | from accession_inv_group_map as gm_
|
|---|
| 175 | join accession_inv_group as g_
|
|---|
| 176 | on g_.accession_inv_group_id = gm_.accession_inv_group_id
|
|---|
| 177 | where g_.group_name = 'blackbox'
|
|---|
| 178 | )
|
|---|
| 179 |
|
|---|
| 180 | print('')
|
|---|
| 181 | print('adding irri group')
|
|---|
| 182 |
|
|---|
| 183 | insert into accession_inv_group_map
|
|---|
| 184 | (inventory_id, accession_inv_group_id, owned_by, owned_date, created_by, created_date)
|
|---|
| 185 | select
|
|---|
| 186 | i.inventory_id,
|
|---|
| 187 | gi.accession_inv_group_id,
|
|---|
| 188 | 184963, --DM
|
|---|
| 189 | GETDATE(),
|
|---|
| 190 | 184963, --DM
|
|---|
| 191 | GETDATE()
|
|---|
| 192 | from (
|
|---|
| 193 | select distinct i.*
|
|---|
| 194 | from inventory as si
|
|---|
| 195 | join accession_inv_group_map as gm
|
|---|
| 196 | on gm.inventory_id = si.inventory_id
|
|---|
| 197 | join accession_inv_group as g
|
|---|
| 198 | on g.accession_inv_group_id = gm.accession_inv_group_id
|
|---|
| 199 | join inventory as i
|
|---|
| 200 | on i.accession_id = si.accession_id
|
|---|
| 201 | join cooperator as c
|
|---|
| 202 | on c.cooperator_id = i.owned_by
|
|---|
| 203 | where g.group_name = 'irri'
|
|---|
| 204 | and si.form_type_code = '**'
|
|---|
| 205 | and si.accession_id != 2142808
|
|---|
| 206 | and i.inventory_id != si.inventory_id
|
|---|
| 207 | and c.site_id = 42
|
|---|
| 208 | ) as i
|
|---|
| 209 | join accession_inv_group as gi
|
|---|
| 210 | on gi.group_name = 'irri'
|
|---|
| 211 | where i.inventory_id not in (
|
|---|
| 212 | select gm_.inventory_id
|
|---|
| 213 | from accession_inv_group_map as gm_
|
|---|
| 214 | join accession_inv_group as g_
|
|---|
| 215 | on g_.accession_inv_group_id = gm_.accession_inv_group_id
|
|---|
| 216 | where g_.group_name = 'irri'
|
|---|
| 217 | )
|
|---|
| 218 |
|
|---|
| 219 | print('')
|
|---|
| 220 | print('relinking inventory')
|
|---|
| 221 |
|
|---|
| 222 | -- relink
|
|---|
| 223 | update inventory set
|
|---|
| 224 | accession_id = 2142808,
|
|---|
| 225 | modified_by = 184963,
|
|---|
| 226 | modified_date = GETDATE()
|
|---|
| 227 | from (
|
|---|
| 228 | select distinct i.*
|
|---|
| 229 | from inventory as si
|
|---|
| 230 | join accession_inv_group_map as gm
|
|---|
| 231 | on gm.inventory_id = si.inventory_id
|
|---|
| 232 | join accession_inv_group as g
|
|---|
| 233 | on g.accession_inv_group_id = gm.accession_inv_group_id
|
|---|
| 234 | join inventory as i
|
|---|
| 235 | on i.accession_id = si.accession_id
|
|---|
| 236 | join cooperator as c
|
|---|
| 237 | on c.cooperator_id = i.owned_by
|
|---|
| 238 | where g.group_name = 'irri'
|
|---|
| 239 | and si.form_type_code = '**'
|
|---|
| 240 | and si.accession_id != 2142808
|
|---|
| 241 | and i.inventory_id != si.inventory_id
|
|---|
| 242 | and c.site_id = 42
|
|---|
| 243 | ) as targets
|
|---|
| 244 | where inventory.inventory_id = targets.inventory_id
|
|---|
| 245 |
|
|---|
| 246 | print('')
|
|---|
| 247 | print('removing old accessions from blackbox group')
|
|---|
| 248 |
|
|---|
| 249 | delete from accession_inv_group_map
|
|---|
| 250 | where accession_inv_group_map_id in (
|
|---|
| 251 | select gm.accession_inv_group_map_id
|
|---|
| 252 | from accession_inv_group_map as gm
|
|---|
| 253 | join accession_inv_group as g
|
|---|
| 254 | on g.accession_inv_group_id = gm.accession_inv_group_id
|
|---|
| 255 | join accession_inv_group_map as bm
|
|---|
| 256 | on bm.inventory_id = gm.inventory_id
|
|---|
| 257 | join accession_inv_group as b
|
|---|
| 258 | on b.accession_inv_group_id = bm.accession_inv_group_id
|
|---|
| 259 | join inventory as i
|
|---|
| 260 | on i.inventory_id = gm.inventory_id
|
|---|
| 261 | join cooperator as c
|
|---|
| 262 | on c.cooperator_id = i.owned_by
|
|---|
| 263 | where i.form_type_code = '**'
|
|---|
| 264 | and i.accession_id != 2142808
|
|---|
| 265 | and c.site_id = 42
|
|---|
| 266 | and g.group_name = 'blackbox'
|
|---|
| 267 | and b.group_name = 'irri'
|
|---|
| 268 | )
|
|---|
| 269 |
|
|---|
| 270 | print('')
|
|---|
| 271 | print('removing old accessions from irri group')
|
|---|
| 272 |
|
|---|
| 273 | delete from accession_inv_group_map
|
|---|
| 274 | where accession_inv_group_map_id in (
|
|---|
| 275 | select gm.accession_inv_group_map_id
|
|---|
| 276 | from accession_inv_group_map as gm
|
|---|
| 277 | join accession_inv_group as g
|
|---|
| 278 | on g.accession_inv_group_id = gm.accession_inv_group_id
|
|---|
| 279 | join inventory as i
|
|---|
| 280 | on i.inventory_id = gm.inventory_id
|
|---|
| 281 | join cooperator as c
|
|---|
| 282 | on c.cooperator_id = i.owned_by
|
|---|
| 283 | where g.group_name = 'irri'
|
|---|
| 284 | and i.form_type_code = '**'
|
|---|
| 285 | and i.accession_id != 2142808
|
|---|
| 286 | and c.site_id = 42
|
|---|
| 287 | )
|
|---|
| 288 |
|
|---|
| 289 | end try begin catch
|
|---|
| 290 |
|
|---|
| 291 | print ERROR_MESSAGE()
|
|---|
| 292 |
|
|---|
| 293 | rollback transaction
|
|---|
| 294 |
|
|---|
| 295 | end catch
|
|---|
| 296 | |
|---|