· 9 years ago · May 11, 2017, 07:12 PM
1/* SafeBaby TwoPointOh update from prior install script */
2
3/* SysInfo.Schema.sql */
4
5
6BEGIN Try
7/****** Script for SelectTopNRows command from SSMS ******/
8declare @tmpDupesAgain table (AltIdent varchar(50), MedicalRecordNum varchar(50), MotherID bigint );
9insert into @tmpDupesAgain (AltIdent, MedicalRecordNum, MotherID)
10
11select pdm.AltIdent , mm.MedicalRecordNum, mm.motherid from
12(SELECT
13 count([MedicalRecordNum]) cnt
14 ,[AltIdent]
15 FROM [dbo].[tblMothers] group by AltIdent) pdm
16 inner join [dbo].[tblMothers] mm on pdm.AltIdent = mm.AltIdent
17 where pdm.cnt > 1 and mm.AltIdent is not null;
18
19 declare @tmpKeepers table (AltIdent varchar(50), MedicalRecordNum varchar(50), MotherID bigint);
20 insert into @tmpKeepers (AltIdent , MedicalRecordNum, MotherID)
21 select da.altident , da.MedicalRecordNum, da.MotherID from
22 (select MAX(MotherID) cntID, AltIdent
23 from @tmpDupesAgain group by AltIdent ) kp
24 inner join @tmpDupesAgain da on kp.cntID = da.MotherID;
25
26 declare @tmpNotKeepers table (AltIdent varchar(50), MedicalRecordNum varchar(50), MotherID bigint);
27 insert into @tmpNotKeepers (AltIdent , MedicalRecordNum, MotherID)
28 select a , b, c from
29 (select da.AltIdent a, da.medicalRecordNum b, da.MotherID c, kp.MotherID o
30 from @tmpDupesAgain da left join @tmpKeepers kp on da.MotherID = kp.motherid where kp.MotherID is null) nk;
31
32 Declare @tmpMRNTrans table (OldMRN varchar(50),AltIdent varchar(50), NewMRN varchar(50))
33 insert into @tmpMRNTrans (OldMRN, AltIdent, NewMRN)
34 Select nk.MedicalRecordNum OldMRN, nk.AltIdent, kp.MedicalRecordNum NewMRN
35 from @tmpNotKeepers nk inner join @tmpKeepers kp on nk.AltIdent = kp.AltIdent;
36
37update tblBottles set MotherMRN = NewMRN from
38 tblBottles a inner join @tmpMRNTrans b on a.MotherMRN = b.OldMRN;
39
40update tblLocations set BinAssignment = NewMRN from
41 tblLocations a inner join @tmpMRNTrans b on a.BinAssignment = b.OldMRN;
42
43update tblPatients set MotherMRN = NewMRN from
44 tblPatients a inner join @tmpMRNTrans b on a.MotherMRN = b.OldMRN;
45
46delete from tblMothers where MedicalRecordNum in (select OldMRN from @tmpMRNTrans);
47
48END Try
49BEGIN CATCH
50 PRINT N'Error CONSOLIDATING MOTHERS'
51END CATCH
52GO
53
54BEGIN TRY
55
56declare @tmpDupes table (MedicalRecordNum varchar(50));
57insert into @tmpDupes (MedicalRecordNum)
58SELECT
59 pt.[MedicalRecordNum]
60FROM [dbo].[tblPatients] pt inner join [dbo].[tblMothers] mt on pt.MedicalRecordNum = mt.MedicalRecordNum;
61
62update dbo.tblBottles set MotherMRN = MotherMRN + '-x' where MotherMRN in (
63SELECT * from @tmpDupes);
64
65update dbo.tblLocations set BinAssignment = BinAssignment + '-x' where BinAssignment in (
66SELECT * from @tmpDupes);
67
68update dbo.tblPatients set MotherMRN = MotherMRN + '-x' where MotherMRN in (
69SELECT * from @tmpDupes);
70
71update dbo.tblMothers set MedicalRecordNum = MedicalRecordNum + '-x' where MedicalRecordNum in (
72SELECT * from @tmpDupes);
73
74
75END TRY
76BEGIN CATCH
77 PRINT N'Error Removing Duplicate MRNS'
78END CATCH
79GO
80
81BEGIN TRY
82IF NOT EXISTS (
83SELECT schema_name
84FROM information_schema.schemata
85WHERE schema_name = 'SysInfo' )
86
87BEGIN
88 EXECUTE ('CREATE SCHEMA [SysInfo] AUTHORIZATION [dbo]')
89END
90
91 PRINT N'Schema SysInfo created '
92
93END TRY
94BEGIN CATCH
95 PRINT N'Schema SysInfo failed'
96 PRINT 'Error Number: ' + str(error_number())
97 PRINT 'Line Number: ' + str(error_line())
98 PRINT error_message()
99END CATCH
100GO
101
102
103BEGIN TRY
104
105IF NOT EXISTS (
106SELECT schema_name
107FROM information_schema.schemata
108WHERE schema_name = 'PTinfo' )
109
110BEGIN
111 EXECUTE ('CREATE SCHEMA [PTinfo] AUTHORIZATION [dbo]')
112END
113
114 PRINT N'Schema PTinfo created '
115
116END TRY
117BEGIN CATCH
118 PRINT N'Schema PTinfo failed'
119 PRINT 'Error Number: ' + str(error_number())
120 PRINT 'Line Number: ' + str(error_line())
121 PRINT error_message()
122END CATCH
123GO
124
125
126BEGIN TRY
127
128IF NOT EXISTS (
129SELECT schema_name
130FROM information_schema.schemata
131WHERE schema_name = 'Storage' )
132
133BEGIN
134 EXECUTE ('CREATE SCHEMA [Storage] AUTHORIZATION [dbo]')
135END
136
137 PRINT N'Schema Storage created '
138
139END TRY
140BEGIN CATCH
141 PRINT N'Schema Storage failed '
142 PRINT 'Error Number: ' + str(error_number())
143 PRINT 'Line Number: ' + str(error_line())
144 PRINT error_message()
145END CATCH
146GO
147
148
149BEGIN TRY
150
151IF NOT EXISTS (
152SELECT schema_name
153FROM information_schema.schemata
154WHERE schema_name = 'Nutrition' )
155
156BEGIN
157 EXECUTE ('CREATE SCHEMA [Nutrition] AUTHORIZATION [dbo]')
158END
159
160 PRINT N'Schema Nutrition created '
161
162END TRY
163BEGIN CATCH
164 PRINT N'Schema Nutrition failed'
165 PRINT 'Error Number: ' + str(error_number())
166 PRINT 'Line Number: ' + str(error_line())
167 PRINT error_message()
168END CATCH
169GO
170
171BEGIN TRY
172IF NOT EXISTS (
173SELECT schema_name
174FROM information_schema.schemata
175WHERE schema_name = 'Rpt' )
176
177BEGIN
178 EXECUTE ('CREATE SCHEMA [Rpt] AUTHORIZATION [dbo]')
179END
180
181 PRINT N'Schema Rpt created '
182
183END TRY
184BEGIN CATCH
185 PRINT N'Schema Rpt failed'
186 PRINT 'Error Number: ' + str(error_number())
187 PRINT 'Line Number: ' + str(error_line())
188 PRINT error_message()
189END CATCH
190GO
191
192/* no user table create on the changeover */
193
194BEGIN TRY
195/* sysinfo.tPermLevel.Table.sql */
196
197/****** Object: Table [SysInfo].[tPermLevel] Script Date: 03/20/2013 11:35:53 ******/
198SET ANSI_NULLS ON
199SET QUOTED_IDENTIFIER ON
200SET ANSI_PADDING ON
201CREATE TABLE [SysInfo].[tPermLevel](
202 [permLevelID] [tinyint] IDENTITY(0,1) NOT NULL,
203 [levelDescript] [varchar](12) NOT NULL,
204 CONSTRAINT [PK_tPermLevel] PRIMARY KEY CLUSTERED
205(
206 [permLevelID] ASC
207)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
208) ON [PRIMARY]
209SET ANSI_PADDING OFF
210
211 PRINT N'Table SysInfo.tPermLevel Successful'
212
213END TRY
214BEGIN CATCH
215 PRINT N'Table SysInfo.tPermLevel failed'
216 PRINT 'Error Number: ' + str(error_number())
217 PRINT 'Line Number: ' + str(error_line())
218 PRINT error_message()
219END CATCH
220GO
221
222SET IDENTITY_INSERT [SysInfo].[tPermLevel] ON;
223GO
224
225BEGIN TRY
226/* sysinfo.tPermLevel.initData.sql */
227
228
229INSERT INTO [SysInfo].[tPermLevel]
230 ([permLevelID]
231 , [levelDescript])
232 VALUES
233 (0,'Disabled')
234 , (1,'Tech')
235 , (2,'Nurse')
236 , (3,'SuperUser')
237 , (4,'Admin')
238 ;
239
240 PRINT N'Sysinfo.tPermLevel data inserted'
241
242END TRY
243BEGIN CATCH
244 PRINT N'Sysinfo.tPermLevel data insert failed'
245 PRINT 'Error Number: ' + str(error_number())
246 PRINT 'Line Number: ' + str(error_line())
247 PRINT error_message()
248END CATCH
249GO
250
251SET IDENTITY_INSERT [SysInfo].[tPermLevel] OFF;
252GO
253
254BEGIN TRY
255
256/* SysInfo.tResource.Table.sql */
257
258/****** Object: Table [SysInfo].[tResource] Script Date: 03/20/2013 11:35:53 ******/
259SET ANSI_NULLS ON
260SET QUOTED_IDENTIFIER ON
261SET ANSI_PADDING ON
262CREATE TABLE [SysInfo].[tResource](
263 [resourceID] [tinyint] IDENTITY(1,1) NOT NULL,
264 [rscName] [varchar](15) NOT NULL,
265 [rscPath] [varchar](250) NOT NULL,
266 CONSTRAINT [PK_tResource] PRIMARY KEY CLUSTERED
267(
268 [resourceID] ASC
269)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
270) ON [PRIMARY]
271SET ANSI_PADDING OFF
272
273 PRINT N'Table SysInfo.tResource created '
274
275END TRY
276BEGIN CATCH
277 PRINT N'Table SysInfo.tResource creation failed'
278 PRINT 'Error Number: ' + str(error_number())
279 PRINT 'Line Number: ' + str(error_line())
280 PRINT error_message()
281END CATCH
282GO
283
284BEGIN TRY
285
286/* SysInfo.tResourcePerm.Table.sql */
287
288/****** Object: Table [SysInfo].[tResourcePerm] Script Date: 03/20/2013 11:35:53 ******/
289SET ANSI_NULLS ON
290SET QUOTED_IDENTIFIER ON
291SET ANSI_PADDING ON
292CREATE TABLE [SysInfo].[tResourcePerm](
293 [resourcePermID] [tinyint] IDENTITY(1,1) NOT NULL,
294 [resourceID] [tinyint] NOT NULL,
295 [permLevelID] [tinyint] NOT NULL,
296 CONSTRAINT [PK_tResourcePerm] PRIMARY KEY CLUSTERED
297(
298 [resourcePermID] ASC
299)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
300) ON [PRIMARY]
301SET ANSI_PADDING OFF
302
303
304ALTER TABLE [SysInfo].[tResourcePerm] WITH CHECK ADD CONSTRAINT [fk_tResourcePerm_tResource] FOREIGN KEY([resourceID])
305REFERENCES [SysInfo].[tResource] ([resourceID])
306
307ALTER TABLE [SysInfo].[tResourcePerm] CHECK CONSTRAINT [fk_tResourcePerm_tResource]
308
309ALTER TABLE [SysInfo].[tResourcePerm] WITH CHECK ADD CONSTRAINT [fk_tResourcePerm_tPermLevel] FOREIGN KEY([permlevelID])
310REFERENCES [SysInfo].[tPermLevel] ([permLevelID])
311
312ALTER TABLE [SysInfo].[tResourcePerm] CHECK CONSTRAINT [fk_tResourcePerm_tPermLevel]
313
314 PRINT N'Table SysInfo.tResourcePerm created '
315
316END TRY
317BEGIN CATCH
318 PRINT N'Table SysInfo.tResourcePerm create failed'
319 PRINT 'Error Number: ' + str(error_number())
320 PRINT 'Line Number: ' + str(error_line())
321 PRINT error_message()
322END CATCH
323GO
324
325/* SysInfo.uspAddEditResource.storedProcedure.sql
326To call the SP:
327EXECUTE SysInfo.uspAddEditResource @resourceID=0, @rscName='New Resource', @rscPath='Path to eventual destruction', @t=0, @n=1, @s=1, @a=0;
328For add, please use @resourceID = 0. For edit, use actual resourceID.
329 */
330
331BEGIN TRY
332EXECUTE ('
333/****** Object: StoredProcedure [SysInfo].[uspAddEditResource] Script Date: 3/20/2013 11:26:19 ******/
334
335-- =============================================
336-- Author: Lisa Bohm (lbohm@paragondsi.com)
337-- Create date: 2013-03-20
338-- Description: Add/edit custom resource buttons
339--
340-- =============================================
341CREATE PROCEDURE [SysInfo].[uspAddEditResource]
342 -- Add the parameters for the stored procedure here
343 @resourceID tinyint
344 , @rscName varchar(15)
345 , @rscPath varchar(250)
346 , @t bit
347 , @n bit
348 , @s bit
349 , @a bit
350AS
351
352 BEGIN
353 -- SET NOCOUNT ON added to prevent extra result sets from
354 -- interfering with SELECT statements.
355 SET NOCOUNT ON;
356
357BEGIN TRANSACTION
358
359/* Check that the resourceID either exists or is zero. */
360
361SET @resourceID = COALESCE((SELECT StR.resourceID
362 FROM SysInfo.tResource StR
363 WHERE StR.resourceID = @resourceID),0);
364
365DECLARE @tmp table (resourceID tinyint)
366;
367
368INSERT INTO @tmp (resourceID)
369VALUES (@resourceID);
370
371IF @resourceID = 0
372 /* insert new record */
373 BEGIN
374 INSERT INTO SysInfo.tResource
375 (rscName
376 , rscPath)
377 OUTPUT inserted.resourceID INTO @tmp
378 VALUES (@rscName
379 , @rscPath);
380 END
381
382 ELSE
383 /* update existing */
384 BEGIN
385 UPDATE SysInfo.tResource
386 SET rscName = @rscName
387 , rscPath = @rscPath
388 WHERE resourceID = @resourceID
389 END
390
391SET @resourceID = (SELECT MAX(resourceID)
392 FROM @tmp);
393
394/* annoying crap to do with each permission level */
395 /* level one = @t */
396
397 IF @t = 0
398 BEGIN
399 DELETE
400 FROM SysInfo.tResourcePerm
401 WHERE permLevelID = 1
402 AND resourceID = @resourceID;
403 END
404 ELSE
405 BEGIN
406 SET @t = COALESCE((SELECT resourcePermID
407 FROM SysInfo.tResourcePerm
408 WHERE permLevelID = 1
409 AND resourceID = @resourceID),0)
410 IF @t = 0
411 BEGIN
412 INSERT INTO SysInfo.tResourcePerm
413 (resourceID, permLevelID)
414 VALUES
415 (@resourceID, 1)
416 END
417 END
418
419 /* level two = @n */
420
421 IF @n = 0
422 BEGIN
423 DELETE
424 FROM SysInfo.tResourcePerm
425 WHERE permLevelID = 2
426 AND resourceID = @resourceID;
427 END
428 ELSE
429 BEGIN
430 SET @n = COALESCE((SELECT resourcePermID
431 FROM SysInfo.tResourcePerm
432 WHERE permLevelID = 2
433 AND resourceID = @resourceID),0)
434 IF @n = 0
435 BEGIN
436 INSERT INTO SysInfo.tResourcePerm
437 (resourceID, permLevelID)
438 VALUES
439 (@resourceID, 2)
440 END
441 END
442 /* level three = @s */
443
444 IF @s = 0
445 BEGIN
446 DELETE
447 FROM SysInfo.tResourcePerm
448 WHERE permLevelID = 3
449 AND resourceID = @resourceID;
450 END
451 ELSE
452 BEGIN
453 SET @s = COALESCE((SELECT resourcePermID
454 FROM SysInfo.tResourcePerm
455 WHERE permLevelID = 3
456 AND resourceID = @resourceID),0)
457 IF @s = 0
458 BEGIN
459 INSERT INTO SysInfo.tResourcePerm
460 (resourceID, permLevelID)
461 VALUES
462 (@resourceID, 3)
463 END
464 END
465 /* level four = @a */
466
467 IF @a = 0
468 BEGIN
469 DELETE
470 FROM SysInfo.tResourcePerm
471 WHERE permLevelID = 4
472 AND resourceID = @resourceID;
473 END
474 ELSE
475 BEGIN
476 SET @a = COALESCE((SELECT resourcePermID
477 FROM SysInfo.tResourcePerm
478 WHERE permLevelID = 4
479 AND resourceID = @resourceID),0)
480 IF @a = 0
481 BEGIN
482 INSERT INTO SysInfo.tResourcePerm
483 (resourceID, permLevelID)
484 VALUES
485 (@resourceID, 4)
486 END
487 END
488
489COMMIT TRANSACTION
490
491END;
492')
493
494 PRINT N'USP SysInfo.uspAddEditResource created'
495
496END TRY
497BEGIN CATCH
498 PRINT N'USP SysInfo.uspAddEditResource create failed'
499 PRINT 'Error Number: ' + str(error_number())
500 PRINT 'Line Number: ' + str(error_line())
501 PRINT error_message()
502END CATCH
503GO
504
505/* SysInfo.uspResourceList.storedProcedure.sql
506To call the SP:
507EXECUTE SysInfo.uspResourceList;
508*/
509BEGIN TRY
510EXECUTE ('
511/****** Object: StoredProcedure [SysInfo].[uspResourceList] Script Date: 3/20/2013 11:26:19 ******/
512-- =============================================
513-- Author: Lisa Bohm (lbohm@paragondsi.com)
514-- Create date: 2013-03-21
515-- Description: Get list of custom resource buttons with perms
516--
517-- =============================================
518CREATE PROCEDURE [SysInfo].[uspResourceList]
519 -- Add the parameters for the stored procedure here
520/* return all resources */
521AS
522
523 BEGIN
524 -- SET NOCOUNT ON added to prevent extra result sets from
525 -- interfering with SELECT statements.
526 SET NOCOUNT ON;
527
528SELECT SyR.resourceID
529 , SyR.rscName
530 , SyR.rscPath
531 , t = MAX(CASE WHEN SyP.permLevelID = 1 THEN 1 ELSE 0 END)
532 , n = MAX(CASE WHEN SyP.permLevelID = 2 THEN 1 ELSE 0 END)
533 , s = MAX(CASE WHEN SyP.permLevelID = 3 THEN 1 ELSE 0 END)
534 , a = MAX(CASE WHEN SyP.permLevelID = 4 THEN 1 ELSE 0 END)
535
536FROM SysInfo.tResource SyR
537 LEFT OUTER JOIN SysInfo.tResourcePerm SyP ON SyR.resourceID = SyP.resourceID
538
539GROUP BY SyR.resourceID
540 , SyR.rscName
541 , SyR.rscPath;
542
543END;
544')
545
546 PRINT N'USP SysInfo.uspResourceList created'
547
548END TRY
549BEGIN CATCH
550 PRINT N'USP SysInfo.uspResourceList create failed'
551 PRINT 'Error Number: ' + str(error_number())
552 PRINT 'Line Number: ' + str(error_line())
553 PRINT error_message()
554END CATCH
555GO
556
557/* SysInfo.uspDeleteResource.storedProcedure.sql
558To call the SP:
559EXECUTE SysInfo.uspDeleteResource @resourceID=1;
560*/
561BEGIN TRY
562EXECUTE ('
563/****** Object: StoredProcedure [SysInfo].[uspDeleteResource] Script Date: 3/20/2013 11:26:19 ******/
564-- =============================================
565-- Author: Lisa Bohm (lbohm@paragondsi.com)
566-- Create date: 2013-03-20
567-- Description: Deletes custom resource buttons and associated perms
568--
569-- =============================================
570CREATE PROCEDURE [SysInfo].[uspDeleteResource]
571 -- Add the parameters for the stored procedure here
572 @resourceID tinyint
573
574AS
575
576 BEGIN
577 -- SET NOCOUNT ON added to prevent extra result sets from
578 -- interfering with SELECT statements.
579 SET NOCOUNT ON;
580
581
582BEGIN TRY
583 DELETE
584 FROM SysInfo.tResourcePerm
585 WHERE resourceID = @resourceID;
586
587 DELETE
588 FROM SysInfo.tResource
589 WHERE resourceID = @resourceID;
590
591
592END TRY
593BEGIN CATCH
594 PRINT ''Error: '' + ERROR_MESSAGE();
595 ROLLBACK TRANSACTION
596END CATCH
597
598END;
599')
600
601 PRINT N'USP SysInfo.uspDeleteResource created'
602
603END TRY
604BEGIN CATCH
605 PRINT N'USP SysInfo.uspDeleteResource create failed'
606 PRINT 'Error Number: ' + str(error_number())
607 PRINT 'Line Number: ' + str(error_line())
608 PRINT error_message()
609END CATCH
610GO
611
612BEGIN TRY
613/* Systems settings revamp */
614
615/****** Object: Table [SysInfo].[tCategory] Script Date: 06/19/2013 11:35:53 ******/
616SET ANSI_NULLS ON
617SET QUOTED_IDENTIFIER ON
618SET ANSI_PADDING ON
619CREATE TABLE [SysInfo].[tCategory](
620 [sysCatID] [tinyint] IDENTITY(1,1) NOT NULL,
621 [sysCatName] [varchar](15) NOT NULL,
622
623 CONSTRAINT [PK_tCategory] PRIMARY KEY CLUSTERED
624(
625 [sysCatID] ASC
626)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
627) ON [PRIMARY]
628SET ANSI_PADDING OFF
629
630 PRINT N'Table SysInfo.tCategory created'
631
632END TRY
633BEGIN CATCH
634 PRINT N'Table SysInfo.tCategory create failed'
635 PRINT 'Error Number: ' + str(error_number())
636 PRINT 'Line Number: ' + str(error_line())
637 PRINT error_message()
638END CATCH
639GO
640
641BEGIN TRY
642/****** Object: Table [SysInfo].[tSubCat] Script Date: 06/19/2013 11:35:53 ******/
643SET ANSI_NULLS ON
644SET QUOTED_IDENTIFIER ON
645SET ANSI_PADDING ON
646CREATE TABLE [SysInfo].[tSubCat](
647 [subCatID] [tinyint] IDENTITY(1,1) NOT NULL,
648 [sysCatID] [tinyint] NOT NULL,
649 [subCatName] [varchar](25) NOT NULL,
650
651 CONSTRAINT [PK_tSubCat] PRIMARY KEY CLUSTERED
652(
653 [subCatID] ASC
654)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
655) ON [PRIMARY]
656SET ANSI_PADDING OFF
657
658ALTER TABLE [SysInfo].[tSubCat] WITH CHECK ADD CONSTRAINT [FK_tSubCat_tCategory] FOREIGN KEY([sysCatID])
659REFERENCES [SysInfo].[tCategory] ([sysCatID])
660
661ALTER TABLE [SysInfo].[tSubCat] CHECK CONSTRAINT [FK_tSubCat_tCategory]
662
663 PRINT N'Table SysInfo.tSubCat created'
664
665END TRY
666BEGIN CATCH
667 PRINT N'Table SysInfo.tSubCat create failed'
668 PRINT 'Error Number: ' + str(error_number())
669 PRINT 'Line Number: ' + str(error_line())
670 PRINT error_message()
671END CATCH
672GO
673
674BEGIN TRY
675/****** Object: Table [SysInfo].[tSetting] Script Date: 06/19/2013 11:35:53 ******/
676SET ANSI_NULLS ON
677SET QUOTED_IDENTIFIER ON
678SET ANSI_PADDING ON
679CREATE TABLE [SysInfo].[tSetting](
680 [settingID] [tinyint] IDENTITY(1,1) NOT NULL,
681 [subCatID] [tinyint] NOT NULL,
682 [settingName] [varchar](15) NOT NULL,
683 [setValue] [varchar](25) NOT NULL,
684 [defaultValue] [varchar](25) NOT NULL,
685
686
687 CONSTRAINT [PK_tSetting] PRIMARY KEY CLUSTERED
688(
689 [settingID] ASC
690)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
691) ON [PRIMARY]
692SET ANSI_PADDING OFF
693
694ALTER TABLE [SysInfo].[tSetting] WITH CHECK ADD CONSTRAINT [FK_tSetting_tSubCat] FOREIGN KEY([subCatID])
695REFERENCES [SysInfo].[tSubCat] ([subCatID])
696
697ALTER TABLE [SysInfo].[tSetting] CHECK CONSTRAINT [FK_tSetting_tSubCat]
698
699
700 PRINT N'Table SysInfo.tSetting created'
701
702END TRY
703BEGIN CATCH
704 PRINT N'Table SysInfo.tSetting create failed'
705 PRINT 'Error Number: ' + str(error_number())
706 PRINT 'Line Number: ' + str(error_line())
707 PRINT error_message()
708END CATCH
709GO
710
711/* temporary table to handle what's currently set up in the system */
712BEGIN TRY
713/****** Object: Table [SysInfo].[tFormerSet] Script Date: 06/19/2013 11:35:53 ******/
714SET ANSI_NULLS ON
715SET QUOTED_IDENTIFIER ON
716SET ANSI_PADDING ON
717CREATE TABLE [SysInfo].[tFormerSet](
718 [formerSetName][varchar](50) NOT NULL,
719 [settingID] [tinyint] NOT NULL,
720 [formerDefault] [varchar](25) NOT NULL,
721
722) ON [PRIMARY]
723SET ANSI_PADDING OFF
724
725 PRINT N'Table SysInfo.tFormerSet created'
726
727END TRY
728BEGIN CATCH
729 PRINT N'Table SysInfo.tFormerSet create failed'
730 PRINT 'Error Number: ' + str(error_number())
731 PRINT 'Line Number: ' + str(error_line())
732 PRINT error_message()
733END CATCH
734GO
735
736SET IDENTITY_INSERT [SysInfo].[tCategory] ON;
737GO
738
739BEGIN TRY
740/* settings initial data */
741
742INSERT INTO [SysInfo].[tCategory]
743 ([sysCatID]
744 , [sysCatName])
745 VALUES
746 (0,'Internal')
747 , (1,'General')
748 , (2,'Expiration')
749 , (3,'HL7')
750 , (4,'Misc')
751 , (5,'Custom Layout')
752 ;
753
754 PRINT N'Table SysInfo.tCategory data inserted'
755
756END TRY
757BEGIN CATCH
758 PRINT N'Table SysInfo.tCategory data insert failed'
759 PRINT 'Error Number: ' + str(error_number())
760 PRINT 'Line Number: ' + str(error_line())
761 PRINT error_message()
762END CATCH
763GO
764
765SET IDENTITY_INSERT [SysInfo].[tCategory] OFF;
766GO
767
768SET IDENTITY_INSERT [SysInfo].[tSubCat] ON;
769GO
770
771BEGIN TRY
772
773INSERT INTO [SysInfo].[tSubCat]
774 ([subCatID]
775 , [sysCatID]
776 , [subCatName])
777 VALUES
778 (1,0,'Database')
779 , (2,1,'Authentication')
780 , (3,1,'Timeout')
781 , (4,1,'App Behavior')
782 , (5,1,'New Child Defaults')
783 , (6,1,'Volume Tracking')
784 , (7,2,'EBM')
785 , (8,2,'Donor Milk')
786 , (9,2,'Reduce When')
787 , (10,3,'Connection')
788 , (11,3,'PID Fields')
789 , (12,3,'ID Zeros')
790 , (13,3,'Child Location PV1')
791 , (14,3,'Message Handling')
792 , (15,3,'After Manual Activate')
793 , (16,4,'Reports')
794 , (17,4, 'Settings')
795 , (18,5, 'Desktop Buttons')
796 , (19,5, 'Handheld Buttons')
797 , (20,4,'Scanner') ;
798
799 PRINT N'Table SysInfo.tSubCat data inserted'
800
801END TRY
802BEGIN CATCH
803 PRINT N'Table SysInfo.tSubCat data insert failed'
804 PRINT 'Error Number: ' + str(error_number())
805 PRINT 'Line Number: ' + str(error_line())
806 PRINT error_message()
807END CATCH
808GO
809
810SET IDENTITY_INSERT [SysInfo].[tSubCat] OFF;
811GO
812
813SET IDENTITY_INSERT [SysInfo].[tSetting] ON;
814GO
815
816BEGIN TRY
817
818INSERT INTO [SysInfo].[tSetting]
819 ([settingID]
820 , [subCatID]
821 , [settingName]
822 , [setValue]
823 , [defaultValue])
824 VALUES
825 (0,1,'donorMaint','01/01/1990','01/01/1900')
826 , (1,1,'version','2.0','0')
827 , (2,2,'LDAP','false','false')
828 , (3,2,'disablePW','false','false')
829 , (4,2,'LDAPDomain','','')
830 , (5,3,'Desktop','5','5')
831 , (6,3,'HandHeld','5','5')
832 , (7,4,'requireCribLbls','true','true')
833 , (8,4,'autoMove','true','true')
834 , (10,4,'autoReceive','false','false')
835 , (12,4,'NotifyNotFed','3','3')
836 , (13,5,'allowBottle','Y','Y')
837 , (14,5,'allowFormula','Y','Y')
838 , (15,5,'allowDonor','N','N')
839 , (16,6,'Trackmls','0','0')
840 , (17,6,'TrackSplit','0','0')
841 , (18,6,'TrackCombine','0','0')
842 , (19,7,'Fresh','9999','9999')
843 , (20,7,'Frozen','9999','9999')
844 , (21,7,'Thawed','9999','9999')
845 , (22,7,'ExpOnPump','false','false')
846 , (23,8,'DonorFresh','9999','9999')
847 , (24,8,'DonorFrozen','9999','9999')
848 , (25,8,'DonorThawed','9999','9999')
849 , (26,8,'PromptDonorExp','true','true')
850 , (27,8,'PromptDonorCals','false','false')
851 , (28,8,'InitialDonor','Frozen','Frozen')
852 , (29,9,'FortifyHours','0','0')
853 , (30,9,'SyringeHours','0','0')
854 , (31,10,'PortNumber','4848','4848')
855 , (32,11,'primaryIDField','18','18')
856 , (33,11,'motherIDField','6','6')
857 , (34,12,'addLeadZero','0','0')
858 , (35,12,'trimLeadZero','0','0')
859 , (36,12,'padToLen','0','0')
860 , (37,13,'section','3','3')
861 , (38,13,'subSection','2','2')
862 , (39,14,'autoActivate','1','1')
863 , (40,15,'printCribLabel','false','false')
864 , (41,15,'promptEditChild','true','true')
865 , (42,15,'promptCreateBin','false','false')
866 , (43,16,'reportLocation','local','local')
867 , (44,17,'settingsPW','','paragon')
868 , (45,17,'alertLockDelay','750','1500')
869 , (46,18,'cmdDD1','Add Patients','Add Patients')
870 , (47,18,'cmdDD2','Print Bottle Labels','Print Bottle Labels')
871 , (48,18,'cmdDD3','Receive Bottles','Receive Bottles')
872 , (49,18,'cmdDD4','Move Milk','Move Milk')
873 , (50,18,'cmdDD5','Fill','Fill')
874 , (51,18,'cmdDD6','Feedings By Patient','Feedings By Patient')
875 , (52,18,'cmdDD7','Bottles Received','Bottles Received')
876 , (53,18,'cmdDD8','Mother''s Milk Report','Mother''s Milk Report')
877 , (54,18,'cmdDD9','Avail Milk Inventory','Avail Milk Inventory')
878 , (55,18,'cmdDD10','Expired Milk','Expired Milk')
879 , (56,19,'cmdLoc1','Move Milk','Move Milk')
880 , (57,19,'cmdLoc2','Dispose','Dispose')
881 , (58,19,'cmdLoc3','Audit','Audit')
882 , (59,19,'cmdLoc4','Receive','Receive')
883 , (60,19,'cmdLoc5','More','More')
884 , (61,19,'cmdLoc6','Fill','Fill')
885 , (62,19,'cmdLoc7','Combine','Combine')
886 , (63,19,'cmdLoc8','Print Bottles','Print Bottles')
887 , (64,19,'cmdLoc9','Fortify','Fortify')
888 , (65,19,'cmdLoc10','Split','Split')
889 , (66,17,'isTest','1','0')
890 , (67,20,'Prefix','','')
891 , (68,20,'Suffix','','')
892 , (69,11,'motherMatchFld','18','18')
893 , (70,4,'directPrint','1','1')
894 , (71,4,'AutoPrOrdLbl','1','1')
895 , (72,17,'EnableHHExit','0','0')
896 , (73,4,'AuditKeepDays','10','10')
897 , (74,4,'AuditArchiveOn','1','1')
898 ;
899
900 PRINT N'Table SysInfo.tSetting data inserted'
901
902END TRY
903BEGIN CATCH
904 PRINT N'Table SysInfo.tSetting data insert failed'
905 PRINT 'Error Number: ' + str(error_number())
906 PRINT 'Line Number: ' + str(error_line())
907 PRINT error_message()
908END CATCH
909GO
910
911SET IDENTITY_INSERT [SysInfo].[tSetting] OFF;
912GO
913
914BEGIN TRY
915
916INSERT INTO [SysInfo].[tFormerSet]
917 ([settingID]
918 , [formerSetName]
919 , [formerDefault])
920 VALUES
921 (0,'lastDMaint','01/01/1900')
922 , (1,'version','0')
923 , (2,'LDAP','false')
924 , (3,'DisablePW','false')
925 , (4,'LogInDomain','')
926 , (5,'Desktop','5')
927 , (6,'HandHeld','5')
928 , (7,'RequireSB','true')
929 , (8,'AutoMove','true')
930 , (10,'AutoRec','false')
931 , (12,'NoFeedNotify','-1')
932 , (13,'DefAllowBottle','Y')
933 , (14,'DefAllowFormula','Y')
934 , (15,'DefAllowDonor','N')
935 , (16,'Trackmls','0')
936 , (17,'TrackSplit','0')
937 , (18,'TrackComb','0')
938 , (19,'Fresh','24')
939 , (20,'Frozen','4320')
940 , (21,'Thawed','30')
941 , (22,'ExpOnPump','true')
942 , (23,'DonorFresh','48')
943 , (24,'DonorFrozen','4320')
944 , (25,'DonorThawed','30')
945 , (26,'PromptDonorExp','false')
946 , (27,'PromptDonorCals','false')
947 , (28,'InitialDonor','Frozen')
948 , (29,'FortHours','0')
949 , (30,'SyringeHours','24')
950 , (31,'PortNumber','4848')
951 , (32,'AccountField','18')
952 , (33,'MMRN','0')
953 , (34,'addLeadZero','0')
954 , (35,'trimLeadZero','0')
955 , (36,'HL7Len','0')
956 , (37,'LocMain','3')
957 , (38,'LocSub','2')
958 , (39,'AutoHL7','1')
959 , (40,'AutoCrib','false')
960 , (41,'EChild','true')
961 , (42,'CreateBins','false')
962 , (43,'reportLocation','local')
963 , (44,'casualPW','paragon')
964 , (45,'alertLockDelay','1500')
965 , (46,'cmdDD1','Add Patients')
966 , (47,'cmdDD2','Print Bottle Labels')
967 , (48,'cmdDD3','Receive Bottles')
968 , (49,'cmdDD4','Move Milk')
969 , (50,'cmdDD5','Fill')
970 , (51,'cmdDD6','Feedings By Patient')
971 , (52,'cmdDD7','Bottles Received')
972 , (53,'cmdDD8','Mother''s Milk Report')
973 , (54,'cmdDD9','Avail Milk Inventory')
974 , (55,'cmdDD10','Expired Milk')
975 , (56,'cmdLoc1','Move Milk')
976 , (57,'cmdLoc2','Dispose')
977 , (58,'cmdLoc3','Audit')
978 , (59,'cmdLoc4','Receive')
979 , (60,'cmdLoc5','More')
980 , (61,'cmdLoc6','Fill')
981 , (62,'cmdLoc7','Combine')
982 , (63,'cmdLoc8','Print Bottles')
983 , (64,'cmdLoc9','Fortify')
984 , (65,'cmdLoc10','Split')
985 , (66,'isTest','0')
986 , (67,'Prefix','')
987 , (68,'Suffix','')
988 , (69,'motherMatchFld','18')
989 , (70,'directPrint','1')
990 , (71,'AutoPrOrdLbl','1')
991 , (72,'EnableHHExit','0')
992 , (73,'AuditKeepDays','10')
993 , (74,'AuditArchiveOn','1');
994
995 PRINT N'Table SysInfo.tFormerSet data inserted'
996
997END TRY
998BEGIN CATCH
999 PRINT N'Table SysInfo.tFormerSet data insert failed'
1000 PRINT 'Error Number: ' + str(error_number())
1001 PRINT 'Line Number: ' + str(error_line())
1002 PRINT error_message()
1003END CATCH
1004GO
1005/* change over from old system */
1006
1007BEGIN TRY
1008/* Do the update of the SysInfo.tSetting table */
1009
1010UPDATE StS
1011SET StS.setValue =
1012CASE WHEN dTS.[Value] IS NULL THEN StFS.formerDefault ELSE left(dTS.[Value],25) END
1013FROM SysInfo.tSetting StS
1014 INNER JOIN SysInfo.tFormerSet StFS ON StS.settingID = StFS.settingID
1015 LEFT OUTER JOIN dbo.tblSystemSettings dTS ON StFS.formerSetName = dTS.Setting
1016WHERE StS.settingID <> 1;
1017
1018/* set mother match field to be whatever the primary ID field is... */
1019DECLARE @acctFld varchar(25)
1020SET @acctFld = COALESCE((SELECT StS.setValue
1021 FROM SysInfo.tSetting StS
1022 WHERE StS.settingID = 32),'');
1023
1024UPDATE SysInfo.tSetting
1025SET setValue = @acctFld
1026WHERE settingID = 69;
1027
1028 PRINT N'Data from old system settings complete'
1029
1030END TRY
1031BEGIN CATCH
1032 PRINT N'Data update, old system settings to new, failed'
1033 PRINT 'Error Number: ' + str(error_number())
1034 PRINT 'Line Number: ' + str(error_line())
1035 PRINT error_message()
1036END CATCH
1037GO
1038
1039/* SysInfo.uspSetListByCat.storedProcedure.sql
1040To call the SP:
1041EXECUTE SysInfo.uspSetListByCat @sysCatName='HL7';
1042 */
1043
1044BEGIN TRY
1045EXECUTE ('
1046/* now get settings by category name */
1047/****** Object: StoredProcedure [SysInfo].[uspSetListByCat] Script Date: 6/25/2013 11:26:19 ******/
1048
1049-- =============================================
1050-- Author: Lisa Bohm (lbohm@paragondsi.com)
1051-- Create date: 2013-06-25
1052-- Description: Get list of system settings by category
1053--
1054-- =============================================
1055CREATE PROCEDURE [SysInfo].[uspSetListByCat]
1056 -- Add the parameters for the stored procedure here
1057@sysCatName varchar(15)
1058
1059AS
1060
1061 BEGIN
1062 -- SET NOCOUNT ON added to prevent extra result sets from
1063 -- interfering with SELECT statements.
1064 SET NOCOUNT ON;
1065
1066SELECT StSC.sysCatID
1067 , StS.subCatID
1068 , StS.settingID
1069 , StS.settingName
1070 , [Value] = StS.setValue
1071 , StS.defaultValue
1072
1073FROM SysInfo.tSetting StS
1074 INNER JOIN SysInfo.tSubCat StSC ON StS.subCatID = StSC.subCatID
1075 INNER JOIN SysInfo.tCategory StC ON StSC.sysCatID = StC.sysCatID
1076
1077WHERE StC.sysCatName = @sysCatName
1078
1079UNION ALL
1080
1081SELECT StSC.sysCatID
1082 , StS.subCatID
1083 , StS.settingID
1084 , StS.settingName
1085 , [Value] = StS.setValue
1086 , StS.defaultValue
1087
1088FROM SysInfo.tSetting StS
1089 INNER JOIN SysInfo.tSubCat StSC ON StS.subCatID = StSC.subCatID
1090
1091 WHERE StSC.subCatName = @sysCatName;
1092END;
1093')
1094
1095
1096 PRINT N'USP SysInfo.uspSetListByCat created'
1097
1098END TRY
1099BEGIN CATCH
1100 PRINT N'USP SysInfo.uspSetListByCat create failed'
1101 PRINT 'Error Number: ' + str(error_number())
1102 PRINT 'Line Number: ' + str(error_line())
1103 PRINT error_message()
1104END CATCH
1105GO
1106
1107/* SysInfo.uspSetList.storedProcedure.sql
1108To call the SP:
1109EXECUTE SysInfo.uspSetList;
1110 */
1111BEGIN TRY
1112EXECUTE ('
1113/****** Object: StoredProcedure [SysInfo].[uspSetList] Script Date: 6/25/2013 11:26:19 ******/
1114-- =============================================
1115-- Author: Lisa Bohm (lbohm@paragondsi.com)
1116-- Create date: 2013-06-25
1117-- Description: Get list of system settings by category
1118--
1119-- =============================================
1120CREATE PROCEDURE [SysInfo].[uspSetList]
1121 -- Add the parameters for the stored procedure here
1122
1123AS
1124
1125 BEGIN
1126 -- SET NOCOUNT ON added to prevent extra result sets from
1127 -- interfering with SELECT statements.
1128 SET NOCOUNT ON;
1129
1130SELECT StSC.sysCatID
1131 , StS.subCatID
1132 , StS.settingID
1133 , StS.settingName
1134 , [Value] = StS.setValue
1135 , StS.defaultValue
1136
1137FROM SysInfo.tSetting StS
1138 INNER JOIN SysInfo.tSubCat StSC ON StS.subCatID = StSC.subCatID
1139 INNER JOIN SysInfo.tCategory StC ON StSC.sysCatID = StC.sysCatID
1140
1141WHERE StC.sysCatID > 0;
1142
1143END;
1144')
1145
1146 PRINT N'USP SysInfo.uspSetList created'
1147
1148END TRY
1149BEGIN CATCH
1150 PRINT N'USP SysInfo.uspSetList create failed'
1151 PRINT 'Error Number: ' + str(error_number())
1152 PRINT 'Line Number: ' + str(error_line())
1153 PRINT error_message()
1154END CATCH
1155GO
1156
1157/* SysInfo.uspSetValByID.storedProcedure.sql
1158To call the SP:
1159EXECUTE SysInfo.uspSetValByID @settingID=2;
1160 */
1161BEGIN TRY
1162EXECUTE ('
1163/****** Object: StoredProcedure [SysInfo].[uspSetValByID] Script Date: 6/25/2013 11:26:19 ******/
1164-- =============================================
1165-- Author: Lisa Bohm (lbohm@paragondsi.com)
1166-- Create date: 2013-07-10
1167-- Description: Get value of a single system setting
1168--
1169-- =============================================
1170CREATE PROCEDURE [SysInfo].[uspSetValByID]
1171 -- Add the parameters for the stored procedure here
1172@settingID tinyint
1173
1174AS
1175 BEGIN
1176 -- SET NOCOUNT ON added to prevent extra result sets from
1177 -- interfering with SELECT statements.
1178 SET NOCOUNT ON;
1179
1180SELECT [Value] = StS.setValue
1181
1182FROM SysInfo.tSetting StS
1183
1184WHERE StS.settingID = @settingID;
1185
1186END;
1187')
1188
1189 PRINT N'USP SysInfo.uspSetValByID created'
1190
1191END TRY
1192BEGIN CATCH
1193 PRINT N'USP SysInfo.uspSetValByID create failed'
1194 PRINT 'Error Number: ' + str(error_number())
1195 PRINT 'Line Number: ' + str(error_line())
1196 PRINT error_message()
1197END CATCH
1198GO
1199
1200BEGIN TRY
1201
1202/****** Object: Table [Nutrition].[tFormula] Script Date: 06/19/2013 11:35:53 ******/
1203SET ANSI_NULLS ON
1204SET QUOTED_IDENTIFIER ON
1205SET ANSI_PADDING ON
1206CREATE TABLE [Nutrition].[tFormula](
1207 [formulaID] [smallint] IDENTITY(1,1) NOT NULL,
1208 [formulaName] [varchar](50) NOT NULL,
1209 [calsPer] [decimal](6,2) NOT NULL,
1210 [isActive] [bit] NOT NULL,
1211
1212 CONSTRAINT [PK_tFormula] PRIMARY KEY CLUSTERED
1213(
1214 [formulaID] ASC
1215)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
1216) ON [PRIMARY]
1217SET ANSI_PADDING OFF
1218ALTER TABLE [Nutrition].[tFormula] ADD CONSTRAINT [DF_tFormula_calsPer] DEFAULT ((0)) FOR [calsPer]
1219ALTER TABLE [Nutrition].[tFormula] ADD CONSTRAINT [UQ_tFormula_formulaName] UNIQUE ([formulaName])
1220ALTER TABLE [Nutrition].[tFormula] ADD CONSTRAINT [DF_tFormula_isActive] DEFAULT ((1)) FOR [isActive];
1221
1222 PRINT N'Table Nutrition.tFormula created'
1223
1224END TRY
1225BEGIN CATCH
1226 PRINT N'Table Nutrition.tFormula create failed'
1227 PRINT 'Error Number: ' + str(error_number())
1228 PRINT 'Line Number: ' + str(error_line())
1229 PRINT error_message()
1230END CATCH
1231GO
1232
1233/* insert generic, unspecified formula value - JUST for carry-over with old data */
1234SET IDENTITY_INSERT [Nutrition].[tFormula] ON;
1235GO
1236
1237INSERT INTO [Nutrition].[tFormula]
1238 (formulaID, formulaName, calsPer)
1239VALUES
1240 (0, 'Unspecified formula', 20);
1241
1242SET IDENTITY_INSERT [Nutrition].[tFormula] OFF;
1243GO
1244
1245BEGIN TRY
1246
1247/****** Object: Table [Nutrition].[tFormulaLot] Script Date: 06/19/2013 11:35:53 ******/
1248SET ANSI_NULLS ON
1249SET QUOTED_IDENTIFIER ON
1250SET ANSI_PADDING ON
1251CREATE TABLE [Nutrition].[tFormulaLot](
1252 [formLotID] [int] IDENTITY(1,1) NOT NULL,
1253 [formulaID] [smallint] NOT NULL,
1254 [formLotCode] [varchar] (25) NOT NULL,
1255 CONSTRAINT [PK_tFormulaLot] PRIMARY KEY CLUSTERED
1256(
1257 [formLotID] ASC
1258)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
1259) ON [PRIMARY]
1260SET ANSI_PADDING OFF
1261
1262ALTER TABLE [Nutrition].[tFormulaLot] WITH CHECK ADD CONSTRAINT [FK_tFormulaLot_tFormula] FOREIGN KEY([formulaID])
1263REFERENCES [Nutrition].[tFormula] ([formulaID])
1264ALTER TABLE [Nutrition].[tFormulaLot] CHECK CONSTRAINT [FK_tFormulaLot_tFormula]
1265
1266 PRINT N'Table Nutrition.tFormulaLot created'
1267
1268END TRY
1269BEGIN CATCH
1270 PRINT N'Table Nutrition.tFormulaLot create failed'
1271 PRINT 'Error Number: ' + str(error_number())
1272 PRINT 'Line Number: ' + str(error_line())
1273 PRINT error_message()
1274END CATCH
1275GO
1276
1277/* insert generic, unspecified formula Lot value for unspecified formula - JUST for carry-over with old data */
1278SET IDENTITY_INSERT [Nutrition].[tFormulaLot] ON;
1279GO
1280
1281INSERT INTO [Nutrition].[tFormulalot]
1282 (formLotID, formulaID, formLotCode)
1283VALUES
1284 (0, 0, 'Unspecified lot');
1285
1286SET IDENTITY_INSERT [Nutrition].[tFormulaLot] OFF;
1287GO
1288
1289BEGIN TRY
1290/* move data over from former table */
1291 /* are there any duplicate values? */
1292
1293INSERT INTO [Nutrition].[tFormula]
1294 (formulaName)
1295SELECT DISTINCT formulaName
1296 FROM dbo.tblFormula;
1297
1298 PRINT N'Table Nutrition.tFormula data inserted'
1299
1300END TRY
1301BEGIN CATCH
1302 PRINT N'Table Nutrition.tFormula data insert failed'
1303 PRINT 'Error Number: ' + str(error_number())
1304 PRINT 'Line Number: ' + str(error_line())
1305 PRINT error_message()
1306END CATCH
1307GO
1308
1309BEGIN TRY
1310/* unspecified lot for each formula */
1311
1312INSERT INTO [Nutrition].[tFormulaLot]
1313 (formulaID, formLotCode)
1314SELECT
1315 NtF.formulaID
1316 , 'Unspecified lot'
1317 FROM Nutrition.tFormula NtF
1318 WHERE NtF.formulaID > 0;
1319
1320 PRINT N'Table Nutrition.tFormulaLot data inserted'
1321
1322END TRY
1323BEGIN CATCH
1324 PRINT N'Table Nutrition.tFormulaLot data insert failed'
1325 PRINT 'Error Number: ' + str(error_number())
1326 PRINT 'Line Number: ' + str(error_line())
1327 PRINT error_message()
1328END CATCH
1329GO
1330
1331BEGIN TRY
1332/* additives */
1333
1334/****** Object: Table [Nutrition].[tAdditiveList] Script Date: 06/19/2013 11:35:53 ******/
1335SET ANSI_NULLS ON
1336SET QUOTED_IDENTIFIER ON
1337SET ANSI_PADDING ON
1338CREATE TABLE [Nutrition].[tAdditiveList](
1339 [additiveID] [smallint] IDENTITY(1,1) NOT NULL,
1340 [additiveName] [varchar](50) NOT NULL,
1341 [unitOfMeas] [varchar](15) NOT NULL,
1342 [isActive] [bit] NOT NULL,
1343 CONSTRAINT [PK_tAdditiveList] PRIMARY KEY CLUSTERED
1344(
1345 [additiveID] ASC
1346)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
1347) ON [PRIMARY]
1348SET ANSI_PADDING OFF
1349
1350ALTER TABLE [Nutrition].[tAdditiveList] ADD CONSTRAINT [DF_tAdditiveList_unitOfMeas] DEFAULT (('oz')) FOR [unitOfMeas]
1351
1352ALTER TABLE [Nutrition].[tAdditiveList] ADD CONSTRAINT [DF_tAdditiveList_isActive] DEFAULT ((1)) FOR [isActive]
1353
1354 PRINT N'Table Nutrition.tAdditiveList created'
1355
1356END TRY
1357BEGIN CATCH
1358 PRINT N'Table Nutrition.tAdditiveList create failed'
1359 PRINT 'Error Number: ' + str(error_number())
1360 PRINT 'Line Number: ' + str(error_line())
1361 PRINT error_message()
1362END CATCH
1363GO
1364
1365BEGIN TRY
1366/* init data */
1367
1368INSERT INTO Nutrition.tAdditiveList
1369 (additiveName
1370 , unitOfMeas
1371 , isActive)
1372
1373SELECT Additive
1374 , UOM
1375 , 1
1376FROM dbo.tblAdditives;
1377
1378 PRINT N'Table Nutrition.tAdditiveList data inserted'
1379
1380END TRY
1381BEGIN CATCH
1382 PRINT N'Table Nutrition.tAdditiveList data insert failed'
1383 PRINT 'Error Number: ' + str(error_number())
1384 PRINT 'Line Number: ' + str(error_line())
1385 PRINT error_message()
1386END CATCH
1387GO
1388
1389BEGIN TRY
1390/* nutrition types */
1391
1392/****** Object: Table [Nutrition].[tCategory] Script Date: 06/19/2013 11:35:53 ******/
1393SET ANSI_NULLS ON
1394SET QUOTED_IDENTIFIER ON
1395SET ANSI_PADDING ON
1396CREATE TABLE [Nutrition].[tCategory](
1397 [nCategoryID] [tinyint] IDENTITY(1,1) NOT NULL,
1398 [nCategoryName] [varchar](20) NOT NULL,
1399 [nCategoryAbbrev] [varchar](9) NOT NULL,
1400 [nCategoryPrefix] char(1) NOT NULL,
1401
1402 CONSTRAINT [PK_tCategory] PRIMARY KEY CLUSTERED
1403(
1404 [nCategoryID] ASC
1405)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
1406) ON [PRIMARY]
1407SET ANSI_PADDING OFF
1408
1409 PRINT N'Table Nutrition.tCategory created'
1410
1411END TRY
1412BEGIN CATCH
1413 PRINT N'Table Nutrition.tCategory create failed'
1414 PRINT 'Error Number: ' + str(error_number())
1415 PRINT 'Line Number: ' + str(error_line())
1416 PRINT error_message()
1417END CATCH
1418GO
1419
1420SET IDENTITY_INSERT [Nutrition].[tCategory] ON;
1421GO
1422
1423BEGIN TRY
1424/* init data */
1425
1426INSERT INTO [Nutrition].[tCategory]
1427 ([nCategoryID]
1428 , [nCategoryName]
1429 , [nCategoryAbbrev]
1430 , [nCategoryPrefix])
1431 VALUES
1432 (0, 'Combination', 'Combined', 'C')
1433 , (1,'Mother''s EBM', 'EBM', 'B')
1434 , (2,'Donor EBM', 'Donor', 'D')
1435 , (3,'Non-EBM Based', 'Formula', 'F')
1436 , (4,'Mother Breastfeed', 'Breast', 'N')
1437 , (5,'Surrogate Breastfeed', 'Wet Nurse','S')
1438 ;
1439
1440 PRINT N'Table Nutrition.tCategory data inserted'
1441
1442END TRY
1443BEGIN CATCH
1444 PRINT N'Table Nutrition.tCategory data insert failed'
1445 PRINT 'Error Number: ' + str(error_number())
1446 PRINT 'Line Number: ' + str(error_line())
1447 PRINT error_message()
1448END CATCH
1449GO
1450SET IDENTITY_INSERT [Nutrition].[tCategory] OFF;
1451GO
1452
1453BEGIN TRY
1454/* okay - milk status table */
1455/****** Object: Table [Nutrition].[tStatusCategory] Script Date: 06/19/2013 11:35:53 ******/
1456SET ANSI_NULLS ON
1457SET QUOTED_IDENTIFIER ON
1458SET ANSI_PADDING ON
1459CREATE TABLE [Nutrition].[tStatusCategory](
1460 [statCatID] [tinyint] IDENTITY(1,1) NOT NULL,
1461 [statCatName] [varchar](15) NOT NULL,
1462
1463 CONSTRAINT [PK_tStatusCategory] PRIMARY KEY CLUSTERED
1464(
1465 [statCatID] ASC
1466)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
1467) ON [PRIMARY]
1468SET ANSI_PADDING OFF
1469
1470 PRINT N'Table Nutrition.tStatusCategory created'
1471
1472END TRY
1473BEGIN CATCH
1474 PRINT N'Table Nutrition.tStatusCategory create failed'
1475 PRINT 'Error Number: ' + str(error_number())
1476 PRINT 'Line Number: ' + str(error_line())
1477 PRINT error_message()
1478END CATCH
1479GO
1480
1481SET IDENTITY_INSERT [Nutrition].[tStatusCategory] ON;
1482GO
1483
1484BEGIN TRY
1485
1486INSERT INTO [Nutrition].[tStatusCategory]
1487 ([statCatID]
1488 , [statCatName])
1489 VALUES
1490 (1,'Label Printed')
1491 , (2,'Available')
1492 , (3,'Expired')
1493 , (4,'Disposed')
1494 , (5,'Event')
1495 ;
1496
1497 PRINT N'Table Nutrition.tStatusCategory data inserted'
1498
1499END TRY
1500BEGIN CATCH
1501 PRINT N'Table Nutrition.tStatusCategory data insert failed'
1502 PRINT 'Error Number: ' + str(error_number())
1503 PRINT 'Line Number: ' + str(error_line())
1504 PRINT error_message()
1505END CATCH
1506GO
1507
1508SET IDENTITY_INSERT [Nutrition].[tStatusCategory] OFF;
1509GO
1510
1511BEGIN TRY
1512/****** Object: Table [Nutrition].[tStatusList] Script Date: 06/19/2013 11:35:53 ******/
1513SET ANSI_NULLS ON
1514SET QUOTED_IDENTIFIER ON
1515SET ANSI_PADDING ON
1516CREATE TABLE [Nutrition].[tStatusList](
1517 [statusID] [tinyint] IDENTITY(1,1) NOT NULL,
1518 [statCatID] [tinyint] NOT NULL,
1519 [statusName] [varchar](20) NOT NULL,
1520 [canEdit] [bit] NOT NULL,
1521 [visible] [bit] NOT NULL,
1522 [statRank] [tinyint] NOT NULL,
1523 CONSTRAINT [PK_tStatusList] PRIMARY KEY CLUSTERED
1524(
1525 [statusID] ASC
1526)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
1527) ON [PRIMARY]
1528SET ANSI_PADDING OFF
1529
1530ALTER TABLE [Nutrition].[tStatusList] WITH CHECK ADD CONSTRAINT [FK_tStatusList_tStatusCategory] FOREIGN KEY([statCatID])
1531REFERENCES [Nutrition].[tStatusCategory] ([statCatID])
1532ALTER TABLE [Nutrition].[tStatusList] CHECK CONSTRAINT [FK_tStatusList_tStatusCategory]
1533ALTER TABLE [Nutrition].[tStatusList] ADD CONSTRAINT [DF_tStatusList_canEdit] DEFAULT ((0)) FOR [canEdit]
1534ALTER TABLE [Nutrition].[tStatusList] ADD CONSTRAINT [DF_tStatusList_visible] DEFAULT ((1)) FOR [visible]
1535ALTER TABLE [Nutrition].[tStatusList] ADD CONSTRAINT [DF_tStatusList_statRank] DEFAULT ((0)) FOR [statRank]
1536
1537 PRINT N'Table Nutrition.tStatusList created'
1538
1539END TRY
1540BEGIN CATCH
1541 PRINT N'Table Nutrition.tStatusList create failed'
1542 PRINT 'Error Number: ' + str(error_number())
1543 PRINT 'Line Number: ' + str(error_line())
1544 PRINT error_message()
1545END CATCH
1546GO
1547
1548SET IDENTITY_INSERT [Nutrition].[tStatusList] ON;
1549GO
1550
1551BEGIN TRY
1552
1553INSERT INTO [Nutrition].[tStatusList]
1554 ([statusID]
1555 , [statCatID]
1556 , [statusName]
1557 , [canEdit]
1558 , [visible]
1559 , [statRank])
1560 VALUES
1561 (1,1,'Not Received',0,0,7)
1562 , (2,2,'Fresh',0,0,6)
1563 , (3,2,'Frozen',0,0,5)
1564 , (4,2,'Thawed',0,0,4)
1565 , (5,2,'Fortified',0,0,3)
1566 , (6,3,'Past Date',0,0,2)
1567 , (7,4,'Fed',0,0,2)
1568 , (8,4,'Discharged',0,1,1)
1569 , (9,4,'Expired',0,1,1)
1570 , (10,4,'Spilled',0,1,1)
1571 , (11,4,'Lost',0,1,1)
1572 , (12,5,'Container Change',0,0,0)
1573 , (14,5,'Pumped',0,0,0)
1574 , (15,4,'Prep Cancel',0,0,0)
1575 , (16,4, 'Empty', 0,1,1)
1576 , (17,5, 'Received', 0,0,0)
1577 , (18,5, 'Swab', 0,0,0)
1578 , (19,5, 'Vol Adjust', 0,0,0)
1579 ;
1580
1581 PRINT N'Table Nutrition.tStatusList data inserted'
1582
1583END TRY
1584BEGIN CATCH
1585 PRINT N'Table Nutrition.tStatusList data insert failed'
1586 PRINT 'Error Number: ' + str(error_number())
1587 PRINT 'Line Number: ' + str(error_line())
1588 PRINT error_message()
1589END CATCH
1590GO
1591
1592SET IDENTITY_INSERT [Nutrition].[tStatusList] OFF;
1593GO
1594
1595
1596BEGIN TRY
1597 /* add their disposal reasons */
1598INSERT INTO [Nutrition].[tStatusList]
1599 ([statCatID]
1600 , [statusName]
1601 , [canEdit]
1602 , [visible])
1603
1604SELECT 4
1605 , LEFT(dDT.[Description],20)
1606 , 1
1607 , 1
1608FROM dbo.tblDisposalTypes dDT
1609LEFT OUTER JOIN [Nutrition].[tStatusList] sl
1610 on sl.statusName = dDt.[Description]
1611Where sl.statusName is null
1612ORDER BY dDT.DisplayOrder;
1613
1614 PRINT N'Table Nutrition.tStatusList custom disposal types transfer data inserted'
1615
1616END TRY
1617BEGIN CATCH
1618 PRINT N'Table Nutrition.tStatusList transfer data insert failed'
1619 PRINT 'Error Number: ' + str(error_number())
1620 PRINT 'Line Number: ' + str(error_line())
1621 PRINT error_message()
1622END CATCH
1623GO
1624
1625BEGIN TRY
1626/* join to settingID for expiration params */
1627
1628/****** Object: Table [Nutrition].[tStatusExpir] Script Date: 06/19/2013 11:35:53 ******/
1629SET ANSI_NULLS ON
1630SET QUOTED_IDENTIFIER ON
1631SET ANSI_PADDING ON
1632CREATE TABLE [Nutrition].[tStatusExpir](
1633 [statExpirID] [tinyint] IDENTITY(1,1) NOT NULL,
1634 [statusID] [tinyint] NOT NULL,
1635 [nCategoryID] [tinyint] NOT NULL,
1636 [settingID] [tinyint] NOT NULL,
1637 CONSTRAINT [PK_tStatusExpir] PRIMARY KEY CLUSTERED
1638(
1639 [statExpirID] ASC
1640)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
1641) ON [PRIMARY]
1642SET ANSI_PADDING OFF
1643
1644ALTER TABLE [Nutrition].[tStatusExpir] WITH CHECK ADD CONSTRAINT [FK_tStatusExpir_tStatusList] FOREIGN KEY([statusID])
1645REFERENCES [Nutrition].[tStatusList] ([statusID])
1646ALTER TABLE [Nutrition].[tStatusExpir] CHECK CONSTRAINT [FK_tStatusExpir_tStatuslist]
1647
1648ALTER TABLE [Nutrition].[tStatusExpir] WITH CHECK ADD CONSTRAINT [FK_tStatusExpir_tCategory] FOREIGN KEY([nCategoryID])
1649REFERENCES [Nutrition].[tCategory] ([nCategoryID])
1650ALTER TABLE [Nutrition].[tStatusExpir] CHECK CONSTRAINT [FK_tStatusExpir_tCategory]
1651
1652ALTER TABLE [Nutrition].[tStatusExpir] WITH CHECK ADD CONSTRAINT [FK_tStatusExpir_tSetting] FOREIGN KEY([settingID])
1653REFERENCES [SysInfo].[tSetting] ([settingID])
1654ALTER TABLE [Nutrition].[tStatusExpir] CHECK CONSTRAINT [FK_tStatusExpir_tSetting]
1655
1656
1657 PRINT N'Table Nutrition.tStatusExpir created'
1658
1659END TRY
1660BEGIN CATCH
1661 PRINT N'Table Nutrition.tStatusExpir create failed'
1662 PRINT 'Error Number: ' + str(error_number())
1663 PRINT 'Line Number: ' + str(error_line())
1664 PRINT error_message()
1665END CATCH
1666GO
1667
1668BEGIN TRY
1669INSERT INTO [Nutrition].[tStatusExpir]
1670 ([statusID]
1671 , [nCategoryID]
1672 , [settingID])
1673 VALUES
1674 (2,1,19)
1675 , (2,2,23)
1676 , (3,1,20)
1677 , (3,2,24)
1678 , (4,1,21)
1679 , (4,2,25)
1680 , (5,1,29)
1681 , (5,2,29)
1682 , (12,1,30)
1683 , (12,2,30)
1684 ;
1685
1686 PRINT N'Table Nutrition.tStatusExpir data inserted'
1687
1688END TRY
1689BEGIN CATCH
1690 PRINT N'Table Nutrition.tStatusExpir data insert failed'
1691 PRINT 'Error Number: ' + str(error_number())
1692 PRINT 'Line Number: ' + str(error_line())
1693 PRINT error_message()
1694END CATCH
1695GO
1696
1697BEGIN TRY
1698/* donor bank layout prep */
1699/****** Object: Table [Nutrition].[tDonorBank] Script Date: 06/19/2013 11:35:53 ******/
1700SET ANSI_NULLS ON
1701SET QUOTED_IDENTIFIER ON
1702SET ANSI_PADDING ON
1703CREATE TABLE [Nutrition].[tDonorBank](
1704 [donorBankID] [tinyint] IDENTITY(1,1) NOT NULL,
1705 [bankName] [varchar](50) NOT NULL,
1706 [isActive] [bit] NOT NULL,
1707
1708 CONSTRAINT [PK_tDonorBank] PRIMARY KEY CLUSTERED
1709(
1710 [donorBankID] ASC
1711)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
1712) ON [PRIMARY]
1713SET ANSI_PADDING OFF
1714
1715ALTER TABLE [Nutrition].[tDonorBank] ADD CONSTRAINT [DF_tDonorBank_isActive] DEFAULT ((1)) FOR [isActive]
1716
1717
1718 PRINT N'Table Nutrition.tDonorBank created'
1719
1720END TRY
1721BEGIN CATCH
1722 PRINT N'Table Nutrition.tDonorBank create failed'
1723 PRINT 'Error Number: ' + str(error_number())
1724 PRINT 'Line Number: ' + str(error_line())
1725 PRINT error_message()
1726END CATCH
1727GO
1728
1729/* insert existing donor banks */
1730
1731SET IDENTITY_INSERT Nutrition.tDonorBank ON;
1732GO
1733
1734BEGIN TRY
1735
1736INSERT INTO Nutrition.tDonorBank (donorBankID
1737 , bankName
1738 , isActive)
1739
1740VALUES (0
1741 , 'Unspecified Donor Bank'
1742 , 1);
1743
1744
1745 PRINT N'Table Nutrition.tDonorBank default value inserted'
1746
1747END TRY
1748BEGIN CATCH
1749 PRINT N'Table Nutrition.tDonorBank data default value failed'
1750 PRINT 'Error Number: ' + str(error_number())
1751 PRINT 'Line Number: ' + str(error_line())
1752 PRINT error_message()
1753END CATCH
1754GO
1755
1756SET IDENTITY_INSERT Nutrition.tDonorBank OFF;
1757GO
1758
1759
1760BEGIN TRY
1761
1762INSERT INTO Nutrition.tDonorBank
1763 (bankName)
1764SELECT DISTINCT dBank.BankName FROM
1765 (SELECT BankName
1766 FROM dbo.tblDonorBanks
1767 UNION ALL
1768 SELECT BankName = LName
1769 FROM dbo.tblMothers
1770 WHERE donor = 'Y') dBank;
1771
1772 PRINT N'Table Nutrition.tDonorBank data inserted'
1773
1774END TRY
1775BEGIN CATCH
1776 PRINT N'Table Nutrition.tDonorBank data insert failed'
1777 PRINT 'Error Number: ' + str(error_number())
1778 PRINT 'Line Number: ' + str(error_line())
1779 PRINT error_message()
1780END CATCH
1781GO
1782
1783BEGIN TRY
1784/* storage locations */
1785
1786/****** Object: Table [Storage].[tLocationType] Script Date: 06/19/2013 11:35:53 ******/
1787SET ANSI_NULLS ON
1788SET QUOTED_IDENTIFIER ON
1789SET ANSI_PADDING ON
1790CREATE TABLE [Storage].[tLocationType](
1791 [locTypeID] [tinyint] IDENTITY(1,1) NOT NULL,
1792 [locTypeName] [varchar](20) NOT NULL,
1793 [locTypePrefix] [char](1) NOT NULL,
1794 CONSTRAINT [PK_tLocationType] PRIMARY KEY CLUSTERED
1795(
1796 [locTypeID] ASC
1797)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
1798) ON [PRIMARY]
1799SET ANSI_PADDING OFF
1800
1801
1802 PRINT N'Table Storage.tLocationType created'
1803
1804END TRY
1805BEGIN CATCH
1806 PRINT N'Table Storage.tLocationType create failed'
1807 PRINT 'Error Number: ' + str(error_number())
1808 PRINT 'Line Number: ' + str(error_line())
1809 PRINT error_message()
1810END CATCH
1811GO
1812
1813SET IDENTITY_INSERT [Storage].[tLocationType] ON;
1814GO
1815
1816BEGIN TRY
1817INSERT INTO [Storage].[tLocationType]
1818 ([locTypeID]
1819 , [locTypeName]
1820 , [locTypePrefix])
1821 VALUES
1822 (1, 'Non-Storage','L')
1823 , (2,'Refrigerator', 'R')
1824 , (3,'Freezer', 'Z')
1825 , (4,'Bin', 'Q')
1826 ;
1827
1828 PRINT N'Table Storage.tLocationType data inserted'
1829
1830END TRY
1831BEGIN CATCH
1832 PRINT N'Table Storage.tLocationType data insert failed'
1833 PRINT 'Error Number: ' + str(error_number())
1834 PRINT 'Line Number: ' + str(error_line())
1835 PRINT error_message()
1836END CATCH
1837GO
1838
1839SET IDENTITY_INSERT [Storage].[tLocationType] OFF;
1840GO
1841
1842BEGIN TRY
1843/* actual location stuff */
1844
1845/****** Object: Table [Storage].[tLocation] Script Date: 06/19/2013 11:35:53 ******/
1846SET ANSI_NULLS ON
1847SET QUOTED_IDENTIFIER ON
1848SET ANSI_PADDING ON
1849CREATE TABLE [Storage].[tLocation](
1850 [locationID] [bigint] IDENTITY(100,1) NOT NULL,
1851 [locationName] [varchar](30) NOT NULL,
1852 [locationCode] [varchar](20) NOT NULL,
1853 [locatedInID] [bigint] NOT NULL,
1854 [locTypeID] [tinyint] NOT NULL,
1855
1856 CONSTRAINT [PK_tLocation] PRIMARY KEY CLUSTERED
1857(
1858 [locationID] ASC
1859)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
1860) ON [PRIMARY]
1861SET ANSI_PADDING OFF
1862
1863ALTER TABLE [Storage].[tLocation] WITH CHECK ADD CONSTRAINT [FK_tLocation_tLocationType] FOREIGN KEY([locTypeID])
1864REFERENCES [Storage].[tLocationType] ([locTypeID])
1865ALTER TABLE [Storage].[tLocation] CHECK CONSTRAINT [FK_tLocation_tLocationType]
1866
1867ALTER TABLE [Storage].[tLocation] ADD CONSTRAINT [DF_tLocation_locatedInID] DEFAULT ((0)) FOR [locatedInID]
1868
1869ALTER TABLE [Storage].[tLocation] ADD CONSTRAINT [DF_tLocation_locTypeID] DEFAULT ((1)) FOR [locTypeID]
1870
1871ALTER TABLE [Storage].[tLocation] ADD CONSTRAINT [UQ_tLocation_locationCode] UNIQUE (locationCode)
1872
1873 PRINT N'Table Storage.tLocation created'
1874
1875END TRY
1876BEGIN CATCH
1877 PRINT N'Table Storage.tLocation create failed'
1878 PRINT 'Error Number: ' + str(error_number())
1879 PRINT 'Line Number: ' + str(error_line())
1880 PRINT error_message()
1881END CATCH
1882GO
1883
1884SET IDENTITY_INSERT [Storage].[tLocation] ON;
1885GO
1886
1887BEGIN TRY
1888
1889/* ADD a root entry to not freak out the foreign key constraint here */
1890INSERT INTO Storage.tLocation
1891 (locationID
1892 , locationName
1893 , locationCode
1894 , locatedInID
1895 , locTypeID)
1896VALUES (0, 'Hospital', 'L001', 0, 1)
1897 , (1, 'Unknown', 'L002', 0, 1)
1898 , (2, 'Disposed', 'L003', 0, 1);
1899
1900
1901 PRINT N'Table Storage.tLocation base data inserted'
1902
1903END TRY
1904BEGIN CATCH
1905 PRINT N'Table Storage.tLocation base data insert failed'
1906 PRINT 'Error Number: ' + str(error_number())
1907 PRINT 'Line Number: ' + str(error_line())
1908 PRINT error_message()
1909END CATCH
1910GO
1911
1912SET IDENTITY_INSERT [Storage].[tLocation] OFF;
1913GO
1914
1915BEGIN TRY
1916
1917ALTER TABLE [Storage].[tLocation] WITH CHECK ADD CONSTRAINT [FK_tLocation_tLocation] FOREIGN KEY([locatedInID])
1918REFERENCES [Storage].[tLocation] ([locationID])
1919
1920ALTER TABLE [Storage].[tLocation] CHECK CONSTRAINT [FK_tLocation_tLocation]
1921
1922
1923 PRINT N'Table Storage.tLocation addtl constraint created'
1924
1925END TRY
1926BEGIN CATCH
1927 PRINT N'Table Storage.tLocation additional constraint create failed'
1928 PRINT 'Error Number: ' + str(error_number())
1929 PRINT 'Line Number: ' + str(error_line())
1930 PRINT error_message()
1931END CATCH
1932GO
1933
1934/* enter existing storage locations now. First just get the locations in */
1935BEGIN TRY
1936INSERT INTO Storage.tLocation
1937 (locationName
1938 , locationCode
1939 , locatedInID
1940 , locTypeID)
1941
1942SELECT left(dL.LocationName,25)
1943 , dL.LocationCode
1944 , 0
1945 , CASE WHEN dL.[Bin] = 'Y' THEN 4 ELSE
1946 CASE WHEN dL.Freezer = 'Y' THEN 3 ELSE
1947 CASE WHEN dL.Fridge = 'Y' THEN 2 ELSE 1
1948 END
1949 END
1950 END
1951
1952FROM dbo.tblLocations dL;
1953
1954 PRINT N'Table Storage.tLocation data inserted'
1955
1956END TRY
1957BEGIN CATCH
1958 PRINT N'Table Storage.tLocation data insert failed'
1959 PRINT 'Error Number: ' + str(error_number())
1960 PRINT 'Line Number: ' + str(error_line())
1961 PRINT error_message()
1962END CATCH
1963GO
1964
1965
1966BEGIN TRY
1967/* now do parent locations */
1968UPDATE Storage.tLocation
1969SET locatedInID = COALESCE((SELECT StL.locationID
1970 FROM dbo.tblLocations PL
1971 LEFT OUTER JOIN Storage.tLocation StL ON PL.parentLocation = StL.locationCode
1972 WHERE PL.locationCode = Storage.tLocation.locationCode
1973 ),0);
1974
1975
1976/* recursion, because it's just so spectacular. The locations that should have gotten deleted, but weren't. Maybe check to see if there are wessels in them too. */
1977DECLARE @ditchMe TABLE (theLocCode varchar(20)
1978 , parentLocCode varchar(20));
1979
1980WITH cteBuildPath AS
1981 (-- the anchor part now is just for the "root" locations
1982 SELECT theLocCode = dtL.locationCode
1983 , pLoc = CAST('P000' AS varchar(50))
1984 FROM dbo.tblLocations dTL
1985 WHERE dTL.locationName <> 'NICU'
1986 AND dTL.parentLocation <> ''
1987 AND NOT EXISTS (SELECT dTP.locationCode
1988 FROM dbo.tblLocations dTP
1989 WHERE dTL.parentLocation = dTP.locationCode)
1990
1991 /* the recursive part */
1992 UNION ALL
1993
1994 SELECT theLocCode = dtL.locationCode
1995 , pLoc = dtL.parentLocation
1996 FROM dbo.tblLocations dTL
1997 INNER JOIN cteBuildPath AS cte ON cte.theLocCode = dTL.parentLocation
1998 )
1999
2000
2001 INSERT INTO @ditchMe
2002 (theLocCode
2003 , parentLocCode)
2004
2005 SELECT cBP.theLocCode
2006 , cBP.pLoc
2007 FROM cteBuildPath cBP;
2008
2009/* delete all these from the new structureeeee */
2010
2011DELETE StL
2012FROM Storage.tLocation StL
2013 WHERE EXISTS (SELECT dM.theLocCode
2014 FROM @ditchMe dM
2015 WHERE StL.locationCode = dM.theLocCode);
2016
2017/* bin assignments later after patients/mothers have been redone */
2018
2019 PRINT N'Table Storage.tLocation parent locations updated'
2020
2021END TRY
2022BEGIN CATCH
2023 PRINT N'Table Storage.tLocation parent location update failed'
2024 PRINT 'Error Number: ' + str(error_number())
2025 PRINT 'Line Number: ' + str(error_line())
2026 PRINT error_message()
2027END CATCH
2028GO
2029
2030BEGIN TRY
2031/* uh huh. Now for the actual "containers". */
2032/****** Object: Table [Nutrition].[tVessel] Script Date: 06/19/2013 11:35:53 ******/
2033SET ANSI_NULLS ON
2034SET QUOTED_IDENTIFIER ON
2035SET ANSI_PADDING ON
2036CREATE TABLE [Nutrition].[tVessel](
2037 [vesselID] [bigint] IDENTITY(100,1) NOT NULL,
2038 [vesselCode] [varchar](20) NOT NULL,
2039 [nCategoryID] [tinyint] NOT NULL,
2040 [mls] [decimal] (7,2) NOT NULL,
2041 [gms] [decimal] (7,2) NOT NULL,
2042 [calsPer] [decimal] (6,2) NOT NULL,
2043 [locationID][bigint] NOT NULL,
2044 CONSTRAINT [PK_tVessel] PRIMARY KEY CLUSTERED
2045(
2046 [vesselID] ASC
2047)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
2048) ON [PRIMARY]
2049SET ANSI_PADDING OFF
2050
2051
2052ALTER TABLE [Nutrition].[tVessel] WITH CHECK ADD CONSTRAINT [FK_tVessel_tCategory] FOREIGN KEY([nCategoryID])
2053REFERENCES [Nutrition].[tCategory] ([nCategoryID])
2054ALTER TABLE [Nutrition].[tVessel] CHECK CONSTRAINT [FK_tVessel_tCategory]
2055
2056ALTER TABLE [Nutrition].[tVessel] WITH CHECK ADD CONSTRAINT [FK_tVessel_tLocation] FOREIGN KEY([locationID])
2057REFERENCES [Storage].[tLocation] ([locationID])
2058ALTER TABLE [Nutrition].[tVessel] CHECK CONSTRAINT [FK_tVessel_tLocation]
2059
2060
2061ALTER TABLE [Nutrition].[tVessel] ADD CONSTRAINT [DF_tVessel_mls] DEFAULT ((1)) FOR [mls]
2062ALTER TABLE [Nutrition].[tVessel] ADD CONSTRAINT [DF_tVessel_gms] DEFAULT ((1)) FOR [gms]
2063ALTER TABLE [Nutrition].[tVessel] ADD CONSTRAINT [DF_tVessel_calsPer] DEFAULT ((0)) FOR [calsPer]
2064ALTER TABLE [Nutrition].[tVessel] ADD CONSTRAINT [DF_tVessel_locationID] DEFAULT ((1)) FOR [locationID]
2065
2066
2067 PRINT N'Table Nutrition.tVessel created'
2068
2069END TRY
2070BEGIN CATCH
2071 PRINT N'Table Nutrition.tVessel create failed'
2072 PRINT 'Error Number: ' + str(error_number())
2073 PRINT 'Line Number: ' + str(error_line())
2074 PRINT error_message()
2075END CATCH
2076GO
2077
2078BEGIN TRY
2079/* and let's fill it from what we've got */
2080INSERT INTO Nutrition.tVessel
2081 (vesselCode
2082 , nCategoryID
2083 , mls
2084 , gms
2085 , calsPer
2086 , locationID)
2087
2088SELECT DISTINCT dB.bottleNumber
2089/* if actually formula in the dB. table then....:
2090 But then, if mother, then.... */
2091 , CASE WHEN dB.Formula = 'Y' THEN 3 ELSE
2092 CASE WHEN dM.Donor = 'Y' THEN 2 ELSE
2093 CASE WHEN LEFT(db.BottleNumber,1) = 'N' THEN 4 ELSE
2094 CASE WHEN dFH.BottleNumber IS NOT NULL THEN 3 ELSE 1 END
2095 END
2096 END
2097 END
2098 , dB.mls
2099 , 1
2100 , COALESCE(dB.EBMCals,20)
2101 , 1
2102
2103FROM dbo.tblBottles dB
2104 LEFT OUTER JOIN dbo.tblMothers dM ON dB.MotherMRN = dM.MedicalRecordNum
2105 LEFT OUTER JOIN (dbo.tblFillHistory dFH
2106 INNER JOIN dbo.tblAdditivesIssued dAI ON dAI.MilkID = dFH.bottleNumber
2107 INNER JOIN dbo.tblFormula dF ON dAI.Additive = dF.FormulaName
2108 ) ON dB.bottleNumber = dFH.BottleNumber
2109
2110UNION ALL
2111
2112SELECT DISTINCT dF.FeedingNumber
2113/* if actually formula in the dB. table then....:
2114 But then, if mother, then.... */
2115 , CASE WHEN dB.Formula = 'Y' THEN 3 ELSE
2116 CASE WHEN dM.Donor = 'Y' THEN 2 ELSE
2117 CASE WHEN LEFT(db.BottleNumber,1) = 'N' THEN 4 ELSE
2118 CASE WHEN dAI.Additive IS NOT NULL THEN 3 ELSE 1 END
2119 END
2120 END
2121 END
2122 , dF.mls
2123 , 1
2124 , COALESCE(dF.EBMCals,20)
2125 , 1
2126
2127FROM dbo.tblFeedings dF
2128 INNER JOIN dbo.tblFillHistory dFH ON dF.FeedingNumber = dFH.FeedingNumber
2129 INNER JOIN dbo.tblBottles dB ON dFH.BottleNumber = dB.bottleNumber
2130 LEFT OUTER JOIN dbo.tblMothers dM ON dB.MotherMRN = dM.MedicalRecordNum
2131 LEFT OUTER jOIN (dbo.tblAdditivesIssued dAI
2132 INNER JOIN dbo.tblFormula dFO ON dAI.Additive = dFO.FormulaName
2133 ) ON dAI.milkID = dFH.BottleNumber
2134 ;
2135
2136 PRINT N'Table Nutrition.tVessel data inserted'
2137
2138END TRY
2139BEGIN CATCH
2140 PRINT N'Table Nutrition.tVessel data insert failed'
2141 PRINT 'Error Number: ' + str(error_number())
2142 PRINT 'Line Number: ' + str(error_line())
2143 PRINT error_message()
2144END CATCH
2145GO
2146
2147BEGIN TRY
2148/* update the locations for the wessels */
2149UPDATE Nutrition.tVessel
2150SET locationID = COALESCE((SELECT CASE WHEN dB.locationCode = 'disposed' THEN 2 ELSE StL.locationID END
2151 FROM dbo.tblBottles dB
2152 LEFT OUTER JOIN Storage.tLocation StL ON dB.locationCode = StL.locationCode
2153 WHERE Nutrition.tVessel.vesselCode = dB.bottleNumber
2154 ),1)
2155WHERE EXISTS (select dB.bottleNumber
2156 FROm dbo.tblBottles dB
2157 WHERE Nutrition.tVessel.vesselCode = dB.bottlenumber);
2158
2159UPDATE Nutrition.tVessel
2160SET locationID = COALESCE((SELECT CASE WHEN dF.locationCode = 'disposed' THEN 2 ELSE StL.locationID END
2161 FROM dbo.tblFeedings dF
2162 LEFT OUTER JOIN Storage.tLocation StL ON dF.locationCode = StL.locationCode
2163 WHERE Nutrition.tVessel.vesselCode = dF.feedingNumber ),1)
2164WHERE EXISTS (select dF.feedingNumber
2165 FROm dbo.tblFeedings dF
2166 WHERE Nutrition.tVessel.vesselCode = dF.feedingnumber)
2167 ;
2168
2169 PRINT N'Table Nutrition.tVessel location data updated'
2170
2171END TRY
2172BEGIN CATCH
2173 PRINT N'Table Nutrition.tVessel location data update failed'
2174 PRINT 'Error Number: ' + str(error_number())
2175 PRINT 'Line Number: ' + str(error_line())
2176 PRINT error_message()
2177END CATCH
2178GO
2179
2180/* formula */
2181BEGIN TRY
2182
2183/****** Object: Table [Nutrition].[tFormulaVessel] Script Date: 06/19/2013 11:35:53 ******/
2184SET ANSI_NULLS ON
2185SET QUOTED_IDENTIFIER ON
2186SET ANSI_PADDING ON
2187CREATE TABLE [Nutrition].[tFormulaVessel](
2188 [vesselFormulaID] [bigint] IDENTITY(1,1) NOT NULL,
2189 [formLotID] [int] NOT NULL,
2190 [vesselID] [bigint] NOT NULL,
2191 CONSTRAINT [PK_tFormulaVessel] PRIMARY KEY CLUSTERED
2192(
2193 [vesselFormulaID] ASC
2194)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
2195) ON [PRIMARY]
2196SET ANSI_PADDING OFF
2197
2198ALTER TABLE [Nutrition].[tFormulaVessel] WITH CHECK ADD CONSTRAINT [FK_tFormulaVessel_tFormulaLot] FOREIGN KEY([formLotID])
2199REFERENCES [Nutrition].[tFormulaLot] ([formLotID])
2200ALTER TABLE [Nutrition].[tFormulaVessel] CHECK CONSTRAINT [FK_tFormulaVessel_tFormulaLot]
2201
2202ALTER TABLE [Nutrition].[tFormulaVessel] WITH CHECK ADD CONSTRAINT [FK_tFormulaVessel_tVessel] FOREIGN KEY([vesselID])
2203REFERENCES [Nutrition].[tVessel] ([vesselID])
2204ALTER TABLE [Nutrition].[tFormulaVessel] CHECK CONSTRAINT [FK_tFormulaVessel_tVessel]
2205
2206 PRINT N'Table Nutrition.tFormulaVessel created'
2207
2208END TRY
2209BEGIN CATCH
2210 PRINT N'Table Nutrition.tFormulaVessel create failed'
2211 PRINT 'Error Number: ' + str(error_number())
2212 PRINT 'Line Number: ' + str(error_line())
2213 PRINT error_message()
2214END CATCH
2215GO
2216
2217/* add all current formula vessels existing in the database. The inner join with the formula table excludes actual additives... */
2218BEGIN TRY
2219
2220INSERT INTO Nutrition.tFormulaVessel
2221 (formLotID
2222 , vesselID)
2223SELECT NFL.formLotID
2224, NtV.vesselID
2225
2226FROM dbo.tblAdditivesIssued dAI
2227 INNER JOIN Nutrition.tVessel NtV ON dAI.milkID = NtV.vesselCode
2228 INNER JOIN Nutrition.tFormula NF ON dAI.Additive = NF.FormulaName
2229 INNER JOIN Nutrition.tFormulaLot NFL ON NF.formulaID = NFL.formulaID
2230WHERE NFL.formLotCode = 'Unspecified lot';
2231
2232INSERT INTO Nutrition.tFormulaVessel
2233 (formLotID
2234 , vesselID)
2235
2236SELECT 0
2237 , NtVF.vesselID
2238FROM Nutrition.tVessel NTVF
2239 WHERE NtVF.nCategoryID = 3
2240 AND NOT EXISTS (SELECT NFV.vesselFormulaID
2241 FROM Nutrition.tFormulaVessel NFV
2242 WHERE NtVF.vesselID = NFV.vesselID);
2243
2244 PRINT N'Table Nutrition.tVesselFormula insert successful'
2245
2246END TRY
2247BEGIN CATCH
2248 PRINT N'Table Nutrition.tVesselFormula insert failed'
2249 PRINT 'Error Number: ' + str(error_number())
2250 PRINT 'Line Number: ' + str(error_line())
2251 PRINT error_message()
2252END CATCH
2253GO
2254
2255BEGIN TRY
2256/* that's the vessels anyway. Now to donors */
2257
2258/****** Object: Table [Nutrition].[tDonorLot] Script Date: 06/19/2013 11:35:53 ******/
2259SET ANSI_NULLS ON
2260SET QUOTED_IDENTIFIER ON
2261SET ANSI_PADDING ON
2262CREATE TABLE [Nutrition].[tDonorLot](
2263 [lotID] [int] IDENTITY(1,1) NOT NULL,
2264 [donorBankID] [tinyint] NOT NULL,
2265 [lotCode] [varchar] (25) NOT NULL,
2266 CONSTRAINT [PK_tDonorLot] PRIMARY KEY CLUSTERED
2267(
2268 [lotID] ASC
2269)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
2270) ON [PRIMARY]
2271SET ANSI_PADDING OFF
2272
2273
2274ALTER TABLE [Nutrition].[tDonorLot] WITH CHECK ADD CONSTRAINT [FK_tDonorLot_tDonorBank] FOREIGN KEY([donorBankID])
2275REFERENCES [Nutrition].[tDonorBank] ([donorBankID])
2276ALTER TABLE [Nutrition].[tDonorLot] CHECK CONSTRAINT [FK_tDonorLot_tDonorBank]
2277
2278 PRINT N'Table Nutrition.tDonorLot created'
2279
2280END TRY
2281BEGIN CATCH
2282 PRINT N'Table Nutrition.tDonorLot create failed'
2283 PRINT 'Error Number: ' + str(error_number())
2284 PRINT 'Line Number: ' + str(error_line())
2285 PRINT error_message()
2286END CATCH
2287GO
2288
2289BEGIN TRY
2290/* init data for donor lots */
2291
2292INSERT INTO Nutrition.tDonorLot
2293 (donorBankID
2294 , lotCode)
2295
2296SELECT COALESCE(NDB.donorBankID,0)
2297 , LEFT(RTRIM(LTRIM(dM.MedicalRecordNum)),25)
2298
2299FROM dbo.tblMothers dM
2300 LEFT OUTER JOIn Nutrition.tDonorBank NDB ON dM.LName = NDB.bankName
2301
2302WHERE dM.Donor = 'Y'
2303 ;
2304
2305 PRINT N'Table Nutrition.tDonorLot data inserted'
2306
2307END TRY
2308BEGIN CATCH
2309 PRINT N'Table Nutrition.tDonorLot data insert failed'
2310 PRINT 'Error Number: ' + str(error_number())
2311 PRINT 'Line Number: ' + str(error_line())
2312 PRINT error_message()
2313END CATCH
2314GO
2315
2316BEGIN TRY
2317/* join bottles with donor lots */
2318
2319/****** Object: Table [Nutrition].[tDonorVessel] Script Date: 06/19/2013 11:35:53 ******/
2320SET ANSI_NULLS ON
2321SET QUOTED_IDENTIFIER ON
2322SET ANSI_PADDING ON
2323CREATE TABLE [Nutrition].[tDonorVessel](
2324 [vesselLotID] [bigint] IDENTITY(1,1) NOT NULL,
2325 [lotID] [int] NOT NULL,
2326 [vesselID] [bigint] NOT NULL,
2327 CONSTRAINT [PK_tDonorVessel] PRIMARY KEY CLUSTERED
2328(
2329 [vesselLotID] ASC
2330)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
2331) ON [PRIMARY]
2332SET ANSI_PADDING OFF
2333
2334
2335ALTER TABLE [Nutrition].[tDonorVessel] WITH CHECK ADD CONSTRAINT [FK_tDonorVessel_tDonorLot] FOREIGN KEY([lotID])
2336REFERENCES [Nutrition].[tDonorLot] ([lotID])
2337ALTER TABLE [Nutrition].[tDonorVessel] CHECK CONSTRAINT [FK_tDonorVessel_tDonorLot]
2338
2339ALTER TABLE [Nutrition].[tDonorVessel] WITH CHECK ADD CONSTRAINT [FK_tDonorVessel_tVessel] FOREIGN KEY([vesselID])
2340REFERENCES [Nutrition].[tVessel] ([vesselID])
2341ALTER TABLE [Nutrition].[tDonorVessel] CHECK CONSTRAINT [FK_tDonorVessel_tVessel]
2342
2343
2344 PRINT N'Table Nutrition.tDonorVessel created'
2345
2346END TRY
2347BEGIN CATCH
2348 PRINT N'Table Nutrition.tDonorVessel create failed'
2349 PRINT 'Error Number: ' + str(error_number())
2350 PRINT 'Line Number: ' + str(error_line())
2351 PRINT error_message()
2352END CATCH
2353GO
2354
2355BEGIN TRY
2356/* add existing data for donor vessels */
2357INSERT INTO Nutrition.tDonorVessel
2358 (lotID
2359 , vesselID)
2360
2361SELECT NDL.lotID
2362 , NtV.vesselID
2363
2364FROM Nutrition.tVessel NtV
2365 INNER JOIn dbo.tblBottles dB ON NtV.vesselCode = dB.BottleNumber
2366 INNER JOIN dbo.tblMothers dD ON dB.MotherMRN = dD.MedicalRecordNum
2367 INNER JOIN Nutrition.tDonorLot NDL ON dD.MedicalRecordNum = NDL.lotCode
2368WHERE NtV.nCategoryID = 2
2369
2370UNION ALL
2371
2372SELECT NDL.lotID
2373 , NtV.vesselID
2374
2375FROM Nutrition.tVessel NtV
2376 INNER JOIn dbo.tblFeedings dF ON NtV.vesselCode = dF.feedingNumber
2377 INNER JOIN dbo.tblMothers dD ON dF.MotherMRN = dD.MedicalRecordNum
2378 INNER JOIN Nutrition.tDonorLot NDL ON dD.MedicalRecordNum = NDL.lotCode
2379
2380WHERE NtV.nCategoryID = 2
2381;
2382
2383 PRINT N'Table Nutrition.tDonorVessel data inserted'
2384
2385END TRY
2386BEGIN CATCH
2387 PRINT N'Table Nutrition.tDonorVessel data insert failed'
2388 PRINT 'Error Number: ' + str(error_number())
2389 PRINT 'Line Number: ' + str(error_line())
2390 PRINT error_message()
2391END CATCH
2392GO
2393
2394BEGIN TRY
2395/* mixed/split bottle origin info */
2396
2397/****** Object: Table [Nutrition].[tVesselOrigin] Script Date: 06/19/2013 11:35:53 ******/
2398SET ANSI_NULLS ON
2399SET QUOTED_IDENTIFIER ON
2400SET ANSI_PADDING ON
2401CREATE TABLE [Nutrition].[tVesselOrigin](
2402 [vesselOriginID] [bigint] IDENTITY(1,1) NOT NULL,
2403 [vesselID] [bigint] NOT NULL,
2404 [targetVesselID] [bigint] NOT NULL,
2405 [mls][decimal](7,2) NOT NULL,
2406 CONSTRAINT [PK_tVesselOrigin] PRIMARY KEY CLUSTERED
2407(
2408 [vesselOriginID] ASC
2409)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
2410) ON [PRIMARY]
2411SET ANSI_PADDING OFF
2412
2413
2414ALTER TABLE [Nutrition].[tVesselOrigin] WITH CHECK ADD CONSTRAINT [FK_tVessel_tVesselOrig] FOREIGN KEY([vesselID])
2415REFERENCES [Nutrition].[tVessel] ([vesselID])
2416ALTER TABLE [Nutrition].[tVesselOrigin] CHECK CONSTRAINT [FK_tVessel_tVesselOrig]
2417
2418ALTER TABLE [Nutrition].[tVesselOrigin] WITH CHECK ADD CONSTRAINT [FK_tVessel_tVesselTgt] FOREIGN KEY([targetVesselID])
2419REFERENCES [Nutrition].[tVessel] ([vesselID])
2420ALTER TABLE [Nutrition].[tVesselOrigin] CHECK CONSTRAINT [FK_tVessel_tVesselTgt]
2421
2422ALTER TABLE [Nutrition].[tVesselOrigin] ADD CONSTRAINT [DF_tVesselOrigin_mls] DEFAULT ((1)) FOR [mls]
2423
2424 PRINT N'Table Nutrition.tVesselOrigin created'
2425
2426END TRY
2427BEGIN CATCH
2428 PRINT N'Table Nutrition.tVesselOrigin create failed'
2429 PRINT 'Error Number: ' + str(error_number())
2430 PRINT 'Line Number: ' + str(error_line())
2431 PRINT error_message()
2432END CATCH
2433GO
2434
2435BEGIN TRY
2436/****** Object: Table [PTinfo].[tHL7MsgType] Script Date: 11/20/2013 11:35:53 ******/
2437SET ANSI_NULLS ON
2438SET QUOTED_IDENTIFIER ON
2439SET ANSI_PADDING ON
2440CREATE TABLE [PTinfo].[tHL7MsgType](
2441 [msgTypeID][tinyint] IDENTITY(1,1) NOT NULL,
2442 [msgTypeDescript] [varchar](25) NOT NULL,
2443
2444CONSTRAINT [PK_tHL7MsgType] PRIMARY KEY CLUSTERED
2445(
2446 [msgTypeID] ASC
2447)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
2448) ON [PRIMARY]
2449SET ANSI_PADDING OFF
2450
2451 PRINT N'Table PTinfo.tHL7MsgType created'
2452
2453END TRY
2454BEGIN CATCH
2455 PRINT N'Table PTinfo.tHL7MsgType create failed'
2456 PRINT 'Error Number: ' + str(error_number())
2457 PRINT 'Line Number: ' + str(error_line())
2458 PRINT error_message()
2459END CATCH
2460GO
2461
2462SET IDENTITY_INSERT [PTinfo].[tHL7MsgType] ON;
2463GO
2464
2465BEGIN TRY
2466
2467INSERT INTO [PTinfo].[tHL7MsgType]
2468 ([msgTypeID]
2469 , [msgTypeDescript])
2470 VALUES
2471 (1, 'Incoming Patient Info')
2472 , (2,'Incoming CPOE Info')
2473 , (3,'Outgoing Feed Info')
2474 ;
2475
2476 PRINT N'Table PTinfo.tHL7MsgType data inserted'
2477
2478END TRY
2479BEGIN CATCH
2480 PRINT N'Table PTinfo.tHL7MsgType data insert failed'
2481 PRINT 'Error Number: ' + str(error_number())
2482 PRINT 'Line Number: ' + str(error_line())
2483 PRINT error_message()
2484END CATCH
2485GO
2486
2487SET IDENTITY_INSERT [PTinfo].[tHL7MsgType] OFF;
2488GO
2489
2490BEGIN TRY
2491/****** Object: Table [PTinfo].[tHL7MsgField] Script Date: 11/20/2013 11:35:53 ******/
2492SET ANSI_NULLS ON
2493SET QUOTED_IDENTIFIER ON
2494SET ANSI_PADDING ON
2495CREATE TABLE [PTinfo].[tHL7MsgField](
2496 [fieldID][smallint] IDENTITY(1,1) NOT NULL,
2497 [msgTypeID] [tinyint] NOT NULL,
2498 [sect] [char](3) NOT NULL,
2499 [fieldNum][tinyint] NOT NULL,
2500 [subFieldNum][tinyint] NOT NULL,
2501 [contentDescript][varchar](30) NOT NULL,
2502
2503CONSTRAINT [PK_tHL7MsgField] PRIMARY KEY CLUSTERED
2504(
2505 [fieldID] ASC
2506)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
2507) ON [PRIMARY]
2508SET ANSI_PADDING OFF
2509
2510ALTER TABLE [PTinfo].[tHL7MsgField] WITH CHECK ADD CONSTRAINT [FK_tHL7MsgField_tHL7MsgType] FOREIGN KEY([msgTypeID])
2511REFERENCES [PTinfo].[tHL7MsgType] ([msgTypeID])
2512ALTER TABLE [PTinfo].[tHL7MsgField] CHECK CONSTRAINT [FK_tHL7MsgField_tHL7MsgType]
2513
2514 PRINT N'Table PTinfo.tHL7MsgField created'
2515
2516END TRY
2517BEGIN CATCH
2518 PRINT N'Table PTinfo.tHL7MsgField create failed'
2519 PRINT 'Error Number: ' + str(error_number())
2520 PRINT 'Line Number: ' + str(error_line())
2521 PRINT error_message()
2522END CATCH
2523GO
2524
2525SET IDENTITY_INSERT [PTinfo].[tHL7MsgField] ON;
2526GO
2527
2528BEGIN TRY
2529
2530INSERT INTO [PTinfo].[tHL7MsgField]
2531 ([fieldID]
2532 , [msgTypeID]
2533 , [sect]
2534 , [fieldNum]
2535 , [subFieldNum]
2536 , [contentDescript])
2537 VALUES
2538 (1,1,'PID',2,1,'Encounter ID')
2539 , (2,1,'PID',3,1,'Medical Record Number')
2540 , (3,1,'PID',5,1,'Patient Last Name')
2541 , (4,1,'PID',5,2,'Patient First Name')
2542 , (5,1,'PID',5,3,'Patient Middle Name')
2543 , (6,1,'PID',6,1,'Possible Mother ID')
2544 , (7,1,'PID',7,1,'Date of Birth')
2545 , (8,1,'PID',8,1,'Gender')
2546 , (9,1,'PID',18,1,'Encounter/Account ID')
2547 , (10,1,'PID',21,1,'Possible Mother ID')
2548 , (11,1,'PID',24,1,'Multiple Birth Indicator')
2549 , (12,1,'PID',25,1,'Birth Order')
2550 , (13,1,'PV1',3,2,'Location')
2551 ;
2552PRINT N'Table PTinfo.tHL7MsgField data inserted'
2553
2554END TRY
2555BEGIN CATCH
2556 PRINT N'Table PTinfo.tHL7MsgField data insert failed'
2557 PRINT 'Error Number: ' + str(error_number())
2558 PRINT 'Line Number: ' + str(error_line())
2559 PRINT error_message()
2560END CATCH
2561GO
2562
2563SET IDENTITY_INSERT [PTinfo].[tHL7Msgfield] OFF;
2564GO
2565
2566
2567BEGIN TRY
2568/* okay - patient stuff here. then history */
2569
2570/****** Object: Table [PTinfo].[tIdentityType] Script Date: 06/19/2013 11:35:53 ******/
2571SET ANSI_NULLS ON
2572SET QUOTED_IDENTIFIER ON
2573SET ANSI_PADDING ON
2574CREATE TABLE [PTinfo].[tIdentityType](
2575 [identTypeID] [tinyint] IDENTITY(1,1) NOT NULL,
2576 [fieldID] [smallint] NOT NULL,
2577
2578 CONSTRAINT [PK_tIdentityType] PRIMARY KEY CLUSTERED
2579(
2580 [identTypeID] ASC
2581)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
2582) ON [PRIMARY]
2583SET ANSI_PADDING OFF
2584
2585ALTER TABLE [PTinfo].[tIdentityType] WITH CHECK ADD CONSTRAINT [FK_tIdentityType_tHL7MsgField] FOREIGN KEY([fieldID])
2586REFERENCES [PTinfo].[tHL7MsgField] ([fieldID])
2587
2588ALTER TABLE [PTinfo].[tIdentityType] CHECK CONSTRAINT [FK_tIdentityType_tHL7MsgField]
2589
2590 PRINT N'Table PTinfo.tIdentityType created'
2591
2592END TRY
2593BEGIN CATCH
2594 PRINT N'Table PTinfo.tIdentityType create failed'
2595 PRINT 'Error Number: ' + str(error_number())
2596 PRINT 'Line Number: ' + str(error_line())
2597 PRINT error_message()
2598END CATCH
2599GO
2600
2601SET IDENTITY_INSERT [PTinfo].[tIdentityType] ON;
2602GO
2603
2604BEGIN TRY
2605INSERT INTO [PTinfo].[tIdentityType]
2606 ([identTypeID]
2607 , [fieldID])
2608 VALUES
2609 (1, 2)
2610 , (2, 9)
2611 , (3, 1)
2612
2613 PRINT N'Table PTinfo.tIdentityType data inserted'
2614
2615END TRY
2616BEGIN CATCH
2617 PRINT N'Table PTinfo.tIdentityType data insert failed'
2618 PRINT 'Error Number: ' + str(error_number())
2619 PRINT 'Line Number: ' + str(error_line())
2620 PRINT error_message()
2621END CATCH
2622GO
2623
2624SET IDENTITY_INSERT [PTinfo].[tIdentityType] OFF;
2625GO
2626
2627BEGIN TRY
2628EXECUTE ('/*--------------------------------------------------
2629This encapsulates getting the primary ID type from the settings.
2630Created Date: 2013-11-21
2631--this is only setting a param. Otherwise I would need to use an inline TVF if it was used as part of a join in a query.
2632*/
2633
2634CREATE FUNCTION PTinfo.udfGetPIDType ()
2635RETURNS TINYINT
2636WITH SCHEMABINDING
2637
2638AS
2639BEGIN;
2640
2641 DECLARE @priPID tinyint;
2642
2643 SET @priPID = (SELECT PIT.identTypeID
2644 FROM SysInfo.tSetting StS
2645 INNER JOIN PTinfo.tHL7MsgField PHM ON StS.setValue = PHM.fieldNum
2646 INNER JOIN PTinfo.tIdentityType PIT ON PHM.fieldID = PIT.fieldID
2647 WHERE StS.settingID = 32);
2648
2649 RETURN(@priPID);
2650END;
2651')
2652
2653 PRINT N'User Defined Function PTinfo.udfGetPIDType created'
2654
2655END TRY
2656BEGIN CATCH
2657 PRINT N'User Defined Function PTinfo.udfGetPIDType create failed'
2658 PRINT 'Error Number: ' + str(error_number())
2659 PRINT 'Line Number: ' + str(error_line())
2660 PRINT error_message()
2661END CATCH
2662GO
2663
2664BEGIN TRY
2665/****** Object: Table [PTinfo].[tPatient] Script Date: 06/19/2013 11:35:53 ******/
2666SET ANSI_NULLS ON
2667SET QUOTED_IDENTIFIER ON
2668SET ANSI_PADDING ON
2669CREATE TABLE [PTinfo].[tPatient](
2670 [pSysID] [bigint] IDENTITY(1,1) NOT NULL,
2671 [firstName] [varchar](50) NOT NULL,
2672 [lastName] [varchar](50) NOT NULL,
2673 [midInit] [char](1) NULL,
2674 [dob] [datetime] NOT NULL,
2675 [recordAdded] [datetime] NOT NULL,
2676
2677 CONSTRAINT [PK_tPatient] PRIMARY KEY CLUSTERED
2678(
2679 [pSysID] ASC
2680)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
2681) ON [PRIMARY]
2682SET ANSI_PADDING OFF
2683
2684
2685 PRINT N'Table PTinfo.tPatient created'
2686
2687END TRY
2688BEGIN CATCH
2689 PRINT N'Table PTinfo.tPatient create failed'
2690 PRINT 'Error Number: ' + str(error_number())
2691 PRINT 'Line Number: ' + str(error_line())
2692 PRINT error_message()
2693END CATCH
2694GO
2695
2696BEGIN TRY
2697/* actual location stuff */
2698/****** Object: Table [Storage].[tLocationAssign] Script Date: 06/19/2013 11:35:53 ******/
2699SET ANSI_NULLS ON
2700SET QUOTED_IDENTIFIER ON
2701SET ANSI_PADDING ON
2702CREATE TABLE [Storage].[tLocationAssign](
2703 [locAssignID] [bigint] IDENTITY(100,1) NOT NULL,
2704 [locationID] [bigint] NOT NULL,
2705 [pSysID] [bigint] NOT NULL,
2706
2707
2708 CONSTRAINT [PK_tLocationAssign] PRIMARY KEY CLUSTERED
2709(
2710 [locAssignID] ASC
2711)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
2712) ON [PRIMARY]
2713SET ANSI_PADDING OFF
2714
2715ALTER TABLE [Storage].[tLocationAssign] WITH CHECK ADD CONSTRAINT [FK_tLocationAssign_tLocation] FOREIGN KEY([locationID])
2716REFERENCES [Storage].[tLocation] ([locationID])
2717ALTER TABLE [Storage].[tLocationAssign] CHECK CONSTRAINT [FK_tLocationAssign_tLocation]
2718
2719ALTER TABLE [Storage].[tLocationAssign] WITH CHECK ADD CONSTRAINT [FK_tLocationAssign_tPatient] FOREIGN KEY([pSysID])
2720REFERENCES [PTinfo].[tPatient] ([pSysID])
2721ALTER TABLE [Storage].[tLocationAssign] CHECK CONSTRAINT [FK_tLocationAssign_tPatient]
2722
2723 PRINT N'Table Storage.tLocationAssign created'
2724
2725END TRY
2726BEGIN CATCH
2727 PRINT N'Table Storage.tLocationAssign create failed'
2728 PRINT 'Error Number: ' + str(error_number())
2729 PRINT 'Line Number: ' + str(error_line())
2730 PRINT error_message()
2731END CATCH
2732GO
2733
2734BEGIN TRY
2735/****** Object: Table [PTinfo].[tIdentity] Script Date: 06/19/2013 11:35:53 ******/
2736SET ANSI_NULLS ON
2737SET QUOTED_IDENTIFIER ON
2738SET ANSI_PADDING ON
2739CREATE TABLE [PTinfo].[tIdentity](
2740 [pID][bigint] IDENTITY(1,1) NOT NULL,
2741 [pSysID] [bigint] NOT NULL,
2742 [identTypeID] [tinyint] NOT NULL,
2743 [IDvalue] [varchar](50) NOT NULL,
2744
2745CONSTRAINT [PK_tIdentity] PRIMARY KEY CLUSTERED
2746(
2747 [pID] ASC
2748)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
2749) ON [PRIMARY]
2750SET ANSI_PADDING OFF
2751
2752ALTER TABLE [PTinfo].[tIdentity] WITH CHECK ADD CONSTRAINT [FK_tIdentity_tIdentityType] FOREIGN KEY([identTypeID])
2753REFERENCES [PTinfo].[tIdentityType] ([identTypeID])
2754ALTER TABLE [PTinfo].[tIdentity] CHECK CONSTRAINT [FK_tIdentity_tIdentityType]
2755
2756ALTER TABLE [PTinfo].[tIdentity] WITH CHECK ADD CONSTRAINT [FK_tIdentity_tPatient] FOREIGN KEY([pSysID])
2757REFERENCES [PTinfo].[tPatient] ([pSysID])
2758ALTER TABLE [PTinfo].[tIdentity] CHECK CONSTRAINT [FK_tIdentity_tPatient]
2759
2760ALTER TABLE [PTinfo].[tIdentity] ADD CONSTRAINT [UQ_tIdentity_perType] UNIQUE([pSysID],[identTypeID])
2761
2762 PRINT N'Table PTinfo.tIdentity created'
2763
2764END TRY
2765BEGIN CATCH
2766 PRINT N'Table PTinfo.tIdentity create failed'
2767 PRINT 'Error Number: ' + str(error_number())
2768 PRINT 'Line Number: ' + str(error_line())
2769 PRINT error_message()
2770END CATCH
2771GO
2772
2773BEGIN TRY
2774/* patient status */
2775
2776/****** Object: Table [PTinfo].[tStatusType] Script Date: 06/19/2013 11:35:53 ******/
2777SET ANSI_NULLS ON
2778SET QUOTED_IDENTIFIER ON
2779SET ANSI_PADDING ON
2780CREATE TABLE [PTinfo].[tStatusType](
2781 [pStatTypeID][tinyint] IDENTITY(1,1) NOT NULL,
2782 [pStatTypeName] [varchar] (15) NOT NULL,
2783
2784CONSTRAINT [PK_tpStatusType] PRIMARY KEY CLUSTERED
2785(
2786 [pStatTypeID] ASC
2787)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
2788) ON [PRIMARY]
2789SET ANSI_PADDING OFF
2790
2791 PRINT N'Table PTinfo.tStatusType created'
2792
2793END TRY
2794BEGIN CATCH
2795 PRINT N'Table PTinfo.tStatusType create failed'
2796 PRINT 'Error Number: ' + str(error_number())
2797 PRINT 'Line Number: ' + str(error_line())
2798 PRINT error_message()
2799END CATCH
2800GO
2801
2802SET IDENTITY_INSERT [PTinfo].[tStatusType] ON;
2803GO
2804
2805BEGIN TRY
2806
2807INSERT INTO [PTinfo].[tStatusType]
2808 ([pStatTypeID]
2809 , [pStatTypeName])
2810 VALUES
2811 (1, 'Active')
2812 , (2,'Discharged')
2813 ;
2814
2815 PRINT N'Table PTinfo.tStatusType data inserted'
2816
2817END TRY
2818BEGIN CATCH
2819 PRINT N'Table PTinfo.tStatusType data insert failed'
2820 PRINT 'Error Number: ' + str(error_number())
2821 PRINT 'Line Number: ' + str(error_line())
2822 PRINT error_message()
2823END CATCH
2824GO
2825
2826SET IDENTITY_INSERT [PTinfo].[tStatusType] OFF;
2827GO
2828
2829BEGIN TRY
2830/****** Object: Table [PTinfo].[tStatus] Script Date: 06/19/2013 11:35:53 ******/
2831SET ANSI_NULLS ON
2832SET QUOTED_IDENTIFIER ON
2833SET ANSI_PADDING ON
2834CREATE TABLE [PTinfo].[tStatus](
2835 [pStatID][bigint] IDENTITY(1,1) NOT NULL,
2836 [pSysID] [bigint] NOT NULL,
2837 [pStatTypeID] [tinyint] NOT NULL,
2838 [statDate] [datetime] NOT NULL,
2839 [isCurrent] bit NOT NULL,
2840
2841CONSTRAINT [PK_tStatus] PRIMARY KEY CLUSTERED
2842(
2843 [pStatID] ASC
2844)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
2845) ON [PRIMARY]
2846SET ANSI_PADDING OFF
2847
2848
2849ALTER TABLE [PTinfo].[tStatus] WITH CHECK ADD CONSTRAINT [FK_tStatus_tStatusType] FOREIGN KEY([pStatTypeID])
2850REFERENCES [PTinfo].[tStatusType] ([pStatTypeID])
2851ALTER TABLE [PTinfo].[tStatus] CHECK CONSTRAINT [FK_tStatus_tStatusType]
2852
2853
2854ALTER TABLE [PTinfo].[tStatus] WITH CHECK ADD CONSTRAINT [FK_tStatus_tPatient] FOREIGN KEY([pSysID])
2855REFERENCES [PTinfo].[tPatient] ([pSysID])
2856ALTER TABLE [PTinfo].[tStatus] CHECK CONSTRAINT [FK_tStatus_tPatient]
2857
2858ALTER TABLE [PTinfo].[tStatus] ADD CONSTRAINT [DF_tStatus_isCurrent] DEFAULT ((1)) FOR [isCurrent]
2859
2860
2861 PRINT N'Table PTinfo.tStatus created'
2862
2863END TRY
2864BEGIN CATCH
2865 PRINT N'Table PTinfo.tStatus create failed'
2866 PRINT 'Error Number: ' + str(error_number())
2867 PRINT 'Line Number: ' + str(error_line())
2868 PRINT error_message()
2869END CATCH
2870GO
2871
2872BEGIN TRY
2873/* child addtl info */
2874
2875/****** Object: Table [PTinfo].[tChildLink] Script Date: 06/19/2013 11:35:53 ******/
2876SET ANSI_NULLS ON
2877SET QUOTED_IDENTIFIER ON
2878SET ANSI_PADDING ON
2879CREATE TABLE [PTinfo].[tChildLink](
2880 [childLinkID][bigint] IDENTITY(1,1) NOT NULL,
2881 [pSysID] [bigint] NOT NULL,
2882 [motherSysID] [bigint] NOT NULL,
2883 [multiple] [char](1) NOT NULL,
2884 [gender] [varchar](15) NOT NULL,
2885 [allowFormula] [bit] NOT NULL,
2886 [allowBottle] [bit] NOT NULL,
2887 [location] varchar(50) NULL,
2888
2889CONSTRAINT [PK_tChildLink] PRIMARY KEY CLUSTERED
2890(
2891 [childLinkID] ASC
2892)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
2893) ON [PRIMARY]
2894SET ANSI_PADDING OFF
2895
2896ALTER TABLE [PTinfo].[tChildLink] WITH CHECK ADD CONSTRAINT [FK_tChildLink_tPatient] FOREIGN KEY([pSysID])
2897REFERENCES [PTinfo].[tPatient] ([pSysID])
2898ALTER TABLE [PTinfo].[tChildLink] CHECK CONSTRAINT [FK_tChildLink_tPatient]
2899
2900ALTER TABLE [PTinfo].[tChildLink] WITH CHECK ADD CONSTRAINT [FK_tChildLink_tPatientMom] FOREIGN KEY([motherSysID])
2901REFERENCES [PTinfo].[tPatient] ([pSysID])
2902ALTER TABLE [PTinfo].[tChildLink] CHECK CONSTRAINT [FK_tChildLink_tPatientMom]
2903
2904
2905ALTER TABLE [PTinfo].[tChildLink] ADD CONSTRAINT [DF_tChildLink_multiple] DEFAULT ((0)) FOR [multiple]
2906ALTER TABLE [PTinfo].[tChildLink] ADD CONSTRAINT [DF_tChildLink_allowBottle] DEFAULT ((1)) FOR [allowBottle]
2907ALTER TABLE [PTinfo].[tChildLink] ADD CONSTRAINT [DF_tChildLink_allowFormula] DEFAULT ((1)) FOR [allowFormula]
2908
2909
2910 PRINT N'Table PTinfo.tChildLink created'
2911
2912END TRY
2913BEGIN CATCH
2914 PRINT N'Table PTinfo.tChildLink create failed'
2915 PRINT 'Error Number: ' + str(error_number())
2916 PRINT 'Line Number: ' + str(error_line())
2917 PRINT error_message()
2918END CATCH
2919GO
2920
2921BEGIN TRY
2922/* mother EBM cals */
2923
2924/****** Object: Table [PTinfo].[tMomInfo] Script Date: 06/19/2013 11:35:53 ******/
2925SET ANSI_NULLS ON
2926SET QUOTED_IDENTIFIER ON
2927SET ANSI_PADDING ON
2928CREATE TABLE [PTinfo].[tMomInfo](
2929 [momInfoID][bigint] IDENTITY(1,1) NOT NULL,
2930 [pSysID] [bigint] NOT NULL,
2931 [calsPer] [decimal] (6,2) NOT NULL,
2932
2933CONSTRAINT [PK_tMomInfo] PRIMARY KEY CLUSTERED
2934(
2935 [momInfoID] ASC
2936)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
2937) ON [PRIMARY]
2938SET ANSI_PADDING OFF
2939
2940ALTER TABLE [PTinfo].[tMomInfo] WITH CHECK ADD CONSTRAINT [FK_tMomInfo_tPatient] FOREIGN KEY([pSysID])
2941REFERENCES [PTinfo].[tPatient] ([pSysID])
2942ALTER TABLE [PTinfo].[tMomInfo] CHECK CONSTRAINT [FK_tMomInfo_tPatient]
2943
2944ALTER TABLE [PTinfo].[tMomInfo] ADD CONSTRAINT [DF_tMomInfo_calsPer] DEFAULT ((20)) FOR [calsPer]
2945
2946
2947 PRINT N'Table PTinfo.tMomInfo created'
2948
2949END TRY
2950BEGIN CATCH
2951 PRINT N'Table PTinfo.tMomInfo create failed'
2952 PRINT 'Error Number: ' + str(error_number())
2953 PRINT 'Line Number: ' + str(error_line())
2954 PRINT error_message()
2955END CATCH
2956GO
2957
2958BEGIN TRY
2959/* docs orders */
2960/****** Object: Table [PTinfo].[tOrderType] Script Date: 06/19/2013 11:35:53 ******/
2961SET ANSI_NULLS ON
2962SET QUOTED_IDENTIFIER ON
2963SET ANSI_PADDING ON
2964CREATE TABLE [PTinfo].[tOrderType](
2965 [OrderTypeID] [tinyint] IDENTITY(1,1) NOT NULL,
2966 [orderTypeName] [varchar](15) NOT NULL,
2967
2968 CONSTRAINT [PK_tOrderType] PRIMARY KEY CLUSTERED
2969(
2970 [orderTypeID] ASC
2971)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
2972) ON [PRIMARY]
2973SET ANSI_PADDING OFF
2974
2975
2976 PRINT N'Table PTinfo.tOrderType created'
2977
2978END TRY
2979BEGIN CATCH
2980 PRINT N'Table PTinfo.tOrderType create failed'
2981 PRINT 'Error Number: ' + str(error_number())
2982 PRINT 'Line Number: ' + str(error_line())
2983 PRINT error_message()
2984END CATCH
2985GO
2986
2987SET IDENTITY_INSERT [PTinfo].[tOrderType] ON;
2988GO
2989
2990BEGIN TRY
2991
2992INSERT INTO [PTinfo].[tOrderType]
2993 ([orderTypeID]
2994 , [orderTypeName])
2995 VALUES
2996 (1, 'Allow Donor')
2997 , (2,'Fortification')
2998 , (3,'Formula')
2999 , (4, 'NPO')
3000 ;
3001
3002 PRINT N'Table PTinfo.tOrderType data inserted'
3003
3004END TRY
3005BEGIN CATCH
3006 PRINT N'Table PTinfo.tOrderType data insert failed'
3007 PRINT 'Error Number: ' + str(error_number())
3008 PRINT 'Line Number: ' + str(error_line())
3009 PRINT error_message()
3010END CATCH
3011GO
3012
3013SET IDENTITY_INSERT [PTinfo].[tOrderType] OFF;
3014GO
3015
3016BEGIN TRY
3017/****** Object: Table [PTinfo].[tDocOrder] Script Date: 06/19/2013 11:35:53 ******/
3018SET ANSI_NULLS ON
3019SET QUOTED_IDENTIFIER ON
3020SET ANSI_PADDING ON
3021CREATE TABLE [PTinfo].[tDocOrder](
3022 [docOrderID][bigint] IDENTITY(1,1) NOT NULL,
3023 [pSysID] [bigint] NOT NULL,
3024 [orderNum][varchar](50) NOT NULL,
3025 [orderTypeID][tinyint] NOT NULL,
3026 [orderNote][varchar](500) NOT NULL,
3027 [orderStart] [datetime] NOT NULL,
3028 [orderEnd] [datetime] NOT NULL,
3029
3030CONSTRAINT [PK_tDocOrder] PRIMARY KEY CLUSTERED
3031(
3032 [docOrderID] ASC
3033)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
3034) ON [PRIMARY]
3035SET ANSI_PADDING OFF
3036
3037ALTER TABLE [PTinfo].[tDocOrder] WITH CHECK ADD CONSTRAINT [FK_tDocOrder_tPatient] FOREIGN KEY([pSysID])
3038REFERENCES [PTinfo].[tPatient] ([pSysID])
3039ALTER TABLE [PTinfo].[tDocOrder] CHECK CONSTRAINT [FK_tDocOrder_tPatient]
3040
3041ALTER TABLE [PTinfo].[tDocOrder] WITH CHECK ADD CONSTRAINT [FK_tDocOrder_tOrderType] FOREIGN KEY([orderTypeID])
3042REFERENCES [PTinfo].[tOrderType] ([orderTypeID])
3043ALTER TABLE [PTinfo].[tDocOrder] CHECK CONSTRAINT [FK_tDocOrder_tOrderType]
3044
3045
3046ALTER TABLE [PTinfo].[tDocOrder] ADD CONSTRAINT [DF_tDocOrder_orderNum] DEFAULT (('none')) FOR [orderNum]
3047
3048ALTER TABLE [PTinfo].[tDocOrder] ADD CONSTRAINT [DF_tDocOrder_orderNote] DEFAULT (('none')) FOR [orderNote]
3049
3050ALTER TABLE [PTinfo].[tDocOrder] ADD CONSTRAINT [DF_tDocOrder_orderEnd] DEFAULT (('2500-01-01')) FOR [orderEnd]
3051
3052
3053 PRINT N'Table PTinfo.tDocOrder created'
3054
3055END TRY
3056BEGIN CATCH
3057 PRINT N'Table PTinfo.tDocOrder create failed'
3058 PRINT 'Error Number: ' + str(error_number())
3059 PRINT 'Line Number: ' + str(error_line())
3060 PRINT error_message()
3061END CATCH
3062GO
3063
3064BEGIN TRY
3065/* mother-vessel join */
3066
3067/****** Object: Table [Nutrition].[tMotherVessel] Script Date: 06/19/2013 11:35:53 ******/
3068SET ANSI_NULLS ON
3069SET QUOTED_IDENTIFIER ON
3070SET ANSI_PADDING ON
3071CREATE TABLE [Nutrition].[tMotherVessel](
3072 [vesselMomID] [bigint] IDENTITY(1,1) NOT NULL,
3073 [pSysID] [bigint] NOT NULL,
3074 [vesselID] [bigint] NOT NULL,
3075 CONSTRAINT [PK_tMotherVessel] PRIMARY KEY CLUSTERED
3076(
3077 [vesselMomID] ASC
3078)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
3079) ON [PRIMARY]
3080SET ANSI_PADDING OFF
3081
3082
3083ALTER TABLE [Nutrition].[tMotherVessel] WITH CHECK ADD CONSTRAINT [FK_tMotherVessel_tPatient] FOREIGN KEY([pSysID])
3084REFERENCES [PTinfo].[tPatient] ([pSysID])
3085ALTER TABLE [Nutrition].[tMotherVessel] CHECK CONSTRAINT [FK_tMotherVessel_tPatient]
3086
3087ALTER TABLE [Nutrition].[tMotherVessel] WITH CHECK ADD CONSTRAINT [FK_tMotherVessel_tVessel] FOREIGN KEY([vesselID])
3088REFERENCES [Nutrition].[tVessel] ([vesselID])
3089ALTER TABLE [Nutrition].[tMotherVessel] CHECK CONSTRAINT [FK_tMotherVessel_tVessel]
3090
3091
3092 PRINT N'Table Nutrition.tMotherVessel created'
3093
3094END TRY
3095BEGIN CATCH
3096 PRINT N'Table Nutrition.tMotherVessel create failed'
3097 PRINT 'Error Number: ' + str(error_number())
3098 PRINT 'Line Number: ' + str(error_line())
3099 PRINT error_message()
3100END CATCH
3101GO
3102
3103BEGIN TRY
3104/* assign bottles */
3105/****** Object: Table [Nutrition].[tVesselAssign] Script Date: 06/19/2013 11:35:53 ******/
3106SET ANSI_NULLS ON
3107SET QUOTED_IDENTIFIER ON
3108SET ANSI_PADDING ON
3109CREATE TABLE [Nutrition].[tVesselAssign](
3110 [vesselAssignID] [bigint] IDENTITY(1,1) NOT NULL,
3111 [pSysID] [bigint] NOT NULL,
3112 [vesselID] [bigint] NOT NULL,
3113 CONSTRAINT [PK_tVesselAssign] PRIMARY KEY CLUSTERED
3114(
3115 [vesselAssignID] ASC
3116)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
3117) ON [PRIMARY]
3118SET ANSI_PADDING OFF
3119
3120
3121ALTER TABLE [Nutrition].[tVesselAssign] WITH CHECK ADD CONSTRAINT [FK_tVesselAssign_tPatient] FOREIGN KEY([pSysID])
3122REFERENCES [PTinfo].[tPatient] ([pSysID])
3123ALTER TABLE [Nutrition].[tVesselAssign] CHECK CONSTRAINT [FK_tVesselAssign_tPatient]
3124
3125
3126ALTER TABLE [Nutrition].[tVesselAssign] WITH CHECK ADD CONSTRAINT [FK_tVesselAssign_tVessel] FOREIGN KEY([vesselID])
3127REFERENCES [Nutrition].[tVessel] ([vesselID])
3128ALTER TABLE [Nutrition].[tVesselAssign] CHECK CONSTRAINT [FK_tVesselAssign_tVessel]
3129
3130ALTER TABLE [Nutrition].[tVesselAssign] ADD CONSTRAINT [UQ_tVesselAssign_vesselID] UNIQUE([vesselID])
3131
3132 PRINT N'Table Nutrition.tVesselAssign created'
3133
3134END TRY
3135BEGIN CATCH
3136 PRINT N'Table Nutrition.tVesselAssign create failed'
3137 PRINT 'Error Number: ' + str(error_number())
3138 PRINT 'Line Number: ' + str(error_line())
3139 PRINT error_message()
3140END CATCH
3141GO
3142
3143BEGIN TRY
3144/* container history */
3145/****** Object: Table [Nutrition].[tVesselHistory] Script Date: 06/19/2013 11:35:53 ******/
3146SET ANSI_NULLS ON
3147SET QUOTED_IDENTIFIER ON
3148SET ANSI_PADDING ON
3149CREATE TABLE [Nutrition].[tVesselHistory](
3150 [vesselHistoryID] [bigint] IDENTITY(1,1) NOT NULL,
3151 [vesselID] [bigint] NOT NULL,
3152 [statusID] [tinyint] NOT NULL,
3153 [dateChange] datetime NOT NULL,
3154 [userChange] varchar(50) NOT NULL,
3155 [expirDate] datetime NOT NULL,
3156 [isCurrent] bit NOT NULL,
3157 CONSTRAINT [PK_tVesselHistory] PRIMARY KEY CLUSTERED
3158(
3159 [VesselHistoryID] ASC
3160)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
3161) ON [PRIMARY]
3162SET ANSI_PADDING OFF
3163
3164
3165ALTER TABLE [Nutrition].[tVesselHistory] WITH CHECK ADD CONSTRAINT [FK_tVesselHistory_tStatusList] FOREIGN KEY([statusID])
3166REFERENCES [Nutrition].[tStatusList] ([statusID])
3167ALTER TABLE [Nutrition].[tVesselHistory] CHECK CONSTRAINT [FK_tVesselHistory_tStatusList]
3168
3169ALTER TABLE [Nutrition].[tVesselHistory] WITH CHECK ADD CONSTRAINT [FK_tVesselHistory_tVessel] FOREIGN KEY([vesselID])
3170REFERENCES [Nutrition].[tVessel] ([vesselID])
3171ALTER TABLE [Nutrition].[tVesselHistory] CHECK CONSTRAINT [FK_tVesselHistory_tVessel]
3172
3173ALTER TABLE [Nutrition].[tVesselHistory] ADD CONSTRAINT [DF_tVessel_isCurrent] DEFAULT ((1)) FOR [isCurrent]
3174
3175
3176 PRINT N'Table Nutrition.tVesselHistory created'
3177
3178END TRY
3179BEGIN CATCH
3180 PRINT N'Table Nutrition.tVesselHistory create failed'
3181 PRINT 'Error Number: ' + str(error_number())
3182 PRINT 'Line Number: ' + str(error_line())
3183 PRINT error_message()
3184END CATCH
3185GO
3186
3187BEGIN TRY
3188/*
3189Bottle history. Note: if fortified, then fresh, frozen and thawed can't be isCurrent....
3190The plan of action is this in terms of an "isCurrent" status:
3191Deal with never recd (along with issued event).
3192Then do the received, then pumped, events.
3193Deal with Fed.
3194Deal with disposed.
3195Deal with expired.
3196Then do fresh.
3197Then do frozen.
3198Then do thawed.
3199THEN do fortified.
3200The do combined stuff.
3201
3202*/
3203
3204/* Issued - the status and the event */
3205
3206INSERT INTO Nutrition.tVesselHistory (vesselID, statusID, dateChange, userChange, expirDate, isCurrent)
3207
3208SELECT NtV.vesselID
3209 , 1
3210 , dB.dateIssued
3211 , 'none Noted'
3212 , COALESCE(dB.expires, '2500-01-01')
3213 , CASE WHEN dB.dateReceived IS NULL THEN
3214 CASE WHEN dB.[status] = 'Issued' then 1 ELSE 0 END
3215 ELSE 0 END
3216
3217FROM Nutrition.tVessel NtV
3218 INNER JOIN dbo.tblBottles dB on NtV.vesselCode = dB.bottleNumber
3219
3220UNION ALL
3221
3222SELECT NtV.vesselID
3223 , 1
3224 , dF.filledOn
3225 , 'none Noted'
3226 , COALESCE(dF.Expires,'2500-01-01')
3227 , CASE WHEN dF.[Status] = 'Not Filled' THEN 1 ELSE 0 END
3228
3229FROM Nutrition.tVessel NtV
3230 INNER JOIN dbo.tblFeedings dF on NtV.vesselCode = dF.FeedingNumber;
3231 ;
3232
3233
3234 /* date received */
3235INSERT INTO Nutrition.tVesselHistory (vesselID, statusID, dateChange, userChange, expirDate, isCurrent)
3236
3237SELECT NtV.vesselID
3238 , 17
3239 , COALESCE(dB.dateReceived, '1900-01-01')
3240 , 'none Noted'
3241 , COALESCE(dB.expires, '2500-01-01')
3242 , 0
3243
3244FROM Nutrition.tVessel NtV
3245 INNER JOIN dbo.tblBottles dB on NtV.vesselCode = dB.bottleNumber
3246
3247 WHERE dB.dateReceived IS NOT NULL
3248 AND dB.[Status] <> 'Issued'
3249UNION ALL
3250SELECT NtV.vesselID
3251 , 17
3252 , dF.filledOn
3253 , dF.Filler
3254 , COALESCE(dF.Expires,'2500-01-01')
3255 , 0
3256
3257FROM Nutrition.tVessel NtV
3258 INNER JOIN dbo.tblFeedings dF on NtV.vesselCode = dF.FeedingNumber;
3259
3260/* date pumped - because, why the heck not. */
3261INSERT INTO Nutrition.tVesselHistory (vesselID, statusID, dateChange, userChange, expirDate, isCurrent)
3262
3263SELECT NtV.vesselID
3264 , 14
3265 , COALESCE(dB.datePumped, '1900-01-01')
3266 , 'none Noted'
3267 , COALESCE(dB.expires, '2500-01-01')
3268 , 0
3269
3270FROM Nutrition.tVessel NtV
3271 INNER JOIN dbo.tblBottles dB on NtV.vesselCode = dB.bottleNumber
3272
3273 WHERE dB.datePumped IS NOT NULL
3274 AND dB.[Status] <> 'Issued'
3275UNION ALL
3276SELECT NtV.vesselID
3277 , 14
3278 , dF.filledOn
3279 , dF.Filler
3280 , COALESCE(dF.Expires,'2500-01-01')
3281 , 0
3282
3283FROM Nutrition.tVessel NtV
3284 INNER JOIN dbo.tblFeedings dF on NtV.vesselCode = dF.FeedingNumber;
3285
3286
3287
3288/* so let's deal with fed. */
3289INSERT INTO Nutrition.tVesselHistory (vesselID, statusID, dateChange, userChange, expirDate, isCurrent)
3290
3291SELECT NtV.vesselID
3292 , 7
3293 , dB.dateFed
3294 , dB.feeder
3295 , COALESCE(dB.expires, '2500-01-01')
3296 , 1
3297
3298FROM Nutrition.tVessel NtV
3299 INNER JOIN dbo.tblBottles dB on NtV.vesselCode = dB.bottleNumber
3300
3301WHERE dB.dateFed IS NOT NULL
3302
3303UNION ALL
3304
3305SELECT NtV.vesselID
3306 , 7
3307 , dF.dateFed
3308 , dF.feeder
3309 , COALESCE(dF.expires, '2500-01-01')
3310 , 1
3311
3312FROM Nutrition.tVessel NtV
3313 INNER JOIN dbo.tblFeedings dF on NtV.vesselCode = dF.feedingNumber
3314WHERE df.dateFed IS NOT NULL; ;
3315
3316/* disposed stuff */
3317INSERT INTO Nutrition.tVesselHistory (vesselID, statusID, dateChange, userChange, expirDate, isCurrent)
3318
3319SELECT NtV.vesselID
3320 , 16
3321 , dB.DateDisposed
3322 , 'none Noted'
3323 , COALESCE(dB.expires, '2500-01-01')
3324 , 1
3325
3326FROM Nutrition.tVessel NtV
3327 INNER JOIN dbo.tblBottles dB on NtV.vesselCode = dB.bottleNumber
3328
3329WHERE dB.[Status] = 'Disposed'
3330 AND dB.DisposalReason = 'Combined'
3331 AND dB.dateFed IS NULL
3332
3333UNION ALL
3334
3335SELECT NtV.vesselID
3336 , 16
3337 , dB.DateDisposed
3338 , 'none Noted'
3339 , COALESCE(dB.expires, '2500-01-01')
3340 , 1
3341
3342FROM Nutrition.tVessel NtV
3343 INNER JOIN dbo.tblBottles dB on NtV.vesselCode = dB.bottleNumber
3344
3345WHERE dB.[Status] = 'Disposed'
3346 AND (dB.DisposalReason = 'Added to Syringe'
3347 OR dB.DisposalReason = 'Split Function')
3348 AND dB.dateFed IS NULL
3349
3350UNION ALL
3351SELECT NtV.vesselID
3352 , NtSL.statusID
3353 , COALESCE(dB.DateDisposed, '1900-01-01')
3354 , 'none Noted'
3355 , COALESCE(dB.expires, '2500-01-01')
3356 , 1
3357
3358FROM Nutrition.tVessel NtV
3359 INNER JOIN dbo.tblBottles dB on NtV.vesselCode = dB.bottleNumber
3360 INNER JOIN Nutrition.tStatusList NtSl ON dB.DisposalReason = NtSL.statusName
3361WHERE dB.[Status] = 'Disposed'
3362 AND NtSL.statCatID = 4
3363 AND dB.dateFed IS NULL
3364
3365UNION ALL
3366SELECT NtV.vesselID
3367 , NtSL.statusID
3368 , dF.DateDisposed
3369 , 'none Noted'
3370 , COALESCE(dF.expires, '2500-01-01')
3371 , 1
3372
3373FROM Nutrition.tVessel NtV
3374 INNER JOIN dbo.tblFeedings dF on NtV.vesselCode = dF.feedingNumber
3375 INNER JOIN Nutrition.tStatusList NtSl ON dF.DisposalReason = NtSL.statusName
3376WHERE dF.[Status] = 'Disposed'
3377 AND NtSL.statCatID = 4
3378 AND df.dateFed IS NULL
3379 ;
3380
3381/* expired stuff */
3382INSERT INTO Nutrition.tVesselHistory (vesselID, statusID, dateChange, userChange, expirDate, isCurrent)
3383
3384SELECT NtV.vesselID
3385 , 6
3386 , COALESCE(dB.expires, '1900-01-01')
3387 , 'none Noted'
3388 , db.Expires
3389 , 1
3390
3391FROM Nutrition.tVessel NtV
3392 INNER JOIN dbo.tblBottles dB on NtV.vesselCode = dB.bottleNumber
3393
3394WHERE dB.[Status] = 'Expired'
3395 AND dB.dateFed IS NULL
3396
3397UNION ALL
3398
3399SELECT NtV.vesselID
3400 , 6
3401 , COALESCE(dF.expires, '2500-01-01')
3402 , 'none Noted'
3403 , COALESCE(dF.expires, '2500-01-01')
3404 , 1
3405
3406FROM Nutrition.tVessel NtV
3407 INNER JOIN dbo.tblFeedings dF on NtV.vesselCode = dF.feedingNumber
3408
3409WHERE dF.[Status] = 'Expired'
3410 AND dF.dateDisposed IS NULL
3411 AND df.dateFed IS NULL;
3412
3413/* new/fresh - just as a note, syringes are all going to be considered thawed or fortified. */
3414INSERT INTO Nutrition.tVesselHistory (vesselID, statusID, dateChange, userChange, expirDate, isCurrent)
3415
3416SELECT NtV.vesselID
3417 , 2
3418 , dB.DateReceived
3419 , 'none Noted'
3420 , COALESCE(dB.expires, '2500-01-01')
3421 , 1
3422
3423FROM Nutrition.tVessel NtV
3424 INNER JOIN dbo.tblBottles dB on NtV.vesselCode = dB.bottleNumber
3425 WHERE dB.DateReceived IS NOT NULL
3426 AND (dB.[Status] = 'New'
3427 OR dB.[Status] = 'Fresh')
3428 AND dB.dateFed IS NULL
3429 AND dB.dateDisposed IS NULL
3430 AND dB.expires > getdate();
3431
3432/* frozen */
3433INSERT INTO Nutrition.tVesselHistory (vesselID, statusID, dateChange, userChange, expirDate, isCurrent)
3434
3435SELECT NtV.vesselID
3436 , 3
3437 , dB.dateReceived
3438 , 'none Noted'
3439 , COALESCE(dB.expires, '2500-01-01')
3440 , 1
3441
3442FROM Nutrition.tVessel NtV
3443 INNER JOIN dbo.tblBottles dB on NtV.vesselCode = dB.bottleNumber
3444
3445WHERE dB.dateReceived IS NOT NULL
3446 AND dB.[status] = 'Frozen'
3447 AND dB.dateFed IS NULL
3448 AND dB.dateDisposed IS NULL
3449 AND dB.expires > getdate();
3450
3451/* thawed - all syringes that aren't fed, expired or disposed are going to get this too b/c as far as statuses and "what you can do", thawed is the biggest equivalent */
3452
3453INSERT INTO Nutrition.tVesselHistory (vesselID, statusID, dateChange, userChange, expirDate, isCurrent)
3454
3455SELECT NtV.vesselID
3456 , 4
3457 , '1900-01-01'
3458 , 'none Noted'
3459 , COALESCE(dB.expires, '2500-01-01')
3460 , 1
3461
3462FROM Nutrition.tVessel NtV
3463 INNER JOIN dbo.tblBottles dB on NtV.vesselCode = dB.bottleNumber
3464
3465WHERE dB.dateReceived IS NOT NULL
3466 AND dB.[status] = 'Thawed'
3467 AND dB.dateFed IS NULL
3468 AND dB.dateDisposed IS NULL
3469 AND dB.expires > getdate()
3470
3471UNION ALL
3472SELECT NtV.vesselID
3473 , 4
3474 , dF.filledOn
3475 , dF.Filler
3476 , COALESCE(dF.expires, '2500-01-01')
3477 , 1
3478
3479FROM Nutrition.tVessel NtV
3480 INNER JOIN dbo.tblFeedings dF on NtV.vesselCode = dF.feedingNumber
3481
3482WHERE dF.[Status] = 'Filled'
3483 AND df.FilledOn IS NOT NULL
3484 AND df.dateFed IS NULL
3485 AND dF.dateDisposed IS NULL
3486 AND dF.expires > getdate();
3487
3488
3489/* fortified. Everything that's been fortified has to get a fortified status. */INSERT INTO Nutrition.tVesselHistory (vesselID, statusID, dateChange, userChange, expirDate, isCurrent)
3490
3491/* bottles */
3492SELECT NtV.vesselID
3493 , 5
3494 , NvH.dateChange
3495 , COALESCE(MAX(dAI.username), 'none Noted')
3496 , COALESCE(dB.expires, '2500-01-01')
3497 , CASE
3498 WHEN dB.dateFed IS NOT NULL THEN 0
3499 WHEN dB.dateDisposed IS NOT NULL THEN 0
3500 WHEN dB.Expires < getdate() THEN 0
3501 ELSE 1
3502 END
3503
3504FROM dbo.tblAdditivesIssued dAI
3505 INNER JOIN Nutrition.tVessel NtV ON dAI.milkID = NtV.vesselCode
3506 INNER JOIN Nutrition.tAdditiveList NAL ON dAI.Additive = NAL.additiveName
3507 INNER JOIn Nutrition.tVesselHistory NVH ON NtV.vesselID = NVH.vesselID
3508 INNER JOIN dbo.tblBottles dB on NtV.vesselCode = dB.bottleNumber
3509
3510WHERE NVH.isCurrent = 1
3511
3512GROUP BY NtV.vesselID
3513 , NvH.dateChange
3514 , NvH.expirDate
3515 , dB.[Status]
3516 , dB.Expires
3517 , dB.dateFed
3518 , dB.dateDisposed
3519 ;
3520
3521INSERT INTO Nutrition.tVesselHistory
3522(vesselID, statusID, dateChange, userChange, expirDate, isCurrent)
3523
3524SELECT NtV.vesselID
3525 , 5
3526 , NvH.dateChange
3527 , COALESCE(MAX(dAI.username), 'none Noted')
3528 , COALESCE(dF.expires, '2500-01-01')
3529 , CASE WHEN dF.[status] = 'Filled' THEN 1 ELSE 0 END
3530
3531
3532FROM dbo.tblAdditivesIssued dAI
3533 INNER JOIN Nutrition.tVessel NtV ON dAI.milkID = NtV.vesselCode
3534 INNER JOIN Nutrition.tAdditiveList NAL ON dAI.Additive = NAL.additiveName
3535 INNER JOIn Nutrition.tVesselHistory NVH ON NtV.vesselID = NVH.vesselID
3536 INNER JOIN dbo.tblFeedings dF on NtV.vesselCode = dF.feedingNumber
3537
3538WHERE NVH.isCurrent = 1
3539
3540GROUP BY NtV.vesselID
3541 , NvH.dateChange
3542 , NvH.expirDate
3543 , dF.[Status]
3544 , dF.Expires
3545 ;
3546
3547/* now for the ones who have fortified isCurrent, get rid of the other isCurrent.. if there is one */
3548
3549UPDATE NVH
3550SET NVH.isCurrent = 0
3551
3552FROM Nutrition.tVesselHistory NVH
3553 INNER JOIN Nutrition.tVesselHistory fortStat ON NVH.vesselID = fortStat.vesselID
3554
3555WHERE NVH.isCurrent = 1
3556 AND NVH.statusID < 5
3557 AND fortStat.isCurrent = 1
3558 AND fortStat.statusID = 5;
3559
3560
3561 PRINT N'Table Nutrition.tVesselHistory first set data inserted'
3562
3563END TRY
3564BEGIN CATCH
3565 PRINT N'Table Nutrition.tVesselHistory first set data insert failed'
3566 PRINT 'Error Number: ' + str(error_number())
3567 PRINT 'Line Number: ' + str(error_line())
3568 PRINT error_message()
3569END CATCH
3570GO
3571
3572/* how to do statuses for combined... issued for date combined, then the usual. */
3573BEGIN TRY
3574BEGIN TRANSACTION
3575
3576DECLARE @tempHisttbl TABLE (origVesselID bigint
3577 , tgtVesselID bigint
3578 , recordAdded DATETIME
3579 , userChange VARCHAR(50)
3580 , statusID tinyint
3581 , expDate datetime
3582 , currStatus varchar(25))
3583
3584INSERT INTO @tempHisttbl
3585 (origVesselID
3586 , tgtVesselID
3587 , recordAdded
3588 , userChange
3589 , statusID
3590 , expDate
3591 , currStatus)
3592
3593SELECT NtV.vesselID
3594 , TgT.vesselID
3595 , MB.dateCombined
3596 , MB.Username
3597 , 12
3598 , COALESCE(dB.expires, '2500-01-01')
3599 , dB.[status]
3600
3601FROM dbo.tblMixedBottles MB
3602 INNER JOIn Nutrition.tVessel NtV ON NtV.vesselCode = MB.OriginalBottle
3603 INNER JOIn Nutrition.tVessel TgT ON TgT.vesselCode = MB.NewBottle
3604 INNER JOIN dbo.tblBottles dB ON MB.originalBottle = dB.bottleNumber
3605
3606UNION ALL
3607
3608SELECT NtV.vesselID
3609 , TgT.vesselID
3610 , FH.fillDate
3611 , FH.filledBY
3612 , 12
3613 , db.Expires
3614 , dB.[status]
3615
3616FROM dbo.tblFillHistory FH
3617 INNER JOIn Nutrition.tVessel NtV ON NtV.vesselCode = FH.BottleNumber
3618 INNER JOIn Nutrition.tVessel TgT ON TgT.vesselCode = FH.FeedingNumber
3619 INNER JOIN dbo.tblBottles dB ON FH.bottleNumber = dB.bottleNumber;
3620
3621/* add to nutrition origin table */
3622INSERT INTO Nutrition.tVesselOrigin
3623 (vesselID
3624 , targetVesselID
3625 , mls)
3626
3627SELECT TH.origVesselID
3628 , TH.tgtVesselID
3629 , 1
3630
3631FROM @tempHisttbl TH;
3632
3633/* add combined or split record */
3634
3635INSERT INTO Nutrition.tVesselHistory (vesselID, statusID, dateChange, userChange, expirDate, isCurrent)
3636
3637SELECT TH.tgtVesselID
3638 , TH.statusID
3639 , Th.recordAdded
3640 , TH.userChange
3641 , Th.expDate
3642 , 0
3643
3644FROM @tempHisttbl TH;
3645
3646/* but what about recursion data? I don't know if it matters because couldn't use original bottle as target bottle...? */
3647
3648
3649PRINT N'History second data set inserted.'
3650
3651COMMIT TRANSACTION
3652END TRY
3653
3654BEGIN CATCH
3655 PRINT N'Err: something wrong with second history insert.'
3656 PRINT 'Error Number: ' + str(error_number())
3657 PRINT 'Line Number: ' + str(error_line())
3658 PRINT error_message()
3659 ROLLBACK TRANSACTION
3660END CATCH
3661GO
3662
3663BEGIN TRY
3664
3665/* now a fortified status for formula */
3666INSERT INTO Nutrition.tVesselHistory (vesselID, statusID, dateChange, userChange, expirDate, isCurrent)
3667SELECT NtV.vesselID
3668 , 5
3669 , MAX(NvHo.dateChange)
3670 , 'none Noted'
3671 , MAX(NvHo.expirDate)
3672 , CASE WHEN NvH.isCurrent IS NULL THEN 1 ELSE 0 END
3673
3674FROM Nutrition.tVessel NtV
3675 INNER JOIN Nutrition.tFormulaVessel NtF ON NtV.vesselID = NtF.vesselID
3676 INNER JOIn Nutrition.tVesselHistory NVHo ON NtV.vesselID = NVHo.vesselID
3677 LEFT OUTER JOIn Nutrition.tVesselHistory NVH ON NtV.vesselID = NVH.vesselID
3678 AND NVH.isCurrent = 1
3679 LEFT OUTER JOIN Nutrition.tVesselHistory NVFH ON NtV.vesselID = NVFH.vesselID
3680 AND NVFH.statusID = 5
3681 AND NvFH.vesselID IS NULL
3682
3683GROUP BY NtV.vesselID
3684 , NtV.vesselCode
3685 , NvH.isCurrent
3686 ;
3687
3688 PRINT N'Table Nutrition.tVesselHistory third set data inserted'
3689
3690END TRY
3691BEGIN CATCH
3692 PRINT N'Table Nutrition.tMotherVessel third set data insert failed'
3693 PRINT 'Error Number: ' + str(error_number())
3694 PRINT 'Line Number: ' + str(error_line())
3695 PRINT error_message()
3696END CATCH
3697GO
3698
3699BEGIN TRY
3700
3701/****** Object: Table [Nutrition].[tAdditiveIssue] Script Date: 06/19/2013 11:35:53 ******/
3702SET ANSI_NULLS ON
3703SET QUOTED_IDENTIFIER ON
3704SET ANSI_PADDING ON
3705
3706CREATE TABLE [Nutrition].[tAdditiveIssue](
3707 [addIssueID] [bigint] IDENTITY(1,1) NOT NULL,
3708 [additiveID] [smallint] NOT NULL,
3709 [vesselID][bigint] NOT NULL,
3710 [doses] [decimal](4,2) NOT NULL,
3711 [vesselHistoryID][bigint] NOT NULL,
3712 CONSTRAINT [PK_tAdditiveIssue] PRIMARY KEY CLUSTERED
3713(
3714 [addIssueID] ASC
3715)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
3716) ON [PRIMARY]
3717
3718ALTER TABLE [Nutrition].[tAdditiveIssue] WITH CHECK ADD CONSTRAINT [FK_tAdditiveIssue_tVessel] FOREIGN KEY([vesselID])
3719REFERENCES [Nutrition].[tVessel] ([vesselID])
3720ALTER TABLE [Nutrition].[tAdditiveIssue] CHECK CONSTRAINT [FK_tAdditiveIssue_tVessel]
3721
3722ALTER TABLE [Nutrition].[tAdditiveIssue] WITH CHECK ADD CONSTRAINT [FK_tAdditiveIssue_tVesselHistory] FOREIGN KEY([vesselHistoryID])
3723REFERENCES [Nutrition].[tVesselHistory] ([vesselHistoryID])
3724ALTER TABLE [Nutrition].[tAdditiveIssue] CHECK CONSTRAINT [FK_tAdditiveIssue_tVesselHistory]
3725
3726
3727ALTER TABLE [Nutrition].[tAdditiveIssue] WITH CHECK ADD CONSTRAINT [FK_tAdditiveIssue_tAdditiveList] FOREIGN KEY([additiveID])
3728REFERENCES [Nutrition].[tAdditiveList] ([additiveID])
3729ALTER TABLE [Nutrition].[tAdditiveIssue] CHECK CONSTRAINT [FK_tAdditiveIssue_tAdditiveList]
3730
3731ALTER TABLE [Nutrition].[tAdditiveIssue] ADD CONSTRAINT [DF_tAdditiveIssue_doses] DEFAULT (1) FOR [doses]
3732
3733 PRINT N'Table Nutrition.tAdditiveIssue created'
3734
3735END TRY
3736BEGIN CATCH
3737 PRINT N'Table Nutrition.tAdditiveIssue create failed'
3738 PRINT 'Error Number: ' + str(error_number())
3739 PRINT 'Line Number: ' + str(error_line())
3740 PRINT error_message()
3741END CATCH
3742GO
3743
3744BEGIN TRY
3745/* orders for specific patient */
3746/****** Object: Table [PTinfo].[tAdditivePatient] Script Date: 06/19/2013 11:35:53 ******/
3747SET ANSI_NULLS ON
3748SET QUOTED_IDENTIFIER ON
3749SET ANSI_PADDING ON
3750CREATE TABLE [PTinfo].[tAdditivePatient](
3751 [addPatientID] [bigint] IDENTITY(1,1) NOT NULL,
3752 [additiveID] [smallint] NOT NULL,
3753 [pSysID] [bigint] NOT NULL,
3754 [dosage] [decimal](4,2) NOT NULL,
3755 CONSTRAINT [PK_tAdditivePatient] PRIMARY KEY CLUSTERED
3756(
3757 [addPatientID] ASC
3758)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
3759) ON [PRIMARY]
3760SET ANSI_PADDING OFF
3761
3762ALTER TABLE [PTinfo].[tAdditivePatient] WITH CHECK ADD CONSTRAINT [FK_tAdditivePatient_tPatient] FOREIGN KEY([pSysID])
3763REFERENCES [PTinfo].[tPatient] ([pSysID])
3764ALTER TABLE [PTinfo].[tAdditivePatient] CHECK CONSTRAINT [FK_tAdditivePatient_tPatient]
3765ALTER TABLE [PTinfo].[tAdditivePatient] WITH CHECK ADD CONSTRAINT [FK_tAdditivePatient_tAdditiveList] FOREIGN KEY([additiveID])
3766REFERENCES [Nutrition].[tAdditiveList] ([additiveID])
3767ALTER TABLE [PTinfo].[tAdditivePatient] CHECK CONSTRAINT [FK_tAdditivePatient_tAdditiveList]
3768
3769ALTER TABLE [PTinfo].[tAdditivePatient] ADD CONSTRAINT [DF_tAdditivePatient_dosage] DEFAULT ((1)) FOR [dosage]
3770
3771 PRINT N'Table PTinfo.tAdditivePatient created'
3772
3773END TRY
3774BEGIN CATCH
3775 PRINT N'Table PTinfo.tAdditivePatient create failed'
3776 PRINT 'Error Number: ' + str(error_number())
3777 PRINT 'Line Number: ' + str(error_line())
3778 PRINT error_message()
3779END CATCH
3780GO
3781
3782
3783/* step one done. Time for step two BREAK */
3784
3785
3786/* now - current patients with identity info */
3787SET IDENTITY_INSERT [PTinfo].[tPatient] ON;
3788GO
3789
3790BEGIN TRY
3791BEGIN TRANSACTION
3792
3793DECLARE @priPID tinyint;
3794SET @priPID = PTinfo.udfGetPIDtype();
3795
3796DECLARE @maMatchPID tinyint;
3797SELECT @maMatchPID = PIT.identTypeID
3798 FROM SysInfo.tSetting StS
3799 INNER JOIN PTinfo.tHL7MsgField PHM ON StS.setValue = PHM.fieldNum
3800 INNER JOIN PTinfo.tIdentityType PIT ON PHM.fieldID = PIT.fieldID
3801 WHERE StS.settingID = 69
3802 AND PHM.sect='PID';
3803
3804
3805DECLARE @tempIDtbl TABLE (pSysID bigint identity(1,1)
3806 , firstName VARCHAR(50)
3807 , lastName VARCHAR(50)
3808 , midInit char(1)
3809 , dob DATETIME
3810 , recordAdded DATETIME
3811 , medRN VARCHAR(50)
3812 , formerMRN varchar(50)
3813 , motherMatch VARCHAR(50)
3814 , altID VARCHAR(50)
3815 , stat tinyint)
3816
3817/* so here. If there is no child for a mother in the system, don't add */
3818
3819
3820INSERT INTO @tempIDtbl (firstName, lastName, midInit, dob, recordAdded, medRN, formerMRN, motherMatch, altID, stat)
3821
3822SELECT DISTINCT dM.fName
3823 , dM.lName
3824 , dM.MInitial
3825 , dM.DOB
3826 , dM.recordAdded
3827 , CASE WHEN dM.medicalRecordNum = dP.medicalRecordNum THEN
3828 dM.medicalRecordNum + '-mr'
3829 ELSE
3830 CASE WHEN dDMRN.medicalRecordNum IS NOT NULL THEN
3831 dM.medicalRecordNum + '-mr'
3832 ELSE dM.medicalRecordNum
3833 END
3834 END
3835 , dM.medicalRecordNum
3836 , motherMatch = NULL
3837 , dM.altIdent
3838 , stat = CASE WHEN dM.[Status] = 'Active' THEN 1 ELSE 2 END
3839
3840FROM dbo.tblMothers dM
3841 INNER JOIN dbo.tblPatients dP ON dM.medicalRecordNum = dP.MotherMRN
3842 /* but wait - IS there a patient with the same MRN as the mother, whether or not the mother is assigned to that patient? */
3843 LEFT OUTER JOIn dbo.tblPatients dDMRN ON dM.medicalRecordNum = dDMRN.medicalRecordNum
3844 WHERE dM.Donor = 'N'
3845
3846UNION ALL
3847
3848SELECT dP.fName
3849 , dP.lName
3850 , dP.MInitial
3851 , dP.DOB
3852 , dP.recordAdded
3853 , dP.medicalRecordNum
3854 , dP.medicalRecordNum
3855 , CASE WHEN dP.medicalRecordNum = dP.MotherMRN THEN dP.MotherMRN + '-mr' ELSE dP.motherMRN END
3856 , NULL
3857 , stat = CASE WHEN dP.[Status] = 'Active' THEN 1 ELSE 2 END
3858
3859FROM dbo.tblPatients dP
3860/* placeholder mothers for patients without mothers. Yes, seriously. */
3861UNION ALL
3862
3863SELECT DISTINCT 'Mother'
3864 , dP.lName
3865 , ''
3866 , '1900-01-01'
3867 , getdate()
3868 , CASE WHEN len(ltrim(rtrim(dP.motherMRN))) > 0 THEN
3869 CASE WHEN dP.medicalRecordNum = dP.MotherMRN THEN dP.MotherMRN + '-mr' ELSE dP.motherMRN END
3870 ELSE dP.medicalRecordNum + '-mr' END
3871 , CASE WHEN len(ltrim(rtrim(dP.motherMRN))) > 0 THEN dP.motherMRN ELSE dP.medicalRecordNum + '-mr' END
3872 , NULL
3873 , NULL
3874 , stat = CASE WHEN dP.[Status] = 'Active' THEN 1 ELSE 2 END
3875
3876FROM dbo.tblPatients dP
3877 LEFT OUTER JOIN dbo.tblMothers dM ON dP.MotherMRN = dM.medicalRecordNum aND dM.Donor = 'N'
3878WHERE dM.MedicalRecordNum IS NULL;
3879
3880/* I feel like I should do some uniqueness check here. I should be okay with the -mr tho. */
3881
3882INSERT INTO PTinfo.tPatient (pSysID, firstname, lastName, midInit, dob, recordAdded)
3883
3884SELECT pSysID
3885 , firstname
3886 , lastName
3887 , midInit
3888 , dob
3889 , recordAdded
3890FROM @tempIDtbl
3891
3892 INSERT INTO PTinfo.tIdentity
3893 (pSysID
3894 , identTypeID
3895 , IDvalue)
3896 SELECT pSysID
3897 , @priPID
3898 , medRN
3899 FROM @tempIDtbl
3900 WHERE medRN IS NOT NULL
3901 AND len(ltrim(rtrim(medRN))) > 0;
3902
3903IF @priPID = 2
3904 BEGIN
3905 INSERT INTO PTinfo.tIdentity
3906 (pSysID
3907 , identTypeID
3908 , IDvalue)
3909 SELECT pSysID
3910 , 1
3911 , altID
3912 FROM @tempIDtbl
3913 WHERE altID IS NOT NULL
3914 AND len(ltrim(rtrim(altID))) > 0;
3915 END
3916
3917/* status info */
3918INSERT INTO PTinfo.tStatus
3919 (pSysID
3920 , pStatTypeID
3921 , statDate
3922 , isCurrent)
3923
3924SELECT
3925 TP.pSysID
3926 , TP.stat
3927 , TP.recordAdded
3928 , 1
3929
3930FROM @tempIDtbl TP;
3931
3932/* addtl child information */
3933
3934INSERT INTO PTinfo.tChildLink
3935 (pSysID
3936 , motherSysID
3937 , multiple
3938 , gender
3939 , allowFormula
3940 , allowBottle
3941 , location)
3942
3943SELECT TP.pSysID
3944 , PtM.pSysID
3945 , CASE WHEN LEN(RTRIM(LTRIM(dP.multiple))) = 1 THEN dP.multiple ELSE '0' END
3946 , dP.Sex
3947 , CASE WHEN dP.allowFormula = 'Y' then 1 ELSE 0 END
3948 , CASE WHEN dP.allowBottle = 'Y' then 1 ELSE 0 END
3949 , dP.location
3950
3951FROM @tempIDtbl TP
3952 INNER JOIN dbo.tblPatients dP ON TP.medRN = dP.medicalRecordNum
3953 INNER JOIN PTinfo.tIdentity PtM ON TP.motherMatch = PtM.IDvalue
3954
3955WHERE PtM.identTypeID = @maMatchPID;
3956
3957/* mom EMB */
3958
3959INSERT INTO PTinfo.tMomInfo
3960 (pSysID, calsPer)
3961
3962SELECT DISTINCT mInfo.pSysID
3963 , mInfo.ebmCals
3964FROM (SELECT TP.pSysID
3965 , ebmCals = CASE WHEN len(dM.EBMcals) = 0 THEN 20 ELSE COALESCE(dM.ebmCals,20) END
3966
3967FROM @tempIDtbl TP
3968 INNER JOIN dbo.tblMothers dM ON TP.formerMRN = dM.medicalRecordNum
3969WHERE dM.Donor = 'N'
3970 AND TP.motherMatch IS NULL
3971/* now for those mothers not in the system but... */
3972
3973UNION ALL
3974SELECT TP.pSysID
3975 , ebmCals = 20
3976
3977FROM @tempIDtbl TP
3978 INNER JOIN @tempIDtbl Mtr ON TP.medRN = Mtr.motherMatch) mInfo;
3979
3980/* allow donor */
3981
3982INSERT INTO PTinfo.tDocOrder
3983 (pSysID
3984 , orderNum
3985 , orderTypeID
3986 , orderNote
3987 , orderStart
3988 , orderEnd)
3989
3990SELECT tP.pSysID
3991 , CASE WHEN LEN(dP.DoctorOrder) = 0 THEN 'none' ELSE COALESCE(dP.doctorOrder,'none') END
3992 , 1
3993 , 'Have parent consent form.'
3994 , dP.recordAdded
3995 , CASE WHEN dP.[Status] = 'Active' THEN '2500-01-01' ELSE dP.recordAdded END
3996
3997FROM @tempIDtbl TP
3998 INNER JOIN dbo.tblPatients dP ON TP.medRN = dP.medicalRecordNum
3999
4000WHERE dP.allowDonor = 'Y';
4001
4002/* fortification order */
4003INSERT INTO PTinfo.tDocOrder
4004 (pSysID
4005 , orderNum
4006 , orderTypeID
4007 , orderNote
4008 , orderStart
4009 , orderEnd)
4010
4011SELECT tP.pSysID
4012 , CASE WHEN LEN(dP.DoctorOrder) = 0 THEN 'none' ELSE COALESCE(dP.doctorOrder,'none') END
4013 , 2
4014 , 'none'
4015 , dP.recordAdded
4016 , CASE WHEN dP.[Status] = 'Active' THEN '2500-01-01' ELSE dP.recordAdded END
4017
4018FROM @tempIDtbl TP
4019 INNER JOIN dbo.tblPatients dP ON TP.medRN = dP.medicalRecordNum
4020
4021WHERE dP.NeedsFortified = 'Y';
4022
4023/* mother vessel join - KK don't forget we added some -mr's here too */
4024INSERT INTO Nutrition.tMotherVessel
4025 (pSysID
4026 , vesselID)
4027
4028SELECT TP.pSysID
4029 , NtV.vesselID
4030
4031FROM Nutrition.tVessel NtV
4032 INNER JOIn dbo.tblBottles dB ON NtV.vesselCode = dB.BottleNumber
4033 INNER JOIN @tempIDtbl TP ON dB.motherMRN = tP.formerMRN
4034
4035WHERE NtV.nCategoryID = 1
4036 AND TP.motherMatch IS NULL
4037
4038UNION ALL
4039
4040SELECT TP.pSysID
4041 , NtV.vesselID
4042
4043FROM Nutrition.tVessel NtV
4044 INNER JOIn dbo.tblFeedings dF ON NtV.vesselCode = dF.feedingNumber
4045 INNER JOIN @tempIDtbl TP ON dF.motherMRN = tP.formerMRN
4046WHERE NtV.nCategoryID = 1
4047 AND tP.motherMatch IS NULL
4048;
4049
4050/* assigned stuff */
4051DECLARE @tempAssn TABLE (pSysID bigint
4052 , vesselID bigint)
4053
4054INSERT INTO @tempAssn (pSysID, vesselID)
4055
4056SELECT TP.pSysID
4057 , NtV.vesselID
4058
4059FROM Nutrition.tVessel NtV
4060 INNER JOIN dbo.tblBottles dB ON NtV.vesselCode = dB.BottleNumber
4061 INNER JOIN @tempIDtbl TP ON dB.fedTo = tP.medRN
4062WHERE dB.fedTO IS NOT NULL
4063 AND len(ltrim(rtrim(db.fedTo))) > 0
4064 AND TP.motherMatch IS NOT NULL
4065
4066UNION ALL
4067
4068SELECT TP.pSysID
4069 , NtV.vesselID
4070
4071FROM Nutrition.tVessel NtV
4072 INNER JOIN dbo.tblFeedings dF ON NtV.vesselCode = dF.feedingNumber
4073 INNER JOIN @tempIDtbl TP ON dF.patientMRN = tP.medRN
4074WHERE dF.patientMRN IS NOT NULL
4075 AND len(ltrim(rtrim(dF.patientMRN))) > 0
4076 AND TP.motherMatch IS NOT NULL;
4077
4078INSERT INTO @tempAssn (pSysID, vesselID)
4079
4080SELECT TP.pSysID
4081 , NtV.vesselID
4082
4083FROM Nutrition.tVessel NtV
4084 INNER JOIn dbo.tblBottles dB ON NtV.vesselCode = dB.BottleNumber
4085 INNER JOIN @tempIDtbl TP ON dB.assignedTo = tP.medRN
4086
4087WHERE dB.assignedTo IS NOT NULL
4088 AND len(ltrim(rtrim(db.assignedTo))) > 0
4089 AND NOT EXISTS (SELECT vesselID
4090 FROM @tempAssn TA
4091 WHERE NtV.vesselID = TA.vesselID)
4092 AND TP.motherMatch IS NOT NULL;
4093
4094
4095INSERT INTO Nutrition.tVesselAssign
4096 (pSysID
4097 , vesselID)
4098
4099SELECT TA.pSysID
4100 , TA.vesselID
4101
4102FROM @tempAssn TA;
4103
4104COMMIT TRANSACTION
4105
4106END TRY
4107
4108BEGIN CATCH
4109 PRINT N'Err: something wrong with patient insert.'
4110 PRINT 'Error Number: ' + str(error_number())
4111 PRINT 'Line Number: ' + str(error_line())
4112 PRINT error_message()
4113 ROLLBACK TRANSACTION
4114END CATCH
4115GO
4116
4117SET IDENTITY_INSERT [PTinfo].[tPatient] OFF;
4118GO
4119
4120/* Step three BREAK */
4121
4122
4123BEGIN TRY
4124/* now this is the tricky part. Additives assigned to patients */
4125
4126DECLARE @priPID tinyint;
4127SET @priPID = PTinfo.udfGetPIDtype();
4128
4129INSERT INTO PTinfo.tAdditivePatient
4130 (additiveID
4131 , pSysID
4132 , dosage)
4133
4134SELECT NAL.additiveID
4135 , PtI.pSysID
4136 , dAP.Dosage
4137
4138FROM dbo.tblAdditivePairing dAP
4139 INNER JOIn dbo.tblAdditives dA ON dAP.AdditiveIndex = dA.AdditiveIndex
4140 INNER JOIn Nutrition.tAdditiveList NAL ON dA.additive = NAL.additiveName
4141 INNER JOIN PTinfo.tIdentity PtI ON dAP.MedicalRecordNum = PtI.IDvalue
4142WHERE PtI.identTypeID = @priPID;
4143
4144/* if there's not a fortified status for a bottle in the additives issued table, make one HERE */
4145
4146INSERT INTO Nutrition.tVesselHistory (vesselID, statusID, dateChange, userChange, expirDate, isCurrent)
4147
4148SELECT NtV.vesselID
4149 , 5
4150 ,'1900-01-01'
4151 , COALESCE(dAI.username, 'none Noted')
4152 , '2500-01-01'
4153 , 0
4154
4155FROM dbo.tblAdditivesIssued dAI
4156 INNER JOIn Nutrition.tAdditiveList NAL ON dAI.additive = NAL.additiveName
4157 INNER JOIN Nutrition.tVessel NtV ON dAI.milkID = NtV.vesselCode
4158 LEFT OUTER JOIN Nutrition.tVesselHistory NtVH ON NtV.vesselID = NtVH.vesselID
4159 AND NtVH.statusID = 5
4160WHERE vesselHistoryID IS NULL;
4161
4162
4163/* issued additives */
4164INSERT INTO Nutrition.tAdditiveIssue
4165 (additiveID
4166 , vesselID
4167 , doses
4168 , vesselHistoryID)
4169
4170SELECT NAL.additiveID
4171 , NtV.vesselID
4172 , CASE WHEN dAI.doses < 100 THEN dAI.doses ELSE 99.99 END
4173 , NtVh.vesselHistoryID
4174
4175FROM dbo.tblAdditivesIssued dAI
4176 INNER JOIn Nutrition.tAdditiveList NAL ON dAI.additive = NAL.additiveName
4177 INNER JOIN Nutrition.tVessel NtV ON dAI.milkID = NtV.vesselCode
4178 LEFT OUTER JOIN Nutrition.tVesselHistory NtVH ON NtV.vesselID = NtVH.vesselID
4179 AND NtVH.statusID = 5;
4180
4181 PRINT N'Table Nutrition.tAdditiveIssue data inserted'
4182
4183END TRY
4184BEGIN CATCH
4185 PRINT N'Table Nutrition.tAdditiveIssue data insert failed'
4186 PRINT 'Error Number: ' + str(error_number())
4187 PRINT 'Line Number: ' + str(error_line())
4188 PRINT error_message()
4189END CATCH
4190GO
4191
4192/* notes table */
4193BEGIN TRY
4194/****** Object: Table [Nutrition].[tVesselNote] Script Date: 06/19/2013 11:35:53 ******/
4195CREATE TABLE [Nutrition].[tvesselNote](
4196 [vesselNoteID] [bigint] IDENTITY(1,1) NOT NULL,
4197 [vesselID] [bigint] NOT NULL,
4198 [vNote] [varchar] (200) NOT NULL,
4199 [vesselHistoryID] [bigint] NOT NULL,
4200 CONSTRAINT [PK_tvesselNote] PRIMARY KEY CLUSTERED
4201(
4202 [vesselNoteID] ASC
4203)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
4204) ON [PRIMARY]
4205
4206SET ANSI_PADDING OFF
4207
4208ALTER TABLE [Nutrition].[tVesselNote] WITH CHECK ADD CONSTRAINT [FK_tVesselNote_tVessel] FOREIGN KEY([vesselID])
4209REFERENCES [Nutrition].[tVessel] ([vesselID])
4210
4211ALTER TABLE [Nutrition].[tVesselNote] CHECK CONSTRAINT [FK_tVesselNote_tVessel]
4212
4213ALTER TABLE [Nutrition].[tVesselNote] WITH CHECK ADD CONSTRAINT [FK_tVesselNote_tVesselHistory] FOREIGN KEY([vesselHistoryID])
4214REFERENCES [Nutrition].[tVesselHistory] ([vesselHistoryID])
4215ALTER TABLE [Nutrition].[tVesselNote] CHECK CONSTRAINT [FK_tVesselNote_tVesselHistory]
4216 PRINT N'Notes table create successful.'
4217END TRY
4218
4219BEGIN CATCH
4220 PRINT N'Err: Notes table create fail.'
4221 PRINT 'Error Number: ' + str(error_number())
4222 PRINT 'Line Number: ' + str(error_line())
4223 PRINT error_message()
4224END CATCH
4225GO
4226
4227/* now add the notes. Try to find some sort of vessel history thing to do this */
4228BEGIN TRY
4229INSERT INTO Nutrition.tVesselNote
4230 (vesselID
4231 , vNote
4232 , vesselHistoryID)
4233
4234select * from (SELECT NtV.vesselID
4235 , dAI.additive
4236 , CASE WHEN COALESCE(MAX(NtFF.vesselHistoryID), 0) > 0 THEN MAX(NtFF.vesselHistoryID) ELSE NtVH.vesselHistoryID END as nid
4237
4238
4239FROM dbo.tblAdditivesIssued dAI
4240 LEFT OUTER JOIn Nutrition.tAdditiveList NAL ON dAI.additive = NAL.additiveName
4241 LEFT OUTER JOIN Nutrition.tFormula NtF ON dAI.additive = NtF.formulaName
4242 INNER JOIN Nutrition.tVessel NtV ON dAI.milkID = NtV.vesselCode
4243 LEFT OUTER JOIN Nutrition.tVesselHistory NtVH ON NtV.vesselID = NtVH.vesselID
4244 AND NtVH.isCurrent = 1
4245 LEFT OUTER JOIN Nutrition.tVesselHistory NtFF ON NtV.vesselID = NtFF.vesselID
4246 AND (NtFF.statusID = 12
4247 OR NtFF.statusID = 5)
4248
4249 WHERE NtF.formulaID IS NULL
4250 AND NAL.additiveID IS NULL
4251
4252GROUP BY NtV.vesselID
4253 , dAI.additive
4254 , NtVH.vesselHistoryID) src where not nid is null
4255
4256 PRINT N'Notes data insert successful.'
4257END TRY
4258
4259BEGIN CATCH
4260 PRINT N'Err: Notes data insert failed.'
4261 PRINT 'Error Number: ' + str(error_number())
4262 PRINT 'Line Number: ' + str(error_line())
4263 PRINT error_message()
4264END CATCH
4265GO
4266
4267/* bin assignments */
4268BEGIN TRY
4269
4270DECLARE @priPID tinyint;
4271SET @priPID = PTinfo.udfGetPIDtype();
4272
4273INSERT INTO Storage.tlocationAssign
4274 (locationID
4275 , pSysID)
4276
4277SELECT StL.locationID
4278 , PtI.pSysID
4279
4280FROM dbo.tblLocations dTL
4281 INNER JOIN Storage.tLocation StL ON DtL.LocationCode = StL.locationCode
4282 LEFT OUTER JOIN PTinfo.tIdentity PtI ON DtL.BinAssignment = PtI.IDvalue
4283 AND PtI.identTypeID = @priPID
4284
4285WHERE len(dTL.BinAssignment) > 0
4286 AND PtI.pSysID IS NOT NULL;
4287
4288 PRINT N'Table Storage.tLocationAssign data inserted'
4289
4290END TRY
4291BEGIN CATCH
4292 PRINT N'Table Storage.tLocationAssign data insert failed'
4293 PRINT 'Error Number: ' + str(error_number())
4294 PRINT 'Line Number: ' + str(error_line())
4295 PRINT error_message()
4296END CATCH
4297GO
4298
4299BEGIN TRY
4300/****** Object: Table [PTinfo].[tHL7message] Script Date: 06/19/2013 11:35:53 ******/
4301SET ANSI_NULLS ON
4302SET QUOTED_IDENTIFIER ON
4303SET ANSI_PADDING ON
4304CREATE TABLE [PTinfo].[tHL7message](
4305 [messageID] [bigint] IDENTITY(1,1) NOT NULL,
4306 [recdDate] [datetime] NOT NULL,
4307 [firstName] [varchar](50) NOT NULL,
4308 [lastName] [varchar](50) NOT NULL,
4309 [midInit] [char](1) NULL,
4310 [dob] [datetime] NOT NULL,
4311 [momPrimaryID] [varchar] (50) NULL,
4312 [gender] [varchar] (15) NULL,
4313 [location] [varchar] (50) NULL,
4314
4315 CONSTRAINT [PK_tHL7message] PRIMARY KEY CLUSTERED
4316(
4317 [messageID] ASC
4318)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
4319) ON [PRIMARY]
4320SET ANSI_PADDING OFF
4321
4322 PRINT N'Table PTinfo.tHL7message created'
4323
4324END TRY
4325BEGIN CATCH
4326 PRINT N'Table PTinfo.tHL7message create failed'
4327 PRINT 'Error Number: ' + str(error_number())
4328 PRINT 'Line Number: ' + str(error_line())
4329 PRINT error_message()
4330END CATCH
4331GO
4332
4333BEGIN TRY
4334/****** Object: Table [PTinfo].[tHL7ID] Script Date: 06/19/2013 11:35:53 ******/
4335SET ANSI_NULLS ON
4336SET QUOTED_IDENTIFIER ON
4337SET ANSI_PADDING ON
4338CREATE TABLE [PTinfo].[tHL7ID](
4339 [hID][bigint] IDENTITY(1,1) NOT NULL,
4340 [messageID] [bigint] NOT NULL,
4341 [identTypeID] [tinyint] NOT NULL,
4342 [IDvalue] [varchar](50) NOT NULL,
4343
4344CONSTRAINT [PK_tHL7ID] PRIMARY KEY CLUSTERED
4345(
4346 [hID] ASC
4347)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
4348) ON [PRIMARY]
4349SET ANSI_PADDING OFF
4350
4351ALTER TABLE [PTinfo].[tHL7ID] WITH CHECK ADD CONSTRAINT [FK_tHL7ID_tIdentityType] FOREIGN KEY([identTypeID])
4352REFERENCES [PTinfo].[tIdentityType] ([identTypeID])
4353ALTER TABLE [PTinfo].[tHL7ID] CHECK CONSTRAINT [FK_tHL7ID_tIdentityType]
4354
4355ALTER TABLE [PTinfo].[tHL7ID] WITH CHECK ADD CONSTRAINT [FK_tHL7ID_tHL7message] FOREIGN KEY([messageID])
4356REFERENCES [PTinfo].[tHL7message] ([messageID])
4357ALTER TABLE [PTinfo].[tHL7ID] CHECK CONSTRAINT [FK_tHL7ID_tHL7message]
4358
4359 PRINT N'Table PTinfo.tHL7ID created'
4360
4361END TRY
4362BEGIN CATCH
4363 PRINT N'Table PTinfo.tHL7ID failed'
4364 PRINT 'Error Number: ' + str(error_number())
4365 PRINT 'Line Number: ' + str(error_line())
4366 PRINT error_message()
4367END CATCH
4368GO
4369
4370SET IDENTITY_INSERT [PTinfo].[tHL7message] ON;
4371GO
4372
4373/* hl7 data */
4374
4375BEGIN TRY
4376BEGIN TRANSACTION
4377
4378DECLARE @priPID tinyint;
4379SET @priPID = PTinfo.udfGetPIDtype();
4380
4381
4382DECLARE @tpIDtbl TABLE (messageID bigint identity(1,1)
4383 , recdDate DATETIME
4384 , firstName VARCHAR(50)
4385 , lastName VARCHAR(50)
4386 , midInit char(1)
4387 , dob DATETIME
4388 , momPrimaryID VARCHAR(50)
4389 , gender VARCHAR(12)
4390 , location VARCHAR(50)
4391 , MRN varchar(50)
4392 , altIdent varchar(50))
4393
4394INSERT INTO @tpIDtbl (firstName, lastName, midInit, dob, recdDate, momPrimaryID, gender, location, MRN, altIdent)
4395
4396SELECT dH.fName
4397 , dH.lName
4398 , dH.MInitial
4399 , dH.birthDate
4400 , dH.dateAdded
4401 , dH.motherMRN
4402 , dH.sex
4403 , dH.location
4404 , dH.MRN
4405 , dH.altIdent
4406
4407FROM dbo.tblHL7 dH;
4408
4409INSERT INTO PTinfo.tHL7message (messageID, firstname, lastName, midInit, dob, recdDate, momPrimaryID, gender, location)
4410
4411SELECT messageID
4412 , firstname
4413 , lastName
4414 , midInit
4415 , dob
4416 , recdDate
4417 , momPrimaryID
4418 , gender
4419 , location
4420FROM @tpIDtbl
4421
4422 INSERT INTO PTinfo.tHL7ID
4423 (messageID
4424 , identTypeID
4425 , IDvalue)
4426 SELECT messageID
4427 , @priPID
4428 , MRN
4429
4430 FROM @tpIDtbl
4431
4432IF @priPID = 2
4433BEGIN
4434
4435 INSERT INTO PTinfo.tHL7ID
4436 (messageID
4437 , identTypeID
4438 , IDvalue)
4439 SELECT messageID
4440 , 1
4441 , altIdent
4442
4443 FROM @tpIDtbl
4444 WHERE altIDent IS NOT NULL
4445END
4446
4447COMMIT TRANSACTION
4448END TRY
4449
4450BEGIN CATCH
4451 PRINT N'Err: something wrong with HL7 data insert.'
4452 PRINT 'Error Number: ' + str(error_number())
4453 PRINT 'Line Number: ' + str(error_line())
4454 PRINT error_message()
4455 ROLLBACK TRANSACTION
4456END CATCH
4457GO
4458
4459SET IDENTITY_INSERT [PTinfo].[tHL7message] OFF;
4460GO
4461
4462BEGIN TRY
4463/* the ever-so-excellent tally table */
4464/****** Object: Table [SysInfo].[tTally] Script Date: 06/19/2013 11:35:53 ******/
4465SET ANSI_NULLS ON
4466SET QUOTED_IDENTIFIER ON
4467SET ANSI_PADDING ON
4468
4469SELECT TOP 1000 --(4 * 1000 = VARBINARY(4000) in length)
4470 N = ISNULL(CAST(
4471 (ROW_NUMBER() OVER (ORDER BY (SELECT NULL))-1)+1
4472 AS INT),0)
4473 INTO SysInfo.tTally
4474 FROM master.sys.all_columns ac1
4475 CROSS JOIN master.sys.all_columns ac2
4476;
4477--===== Add the quintessential PK for performance.
4478 ALTER TABLE SysInfo.tTally
4479 ADD CONSTRAINT PK_tTally
4480 PRIMARY KEY CLUSTERED (N) WITH FILLFACTOR = 100;
4481
4482 PRINT N'Table SysInfo.tTally created'
4483
4484END TRY
4485BEGIN CATCH
4486 PRINT N'Table SysInfo.tTally failed'
4487 PRINT 'Error Number: ' + str(error_number())
4488 PRINT 'Line Number: ' + str(error_line())
4489 PRINT error_message()
4490END CATCH
4491GO
4492
4493/* audit tables */
4494BEGIN TRY
4495/****** Object: Table [SysInfo].[tAuditCategory] Script Date: 06/19/2013 11:35:53 ******/
4496SET ANSI_NULLS ON
4497SET QUOTED_IDENTIFIER ON
4498SET ANSI_PADDING ON
4499
4500CREATE TABLE [SysInfo].[tAuditCategory](
4501 [auditCatID] [tinyint] IDENTITY(1,1) NOT NULL,
4502 [auditCatName] [varchar](15) NOT NULL,
4503
4504 CONSTRAINT [PK_tAuditCategory] PRIMARY KEY CLUSTERED
4505(
4506 [auditCatID] ASC
4507)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
4508) ON [PRIMARY]
4509SET ANSI_PADDING OFF
4510
4511PRINT N'Table SysInfo.tAuditCategory created'
4512
4513END TRY
4514BEGIN CATCH
4515 PRINT N'Table SysInfo.tAuditCategory failed'
4516 PRINT 'Error Number: ' + str(error_number())
4517 PRINT 'Line Number: ' + str(error_line())
4518 PRINT error_message()
4519END CATCH
4520GO
4521
4522SET IDENTITY_INSERT [SysInfo].[tAuditCategory] ON;
4523GO
4524
4525BEGIN TRY
4526INSERT INTO [SysInfo].[tAuditCategory]
4527 ([auditCatID]
4528 , [auditCatName])
4529 VALUES
4530 (1,'Access')
4531 , (2,'Avert')
4532 , (3,'View')
4533 , (4,'Data')
4534;
4535
4536PRINT N'Table SysInfo.tAuditCategory data inserted'
4537
4538END TRY
4539BEGIN CATCH
4540 PRINT N'Table SysInfo.tAuditCategory data insert failed'
4541 PRINT 'Error Number: ' + str(error_number())
4542 PRINT 'Line Number: ' + str(error_line())
4543 PRINT error_message()
4544END CATCH
4545GO
4546SET IDENTITY_INSERT [SysInfo].[tAuditCategory] OFF;
4547GO
4548
4549BEGIN TRY
4550
4551/****** Object: Table [SysInfo].[tAuditType] Script Date: 06/19/2013 11:35:53 ******/
4552SET ANSI_NULLS ON
4553SET QUOTED_IDENTIFIER ON
4554SET ANSI_PADDING ON
4555
4556CREATE TABLE [SysInfo].[tAuditType](
4557 [auditTypeID] [tinyint] IDENTITY(1,1) NOT NULL,
4558 [auditCatID] [tinyint] NOT NULL,
4559 [auditTypeName] [varchar](15) NOT NULL,
4560
4561 CONSTRAINT [PK_tAuditType] PRIMARY KEY CLUSTERED
4562(
4563 [auditTypeID] ASC
4564)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
4565) ON [PRIMARY]
4566SET ANSI_PADDING OFF
4567
4568ALTER TABLE [SysInfo].[tAuditType] WITH CHECK ADD CONSTRAINT [fk_tAuditType_tAuditCategory] FOREIGN KEY([auditCatID])
4569REFERENCES [SysInfo].[tAuditCategory] ([auditCatID])
4570ALTER TABLE [SysInfo].[tAuditType] CHECK CONSTRAINT [fk_tAuditType_tAuditCategory]
4571
4572PRINT N'Table SysInfo.tAuditCategory created'
4573
4574END TRY
4575BEGIN CATCH
4576 PRINT N'Table SysInfo.tAuditCategory failed'
4577 PRINT 'Error Number: ' + str(error_number())
4578 PRINT 'Line Number: ' + str(error_line())
4579 PRINT error_message()
4580END CATCH
4581GO
4582
4583SET IDENTITY_INSERT [SysInfo].[tAuditType] ON;
4584GO
4585
4586BEGIN TRY
4587INSERT INTO [SysInfo].[tAuditType]
4588 ([auditTypeID]
4589 , [auditCatID]
4590 , [auditTypeName])
4591 VALUES
4592 (1,1,'login error')
4593 , (2,1,'login success')
4594 , (3,1,'log out manual')
4595 , (4,1,'log out auto')
4596 , (5, 2,'feed error')
4597 , (6,2, 'prep error')
4598 , (7,3, 'single record')
4599 , (8,3, 'report run')
4600 , (9,4, 'add record')
4601 , (10,4, 'edit record')
4602 , (11, 4, 'delete record')
4603 , (12,2, 'patient info')
4604 , (13,3, 'HL7 record');
4605
4606 PRINT N'Table SysInfo.tAuditType data inserted'
4607
4608END TRY
4609BEGIN CATCH
4610 PRINT N'Table SysInfo.tAuditType data insert failed'
4611 PRINT 'Error Number: ' + str(error_number())
4612 PRINT 'Line Number: ' + str(error_line())
4613 PRINT error_message()
4614END CATCH
4615GO
4616
4617SET IDENTITY_INSERT [SysInfo].[tAuditType] OFF;
4618GO
4619
4620BEGIN TRY
4621/****** Object: Table [SysInfo].[tAccessLog] Script Date: 06/19/2013 11:35:53 ******/
4622SET ANSI_NULLS ON
4623SET QUOTED_IDENTIFIER ON
4624SET ANSI_PADDING ON
4625CREATE TABLE [SysInfo].[tAccessLog](
4626 [accessLogID] [bigint] IDENTITY(1,1) NOT NULL,
4627 [accessDate] [datetime] NOT NULL,
4628 [userName] [varchar](50) NOT NULL,
4629 [auditTypeID] [tinyint] NOT NULL,
4630 [unit] [varchar](15) NOT NULL,
4631
4632 CONSTRAINT [PK_tAccessLog] PRIMARY KEY CLUSTERED
4633(
4634 [accessLogID] ASC
4635)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
4636) ON [PRIMARY]
4637SET ANSI_PADDING OFF
4638
4639ALTER TABLE [SysInfo].[tAccessLog] WITH CHECK ADD CONSTRAINT [fk_tAccessLog_tAuditType] FOREIGN KEY([auditTypeID])
4640REFERENCES [SysInfo].[tAuditType] ([auditTypeID])
4641ALTER TABLE [SysInfo].[tAccessLog] CHECK CONSTRAINT [fk_tAccessLog_tAuditType]
4642
4643PRINT N'Table SysInfo.tAccessLog created'
4644
4645END TRY
4646BEGIN CATCH
4647 PRINT N'Table SysInfo.tAccessLog failed'
4648 PRINT 'Error Number: ' + str(error_number())
4649 PRINT 'Line Number: ' + str(error_line())
4650 PRINT error_message()
4651END CATCH
4652GO
4653
4654/* insert data for failed logins */
4655BEGIN TRY
4656INSERT INTO [SysInfo].[tAccessLog]
4657 ([accessDate]
4658 , [userName]
4659 , [auditTypeID]
4660 , [unit])
4661
4662SELECT dFL.Attempted
4663 , LEFT(dFL.UserName,50)
4664 , 1
4665 , 'unknown'
4666 FROM dbo.tblFailedLogins dFL;
4667
4668 PRINT N'Table SysInfo.tAccessLog data inserted'
4669
4670END TRY
4671BEGIN CATCH
4672 PRINT N'Table SysInfo.tAccessLog data insert failed'
4673 PRINT 'Error Number: ' + str(error_number())
4674 PRINT 'Line Number: ' + str(error_line())
4675 PRINT error_message()
4676END CATCH
4677GO
4678
4679BEGIN TRY
4680/****** Object: Table [SysInfo].[tRecordView] Script Date: 06/19/2013 11:35:53 ******/
4681SET ANSI_NULLS ON
4682SET QUOTED_IDENTIFIER ON
4683SET ANSI_PADDING ON
4684CREATE TABLE [SysInfo].[tRecordView](
4685 [recordViewID] [bigint] IDENTITY(1,1) NOT NULL,
4686 [viewDate] [datetime] NOT NULL,
4687 [userName] [varchar](50) NOT NULL,
4688 [auditTypeID] [tinyint] NOT NULL,
4689 [pSysID] [varchar](25) NOT NULL,
4690
4691 CONSTRAINT [PK_tRecordView] PRIMARY KEY CLUSTERED
4692(
4693 [RecordViewID] ASC
4694)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
4695) ON [PRIMARY]
4696SET ANSI_PADDING OFF
4697
4698ALTER TABLE [SysInfo].[tRecordView] WITH CHECK ADD CONSTRAINT [fk_tRecordView_tAuditType] FOREIGN KEY([auditTypeID])
4699REFERENCES [SysInfo].[tAuditType] ([auditTypeID])
4700ALTER TABLE [SysInfo].[tRecordView] CHECK CONSTRAINT [fk_tRecordView_tAuditType]
4701
4702PRINT N'Table SysInfo.tRecordView created'
4703
4704END TRY
4705BEGIN CATCH
4706 PRINT N'Table SysInfo.tRecordView failed'
4707 PRINT 'Error Number: ' + str(error_number())
4708 PRINT 'Line Number: ' + str(error_line())
4709 PRINT error_message()
4710END CATCH
4711GO
4712
4713BEGIN TRY
4714/****** Object: Table [SysInfo].[tReportView] Script Date: 06/19/2013 11:35:53 ******/
4715SET ANSI_NULLS ON
4716SET QUOTED_IDENTIFIER ON
4717SET ANSI_PADDING ON
4718
4719CREATE TABLE [SysInfo].[tReportView](
4720 [reportViewID] [bigint] IDENTITY(1,1) NOT NULL,
4721 [viewDate] [datetime] NOT NULL,
4722 [userName] [varchar](50) NOT NULL,
4723 [auditTypeID] [tinyint] NOT NULL,
4724 [reportName] [varchar](25) NOT NULL,
4725 [startDate] [date] NOT NULL,
4726 [endDate] [date] NOT NULL,
4727 [addtlParamValue] [varchar](50) NOT NULL,
4728
4729 CONSTRAINT [PK_tReportView] PRIMARY KEY CLUSTERED
4730(
4731 [reportViewID] ASC
4732)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
4733) ON [PRIMARY]
4734SET ANSI_PADDING OFF
4735
4736ALTER TABLE [SysInfo].[tReportView] WITH CHECK ADD CONSTRAINT [fk_tReportView_tAuditType] FOREIGN KEY([auditTypeID])
4737REFERENCES [SysInfo].[tAuditType] ([auditTypeID])
4738
4739ALTER TABLE [SysInfo].[tReportView] CHECK CONSTRAINT [fk_tReportView_tAuditType]
4740
4741PRINT N'Table SysInfo.tReportView created'
4742
4743END TRY
4744BEGIN CATCH
4745 PRINT N'Table SysInfo.tReportView failed'
4746 PRINT 'Error Number: ' + str(error_number())
4747 PRINT 'Line Number: ' + str(error_line())
4748 PRINT error_message()
4749END CATCH
4750GO
4751
4752BEGIN TRY
4753/****** Object: Table [SysInfo].[tDataChange] Script Date: 06/19/2013 11:35:53 ******/
4754SET ANSI_NULLS ON
4755SET QUOTED_IDENTIFIER ON
4756SET ANSI_PADDING ON
4757CREATE TABLE [SysInfo].[tDataChange](
4758 [dataChangeID] [bigint] IDENTITY(1,1) NOT NULL,
4759 [viewDate] [datetime] NOT NULL,
4760 [userName] [varchar](50) NOT NULL,
4761 [auditTypeID] [tinyint] NOT NULL,
4762 [tableName] [varchar](35) NOT NULL,
4763 [recordID] [bigint] NOT NULL,
4764 [previousEntry] [varchar](1000) NOT NULL,
4765
4766 CONSTRAINT [PK_tDataChange] PRIMARY KEY CLUSTERED
4767(
4768 [dataChangeID] ASC
4769)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
4770) ON [PRIMARY]
4771SET ANSI_PADDING OFF
4772
4773ALTER TABLE [SysInfo].[tDataChange] WITH CHECK ADD CONSTRAINT [fk_tDataChange_tAuditType] FOREIGN KEY([auditTypeID])
4774REFERENCES [SysInfo].[tAuditType] ([auditTypeID])
4775
4776ALTER TABLE [SysInfo].[tDataChange] CHECK CONSTRAINT [fk_tDataChange_tAuditType]
4777
4778PRINT N'Table SysInfo.tDataChange created'
4779
4780END TRY
4781BEGIN CATCH
4782 PRINT N'Table SysInfo.tDataChange failed'
4783 PRINT 'Error Number: ' + str(error_number())
4784 PRINT 'Line Number: ' + str(error_line())
4785 PRINT error_message()
4786END CATCH
4787GO
4788
4789BEGIN TRY
4790/****** Object: Table [SysInfo].[tReasonCode] Script Date: 06/19/2013 11:35:53 ******/
4791SET ANSI_NULLS ON
4792SET QUOTED_IDENTIFIER ON
4793SET ANSI_PADDING ON
4794CREATE TABLE [SysInfo].[tReasonCode](
4795 [reasonCodeID] [tinyint] IDENTITY(1,1) NOT NULL,
4796 [reason] [varchar](100) NOT NULL,
4797
4798 CONSTRAINT [PK_tReasonCode] PRIMARY KEY CLUSTERED
4799(
4800 [reasonCodeID] ASC
4801)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
4802) ON [PRIMARY]
4803SET ANSI_PADDING OFF
4804
4805PRINT N'Table SysInfo.tReasonCode created'
4806
4807END TRY
4808BEGIN CATCH
4809 PRINT N'Table SysInfo.tReasonCode failed'
4810 PRINT 'Error Number: ' + str(error_number())
4811 PRINT 'Line Number: ' + str(error_line())
4812 PRINT error_message()
4813END CATCH
4814GO
4815
4816SET IDENTITY_INSERT [SysInfo].[tReasonCode] ON;
4817GO
4818
4819BEGIN TRY
4820INSERT INTO [SysInfo].[tReasonCode]
4821 ([reasonCodeID]
4822 , [reason])
4823 VALUES
4824 (0,'Successful')
4825 , (1, 'Add/Edit Failed')
4826 , (2,'Cannot combine fortified or swabbed milk.')
4827 , (3, 'Cannot combine milk back into any source container.')
4828 , (4, 'Cannot freeze thawed or fortified milk.')
4829 , (5, 'Cannot move bin between a freezer and a non-freezer when it contains milk.')
4830 , (6, 'Cannot delete a location when it or a sub-location contains milk.')
4831 , (7, 'System-required information cannot be deleted.')
4832 , (8, 'Cannot move milk into a non-storage location.')
4833 , (9, 'Cannot perform task with disposed, expired, fed or not-received milk.')
4834 , (10, 'Cannot move milk from a discharged mother.')
4835 , (11, 'Milk in this container is expired.')
4836 , (12, 'Milk in this container belongs to another mother.')
4837 , (13, 'Container assigned to a different patient.')
4838 , (14, 'Patient is not allowed to receive donor milk.')
4839 , (15, 'Patient is not allowed to receive formula.')
4840 , (16, 'Incorrect fortification for patient.')
4841 , (17, 'Patient is discharged.')
4842 , (18, 'Mother and patient do not match.')
4843 , (19, 'Expected data associated with scan not found.')
4844 , (20, 'Record with this data already exists in system.')
4845 , (21, 'Storage assigned to a different mother.')
4846 , (22, 'Record already associated with this information.')
4847 , (23, 'Record already associated with different information.')
4848 , (24, 'The bar code scanned is not a valid milk container.')
4849 , (25, 'This container marked as disposed.')
4850 , (26, 'This container marked as already fed.')
4851 , (27, 'This container never received.')
4852 , (28, 'Patient not allowed feed by bottle.')
4853 , (29, 'Unspecified error.');
4854
4855 PRINT N'Table SysInfo.tReasonCode data inserted'
4856
4857END TRY
4858BEGIN CATCH
4859 PRINT N'Table SysInfo.tReasonCode data insert failed'
4860 PRINT 'Error Number: ' + str(error_number())
4861 PRINT 'Line Number: ' + str(error_line())
4862 PRINT error_message()
4863END CATCH
4864GO
4865
4866SET IDENTITY_INSERT [SysInfo].[tReasonCode] OFF;
4867GO
4868
4869BEGIN TRY
4870/****** Object: Table [SysInfo].[tAvertLog] Script Date: 06/19/2013 11:35:53 ******/
4871SET ANSI_NULLS ON
4872SET QUOTED_IDENTIFIER ON
4873SET ANSI_PADDING ON
4874CREATE TABLE [SysInfo].[tAvertLog](
4875 [avertLogID] [bigint] IDENTITY(1,1) NOT NULL,
4876 [avertDate] [datetime] NOT NULL,
4877 [userName] [varchar](50) NOT NULL,
4878 [auditTypeID] [tinyint] NOT NULL,
4879 [firstScan] [varchar](25) NOT NULL,
4880 [secondScan] [varchar](25) NOT NULL,
4881 [reasonCodeID] [tinyint] NOT NULL,
4882
4883 CONSTRAINT [PK_tAvertLog] PRIMARY KEY CLUSTERED
4884(
4885 [avertLogID] ASC
4886)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
4887) ON [PRIMARY]
4888SET ANSI_PADDING OFF
4889
4890ALTER TABLE [SysInfo].[tAvertLog] WITH CHECK ADD CONSTRAINT [fk_tAvertLog_tAuditType] FOREIGN KEY([auditTypeID])
4891REFERENCES [SysInfo].[tAuditType] ([auditTypeID])
4892ALTER TABLE [SysInfo].[tAvertLog] CHECK CONSTRAINT [fk_tAvertLog_tAuditType]
4893ALTER TABLE [SysInfo].[tAvertLog] WITH CHECK ADD CONSTRAINT [fk_tAvertLog_tReasonCode] FOREIGN KEY([reasonCodeID])
4894REFERENCES [SysInfo].[tReasonCode] ([reasonCodeID])
4895ALTER TABLE [SysInfo].[tAvertLog] CHECK CONSTRAINT [fk_tAvertLog_tReasonCode]
4896
4897PRINT N'Table SysInfo.tAvertLog created'
4898
4899END TRY
4900BEGIN CATCH
4901 PRINT N'Table SysInfo.tAvertLog failed'
4902 PRINT 'Error Number: ' + str(error_number())
4903 PRINT 'Line Number: ' + str(error_line())
4904 PRINT error_message()
4905END CATCH
4906GO
4907/* insert data for avert log. This is going to be a royal pain in the toooshy. FIX ME! */
4908
4909BEGIN TRY
4910/* due to unforseen strangenesses in the case nesting, need to do something about the rejection table. Like maybe update it. */
4911
4912UPDATE dbo.tblRejectedFeedings
4913SET RejectionReason = CASE
4914 WHEN rejectionReason LIKE '%expired%' THEN '11'
4915 WHEN rejectionReason LIKE '%status is fed' THEN '26'
4916 WHEN rejectionReason LIKE '%disposed%' THEN '25'
4917 WHEN rejectionReason LIKE '%status is issued%' THEN '27'
4918 WHEN rejectionReason LIKE '%status is Not Filled%' THEN '27'
4919 WHEN rejectionReason LIKE '%not received%' THEN '27'
4920 WHEN rejectionReason LIKE '%not a valid%' THEN '24'
4921 WHEN rejectionReason LIKE '%error finding%' THEN '19'
4922 WHEN rejectionReason LIKE '%is not in%' THEN '24'
4923 WHEN rejectionReason LIKE '%invalid cribside label%' THEN '19'
4924 WHEN rejectionReason LIKE '%unable to find%' THEN '19'
4925 WHEN rejectionReason LIKE '%discharged%' THEN '17'
4926 WHEN rejectionReason LIKE '%assigned to another%' THEN '13'
4927 WHEN rejectionReason LIKE '%does not exist%' THEN '24'
4928 WHEN rejectionReason LIKE '%not allowed to be bottle%' THEN '28'
4929 WHEN rejectionReason LIKE '%formula feed%' THEN '15'
4930 WHEN rejectionReason LIKE '%not allowed donor milk%' THEN '14'
4931 WHEN rejectionReason LIKE '%different mothers%' THEN '12'
4932 WHEN rejectionReason LIKE '%unfortified bottle%' THEN '16'
4933 WHEN rejectionReason LIKE '%wrong bin%' THEN '21'
4934 WHEN rejectionReason LIKE '%does not belong to%' THEN '12'
4935 WHEN rejectionReason LIKE '%wrong mother%' THEN '12'
4936 ELSE RejectionReason
4937END;
4938
4939UPDATE dbo.tblRejectedFeedings
4940SET RejectionReason = CASE WHEN isNumeric(RejectionReason) = 0 THEN '29' ELSE RejectionReason END;
4941
4942INSERT INTO [SysInfo].[tAvertLog]
4943 ([avertDate]
4944 , [userName]
4945 , [auditTypeID]
4946 , [firstScan]
4947 , [secondScan]
4948 , [reasonCodeID])
4949
4950SELECT dRF.RejectionDate
4951 , dRF.[User]
4952 /* case statement between prep and feed prep 6, feed 5*/
4953 , CASE WHEN rejectionReason = '21' THEN 6 ELSE 5 END
4954 , LEFT(RTRIM(LTRIM(dRF.FeedingLabel)),25)
4955 , LEFT(RTRIM(LTRIM(dRF.MedicalRecordNumber)),25)
4956 /* ugly case statement about reason.. */
4957 , CAST(dRF.rejectionReason AS INT)
4958 FROM dbo.tblRejectedFeedings dRF;
4959
4960 PRINT N'Table SysInfo.tAvertLog data inserted'
4961
4962END TRY
4963BEGIN CATCH
4964 PRINT N'Table SysInfo.tAvertLog data insert failed'
4965 PRINT 'Error Number: ' + str(error_number())
4966 PRINT 'Line Number: ' + str(error_line())
4967 PRINT error_message()
4968END CATCH
4969GO
4970
4971BEGIN TRY
4972/****** Object: Table [SysInfo].[tHospLocation] Script Date: 12/19/2013 11:35:53 ******/
4973SET ANSI_NULLS ON
4974SET QUOTED_IDENTIFIER ON
4975SET ANSI_PADDING ON
4976
4977CREATE TABLE [SysInfo].[tHospLocation](
4978 [hospID] [tinyint] IDENTITY(1,1) NOT NULL,
4979 [hospName] [varchar](50) NOT NULL,
4980 [domainName] [varchar](25) NOT NULL,
4981
4982 CONSTRAINT [PK_tHospLocation] PRIMARY KEY CLUSTERED
4983(
4984 [hospID] ASC
4985)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
4986) ON [PRIMARY]
4987SET ANSI_PADDING OFF
4988
4989
4990PRINT N'Table SysInfo.tHospLocation created'
4991
4992END TRY
4993BEGIN CATCH
4994 PRINT N'Table SysInfo.tHospLocation failed'
4995 PRINT 'Error Number: ' + str(error_number())
4996 PRINT 'Line Number: ' + str(error_line())
4997 PRINT error_message()
4998END CATCH
4999GO
5000
5001SET IDENTITY_INSERT [SysInfo].[tHospLocation] ON;
5002GO
5003
5004BEGIN TRY
5005INSERT INTO [SysInfo].[tHospLocation]
5006 ([hospID]
5007 , [hospName]
5008 , [domainName])
5009 VALUES
5010 (0,'Default Location', 'DomainName')
5011 ;
5012
5013 PRINT N'Table SysInfo.tHospLocation data inserted'
5014
5015END TRY
5016BEGIN CATCH
5017 PRINT N'Table SysInfo.tHospLocation data insert failed'
5018 PRINT 'Error Number: ' + str(error_number())
5019 PRINT 'Line Number: ' + str(error_line())
5020 PRINT error_message()
5021END CATCH
5022GO
5023
5024SET IDENTITY_INSERT [SysInfo].[tHospLocation] OFF;
5025GO
5026
5027BEGIN TRY
5028/****** Object: Table [SysInfo].[tLDAPgrp] Script Date: 12/19/2013 11:35:53 ******/
5029SET ANSI_NULLS ON
5030SET QUOTED_IDENTIFIER ON
5031SET ANSI_PADDING ON
5032CREATE TABLE [SysInfo].[tLDAPgrp](
5033 [groupID] [tinyint] IDENTITY(1,1) NOT NULL,
5034 [hospID] [tinyint] NOT NULL,
5035 [LDAPgroupName] [varchar](25) NOT NULL,
5036 [permLevelID] [tinyint] NOT NULL,
5037
5038
5039 CONSTRAINT [PK_tGroupID] PRIMARY KEY CLUSTERED
5040(
5041 [groupID] ASC
5042)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
5043) ON [PRIMARY]
5044SET ANSI_PADDING OFF
5045
5046ALTER TABLE [SysInfo].[tLDAPgrp] WITH CHECK ADD CONSTRAINT [fk_tLDAPgrp_tHospLocation] FOREIGN KEY([hospID])
5047REFERENCES [SysInfo].[tHospLocation] ([hospID])
5048
5049ALTER TABLE [SysInfo].[tLDAPgrp] CHECK CONSTRAINT [fk_tLDAPgrp_tHospLocation]
5050
5051ALTER TABLE [SysInfo].[tLDAPgrp] WITH CHECK ADD CONSTRAINT [fk_tLDAPgrp_tPermLevel] FOREIGN KEY([permLevelID])
5052REFERENCES [SysInfo].[tPermLevel] ([permLEvelID])
5053ALTER TABLE [SysInfo].[tLDAPgrp] CHECK CONSTRAINT [fk_tLDAPgrp_tPermLevel]
5054
5055ALTER TABLE [SysInfo].[tLDAPgrp] ADD CONSTRAINT [DF_tLDAPgrp_permLevel] DEFAULT ((0)) FOR [permLevelID]
5056
5057
5058PRINT N'Table SysInfo.tLDAPgrp created'
5059
5060END TRY
5061BEGIN CATCH
5062 PRINT N'Table SysInfo.tLDAPgrp failed'
5063 PRINT 'Error Number: ' + str(error_number())
5064 PRINT 'Line Number: ' + str(error_line())
5065 PRINT error_message()
5066END CATCH
5067GO
5068
5069SET IDENTITY_INSERT [SysInfo].[tLDAPgrp] ON;
5070GO
5071
5072BEGIN TRY
5073INSERT INTO [SysInfo].[tLDAPgrp]
5074 ([groupID]
5075 , [hospID]
5076 , [LDAPgroupName]
5077 , [permLevelID])
5078 VALUES
5079 (1,0,'SafeBabyTechs', 1)
5080 , (2,0,'SafeBabyNurses', 2)
5081 , (3,0,'SafeBabySupers', 3)
5082 , (4,0,'SafeBabyAdmins', 4)
5083 ;
5084
5085 PRINT N'Table SysInfo.tLDAPgrp data inserted'
5086
5087END TRY
5088BEGIN CATCH
5089 PRINT N'Table SysInfo.tLDAPgrp data insert failed'
5090 PRINT 'Error Number: ' + str(error_number())
5091 PRINT 'Line Number: ' + str(error_line())
5092 PRINT error_message()
5093END CATCH
5094GO
5095
5096SET IDENTITY_INSERT [SysInfo].[tLDAPgrp] OFF;
5097GO
5098/* end new table structure and data transfer */
5099
5100/* helpful views */
5101-- =============================================
5102-- Author: Lisa Bohm (lbohm@paragondsi.com)
5103-- Create date: 2013-07-18
5104-- Description: Returns data mimicing previous patient tbl
5105-- Modified: 2013-11-21 to handle the HL7MsgField tbl add
5106-- =============================================
5107CREATE VIEW [PTinfo].vChild
5108AS
5109
5110SELECT PtC.pSysID
5111 , IDtype = pHF.contentDescript
5112 , primaryID = PtI.IDvalue
5113 , PtC.firstName
5114 , PtC.midInit
5115 , PtC.lastName
5116 , motherSysID = pCL.motherSysID
5117 , motherPrimaryID = PMI.IDvalue
5118 , PtC.dob
5119 , pCL.multiple
5120 , PtC.recordAdded
5121 , pCL.gender
5122 , pCL.allowFormula
5123 , pCL.allowBottle
5124 , PtSt.pStatTypeName
5125 , pCL.location
5126 , additiveAssigned = COALESCE(substring((SELECT DISTINCT ', ' + NtAL.additiveName
5127 FROM PTinfo.tAdditivePatient PAP
5128 INNER JOIN Nutrition.tAdditiveList NtAL ON PAP.additiveID = NtAL.additiveID
5129 WHERE PAP.pSysID = PTc.pSysID
5130 FOR XML PATH ('')),3,75), '')
5131 , allowDonor = MAX(CASE WHEN PDOrd.orderTypeID = 1 THEN 1 ELSE 0 END)
5132 , currentNPO = MAX(CASE WHEN PDOrd.orderTypeID = 4 THEN 1 ELSE 0 END)
5133
5134FROM PTinfo.tPatient PTc
5135 INNER JOIN PtInfo.tIdentity PtI ON PtC.pSysID = PtI.pSysID
5136 INNER JOIN PTinfo.tIdentityType pIT ON PtI.identTypeID = pIT.identTypeID
5137 INNER JOIN PTinfo.tHL7MsgField PHF ON PIT.fieldID = PHF.fieldID
5138 INNER JOIN SysInfo.tSetting StS ON pHF.fieldNum = StS.setValue
5139 INNER JOIN PTinfo.tChildLink pCL ON PtC.psysID = pCL.pSysID
5140 INNER JOIN PtInfo.tPatient ptM ON PCL.motherSysID = ptM.pSysID
5141 INNER JOIN PtInfo.tIdentity PMI ON PtM.pSysID = PMI.pSysID
5142 INNER JOIN PTinfo.tIdentityType pMT ON PMI.identTypeID = pMT.identTypeID
5143 INNER JOIN PTinfo.tHL7MsgField PHFM ON pMT.fieldID = PHFM.fieldID
5144 INNER JOIN SysInfo.tSetting StSM ON pHFM.fieldNum = StSM.setValue
5145 INNER JOIN PTinfo.tStatus PtS ON PtC.pSysID = PtS.pSysID
5146 INNER JOIn PTinfo.tStatusType PtST ON PtS.pStatTypeID = PtST.pStatTypeID
5147 LEFT OUTER JOIN PTinfo.tDocOrder PDOrd ON PtC.pSysID = PDOrd.pSysID
5148 AND PDord.orderEnd > getdate()
5149
5150WHERE StS.settingID = 32
5151 AND StSM.settingID = 32
5152 AND PtS.isCurrent = 1
5153 AND PtS.pStatTypeID = 1
5154
5155GROUP BY PtC.pSysID
5156 , pHF.contentDescript
5157 , PtI.IDvalue
5158 , PtC.firstName
5159 , PtC.midInit
5160 , PtC.lastName
5161 , PMI.IDvalue
5162 , PtC.dob
5163 , pCL.multiple
5164 , PtC.recordAdded
5165 , pCL.gender
5166 , pCL.allowFormula
5167 , pCL.allowBottle
5168 , PtSt.pStatTypeName
5169 , pCL.location
5170 , pCL.motherSysID;
5171GO
5172
5173 PRINT N'View PTinfo.vChild created';
5174GO
5175
5176-- =============================================
5177-- Author: Lisa Bohm (lbohm@paragondsi.com)
5178-- Create date: 2013-08-14
5179-- Description: Returns data mimicking previous mother tbl
5180-- Modified: 2013-11-21 to handle the HL7MsgField tbl add
5181-- =============================================
5182CREATE VIEW [PTinfo].vMother
5183AS
5184
5185SELECT PtM.pSysID
5186 , IDtype = PHF.contentDescript
5187 , primaryID = PtI.IDvalue
5188 , PtM.firstName
5189 , PtM.midInit
5190 , PtM.lastName
5191 , PtM.dob
5192 , PtM.recordAdded
5193 , PtSt.pStatTypeName
5194 , childFName = PtC.firstName
5195 , childLName = PtC.lastName
5196 , childSysID = PtC.pSysID
5197 , childPrimaryID = PCI.IDvalue
5198 , childStatus = PCST.pStatTypeName
5199
5200FROM PTinfo.tPatient PTM
5201 INNER JOIN PtInfo.tIdentity PtI ON PtM.pSysID = PtI.pSysID
5202 INNER JOIN PTinfo.tIdentityType pIT ON PtI.identTypeID = pIT.identTypeID
5203 INNER JOIN PTinfo.tHL7MsgField PHF ON PIT.fieldID = PHF.fieldID
5204 INNER JOIN SysInfo.tSetting StS ON pHF.fieldNum = StS.setValue
5205 INNER JOIN PTinfo.tMomInfo pMI ON PtM.psysID = pMI.pSysID
5206 INNER JOIN PTinfo.tStatus PtS ON PtM.pSysID = PtS.pSysID
5207 INNER JOIn PTinfo.tStatusType PtST ON PtS.pStatTypeID = PtST.pStatTypeID
5208 LEFT OUTER JOIN (PTinfo.tChildLink pCL
5209 INNER JOIN PtInfo.tPatient ptC ON PCL.pSysID = ptC.pSysID
5210 INNER JOIN PtInfo.tIdentity PCI ON PtC.pSysID = PCI.pSysID
5211 INNER JOIN PTinfo.tIdentityType pCT ON PCI.identTypeID = pCT.identTypeID
5212 INNER JOIN PTinfo.tHL7MsgField PCF ON PCT.fieldID = PCF.fieldID
5213 INNER JOIN SysInfo.tSetting StSC ON pCF.fieldNum = StSC.setValue
5214 INNER JOIN PTinfo.tStatus PCS ON PtC.pSysID = PCS.pSysID
5215 INNER JOIn PTinfo.tStatusType PCST ON PCS.pStatTypeID = PCST.pStatTypeID
5216 ) ON PtM.psysID = pCL.motherSysID
5217 AND StSC.settingID = 32
5218 AND PCS.isCurrent = 1
5219
5220WHERE StS.settingID = 32
5221 AND PtS.isCurrent = 1
5222 AND PtS.pStatTypeID = 1;
5223
5224GO
5225
5226 PRINT N'View PTinfo.vMother created';
5227GO
5228
5229-- =============================================
5230-- Author: Lisa Bohm (lbohm@paragondsi.com)
5231-- Create date: 2013-08-14
5232-- Description: Returns data from the HL7 table
5233-- Modified: 2013-11-21 to handle the HL7MsgField tbl add
5234-- =============================================
5235CREATE VIEW [PTinfo].vHL7
5236AS
5237
5238SELECT HLM.messageID
5239 , IDtype = PHF.contentDescript
5240 , primaryID = HLI.IDvalue
5241 , HLM.firstName
5242 , HLM.midInit
5243 , HLM.lastName
5244 , HLM.dob
5245 , HLM.gender
5246 , HLM.location
5247 , motherID = HLM.momPrimaryID
5248 , HLM.recdDate
5249 , patientType = CASE WHEN HLM.dob > DATEADD(year,-2,getdate()) THEN 1 ELSE 0 END
5250
5251FROM PTinfo.tHL7message HLM
5252 INNER JOIN PtInfo.tHL7ID HLI ON HLM.messageID = HLI.messageID
5253 INNER JOIN PTinfo.tIdentityType pIT ON HLI.identTypeID = pIT.identTypeID
5254 INNER JOIN PTinfo.tHL7MsgField PHF ON pIT.fieldID = PHF.fieldID
5255 INNER JOIN SysInfo.tSetting StS ON PHF.fieldNum = StS.setValue
5256
5257WHERE StS.settingID = 32;
5258GO
5259
5260 PRINT N'View PTinfo.vHL7 created';
5261GO
5262/* usage SPs */
5263
5264
5265/* Sysinfo.uspReasonList.storedProcedure.sql
5266To call the SP:
5267EXECUTE Sysinfo.uspReasonList; */
5268
5269BEGIN TRY
5270EXECUTE ('/****** Object: StoredProcedure [SysInfo].[uspReasonList] Script Date: 3/20/2013 11:26:19 ******/
5271-- =============================================
5272-- Author: Lisa Bohm (lbohm@paragondsi.com)
5273-- Create date: 2013-12-04
5274-- Description: Get list of reason codes
5275--
5276-- =============================================
5277CREATE PROCEDURE [SysInfo].[uspReasonList]
5278 -- Add the parameters for the stored procedure here
5279/* return all reasons */
5280AS
5281
5282 BEGIN
5283 -- SET NOCOUNT ON added to prevent extra result sets from
5284 -- interfering with SELECT statements.
5285 SET NOCOUNT ON;
5286
5287SELECT SyC.reasonCodeID
5288 , SyC.reason
5289
5290FROM SysInfo.tReasonCode SyC;
5291
5292END;
5293')
5294
5295 PRINT N'USP Sysinfo.uspReasonList created'
5296
5297END TRY
5298BEGIN CATCH
5299 PRINT N'USP Sysinfo.uspReasonList create failed'
5300 PRINT 'Error Number: ' + str(error_number())
5301 PRINT 'Line Number: ' + str(error_line())
5302 PRINT error_message()
5303END CATCH
5304GO
5305
5306/* Sysinfo.uspReasonByCode.storedProcedure.sql
5307To call the SP:
5308EXECUTE Sysinfo.uspReasonByCode @reasonCodeID=1; */
5309
5310BEGIN TRY
5311EXECUTE ('/****** Object: StoredProcedure [SysInfo].[uspReasonByCode] Script Date: 3/20/2013 11:26:19 ******/
5312-- =============================================
5313-- Author: Lisa Bohm (lbohm@paragondsi.com)
5314-- Create date: 2013-12-04
5315-- Description: Get reason by code
5316--
5317-- =============================================
5318CREATE PROCEDURE [SysInfo].[uspReasonByCode]
5319 -- Add the parameters for the stored procedure here
5320@reasonCodeID tinyint
5321
5322AS
5323
5324 BEGIN
5325 -- SET NOCOUNT ON added to prevent extra result sets from
5326 -- interfering with SELECT statements.
5327 SET NOCOUNT ON;
5328
5329SELECT SyC.reasonCodeID
5330 , SyC.reason
5331
5332FROM SysInfo.tReasonCode SyC
5333
5334WHERE SyC.reasonCodeID = @reasonCodeID;
5335
5336END;
5337')
5338
5339 PRINT N'USP Sysinfo.uspReasonByCode created'
5340
5341END TRY
5342BEGIN CATCH
5343 PRINT N'USP Sysinfo.uspReasonByCode create failed'
5344 PRINT 'Error Number: ' + str(error_number())
5345 PRINT 'Line Number: ' + str(error_line())
5346 PRINT error_message()
5347END CATCH
5348GO
5349
5350/* Sysinfo.uspAddAccessEntry.storedProcedure.sql
5351To call the SP:
5352EXECUTE Sysinfo.uspAddAccessEntry @username='admin', @auditTypeID=3, @unit='HH'; */
5353
5354BEGIN TRY
5355EXECUTE ('/****** Object: StoredProcedure [SysInfo].[uspAddAccessEntry] Script Date: 3/20/2013 11:26:19 ******/
5356-- =============================================
5357-- Author: Lisa Bohm (lbohm@paragondsi.com)
5358-- Create date: 2013-12-04
5359-- Description: Add entry to access audit table
5360--
5361-- =============================================
5362CREATE PROCEDURE [SysInfo].[uspAddAccessEntry]
5363 -- Add the parameters for the stored procedure here
5364@username varchar(50)
5365, @auditTypeID tinyint
5366, @unit varchar(15)
5367
5368AS
5369
5370 BEGIN
5371 -- SET NOCOUNT ON added to prevent extra result sets from
5372 -- interfering with SELECT statements.
5373 SET NOCOUNT ON;
5374
5375INSERT INTO SysInfo.tAccessLog
5376 (accessDate, userName, auditTypeID, unit)
5377
5378VALUES
5379 (getdate()
5380 , @userName
5381 , @auditTypeID
5382 , @unit);
5383
5384END;
5385')
5386
5387 PRINT N'USP Sysinfo.uspAddAccessEntry created'
5388
5389END TRY
5390BEGIN CATCH
5391 PRINT N'USP Sysinfo.uspAddAccessEntry create failed'
5392 PRINT 'Error Number: ' + str(error_number())
5393 PRINT 'Line Number: ' + str(error_line())
5394 PRINT error_message()
5395END CATCH
5396GO
5397
5398/* Sysinfo.uspAddAvertEntry.storedProcedure.sql
5399To call the SP:
5400EXECUTE Sysinfo.uspAddAvertEntry @username='admin', @auditTypeID=5, @firstScan='B1034', @secondScan='1354852', @reasonCodeID=15; */
5401
5402BEGIN TRY
5403EXECUTE ('/****** Object: StoredProcedure [SysInfo].[uspAddAvertEntry] Script Date: 3/20/2013 11:26:19 ******/
5404-- =============================================
5405-- Author: Lisa Bohm (lbohm@paragondsi.com)
5406-- Create date: 2013-12-04
5407-- Description: Add entry to avert audit table
5408--
5409-- =============================================
5410CREATE PROCEDURE [SysInfo].[uspAddAvertEntry]
5411 -- Add the parameters for the stored procedure here
5412@username varchar(50)
5413, @auditTypeID tinyint
5414, @firstScan varchar(25)
5415, @secondScan varchar(25)
5416, @reasonCodeID tinyint
5417
5418AS
5419 BEGIN
5420 -- SET NOCOUNT ON added to prevent extra result sets from
5421 -- interfering with SELECT statements.
5422 SET NOCOUNT ON;
5423
5424INSERT INTO SysInfo.tAvertLog
5425 (avertDate, userName, auditTypeID, firstScan, secondScan, reasonCodeID)
5426
5427VALUES
5428 (getdate()
5429 , @userName
5430 , @auditTypeID
5431 , @firstScan
5432 , @secondScan
5433 , @reasonCodeID);
5434
5435END;
5436')
5437
5438 PRINT N'USP Sysinfo.uspAddAvertEntry created'
5439
5440END TRY
5441BEGIN CATCH
5442 PRINT N'USP Sysinfo.uspAddAvertEntry create failed'
5443 PRINT 'Error Number: ' + str(error_number())
5444 PRINT 'Line Number: ' + str(error_line())
5445 PRINT error_message()
5446END CATCH
5447GO
5448
5449/* Sysinfo.uspAddDataChange.storedProcedure.sql
5450To call the SP:
5451EXECUTE Sysinfo.uspAddDataChange @username='admin', @auditTypeID=10, @tableName='PTinfo.tPatient', @recordID=110, @previousEntry='Joe, Smith, L, 20131201, blah blah'; */
5452
5453BEGIN TRY
5454EXECUTE ('/****** Object: StoredProcedure [SysInfo].[uspAddDataChange] Script Date: 3/20/2013 11:26:19 ******/
5455-- =============================================
5456-- Author: Lisa Bohm (lbohm@paragondsi.com)
5457-- Create date: 2013-12-04
5458-- Description: Add entry to avert audit table
5459--
5460-- =============================================
5461CREATE PROCEDURE [SysInfo].[uspAddDataChange]
5462 -- Add the parameters for the stored procedure here
5463@username varchar(50)
5464, @auditTypeID tinyint
5465, @tableName varchar(35)
5466, @recordID bigint
5467, @previousEntry varchar(1000)
5468
5469AS
5470
5471 BEGIN
5472 -- SET NOCOUNT ON added to prevent extra result sets from
5473 -- interfering with SELECT statements.
5474 SET NOCOUNT ON;
5475
5476INSERT INTO SysInfo.tDataChange
5477 (viewDate, userName, auditTypeID, tableName, recordID, previousEntry)
5478
5479VALUES
5480 (getdate()
5481 , @userName
5482 , @auditTypeID
5483 , @tableName
5484 , @recordID
5485 , @previousEntry);
5486
5487END;
5488')
5489
5490 PRINT N'USP Sysinfo.uspAddDataChange created'
5491
5492END TRY
5493BEGIN CATCH
5494 PRINT N'USP Sysinfo.uspAddDataChange create failed'
5495 PRINT 'Error Number: ' + str(error_number())
5496 PRINT 'Line Number: ' + str(error_line())
5497 PRINT error_message()
5498END CATCH
5499GO
5500
5501/* Sysinfo.uspRecordView.storedProcedure.sql
5502To call the SP:
5503EXECUTE Sysinfo.uspRecordView @username='admin', @auditTypeID=7, @pSysID=120; */
5504
5505BEGIN TRY
5506EXECUTE ('/****** Object: StoredProcedure [SysInfo].[uspAddRecordView] Script Date: 3/20/2013 11:26:19 ******/
5507-- =============================================
5508-- Author: Lisa Bohm (lbohm@paragondsi.com)
5509-- Create date: 2013-12-04
5510-- Description: Add entry to record view table
5511--
5512-- =============================================
5513CREATE PROCEDURE [SysInfo].[uspAddRecordView]
5514 -- Add the parameters for the stored procedure here
5515@username varchar(50)
5516, @auditTypeID tinyint
5517, @pSysID bigint
5518
5519AS
5520
5521 BEGIN
5522 -- SET NOCOUNT ON added to prevent extra result sets from
5523 -- interfering with SELECT statements.
5524 SET NOCOUNT ON;
5525
5526INSERT INTO SysInfo.tRecordView
5527 (viewDate, userName, auditTypeID, pSysID)
5528
5529VALUES
5530 (getdate()
5531 , @userName
5532 , @auditTypeID
5533 , @pSysID);
5534
5535END;
5536')
5537
5538 PRINT N'USP Sysinfo.uspRecordView created'
5539
5540END TRY
5541BEGIN CATCH
5542 PRINT N'USP Sysinfo.uspRecordView create failed'
5543 PRINT 'Error Number: ' + str(error_number())
5544 PRINT 'Line Number: ' + str(error_line())
5545 PRINT error_message()
5546END CATCH
5547GO
5548
5549/* Sysinfo.uspReportView.storedProcedure.sql
5550To call the SP:
5551EXECUTE Sysinfo.uspReportView @username='admin', @auditTypeID=8, @reportName='Vessel History', @startDate='19000101', @endDate='25000101', @addtilParamValue='B103'; */
5552
5553BEGIN TRY
5554EXECUTE ('/****** Object: StoredProcedure [SysInfo].[uspAddReportView] Script Date: 3/20/2013 11:26:19 ******/
5555-- =============================================
5556-- Author: Lisa Bohm (lbohm@paragondsi.com)
5557-- Create date: 2013-12-04
5558-- Description: Add entry to record view table
5559--
5560-- =============================================
5561CREATE PROCEDURE [SysInfo].[uspAddReportView]
5562 -- Add the parameters for the stored procedure here
5563@username varchar(50)
5564, @auditTypeID tinyint
5565, @reportName varchar(25)
5566, @startDate date
5567, @endDate date
5568, @addtlParamValue varchar(50)
5569
5570AS
5571
5572 BEGIN
5573 -- SET NOCOUNT ON added to prevent extra result sets from
5574 -- interfering with SELECT statements.
5575 SET NOCOUNT ON;
5576
5577INSERT INTO SysInfo.tReportView
5578 (viewDate, userName, auditTypeID, reportName, startDate, endDate, addtlParamValue)
5579
5580VALUES
5581 (getdate()
5582 , @userName
5583 , @auditTypeID
5584 , @reportName
5585 , @startDate
5586 , @endDate
5587 , @addtlParamValue);
5588
5589END;
5590')
5591
5592 PRINT N'USP Sysinfo.uspReportView created'
5593
5594END TRY
5595BEGIN CATCH
5596 PRINT N'USP Sysinfo.uspReportView create failed'
5597 PRINT 'Error Number: ' + str(error_number())
5598 PRINT 'Line Number: ' + str(error_line())
5599 PRINT error_message()
5600END CATCH
5601GO
5602
5603/* Nutrition.uspGetFormulaList.storedProcedure.sql
5604To call the SP:
5605EXECUTE Nutrition.uspGetFormulaList; */
5606
5607BEGIN TRY
5608EXECUTE ('
5609/****** Object: StoredProcedure [Nutrition].[uspGetFormulaList] Script Date: 8/12/2013 11:26:19 ******/
5610-- =============================================
5611-- Author: Lisa Bohm (lbohm@paragondsi.com)
5612-- Create date: 2013-08-12
5613-- Description: gets formula list
5614--
5615-- =============================================
5616CREATE PROCEDURE [Nutrition].[uspGetFormulaList]
5617 -- Add the parameters for the stored procedure here
5618 AS
5619
5620 BEGIN
5621 -- SET NOCOUNT ON added to prevent extra result sets from
5622 -- interfering with SELECT statements.
5623 SET NOCOUNT ON;
5624
5625SELECT NtF.formulaID
5626 , NtF.formulaName
5627 , NtF.calsPer
5628 , NFL.formLotID
5629
5630FROM Nutrition.tFormula NtF
5631 LEFT OUTER JOIN Nutrition.tFormulaLot NFL ON NtF.formulaID = NFL.formulaID
5632 AND NFL.formLotCode = ''Unspecified lot''
5633
5634WHERE NtF.isActive = 1
5635 AND NtF.formulaID > 0;
5636
5637END;
5638')
5639
5640 PRINT N'USP Nutrition.uspGetFormulaList created'
5641
5642END TRY
5643BEGIN CATCH
5644 PRINT N'USP Nutrition.uspGetFormulaList create failed'
5645 PRINT 'Error Number: ' + str(error_number())
5646 PRINT 'Line Number: ' + str(error_line())
5647 PRINT error_message()
5648END CATCH
5649GO
5650
5651/* Nutrition.uspAddEditFormula.storedProcedure.sql
5652To call the SP:
5653EXECUTE Nutrition.uspAddEditFormula @formulaID=0, @formulaName='New Formula', @calsPer=20, @username='admin';
5654For add, please use @formulaID = 0. For edit, use actual formulaID. Returns formulaID, errCode (fields) */
5655BEGIN TRY
5656EXECUTE ('
5657-- =============================================
5658-- Author: Lisa Bohm (lbohm@paragondsi.com)
5659-- Create date: 2013-07-10
5660-- Description: Add/Edit formula
5661-- Modified: 2013-12-05 add audit tracking call
5662-- =============================================
5663CREATE PROCEDURE [Nutrition].[uspAddEditFormula]
5664 -- Add the parameters for the stored procedure here
5665@formulaID smallint
5666, @formulaName varchar(50)
5667, @calsPer decimal(6,2)
5668, @username varchar(50)
5669
5670AS
5671DECLARE @returnID smallint
5672 , @isActive bit
5673 , @already bit
5674 , @errCode tinyint
5675 , @theNewID int
5676 , @prevEntry varchar(1000);
5677
5678DECLARE @theInfo TABLE ([formulaID] smallint);
5679
5680 BEGIN
5681 -- SET NOCOUNT ON added to prevent extra result sets from
5682 -- interfering with SELECT statements.
5683 SET NOCOUNT ON;
5684
5685BEGIN TRY
5686
5687IF @formulaID > 0
5688 -- formula exists so update --
5689 BEGIN
5690 SELECT @prevEntry = NtF.formulaName + ''; '' + CAST(NtF.calsPer AS VARCHAR) + ''; '' + CAST(NtF.isActive AS VARCHAR) + ''.''
5691 FROM Nutrition.tFormula NtF
5692 WHERE NtF.formulaID = @formulaID;
5693
5694 UPDATE Nutrition.tFormula
5695 SET formulaName = @formulaName
5696 , calsPer = @calsPer
5697 WHERE formulaID = @formulaID;
5698
5699 EXECUTE Sysinfo.uspAddDataChange @username=@username, @auditTypeID=10, @tableName=''Nutrition.tFormula'', @recordID=@formulaID, @previousEntry=@prevEntry;
5700
5701 SET @returnID = @formulaID;
5702 SET @errCode = 0;
5703
5704 SELECT formulaID = @returnID
5705 , errCode = @errCode;
5706 END
5707
5708ELSE
5709 BEGIN
5710
5711 SET @already = COALESCE(
5712 (SELECT NtF.formulaID
5713 FROM Nutrition.tFormula NtF
5714 WHERE NtF.formulaName = @formulaName), 0)
5715
5716 IF @already > 0
5717 BEGIN
5718 /* if inactive, activate */
5719 SELECT @isActive = NF.isActive
5720 FROM Nutrition.tFormula NF
5721 WHERE NF.formulaID = @already;
5722
5723 IF @isActive = 0
5724 BEGIN
5725 UPDATE Nutrition.tFormula
5726 SET isActive = 1
5727 WHERE formulaID = @already;
5728
5729 EXECUTE Sysinfo.uspAddDataChange @username=@username, @auditTypeID=10, @tableName=''Nutrition.tFormula'', @recordID=@already, @previousEntry=''isActive=0'';
5730
5731 SET @errCode = 0
5732 SELECT formulaID = @formulaID
5733 , errCode = @errCode;
5734 END
5735 ELSE
5736 BEGIN
5737 SET @errCode = 20;
5738
5739 SELECT formulaID = @already
5740 , errCode = @errCode;
5741 END
5742 END
5743 ELSE
5744 BEGIN
5745 -- formula doesn''t exist so add --
5746 INSERT INTO Nutrition.tFormula
5747 (formulaName
5748 , calsPer
5749 , isActive)
5750 OUTPUT INSERTED.formulaID INTO @theInfo
5751
5752 VALUES (@formulaName
5753 , @calsPer
5754 , 1)
5755
5756 SET @returnID = (SELECT formulaID
5757 FROM @theInfo);
5758
5759 EXECUTE Sysinfo.uspAddDataChange @username=@username, @auditTypeID=9, @tableName=''Nutrition.tFormula'', @recordID=@returnID, @previousEntry=''N/A'';
5760
5761 /* add unknown formula lot for people not using */
5762 INSERT INTO Nutrition.tFormulaLot
5763 (formulaID, formLotCode)
5764
5765 VALUES (@returnID
5766 , ''Unspecified lot'');
5767
5768 SET @theNewID = SCOPE_IDENTITY();
5769
5770 EXECUTE Sysinfo.uspAddDataChange @username=@username, @auditTypeID=9, @tableName=''Nutrition.tFormulaLot'', @recordID=@theNewID, @previousEntry=''N/A'';
5771
5772 SET @errCode = 0;
5773 SELECT formulaID = @returnID
5774 , errCode = @errCode;
5775 END
5776 END
5777
5778END TRY
5779
5780BEGIN CATCH
5781 SET @errCode = 1;
5782 SELECT formulaID = 0
5783 , errCode = @errCode;
5784 END CATCH
5785
5786END;
5787')
5788
5789 PRINT N'USP Nutrition.uspAddEditFormula created'
5790
5791END TRY
5792BEGIN CATCH
5793 PRINT N'USP Nutrition.uspAddEditFormula create failed'
5794 PRINT 'Error Number: ' + str(error_number())
5795 PRINT 'Line Number: ' + str(error_line())
5796 PRINT error_message()
5797END CATCH
5798GO
5799
5800
5801/* Nutrition.uspAuditMilkByCode.storedProcedure.sql
5802To call the SP:
5803EXECUTE Nutrition.uspAuditMilkByCode @vesselCode='B141', @isView=1, @username='admin'; */
5804
5805BEGIN TRY
5806EXECUTE ('
5807/****** Object: StoredProcedure [Nutrition].[uspAuditMilkByCode] Script Date: 7/16/2013 10:40:17 AM ******/
5808-- =============================================
5809-- Author:Lisa Bohm (lbohm@paragondsi.com)
5810-- Create date: 2013-07-16
5811-- Description:Returns vessel data for a vessel code
5812-- Modified: 2013-11-21 to handle the HL7MsgField tbl add
5813-- 2013-12-05 add audit tracking calls
5814-- 2014-02-26 xml special character display fixing
5815-- =============================================
5816CREATE PROCEDURE [Nutrition].[uspAuditMilkByCode]
5817-- Add the parameters for the stored procedure here
5818
5819@vesselCode varchar(50)
5820, @isView bit = 0
5821, @username varchar(50)
5822
5823AS
5824DECLARE @priPID tinyint
5825 , @volTracked bit
5826 , @already smallint
5827 , @errCode varchar(75)
5828 , @auditBottle bigint
5829 , @pSysID bigint
5830
5831BEGIN
5832-- SET NOCOUNT ON added to prevent extra result sets from
5833-- interfering with SELECT statements.
5834SET NOCOUNT ON;
5835
5836SET @priPID = PTinfo.udfGetPIDtype();
5837
5838SET @volTracked = (SELECT StS.setValue
5839 FROM Sysinfo.tSetting StS
5840 WHERE StS.settingID = 16);
5841
5842SET @auditBottle = COALESCE((SELECT NtV.vesselID
5843 FROM Nutrition.tVessel NtV
5844 WHERE NtV.vesselCode = @vesselCode),0);
5845
5846
5847IF @auditBottle > 0
5848BEGIN
5849
5850/* temp table for contents */
5851DECLARE @contents TABLE (oVID bigint
5852 , vesselID bigint)
5853
5854DECLARE @contType TABLE (cType char(4)
5855 , cVal bigint
5856 , oVID bigint)
5857
5858SET @already = COALESCE((SELECT COUNT(NVO.vesselOriginID)
5859 FROM Nutrition.tVesselOrigin NVO
5860 INNER JOIN Nutrition.tVessel NTVo ON NVO.targetVesselID = NTVo.vesselID
5861 WHERE NtVO.vesselCode = @vesselCode), 0)
5862
5863IF @already = 0
5864 BEGIN
5865
5866 /* it''s already a root bottle so no distincts needed here */
5867
5868 INSERT INTO @contType
5869 (cType
5870 , cVal
5871 , oVID)
5872
5873 SELECT CASE WHEN NMV.pSysID IS NULL THEN
5874 CASE WHEN NDV.lotID IS NULL THEN
5875 CASE WHEN NFV.formLotID IS NULL THEN ''Err!'' ELSE ''flID'' END
5876 ELSE ''dlID'' END
5877 ELSE ''msID'' END
5878
5879 , CASE WHEN NMV.pSysID IS NULL THEN
5880 CASE WHEN NDV.lotID IS NULL THEN
5881 CASE WHEN NFV.formLotID IS NULL THEN 0 ELSE NFV.formLotID END
5882 ELSE NDV.lotID END
5883 ELSE NMV.pSysID END
5884 , @auditBottle
5885
5886
5887 FROM Nutrition.tVessel NTVi
5888 LEFT OUTER JOIN Nutrition.tMotherVessel NMV ON NtVI.vesselID = NMV.vesselID
5889 LEFT OUTER JOIn Nutrition.tDonorVessel NDV ON NTVi.vesselID = NDV.vesselID
5890 LEFT OUTER JOIn Nutrition.tFormulaVessel NFV ON NTVi.vesselID = NFV.vesselID
5891
5892 WHERE NtVi.vesselCode = @vesselCode;
5893/* add any additives */
5894
5895 INSERT INTO @contType
5896 (cType
5897 , cVal
5898 , oVID)
5899
5900 SELECT ''adID''
5901 , NTai.additiveID
5902 , @auditBottle
5903
5904 FROM Nutrition.tAdditiveIssue NTai
5905 WHERE NtAI.vesselID = @auditBottle;
5906INSERT INTO @contents
5907 (oVID
5908 , vesselID)
5909 VALUES
5910 (@auditBottle
5911 , @auditBottle);
5912
5913 END
5914ELSE
5915 BEGIN
5916
5917 WITH cteBuildPath AS
5918 (-- the anchor part now is just for the original bottle
5919 SELECT oVessel = @auditBottle
5920 , tgtVessel = NtV.vesselID
5921 FROM Nutrition.tVessel NtV
5922 WHERE NtV.vesselID = @auditBottle
5923
5924 /* the recursive part */
5925 UNION ALL
5926
5927 SELECT oVessel = NVOl.vesselID
5928 , tgtVessel = NVOl.targetVesselID
5929 FROM Nutrition.tVesselOrigin NVOl
5930 INNER JOIN cteBuildPath AS cte ON cte.oVessel = NVOl.targetVesselID
5931 )
5932
5933
5934 INSERT INTO @contents
5935 (oVID
5936 , vesselID)
5937
5938 SELECT @auditBottle
5939 , NTVo.vesselID
5940 FROM cteBuildPath RTv
5941 INNER JOIN Nutrition.tVessel NTVo ON RTv.oVessel = NTVo.vesselID
5942 ;
5943
5944/* mother sys IDs */
5945 INSERT INTO @contType
5946 (cType
5947 , cVal
5948 , oVID)
5949
5950 SELECT DISTINCT ''msID''
5951 , NMV.pSysID
5952 , @auditBottle
5953 FROM Nutrition.tMotherVessel NMV
5954 INNER JOIN @contents ct ON cT.vesselID = NMV.vesselID;
5955
5956/* donor lots */
5957 INSERT INTO @contType
5958 (cType
5959 , cVal
5960 , oVID)
5961
5962 SELECT DISTINCT ''dlID''
5963 , NDV.lotID
5964 , @auditBottle
5965 FROM Nutrition.tDonorVessel NDV
5966 INNER JOIN @contents ct ON cT.vesselID = NDV.vesselID;
5967
5968/* formula lots */
5969 INSERT INTO @contType
5970 (cType
5971 , cVal
5972 , oVID)
5973
5974 SELECT DISTINCT ''flID''
5975 , NFV.formLotID
5976 , @auditBottle
5977 FROM Nutrition.tFormulaVessel NFV
5978 INNER JOIN @contents ct ON cT.vesselID = NFV.vesselID;
5979
5980/* add any additives */
5981
5982 INSERT INTO @contType
5983 (cType
5984 , cVal
5985 , oVID)
5986
5987 SELECT DISTINCT ''adID''
5988 , NTai.additiveID
5989 , @auditBottle
5990 FROM Nutrition.tAdditiveIssue NTai
5991 INNER JOIN @contents ct ON cT.vesselID = NTai.vesselID;
5992 END
5993
5994SELECT NtV.vesselID
5995 , NtV.vesselCode
5996 , NtV.nCategoryID
5997 , [contains] = NtC.nCategoryAbbrev
5998 , motherSysID = COALESCE(substring(mSys.mSysList,3,75), '''')
5999 , momPrimaryID = COALESCE(substring(mID.idList,3,75), '''')
6000 , momName = COALESCE(substring(REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( mNm.nmList, ''&'', ''&'' ), ''''', '''''''' ), ''"'', ''"'' ), ''<'', ''<'' ), ''>'', ''>''),3,75), '''')
6001 , formula = COALESCE(substring(REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( fl.flList, ''&'', ''&'' ), ''''', '''''''' ), ''"'', ''"'' ), ''<'', ''<'' ), ''>'', ''>'' ),3,75), '''')
6002 , additive = COALESCE(substring(REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( adl.adList, ''&'', ''&'' ), ''''', '''''''' ), ''"'', ''"'' ), ''<'', ''<'' ), ''>'', ''>''),3,75), '''')
6003 , assignedSysID = COALESCE(MAX(NtA.pSysID), '''')
6004 , assignedPrimaryID = COALESCE(MAX(PtC.IDvalue), '''')
6005 , patientName = COALESCE(MAX(
6006 CASE WHEN len(PtCp.midInit) > 0 THEN
6007 PtCP. firstName + '' '' + PtCP.midInit + ''. '' + PtCP.lastName
6008 ELSE
6009 PtCP. firstName + '' '' + PtCP.lastName
6010 END
6011 ), '''')
6012 , datePump = MAX(CASE WHEN OthH.statusID = 14 THEN OtHH.dateChange ELSE ''1900-01-01'' END)
6013 , dateRecd = MAX(CASE WHEN OthH.statusID = 17 THEN OtHH.dateChange ELSE ''1900-01-01'' END)
6014 , statusID = NtH.statusID
6015 , statusDescript = CASE WHEN NtH.statusID = 7 THEN NtSL.statusName ELSE NtSC.statCatName + '': '' + NtSL.statusName END
6016 , statusDate = NtH.dateChange
6017 , NtH.expirDate
6018 , dateFed = MAX(CASE WHEN OthH.statusID = 7 THEN OtHH.dateChange ELSE ''1900-01-01'' END)
6019 , mls = CASE WHEN @volTracked = 1 THEN NtV.mls ELSE NULL END
6020 , gms = CASE WHEN @volTracked = 1 THEN NtV.gms ELSE NULL END
6021 , cals = NtV.calsPer
6022 , location = COALESCE(PCL.location, '''')
6023 , origBottles = COALESCE(substring(org.orvs,3,75), '''')
6024 , expSoonest = COALESCE(substring(EXPS.vesselCode,3,75), '''')
6025 , freshest = COALESCE(substring(FRESH.vesselCode,3,75), '''')
6026 , donorLot = COALESCE(substring(REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( dl.dllist, ''&'', ''&'' ), ''''', '''''''' ), ''"'', ''"'' ), ''<'', ''<'' ), ''>'', ''>''),3,75), '''')
6027 , vesselNote = COALESCE(substring(REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( note.nList, ''&'', ''&'' ), ''''', '''''''' ), ''"'', ''"'' ), ''<'', ''<'' ), ''>'', ''>''),3,75), '''')
6028 , vesselLoc = StL.locationCode
6029
6030FROM Nutrition.tVessel NtV
6031 INNER JOIN Nutrition.tCategory NtC ON NtV.nCategoryID = NtC.nCategoryID
6032 INNER JOIN Nutrition.tVesselHistory NtH ON NtV.vesselID = NtH.vesselID
6033 INNER JOIN Nutrition.tStatusList NtSL ON NtH.statusID = NtSL.statusID
6034 INNER JOIN Nutrition.tStatusCategory NtSC ON NtSL.statCatID = NtSC.statCatID
6035 LEFT OUTER JOIN Storage.tLocation StL ON NtV.locationID = StL.locationID
6036 LEFT OUTER JOIN Nutrition.tVesselHistory OthH ON NtV.vesselID = OthH.vesselID
6037 LEFT OUTER JOIN (Nutrition.tVesselAssign NtA
6038 INNER JOIN PTinfo.tPatient PtCP ON NtA.pSysID = PtCP.pSysID
6039 INNER JOIN PTinfo.tIdentity PtC ON NtA.pSysID = PtC.pSysID
6040 AND PtC.identTypeID = @priPID
6041 INNER JOIN PTinfo.tChildLink PCL ON PtCP.pSysID = PCL.pSysID
6042 ) ON NtV.vesselID = NtA.vesselID
6043 OUTER APPLY (SELECT '', '' + CAST(MCT.cVal as varchar)
6044 FROM @contType MCT
6045 WHERE MCT.cType = ''msID''
6046 FOR XML PATH ('''')) mSys (mSysList)
6047 OUTER APPLY (SELECT '', '' + PtM.IDvalue
6048 FROM @contType NMVo
6049 LEFT OUTER JOIN PTinfo.tIdentity PtM ON nMVO.cVal = PtM.pSysID
6050 AND PtM.identTypeID = @priPID
6051 WHERE NMVo.cType = ''msID''
6052 FOR XML PATH ('''')) mID (idList)
6053 OUTER APPLY (SELECT '', '' + PtMP.firstName + '' '' + PtMP.lastName
6054 FROM @contType NMVo
6055 LEFT OUTER JOIN (PTinfo.tIdentity PtM
6056 INNER JOIN PTinfo.tPatient PtMP ON PtM.pSysID = PtMP.pSysID
6057 AND PtM.identTypeID = @priPID
6058 ) ON nMVO.cVal = PtM.pSysID
6059 WHERE NMVo.cType = ''msID''
6060 FOR XML PATH ('''')) mNm (nmList)
6061 OUTER APPLY (SELECT TOP 3 '', '' + NTFC.vesselCode
6062 FROM Nutrition.tVessel NtFC
6063 INNER JOIN Nutrition.tMotherVessel NtMC ON NtFC.vesselID = NtMC.vesselID
6064 INNER JOIN Nutrition.tVesselHistory NtFH ON NtFC.vesselID = NtFH.vesselID
6065 INNER JOIn Nutrition.tStatusList NtFS ON NtFH.statusID = NtFS.statusID
6066 INNER JOIN @contType mSysID ON NtV.vesselID = mSysID.oVID
6067 WHERE NtFH.isCurrent = 1
6068 AND NtFS.statCatID = 2
6069 AND mSysID.cType = ''msID''
6070 AND NtMC.pSysID = mSysID.cVal
6071 ORDER BY NTFH.expirDate
6072 FOR XML PATH('''')) EXPS (vesselCode)
6073 OUTER APPLY (SELECT TOP 3 '', '' + NTFC.vesselCode
6074 FROM Nutrition.tVessel NtFC
6075 INNER JOIN Nutrition.tMotherVessel NtMC ON NtFC.vesselID = NtMC.vesselID
6076 INNER JOIN Nutrition.tVesselHistory NtFH ON NtFC.vesselID = NtFH.vesselID
6077 INNER JOIn Nutrition.tStatusList NtFS ON NtFH.statusID = NtFS.statusID
6078 INNER JOIN @contType mSysID ON NtV.vesselID = mSysID.oVID
6079 WHERE NtFH.isCurrent = 1
6080 AND NtFS.statCatID = 2
6081 AND mSysID.cType = ''msID''
6082 AND NtMC.pSysID = mSysID.cVal
6083 ORDER BY NTFH.expirDate DESC
6084 FOR XML PATH('''')) FRESH (vesselCode)
6085 OUTER APPLY (SELECT '', '' + NTF.formulaName + '': '' + NFL.formLotCode
6086 FROM @contType NFVo
6087 INNER JOIN Nutrition.tFormulaLot NFL ON NFvo.cVal = NFL.formLotID
6088 INNER JOIN Nutrition.tFormula NTF ON NFL.formulaID = NTF.formulaID
6089 WHERE NFVo.cType =''flID''
6090 FOR XML PATH ('''')) fL (flList)
6091 OUTER APPLY (SELECT '', '' + NtAL.additiveName
6092 FROM @contType NtAI
6093 INNER JOIN Nutrition.tAdditiveList NtAL ON NtAI.cVal = NtAL.additiveID
6094 WHERE NtAI.cType = ''adID''
6095 FOR XML PATH ('''')) adl (adlist)
6096 OUTER APPLY (SELECT '', '' + NtOVC.vesselCode
6097 FROM Nutrition.tVesselOrigin NtOV
6098 INNER JOIN Nutrition.tVessel NtOVC ON NtOV.vesselID = NtOVC.vesselID
6099 WHERE NtV.vesselID = NtOV.targetVesselID
6100 AND LEFT(NtOVC.vesselCode,2) <> ''RT''
6101 FOR XML PATH ('''')) org (orvs)
6102 OUTER APPLY (SELECT '', '' + NDB.bankName + '': '' + NDL.lotCode
6103 FROM @contType NDVo
6104 INNER JOIN Nutrition.tDonorLot NDL ON NDVo.cVal = NDL.lotID
6105 INNER JOIN Nutrition.tDonorBank NDB ON NDL.donorBankID = NDB.donorBankID
6106 WHERE NDVo.cType = ''dlID''
6107 FOR XML PATH ('''')) dl (dlList)
6108 OUTER APPLY (SELECT ''; '' + NVN.vNote
6109 FROM Nutrition.tVesselNote NVN
6110 INNER JOIN @contents NCT ON NVN.vesselID = NCT.vesselID
6111 FOR XML PATH ('''')) note (nList)
6112
6113WHERE NtV.vesselID = @auditBottle
6114 AND NtH.isCurrent = 1
6115GROUP BY NtV.vesselID
6116 , NtV.vesselCode
6117 , NtV.nCategoryID
6118 , NtV.mls
6119 , NtV.gms
6120 , NtV.calsPer
6121 , NtH.expirDate
6122 , NtH.statusID
6123 , NtH.dateChange
6124 , NtSL.statusName
6125 , NtSC.statCatName
6126 , PCL.location
6127 , NtC.nCategoryAbbrev
6128 , EXPS.vesselCode
6129 , FRESH.vesselCode
6130 , mSys.mSysList
6131 , mID.idList
6132 , mNm.nmList
6133 , fl.flList
6134 , adl.adList
6135 , org.orvs
6136 , dl.dlList
6137 , note.nList
6138 , StL.locationCode
6139;
6140
6141IF @isView = 1
6142 BEGIN
6143 /*auditing - so get again who the bottle is assigneddddd to, if it is */
6144 SET @pSysID = COALESCE((SELECT NVA.pSysID
6145 FROM Nutrition.tVesselAssign NVA
6146 WHERE NVA.vesselID = @auditBottle),0);
6147
6148 IF @pSySID > 0
6149 BEGIN
6150 EXECUTE SysInfo.uspAddRecordView @username=@username, @auditTypeID=7, @pSysID=@pSysID;
6151 END
6152 /*auditing - so get again what mother the vessel belongs to, if it is */
6153 SET @pSysID = COALESCE((SELECT NMV.pSysID
6154 FROM Nutrition.tMotherVessel NMV
6155 WHERE NMV.vesselID = @auditBottle),0);
6156 IF @pSySID > 0
6157 BEGIN
6158 EXECUTE SysInfo.uspAddRecordView @username=@username, @auditTypeID=7, @pSysID=@pSysID;
6159 END
6160 END
6161END
6162ELSE
6163 BEGIN
6164 SET @errCode = 19
6165 SELECT @errCode;
6166 END
6167
6168END;
6169')
6170
6171 PRINT N'USP Nutrition.uspAuditMilkByCode created'
6172
6173END TRY
6174BEGIN CATCH
6175 PRINT N'USP Nutrition.uspAuditMilkByCode create failed'
6176 PRINT 'Error Number: ' + str(error_number())
6177 PRINT 'Line Number: ' + str(error_line())
6178 PRINT error_message()
6179END CATCH
6180GO
6181
6182/* Storage.uspAddEditLocation.storedProcedure.sql
6183To call the SP:
6184EXECUTE Nutrition.uspAddEditLocation @locationID=0, @locationName='Silly Fridge', @locatedInID='181', @locTypeID=2, @pSysID=0, @username='admin';
6185For add use locationID=0. Edit use actual locationID */
6186
6187BEGIN TRY
6188EXECUTE ('
6189/****** Object: StoredProcedure [Storage].[uspAddEditLocation] Script Date: 6/28/2013 11:26:19 ******/
6190-- =============================================
6191-- Author: Lisa Bohm (lbohm@paragondsi.com)
6192-- Create date: 2013-06-28
6193-- Description: add new location/ edit current
6194-- Modified: 2013-12-09 add audit tracking call
6195-- =============================================
6196CREATE PROCEDURE [Storage].[uspAddEditLocation]
6197 -- Add the parameters for the stored procedure here
6198@locationID bigint = 0
6199, @locationName varchar(50)
6200, @locatedInID bigint = 0
6201, @locTypeID tinyint
6202, @pSysID bigint = 0
6203, @username varchar(50)
6204
6205AS
6206DECLARE @locationCode varchar(20)
6207 , @codePrefix char(1)
6208 , @already bit
6209 , @chillType varchar(12)
6210 , @errCode tinyint
6211 , @gotMilk smallint
6212 , @prevEntry varchar(1000)
6213 , @theNewID bigint;
6214
6215BEGIN TRY
6216 -- SET NOCOUNT ON added to prevent extra result sets from
6217 -- interfering with SELECT statements.
6218 SET NOCOUNT ON;
6219BEGIN TRANSACTION
6220
6221/* pass along the chill type. ''Cause, well, we''ll need it. */
6222
6223IF @locTypeID = 4
6224 BEGIN
6225 SELECT @chillType = CASE WHEN StL.locTypeID = 2 THEN ''Fridge'' ELSE
6226 CASE WHEN StL.locTypeID = 3 THEN ''Freezer'' ELSE
6227 CASE WHEN StL.locTypeID = 1 THEN ''Non-Storage'' ELSE '''' END
6228 END
6229 END
6230 FROM Storage.tLocation STL
6231 WHERE StL.locationID = @locatedInID
6232
6233 END
6234ELSE
6235 BEGIN
6236 SET @chillType = CASE WHEN @locTypeID = 2 THEN ''Fridge'' ELSE
6237 CASE WHEN @locTypeID = 3 THEN ''Freezer'' ELSE
6238 CASE WHEN @locTypeID = 1 THEN ''Non-Storage'' ELSE '''' END
6239 END
6240 END
6241 END
6242
6243IF @locationID = 0
6244 /* the add procedure */
6245 BEGIN TRY
6246 INSERT INTO Storage.tLocation
6247 ([LocationName]
6248 ,[LocationCode]
6249 ,[locatedInID]
6250 ,[locTypeID])
6251 VALUES
6252 (@locationName
6253 , ''tmp'' + left(@locationName, 15)
6254 , @locatedInID
6255 , @locTypeID
6256 );
6257/* if location type is fridge or freeeeezer.. */
6258
6259
6260 SET @locationID = SCOPE_IDENTITY();
6261 SET @codePrefix = (SELECT LT.locTypePrefix
6262 FROM Storage.tLocationType LT
6263 WHERE locTypeID = @locTypeID)
6264
6265 SET @locationCode = @codePrefix + CAST(@locationID AS VARCHAR)
6266
6267 /* WAIT! Does this exist? No uniqueness violation blargs here */
6268
6269 SET @already = COALESCE((SELECT 1
6270 FROM Storage.tLocation StL
6271 WHERE StL.locationCode = @locationCode),0)
6272
6273 IF @already = 1
6274 BEGIN
6275 SET @locationCode = @codePrefix + ''0'' + CAST(@locationID AS VARCHAR)
6276 END
6277
6278
6279
6280 /* okay, then add the location code */
6281 UPDATE Storage.tLocation
6282 SET locationCode = @locationCode
6283 WHERE locationID = @locationID;
6284
6285 EXECUTE Sysinfo.uspAddDataChange @username=@username, @auditTypeID=9, @tableName=''Storage.tLocation'', @recordID=@locationID, @previousEntry=''N/A'';
6286
6287 SET @errCode = 0
6288 SELECT locationID = @locationID
6289 , locationCode = @locationCode
6290 , chillType = @chillType
6291 , errCode = @errCode;
6292
6293
6294 END TRY
6295 BEGIN CATCH
6296 SET @errCode = 1
6297 SELECT locationID = @locationID
6298 , locationCode = @locationCode
6299 , chillType = @chillType
6300 , errCode = @errCode;
6301 END CATCH
6302ELSE
6303 /* the edit.... */
6304BEGIN
6305 DECLARE @currLocIn bigint
6306 , @currChillType tinyint
6307 , @newChillType tinyint
6308 /* do they want to change the parent location? */
6309 SELECT @currLocIn = StLc.locatedInID
6310 , @locationCode = StLc.locationCode
6311 FROM Storage.tLocation StLc
6312 WHERE StLc.locationID = @locationID;
6313
6314 IF @currLocIn = @locatedInID
6315 BEGIN
6316 SELECT @prevEntry = ''locName: '' + StL.locationName + ''.''
6317 FROM Storage.tLocation StL
6318 WHERE StL.locationID = @locationID;
6319
6320 UPDATE Storage.tLocation
6321 SET locationName = @locationName
6322 WHERE locationID = @locationID;
6323
6324 EXECUTE Sysinfo.uspAddDataChange @username=@username, @auditTypeID=10, @tableName=''Storage.tLocation'', @recordID=@locationID, @previousEntry=@prevEntry;
6325
6326 SET @errCode = 0;
6327 SELECT locationID = @locationID
6328 , locationCode = @locationCode
6329 , chillType = @chillType
6330 , errCode = @errCode;
6331 END
6332 ELSE
6333 BEGIN
6334 SELECT @currChillType = CASE WHEN STL.locTypeID < 4 THEN
6335 CASE WHEN STL.locTypeID = 3 THEN 3 ELSE 2 END
6336 ELSE
6337 CASE WHEN BPT.locTypeID = 3 THEN 3 ELSE 2 END
6338 END
6339 FROM Storage.tLocation STL
6340 LEFT OUTER JOIn Storage.tLocation BPT ON StL.locatedInID = BPT.locationID
6341 WHERE STL.locationID = @currLocIn;
6342
6343 SELECT @newChillType = CASE WHEN STL.locTypeID < 4 THEN
6344 CASE WHEN STL.locTypeID = 3 THEN 3 ELSE 2 END
6345 ELSE
6346 CASE WHEN BPT.locTypeID = 3 THEN 3 ELSE 2 END
6347 END
6348 FROM Storage.tLocation STL
6349 LEFT OUTER JOIn Storage.tLocation BPT ON StL.locatedInID = BPT.locationID
6350 WHERE STL.locationID = @locatedinID;
6351
6352 IF @currChillType = @newChillType
6353 BEGIN
6354 /* audit info */
6355 SELECT @prevEntry = StL.locationName + ''; '' + CAST(StL.locatedInID AS VARCHAR) + ''.''
6356 FROM Storage.tLocation StL
6357 WHERE StL.locationID = @locationID;
6358
6359 UPDATE Storage.tLocation
6360 SET locationName = @locationName
6361 , locatedInID = @locatedInID
6362 WHERE locationID = @locationID;
6363
6364 /* audit record */
6365 EXECUTE Sysinfo.uspAddDataChange @username=@username, @auditTypeID=10, @tableName=''Storage.tLocation'', @recordID=@locationID, @previousEntry=@prevEntry;
6366
6367 SET @errCode = 0
6368 SELECT locationID = @locationID
6369 , locationCode = @locationCode
6370 , chillType = @chillType
6371 , errCode = @errCode;
6372
6373 END
6374 ELSE -- got milk?
6375 BEGIN
6376 WITH cteLocTree AS
6377 (-- the anchor part now is just for the original bottle
6378 SELECT origLocID = STL.locationID
6379 , parentLoc = STL.locatedInID
6380 , locCode = StL.locationCode
6381-- , hLevel = 1
6382
6383 FROM Storage.tLocation STL
6384 WHERE STL.locationID = @locationID
6385
6386 /* the recursive part */
6387 UNION ALL
6388
6389 SELECT origLocID = STLo.locationID
6390 , parentLoc = STLo.locatedInID
6391 , locCode = STLo.locationCode
6392-- , hLevel = cte.hLevel + 1
6393
6394 FROM Storage.tLocation STLo
6395 INNER JOIN cteLocTree cte ON cte.origLocID = STLo.locatedInID
6396 )
6397
6398 SELECT @gotMilk = COALESCE(COUNT(DISTINCT NtV.vesselID), 0)
6399 FROM cteLocTree CLT
6400 LEFT OUTER JOIN Nutrition.tVessel NTV ON CLT.origLocID = NTV.locationID
6401 ;
6402
6403 IF @gotMilk > 0
6404 BEGIN
6405 SET @errCode = 5
6406 SELECT locationID = @locationID
6407 , locationCode = @locationCode
6408 , chillType = @chillType
6409 , errCode = @errCode;
6410 END
6411 ELSE
6412 BEGIN
6413 /* audit info */
6414 SELECT @prevEntry = StL.locationName + ''; '' + CAST(StL.locatedInID AS VARCHAR) + ''.''
6415 FROM Storage.tLocation StL
6416 WHERE StL.locationID = @locationID;
6417
6418 UPDATE Storage.tLocation
6419 SET locationName = @locationName
6420 , locatedInID = @locatedInID
6421 WHERE locationID = @locationID;
6422
6423 /* audit record */
6424 EXECUTE Sysinfo.uspAddDataChange @username=@username, @auditTypeID=10, @tableName=''Storage.tLocation'', @recordID=@locationID, @previousEntry=@prevEntry;
6425
6426 SET @errCode = 0
6427 SELECT locationID = @locationID
6428 , locationCode = @locationCode
6429 , chillType = @chillType
6430 , errCode = @errCode;
6431 END
6432 END
6433 END
6434END
6435
6436IF @psysID > 0
6437 BEGIN
6438 /* is there an assignment already? */
6439 SELECT @theNewID = SLA.locAssignID
6440 , @prevEntry = CAST(SLA.locationID AS VARCHAR) + ''; '' + CAST(SLA.pSysID AS VARCHAR) + ''.''
6441 FROM Storage.tLocationAssign SLA
6442 WHERE locationID = @locationID
6443 AND pSysID <> @pSysID;
6444
6445 SET @theNewID = COALESCE(@theNewID, 0);
6446
6447 IF @theNewID > 0
6448 BEGIN
6449 DELETE
6450 FROM Storage.tLocationAssign
6451 WHERE locAssignID = @theNewID;
6452
6453 EXECUTE Sysinfo.uspAddDataChange @username=@username, @auditTypeID=11, @tableName=''Storage.tLocationAssign'', @recordID=@theNewID, @previousEntry=@prevEntry;
6454 END
6455
6456 SET @already = COALESCE((SELECT 1
6457 FROM Storage.tLocationAssign StLA
6458 WHERE StLA.pSysID = @pSysID
6459 AND StLA.locationID = @locationID),0)
6460 IF @already = 0
6461 BEGIN
6462 INSERT INTO Storage.tLocationAssign
6463 (locationID
6464 , pSysID)
6465
6466 VALUES (@locationID
6467 , @pSysID);
6468
6469 SET @theNewID = SCOPE_IDENTITY();
6470
6471 EXECUTE Sysinfo.uspAddDataChange @username=@username, @auditTypeID=9, @tableName=''Storage.tLocationAssign'', @recordID=@theNewID, @previousEntry=''N/A'';
6472
6473 END
6474 END
6475
6476COMMIT TRANSACTION
6477
6478END TRY
6479BEGIN CATCH
6480 SET @errCode = 1
6481 SELECT locationID = @locationID
6482 , locationCode = @locationCode
6483 , chillType = @chillType
6484 , errCode = @errCode;
6485 ROLLBACK TRANSACTION
6486END CATCH;
6487')
6488
6489 PRINT N'USP Storage.uspAddEditLocation created'
6490
6491END TRY
6492BEGIN CATCH
6493 PRINT N'USP Storage.uspAddEditLocation create failed'
6494 PRINT 'Error Number: ' + str(error_number())
6495 PRINT 'Line Number: ' + str(error_line())
6496 PRINT error_message()
6497END CATCH
6498GO
6499/* SysInfo.uspEditSetVal.storedProcedure.sql
6500To call the SP:
6501EXECUTE SysInfo.uspSetVal @settingID=2, @setValue='true', @username='admin'; */
6502
6503BEGIN TRY
6504EXECUTE ('
6505/****** Object: StoredProcedure [SysInfo].[uspEditSetVal] Script Date: 3/20/2013 11:26:19 ******/
6506-- =============================================
6507-- Author: Lisa Bohm (lbohm@paragondsi.com)
6508-- Create date: 2013-03-20
6509-- Description: Edit setting value
6510-- Modified: 2013-12-16 add audit tracking call
6511-- =============================================
6512CREATE PROCEDURE [SysInfo].[uspEditSetVal]
6513 -- Add the parameters for the stored procedure here
6514 @settingID tinyint
6515 , @setValue varchar(25)
6516 , @username varchar(50)
6517
6518AS
6519DECLARE @rowCt int
6520 , @prevEntry varchar(1000);
6521
6522 BEGIN
6523 -- SET NOCOUNT ON added to prevent extra result sets from
6524 -- interfering with SELECT statements.
6525 SET NOCOUNT ON;
6526
6527BEGIN TRANSACTION
6528
6529SELECT settingID
6530FROM SysInfo.tSetting
6531WHERE settingID = @settingID
6532SET @rowCt = @@ROWCOUNT;
6533
6534IF @rowCt = 0
6535 BEGIN
6536 PRINT N''ERR: No setting exists for this ID.''
6537 END
6538ELSE
6539 BEGIN
6540 /* get audit record stuff */
6541 SELECT @prevEntry = CAST(StS.setValue AS VARCHAR) + ''.''
6542 FROM SysInfo.tSetting StS
6543 WHERE StS.settingID = @settingID;
6544
6545 UPDATE
6546 SysInfo.tSetting
6547 SET setValue = @setValue
6548 WHERE settingID = @settingID;
6549
6550 EXECUTE Sysinfo.uspAddDataChange @username=@username, @auditTypeID=10, @tableName=''SysInfo.tSetting'', @recordID=@settingID, @previousEntry=@prevEntry;
6551
6552 END
6553COMMIT TRANSACTION
6554END;
6555')
6556
6557 PRINT N'USP SysInfo.uspEditSetVal created'
6558
6559END TRY
6560BEGIN CATCH
6561 PRINT N'USP SysInfo.uspEditSetVal create failed'
6562 PRINT 'Error Number: ' + str(error_number())
6563 PRINT 'Line Number: ' + str(error_line())
6564 PRINT error_message()
6565END CATCH
6566GO
6567
6568/* PTinfo.uspAddEditPatient.storedProcedure.sql
6569To call the SP:
6570DECLARE @rID bigint, @err varchar(75);
6571EXECUTE PTinfo.uspAddEditPatient @pSysID=0, @primaryID='sa123456', @fName='Baby', @lName='Doe', @mInit='B', @dob='20131115', @rID = @returnID OUTPUT, @errCode = @errCode OUTPUT, @username='System/HL7';
6572For add use @pSysID=0, for edit use actual pSysID */
6573
6574BEGIN TRY
6575EXECUTE ('-- =============================================
6576-- Author: Lisa Bohm (lbohm@paragondsi.com)
6577-- Create date: 2013-07-22
6578-- Description: Add/Edit a base patient/mom info
6579-- Modified: 2013-11-21 to handle the HL7MsgField tbl add
6580-- 2013-12-05 add audit tracking call
6581-- =============================================
6582CREATE PROCEDURE [PTinfo].[uspAddEditPatient]
6583 -- Add the parameters for the stored procedure here
6584@pSysID bigint = 0
6585, @primaryID varchar(50)
6586, @fName varchar(50) = NULL
6587, @lName varchar(50) = NULL
6588, @mInit char(1) = NULL
6589, @dob datetime = NULL
6590, @returnID bigint OUTPUT
6591, @errCode tinyint OUTPUT
6592, @username varchar(50)
6593
6594AS
6595DECLARE
6596 @already bit
6597 , @priPID tinyint
6598 , @theNewID bigint
6599 , @prevEntry varchar(1000);
6600
6601BEGIN
6602 -- SET NOCOUNT ON added to prevent extra result sets from
6603 -- interfering with SELECT statements.
6604 SET NOCOUNT ON;
6605
6606SET @returnID = @pSysID
6607SET @priPID = PTinfo.udfGetPIDtype();
6608
6609/* CANNOT do merge! Does patient primary ID exist? */
6610SET @already = COALESCE(
6611 (SELECT 1
6612 FROM PTinfo.tPatient PtP
6613 INNER JOIN PTinfo.tIdentity PtI ON PtP.pSysID = PtI.pSysID
6614 WHERE PtI.IDvalue = @primaryID
6615 AND PtP.pSysID <> @pSysID
6616 AND PtI.identTypeID = @priPID), 0)
6617
6618IF @already = 1
6619 -- patient ID exists but not this patient --
6620 BEGIN
6621 SET @errCode = 23
6622 SELECT returnID = @returnID
6623 , errCode = @errCode;
6624 END
6625ELSE
6626 BEGIN
6627 IF @pSysID <> 0
6628 -- update, return @pSysID
6629 BEGIN
6630 /* audit information */
6631 SELECT @prevEntry = PtP.firstName + ''; '' + PtP.lastName + ''; '' + PtP.midInit + ''; '' + CAST(PtP.dob AS varchar) + ''.''
6632 FROM PTinfo.tPatient PtP
6633 WHERE PtP.pSysID = @pSysID;
6634
6635 UPDATE PTinfo.tPatient
6636 SET firstName = @fName
6637 , lastName = @lName
6638 , midinit = @mInit
6639 , DOB = @dob
6640 WHERE pSysID = @pSysID;
6641
6642 EXECUTE Sysinfo.uspAddDataChange @username=@username, @auditTypeID=10, @tableName=''PTinfo.tPatient'', @recordID=@pSysID, @previousEntry= @prevEntry;
6643
6644 SET @errCode = 0
6645 SELECT returnID = @returnID
6646 , errCode = @errCode;
6647 END
6648 ELSE
6649 -- patient doesn''t exist so add --
6650 BEGIN TRY
6651 BEGIN TRANSACTION
6652 INSERT INTO PTinfo.tPatient
6653 (firstName
6654 , lastName
6655 , midInit
6656 , DOB
6657 , RecordAdded
6658 )
6659 VALUES (@fName
6660 , @lName
6661 , @mInit
6662 , @dob
6663 , getdate()
6664 );
6665
6666 SET @returnID = SCOPE_IDENTITY();
6667 EXECUTE Sysinfo.uspAddDataChange @username=@username, @auditTypeID=9, @tableName=''PTinfo.tPatient'', @recordID=@returnID, @previousEntry= ''N/A'';
6668
6669 INSERT INTO PTinfo.tIdentity
6670 (identTypeID
6671 , pSysID
6672 , IDvalue)
6673 VALUES
6674 (@priPID
6675 , @returnID
6676 , @primaryID);
6677
6678 SET @theNewID = SCOPE_IDENTITY();
6679 EXECUTE Sysinfo.uspAddDataChange @username=@username, @auditTypeID=9, @tableName=''PTinfo.tIdentity'', @recordID=@theNewID, @previousEntry= ''N/A'';
6680
6681 COMMIT TRANSACTION
6682 SET @errCode = 0
6683 SELECT returnID = @returnID
6684 , errCode = @errCode;
6685 END TRY
6686 BEGIN CATCH
6687 ROLLBACK TRANSACTION
6688 SET @errCode = 1
6689 SELECT returnID = @returnID
6690 , errCode = @errCode;
6691 PRINT N''Err: Data not successfully added or updated.''
6692 END CATCH
6693 END
6694END ;
6695')
6696
6697 PRINT N'USP PTinfo.uspAddEditPatient created'
6698
6699END TRY
6700BEGIN CATCH
6701 PRINT N'USP PTinfo.uspAddEditPatient create failed'
6702 PRINT 'Error Number: ' + str(error_number())
6703 PRINT 'Line Number: ' + str(error_line())
6704 PRINT error_message()
6705END CATCH
6706GO
6707
6708/* PTinfo.uspAddEditChildLink.storedProcedure.sql
6709To call the SP:
6710EXECUTE PTinfo.uspAddEditChildLink @pSysID=1, @motherSysID=2, @multiple=0, @gender='male', @allowFormula=1, @allowBottle=1, @location='Room 201', @username='admin';
6711 */
6712
6713BEGIN TRY
6714EXECUTE ('/****** Object: StoredProcedure [PTinfo].[uspAddEditChildLink] Script Date: 12/07/2012 11:26:19 ******/
6715-- =============================================
6716-- Author: Lisa Bohm (lbohm@paragondsi.com)
6717-- Create date: 2013-07-22
6718-- Description: Add/Edit a child addtl info
6719-- Modified: 2013-12-05 add audit tracking call
6720-- =============================================
6721CREATE PROCEDURE [PTinfo].[uspAddEditChildLink]
6722 -- Add the parameters for the stored procedure here
6723@pSysID bigint = 0
6724, @motherSysID bigint
6725, @multiple char(1) = ''0''
6726, @gender varchar(15) = ''Undetermined''
6727, @allowFormula bit = 0
6728, @allowBottle bit = 0
6729, @location varchar(50) = NULL
6730, @username varchar(50)
6731
6732 AS
6733 DECLARE
6734 @already bit
6735 , @errCode tinyint
6736 , @theNewID bigint
6737 , @prevEntry varchar(1000)
6738BEGIN
6739 -- SET NOCOUNT ON added to prevent extra result sets from
6740 -- interfering with SELECT statements.
6741 SET NOCOUNT ON;
6742
6743/* CANNOT do merge! Does patient info already exist? */
6744SET @already = COALESCE(
6745 (SELECT 1
6746 FROM PTinfo.tChildLink PtC
6747 WHERE PtC.pSysID = @pSysID), 0)
6748
6749IF @already = 1
6750 -- patient info exists so edit. --
6751 BEGIN
6752 /* audit information */
6753 SELECT @prevEntry = PCL.multiple + ''; '' + PCL.gender + ''; '' + CAST(PCL.allowFormula as varchar) + ''; '' + CAST(PCL.allowBottle AS varchar) + ''; '' + PCL.location + ''.''
6754 , @theNewID = PCL.childLinkID
6755 FROM PTinfo.tChildLink PCL
6756 WHERE PCL.pSysID = @pSysID;
6757
6758 UPDATE PTinfo.tChildLink
6759 SET multiple = @multiple
6760 , gender = @gender
6761 , allowFormula = @allowFormula
6762 , allowBottle = @allowBottle
6763 , location = @location
6764 WHERE pSysID = @pSysID;
6765
6766 EXECUTE Sysinfo.uspAddDataChange @username=@username, @auditTypeID=10, @tableName=''PTinfo.tChildLink'', @recordID=@theNewID, @previousEntry= @prevEntry;
6767
6768 SET @errCode = 0
6769 SELECT errCode = @errCode;
6770 END
6771
6772ELSE
6773 BEGIN
6774 -- patient info doesn''t exist so add --
6775 BEGIN TRY
6776 INSERT INTO PTinfo.tChildLink
6777 (pSysID
6778 , motherSysID
6779 , multiple
6780 , gender
6781 , allowFormula
6782 , allowBottle
6783 , location
6784 )
6785 VALUES (@pSysID
6786 , @motherSysID
6787 , @multiple
6788 , @gender
6789 , @allowFormula
6790 , @allowBottle
6791 , @location
6792 );
6793
6794 SET @theNewID = SCOPE_IDENTITY();
6795
6796 EXECUTE Sysinfo.uspAddDataChange @username=@username, @auditTypeID=9, @tableName=''PTinfo.tChildLink'', @recordID=@theNewID, @previousEntry=''N/A'';
6797
6798 SET @errCode = 0
6799 SELECT errCode = @errCode;
6800
6801 END TRY
6802 BEGIN CATCH
6803 SET @errCode = 1
6804 SELECT errCode = @errCode;
6805 END CATCH
6806END
6807END;
6808')
6809
6810 PRINT N'USP PTinfo.uspAddEditChildLink created'
6811
6812END TRY
6813BEGIN CATCH
6814 PRINT N'USP PTinfo.uspAddEditChildLink create failed'
6815 PRINT 'Error Number: ' + str(error_number())
6816 PRINT 'Line Number: ' + str(error_line())
6817 PRINT error_message()
6818END CATCH
6819GO
6820
6821/* PTinfo.uspAddEditMomInfo.storedProcedure.sql
6822To call the SP:
6823EXECUTE PTinfo.uspAddEditMomInfo @pSysID=1, @calsPer=20, @username='admin'; */
6824
6825BEGIN TRY
6826EXECUTE ('/****** Object: StoredProcedure [PTinfo].[uspAddEditMomInfo] Script Date: 12/07/2012 11:26:19 ******/
6827
6828-- =============================================
6829-- Author: Lisa Bohm (lbohm@paragondsi.com)
6830-- Create date: 2013-07-22
6831-- Description: Add/Edit a mother''s addtl info
6832-- Modified: 2013-12-05 add audit tracking call
6833-- =============================================
6834CREATE PROCEDURE [PTinfo].[uspAddEditMomInfo]
6835 -- Add the parameters for the stored procedure here
6836@pSysID bigint = 0
6837, @calsPer decimal(6,2)
6838, @username varchar(50)
6839
6840 AS
6841 DECLARE
6842 @already bit
6843 , @errCode tinyint
6844 , @prevEntry varchar(1000)
6845 , @theNewID bigint;
6846BEGIN
6847 -- SET NOCOUNT ON added to prevent extra result sets from
6848 -- interfering with SELECT statements.
6849 SET NOCOUNT ON;
6850
6851/* CANNOT do merge! Does patient info already exist? */
6852SET @already = COALESCE(
6853 (SELECT 1
6854 FROM PTinfo.tMomInfo PtM
6855 WHERE PtM.pSysID = @pSysID), 0)
6856
6857IF @already = 1
6858 -- Mom info exists so edit. --
6859 BEGIN
6860 /* audit information */
6861 SELECT @prevEntry = CAST(PMI.calsPer AS VARCHAR) + ''.''
6862 , @theNewID = PMI.momInfoID
6863 FROM PTinfo.tMomInfo PMI
6864 WHERE PMI.pSysID = @pSysID;
6865
6866 UPDATE PTinfo.tMomInfo
6867 SET calsPer = @calsPer
6868 WHERE pSysID = @pSysID;
6869
6870 EXECUTE Sysinfo.uspAddDataChange @username=@username, @auditTypeID=10, @tableName=''PTinfo.tMomInfo'', @recordID=@theNewID, @previousEntry= @prevEntry;
6871
6872 SET @errCode = 0
6873 SELECT errCode = @errCode;
6874 END
6875ELSE
6876 BEGIN
6877 -- mom info doesn''t exist so add --
6878 BEGIN TRANSACTION
6879 BEGIN TRY
6880 INSERT INTO PTinfo.tMomInfo
6881 (pSysID
6882 , calsPer
6883 )
6884 VALUES (@pSysID
6885 , @calsPer
6886 );
6887 SET @theNewID = SCOPE_IDENTITY();
6888 EXECUTE Sysinfo.uspAddDataChange @username=@username, @auditTypeID=9, @tableName=''PTinfo.tMomInfo'', @recordID=@theNewID, @previousEntry=''N/A'';
6889 SET @errCode = 0
6890 SELECT errCode = @errCode;
6891 COMMIT TRANSACTION
6892 END TRY
6893 BEGIN CATCH
6894 ROLLBACK TRANSACTION
6895 SET @errCode = 1
6896 SELECT errCode = @errCode;
6897 PRINT N''Err: Data not successfully added or updated.''
6898 END CATCH
6899END
6900END ;
6901')
6902
6903 PRINT N'USP PTinfo.uspAAddEditMomInfo created'
6904
6905END TRY
6906BEGIN CATCH
6907 PRINT N'USP PTinfo.uspAddEditMomInfo create failed'
6908 PRINT 'Error Number: ' + str(error_number())
6909 PRINT 'Line Number: ' + str(error_line())
6910 PRINT error_message()
6911END CATCH
6912GO
6913
6914/* Nutrition.uspAssignVessel.storedProcedure.sql
6915To call the SP:
6916EXECUTE Nutrition.uspAssignVessel @vesselID=100, @pSysID=2, @username='admin'; */
6917
6918BEGIN TRY
6919EXECUTE ('/****** Object: StoredProcedure [Nutrition].[uspAssignVessel] Script Date: 6/28/2013 11:26:19 ******/
6920-- =============================================
6921-- Author: Lisa Bohm (lbohm@paragondsi.com)
6922-- Create date: 2013-07-22
6923-- Description: Assign Vessel
6924--
6925-- =============================================
6926CREATE PROCEDURE [Nutrition].[uspAssignVessel]
6927 -- Add the parameters for the stored procedure here
6928@vesselID bigint
6929, @pSysID bigint
6930, @username varchar(50)
6931
6932 AS
6933
6934DECLARE @already bigint
6935 , @errCode tinyint
6936 , @theNewID bigint
6937
6938BEGIN
6939 -- SET NOCOUNT ON added to prevent extra result sets from
6940 -- interfering with SELECT statements.
6941 SET NOCOUNT ON;
6942
6943SET @already = COALESCE(
6944 (SELECT NVA.pSysID
6945 FROM Nutrition.tVesselAssign NVA
6946 WHERE NVA.vesselID = @vesselID), 0)
6947
6948
6949IF @already > 0
6950 -- vessel is assigned --
6951 BEGIN
6952 IF @already = @pSysID
6953 /* already assigned to this patient */
6954 BEGIN
6955 SET @errCode = 0
6956 SELECT errCode = @errCode;
6957 END
6958 ELSE
6959 /* already assigned to DIFFERENT patient */
6960 BEGIN
6961 SET @errCode = 23
6962 SELECT errCode = @errCode;
6963 END
6964 END
6965ELSE
6966 /* not assigned. add record */
6967 BEGIN
6968 /* find the mother pSysID for the child */
6969 INSERT INTO Nutrition.tVesselAssign (vesselID, pSysID)
6970 VALUES (@vesselID, @pSysID);
6971
6972 SET @theNewID = SCOPE_IDENTITY();
6973 /* auditing */
6974 EXECUTE Sysinfo.uspAddDataChange @username=@username, @auditTypeID=9, @tableName=''Nutrition.tVesselAssign'', @recordID=@theNewID, @previousEntry=''N/A'';
6975
6976 SET @errCode = 0
6977 SELECT errCode = @errCode;
6978 END
6979
6980END;
6981')
6982
6983 PRINT N'USP Nutrition.uspAssignVessel created'
6984
6985END TRY
6986BEGIN CATCH
6987 PRINT N'USP Nutrition.uspAssignVessel create failed'
6988 PRINT 'Error Number: ' + str(error_number())
6989 PRINT 'Line Number: ' + str(error_line())
6990 PRINT error_message()
6991END CATCH
6992GO
6993
6994/* Nutrition.uspAddEditVessel.storedProcedure.sql
6995To call the SP:
6996EXECUTE Nutrition.uspAddEditVessel @vesselID=0, @nCategoryID=1, @mls=20, @gms=20, @calsPer=20, @locationID=100, @username='admin', @addAmount=1;
6997Use @vesselID=0 to add, actual vessel ID for edit */
6998
6999BEGIN TRY
7000EXECUTE ('/****** Object: StoredProcedure [Nutrition].[uspAddEditVessel] Script Date: 6/28/2013 11:26:19 ******/
7001-- =============================================
7002-- Author: Lisa Bohm (lbohm@paragondsi.com)
7003-- Create date: 2013-07-22
7004-- Description: Add milk vessel
7005-- Modified: 2013-12-05 add audit tracking call
7006-- =============================================
7007CREATE PROCEDURE [Nutrition].[uspAddEditVessel]
7008 -- Add the parameters for the stored procedure here
7009@vesselID bigint
7010, @nCategoryID tinyint
7011, @mls decimal(7,2) = 1
7012, @gms decimal(7,2) = 1
7013, @calsPer decimal(6,2) = 20
7014, @locationID bigint = 1
7015, @username varchar(50)
7016/* add amount for new wessels */
7017, @addAmount smallint = 1
7018
7019 AS
7020 DECLARE @vesselCode varchar(20)
7021 , @errCode tinyint
7022 , @prevEntry varchar(1000)
7023 /* need a table variable for return values */
7024 DECLARE @addVessel TABLE (nCategoryID tinyint, mls decimal(7,2), gms decimal(7,2), calsPer decimal(6,2), locationID bigint, vNum smallint)
7025 DECLARE @newVessel TABLE (vesselID bigint, vesselCode varchar(20))
7026
7027
7028BEGIN
7029 -- SET NOCOUNT ON added to prevent extra result sets from
7030 -- interfering with SELECT statements.
7031 SET NOCOUNT ON;
7032
7033IF @vesselID = 0
7034BEGIN
7035
7036INSERT INTO @addVessel (nCategoryID, mls, gms, calsPer, locationID, vNum)
7037
7038SELECT @nCategoryID
7039 , @mls
7040 , @gms
7041 , @calsPer
7042 , @locationID
7043 , STT.N
7044
7045 FROM SysInfo.tTally STT
7046 WHERE STT.N <= @addAmount;
7047
7048
7049/* get vessel code prefix */
7050 SET @vesselCode = (SELECT nCategoryPrefix
7051 FROM Nutrition.tCategory NC
7052 WHERE NC.nCategoryID = @nCategoryID)
7053
7054 BEGIN TRY
7055 BEGIN TRANSACTION
7056
7057 INSERT INTO Nutrition.tVessel
7058 (vesselCode
7059 , nCategoryID
7060 , mls
7061 , gms
7062 , calsPer
7063 , locationID)
7064OUTPUT Inserted.vesselID, Inserted.vesselCode INTO @newVessel
7065
7066SELECT @vesselCode
7067 , nCategoryID
7068 , mls
7069 , gms
7070 , calsPer
7071 , locationID
7072FROM @addVessel;
7073
7074UPDATE NV
7075SET NV.vesselCode = CASE WHEN NtV.vesselCode IS NULL THEN @vesselCode + CAST(NV.vesselID AS VARCHAR) ELSE @vesselCode + ''0'' + CAST(@vesselID AS VARCHAR) END
7076FROM @newVessel NV
7077LEFT OUTER JOIN Nutrition.tVessel NtV ON NtV.vesselCode = @vesselCode + CAST(NV.vesselID AS VARCHAR);
7078
7079
7080 /* okay, then add the updated vessel code */
7081 UPDATE NtV
7082 SET NtV.vesselCode = NV.vesselCode
7083 FROM Nutrition.tVessel NtV
7084 INNER JOIN @newVessel NV ON NtV.vesselID = NV.vesselID;
7085
7086 /* insert status "issued" */
7087 INSERT INTO Nutrition.tVesselHistory
7088 (vesselID, statusID, dateChange, userChange, expirDate, isCurrent)
7089 SELECT
7090 NV.vesselID
7091 , 1
7092 , getdate()
7093 , @username
7094 , ''2500-01-01''
7095 , 1
7096 FROM @newVessel NV;
7097
7098 /* la la la audit records- do directly to avoid looooooooooping */
7099 INSERT INTO SysInfo.tDataChange
7100 (viewDate, userName, auditTypeID, tableName, recordID, previousEntry)
7101
7102 SELECT getdate()
7103 , @username
7104 , 9
7105 , ''Nutrition.tVessel''
7106 , NV.vesselID
7107 , ''N/A''
7108
7109 FROM @newVessel NV;
7110
7111
7112 COMMIT TRANSACTION
7113 SET @errCode = 0
7114 SELECT vesselID = NV.vesselID
7115 , vesselCode = NV.vesselCode
7116 , errCode = @errCode
7117 FROM @newVessel NV;
7118
7119 END TRY
7120 BEGIN CATCH
7121 SET @errCode = 1
7122 SELECT vesselID = NV.vesselID
7123 , vesselCode = NV.vesselCode
7124 , errCode = @errCode
7125 FROM @newVessel NV;
7126 END CATCH
7127END
7128ELSE
7129/* update */
7130 BEGIN
7131 /* set up audit previous record */
7132 SELECT @prevEntry = NtV.vesselCode + ''; '' + CAST(NtV.mls AS VARCHAR) + ''; '' + CAST(NtV.gms AS VARCHAR) + ''; '' + CAST(NtV.calsper AS VARCHAR) + ''.''
7133 FROM Nutrition.tVessel NtV
7134 WHERE NtV.vesselID = @vesselID;
7135
7136 UPDATE Nutrition.tVessel
7137 SET mls = @mls
7138 , gms = @gms
7139 , calsPer = @calsPer
7140 WHERE vesselID = @vesselID;
7141
7142 /* get vessel code */
7143 SELECT @vesselCode = vesselCode
7144 FROM Nutrition.tVessel
7145 WHERE vesselID = @vesselID;
7146
7147 EXECUTE Sysinfo.uspAddDataChange @username=@username, @auditTypeID=10, @tableName=''Nutrition.tVessel'', @recordID=@vesselID, @previousEntry=@prevEntry;
7148
7149 SET @errCode = 0
7150 SELECT vesselID = @vesselID
7151 , vesselCode = @vesselCode
7152 , errCode = @errCode;
7153
7154 END
7155END;
7156')
7157
7158 PRINT N'USP Nutrition.uspAddEditVessel created'
7159
7160END TRY
7161BEGIN CATCH
7162 PRINT N'USP Nutrition.uspAddEditVessel create failed'
7163 PRINT 'Error Number: ' + str(error_number())
7164 PRINT 'Line Number: ' + str(error_line())
7165 PRINT error_message()
7166END CATCH
7167GO
7168
7169/* Nutrition.uspAddMotherVessel.storedProcedure.sql
7170To call the SP:
7171EXECUTE Nutrition.uspAddMotherVessel @vesselXML='<vessel id="150"/>
7172<vessel id="151"/>
7173<vessel id="152"/>', @pSysID=2, @username='admin'; */
7174
7175BEGIN TRY
7176EXECUTE ('/****** Object: StoredProcedure [Nutrition].[uspAddMotherVessel] Script Date: 6/28/2013 11:26:19 ******/
7177-- =============================================
7178-- Author: Lisa Bohm (lbohm@paragondsi.com)
7179-- Create date: 2013-07-22
7180-- Description: Add mother''s info to vessel, batch using XML.
7181-- Modified: 2013-12-05 add audit tracking call
7182--Testing Data
7183/*DECLARE @vesselXML as XML
7184
7185SET @vesselXML = ''
7186<vessel id="150"/>s
7187<vessel id="151"/>
7188<vessel id="152"/>
7189'' */
7190-- =============================================
7191CREATE PROCEDURE [Nutrition].[uspAddMotherVessel]
7192 -- Add the parameters for the stored procedure here
7193@vesselXML XML
7194, @pSysID bigint
7195, @username varchar(50)
7196
7197 AS
7198 DECLARE @vesselInfo TABLE (vesselID bigint, errCode tinyint)
7199BEGIN
7200 -- SET NOCOUNT ON added to prevent extra result sets from
7201 -- interfering with SELECT statements.
7202 SET NOCOUNT ON;
7203
7204INSERT INTO @vesselInfo (vesselID, errCode)
7205 SELECT vInfo.value(''@id'', ''varchar(20)'') as vesselID
7206 , NULL
7207 FROM @vesselXML.nodes(''./vessel'') as data ( vInfo );
7208
7209/* get assigned yet? */
7210
7211UPDATE VI
7212 SET errCode = CASE WHEN NMV.pSysID IS NULL THEN
7213 CASE WHEN NDV.lotID IS NULL THEN
7214 CASE WHEN NFV.formLotID IS NULL THEN 0 ELSE
7215 23 END
7216 ELSE 23 END
7217 ELSE
7218 CASE WHEN NMV.pSysID = @pSysID THEN 22 ELSE
7219 23 END
7220 END
7221 FROM @vesselInfo VI
7222 /* already assigned to DIFFERENT mother */
7223 LEFT OUTER JOIn Nutrition.tMotherVessel NMV ON VI.vesselID = NMV.vesselID
7224 /* check for donor lot */
7225 LEFT OUTER JOIN Nutrition.tDonorVessel NDV ON VI.vesselID = NDV.vesselID
7226 /* check for formula */
7227 LEFT OUTER JOIN Nutrition.tFormulaVessel NFV ON VI.vesselID = NFV.vesselID;
7228BEGIN TRY
7229BEGIN TRANSACTION
7230
7231 /* okay, insert */
7232 INSERT INTO Nutrition.tMotherVessel
7233 (vesselID
7234 , pSysID)
7235 SELECT VI.vesselID
7236 , @pSysID
7237 FROM @vesselInfo VI
7238 WHERE VI.errCode = 0
7239;
7240
7241 /* uh huh. Now, audit tracking. No SP to avoid loooooping. */
7242 INSERT INTO SysInfo.tDataChange
7243 (viewDate, userName, auditTypeID, tableName, recordID, previousEntry)
7244
7245SELECT getdate()
7246 , @username
7247 , 9
7248 , ''Nutrition.tMotherVessel''
7249 , NMV.vesselMomID
7250 , ''N/A''
7251
7252 FROM @vesselInfo VI
7253 INNER JOIN Nutrition.tMotherVessel NMV ON VI.vesselID = NMV.vesselID
7254 WHERE NMV.pSysID = @pSysID
7255 AND VI.errCode = 0;
7256
7257SELECT vesselID, pSysID = @pSysID, errCode
7258FROM @vesselInfo;
7259COMMIT TRANSACTION
7260END TRY
7261BEGIN CATCH
7262ROLLBACK TRANSACTION
7263
7264END CATCH
7265END;
7266')
7267
7268 PRINT N'USP Nutrition.uspAddMotherVessel created'
7269
7270END TRY
7271BEGIN CATCH
7272 PRINT N'USP Nutrition.uspAddMotherVessel create failed'
7273 PRINT 'Error Number: ' + str(error_number())
7274 PRINT 'Line Number: ' + str(error_line())
7275 PRINT error_message()
7276END CATCH
7277GO
7278
7279/* Nutrition.uspAddDonorVessel.storedProcedure.sql
7280To call the SP:
7281EXECUTE Nutrition.uspAddDonorVessel @vesselID=100, @lotID=2, @statusID=3, @expirDate='25000101', @userChange='admin'; */
7282
7283BEGIN TRY
7284EXECUTE ('/****** Object: StoredProcedure [Nutrition].[uspAddDonorVessel] Script Date: 6/28/2013 11:26:19 ******/
7285-- =============================================
7286-- Author: Lisa Bohm (lbohm@paragondsi.com)
7287-- Create date: 2013-07-22
7288-- Description: Add donor Lot''s info to vessel, batch using XML
7289--Testing Data
7290-- Modified: 2013-12-05 add audit tracking call
7291/*DECLARE @vesselXML as XML
7292
7293SET @vesselXML = ''
7294<vessel id="150"/>
7295<vessel id="151"/>
7296<vessel id="152"/>
7297'' */
7298-- =============================================
7299CREATE PROCEDURE [Nutrition].[uspAddDonorVessel]
7300 -- Add the parameters for the stored procedure here
7301@vesselXML XML
7302, @lotID int
7303, @statusID tinyint
7304, @expirDate datetime
7305, @userChange varchar(50)
7306
7307
7308 AS
7309 DECLARE @vesselInfo TABLE (vesselID bigint, errCode tinyint)
7310BEGIN
7311 -- SET NOCOUNT ON added to prevent extra result sets from
7312 -- interfering with SELECT statements.
7313 SET NOCOUNT ON;
7314
7315INSERT INTO @vesselInfo (vesselID, errCode)
7316 SELECT vInfo.value(''@id'', ''varchar(20)'') as vesselID
7317 , NULL
7318 FROM @vesselXML.nodes(''./vessel'') as data ( vInfo );
7319
7320UPDATE VI
7321 SET errCode = CASE WHEN NDV.lotID IS NULL THEN
7322 CASE WHEN NMV.pSysID IS NULL THEN
7323 CASE WHEN NFV.formLotID IS NULL THEN 0 ELSE
7324 23 END
7325 ELSE 23 END
7326 ELSE
7327 CASE WHEN NDV.lotID = @lotID THEN 22 ELSE
7328 23 END
7329 END
7330 FROM @vesselInfo VI
7331 /* already assigned to mother */
7332 LEFT OUTER JOIn Nutrition.tMotherVessel NMV ON VI.vesselID = NMV.vesselID
7333 /* check for donor lot */
7334 LEFT OUTER JOIN Nutrition.tDonorVessel NDV ON VI.vesselID = NDV.vesselID
7335 /* check for formula */
7336 LEFT OUTER JOIN Nutrition.tFormulaVessel NFV ON VI.vesselID = NFV.vesselID;
7337
7338 BEGIN TRY
7339 BEGIN TRANSACTION
7340 /* okay, insert */
7341 INSERT INTO Nutrition.tDonorVessel
7342 (vesselID
7343 , lotID)
7344 SELECT VI.vesselID
7345 , @lotID
7346 FROM @vesselInfo VI
7347 WHERE VI.errCode = 0;
7348
7349 /* don''t use the SP to avoid looping here. Direct code. */
7350 INSERT INTO SysInfo.tDataChange
7351 (viewDate, userName, auditTypeID, tableName, recordID, previousEntry)
7352
7353SELECT getdate()
7354 , @userChange
7355 , 9
7356 , ''Nutrition.tDonorVessel''
7357 , NDV.vesselLotID
7358 , ''N/A''
7359
7360 FROM @vesselInfo VI
7361 INNER JOIN Nutrition.tDonorVessel NDV ON VI.vesselID = NDV.vesselID
7362 WHERE NDV.lotID = @lotID
7363 AND VI.errCode = 0;
7364
7365 /* set current status as NOT current */
7366 UPDATE NVH
7367 SET isCurrent = 0
7368 FROM Nutrition.tVesselHistory NVH
7369 INNER JOIN @vesselInfo VI ON NVH.vesselID = VI.vesselID
7370 WHERE NVH.isCurrent = 1
7371 AND VI.errCode = 0;
7372
7373 /* then add received status */
7374 INSERT INTO Nutrition.tVesselHistory (vesselID, statusID, dateChange, userChange, expirDate, isCurrent)
7375 SELECT VI.vesselID
7376 , 17
7377 , getdate()
7378 , @userChange
7379 , @expirDate
7380 , 0
7381 FROM @vesselInfo VI
7382 WHERE VI.errCode = 0;
7383
7384 /* then add the status status */
7385
7386 INSERT INTO Nutrition.tVesselHistory (vesselID, statusID, dateChange, userChange, expirDate, isCurrent)
7387 SELECT VI.vesselID
7388 , @statusID
7389 , getdate()
7390 , @userChange
7391 , @expirDate
7392 , 1
7393 FROM @vesselInfo VI
7394 WHERE VI.errCode = 0;
7395
7396SELECT vesselID, @lotID, errCode
7397FROM @vesselInfo;
7398
7399COMMIT TRANSACTION
7400END TRY
7401BEGIN CATCH
7402ROLLBACK TRANSACTION
7403END CATCH
7404
7405END;
7406')
7407
7408 PRINT N'USP Nutrition.uspAddDonorVessel created'
7409
7410END TRY
7411BEGIN CATCH
7412 PRINT N'USP Nutrition.uspAddDonorVessel create failed'
7413 PRINT 'Error Number: ' + str(error_number())
7414 PRINT 'Line Number: ' + str(error_line())
7415 PRINT error_message()
7416END CATCH
7417GO
7418
7419/* Nutrition.uspAddFormulaVessel.storedProcedure.sql
7420To call the SP:
7421EXECUTE Nutrition.uspAddFormulaVessel @vesselXML='<vessel id="116"/><vessel id="125"/><vessel id="123"/><vessel id="151"/>', @formlotID=2, @userChange='admin';
7422 */
7423
7424BEGIN TRY
7425EXECUTE ('/****** Object: StoredProcedure [Nutrition].[uspAddFormulaVessel] Script Date: 6/28/2013 11:26:19 ******/
7426-- =============================================
7427-- Author: Lisa Bohm (lbohm@paragondsi.com)
7428-- Create date: 2013-07-22
7429-- Description: Add Formula info to vessel, batch using XML
7430-- Modified: 2013-12-05 add audit tracking call
7431--Testing Data
7432/*DECLARE @vesselXML as XML
7433
7434SET @vesselXML = ''
7435<vessel id="116"/>
7436<vessel id="125"/>
7437<vessel id="123"/>
7438<vessel id="151"/>
7439'' */
7440-- =============================================
7441CREATE PROCEDURE [Nutrition].[uspAddFormulaVessel]
7442 -- Add the parameters for the stored procedure here
7443@vesselXML XML
7444, @formLotID smallint
7445, @userChange varchar(50)
7446
7447 AS
7448 DECLARE @expDate datetime
7449
7450 DECLARE @vesselInfo TABLE (vesselID bigint, errCode varchar(75))
7451
7452SET @expDate = ''2500-01-01'';
7453BEGIN
7454 -- SET NOCOUNT ON added to prevent extra result sets from
7455 -- interfering with SELECT statements.
7456 SET NOCOUNT ON;
7457
7458INSERT INTO @vesselInfo (vesselID, errCode)
7459 SELECT vInfo.value(''@id'', ''varchar(20)'') as vesselID
7460 , NULL
7461 FROM @vesselXML.nodes(''./vessel'') as data ( vInfo );
7462
7463UPDATE VI
7464 SET errCode = CASE WHEN NFV.formLotID IS NULL THEN
7465 CASE WHEN NMV.pSysID IS NULL THEN
7466 CASE WHEN NDV.lotID IS NULL THEN 0 ELSE
7467 23 END
7468 ELSE 23 END
7469 ELSE
7470 CASE WHEN NFV.formLotID = @formLotID THEN 22 ELSE
7471 23 END
7472 END
7473 FROM @vesselInfo VI
7474 /* already assigned to mother */
7475 LEFT OUTER JOIn Nutrition.tMotherVessel NMV ON VI.vesselID = NMV.vesselID
7476 /* check for Formula lot */
7477 LEFT OUTER JOIN Nutrition.tDonorVessel NDV ON VI.vesselID = NDV.vesselID
7478 /* check for formula */
7479 LEFT OUTER JOIN Nutrition.tFormulaVessel NFV ON VI.vesselID = NFV.vesselID
7480
7481 ;
7482BEGIN TRY
7483BEGIN TRANSACTION
7484 /* okay, insert */
7485 INSERT INTO Nutrition.tFormulaVessel
7486 (vesselID
7487 , formLotID)
7488 SELECT VI.vesselID
7489 , @formLotID
7490 FROM @vesselInfo VI
7491 WHERE VI.errCode = 0
7492;
7493 /* uh huh. Now, audit tracking. No SP to avoid loooooping. */
7494 INSERT INTO SysInfo.tDataChange
7495 (viewDate, userName, auditTypeID, tableName, recordID, previousEntry)
7496
7497SELECT getdate()
7498 , @userChange
7499 , 9
7500 , ''Nutrition.tFormulaVessel''
7501 , NFV.vesselFormulaID
7502 , ''N/A''
7503
7504 FROM @vesselInfo VI
7505 INNER JOIN Nutrition.tFormulaVessel NFV ON VI.vesselID = NFV.vesselID
7506 WHERE NFV.formLotID = @formLotID
7507 AND VI.errCode = 0;
7508
7509/* received status event 17 */
7510INSERT INTO Nutrition.tVesselHistory
7511 (vesselID
7512 , statusID
7513 , dateChange
7514 , userChange
7515 , expirDate
7516 , isCurrent)
7517SELECT VI.vesselID
7518 , 17
7519 , getdate()
7520 , @userChange
7521 , @expDate
7522 , 0
7523 FROM @vesselInfo VI
7524 WHERE VI.errCode = 0;
7525/* set previous "iscurrent" to 0 */
7526
7527UPDATE NVH
7528SET NVH.isCurrent = 0
7529FROM Nutrition.tVesselHistory NVH
7530INNER JOIN @vesselInfo VI ON NVH.vesselID = VI.vesselID
7531WHERE VI.errCode = 0
7532 AND NVH.isCurrent = 1;
7533
7534
7535/* fortified status 5 */
7536INSERT INTO Nutrition.tVesselHistory
7537 (vesselID
7538 , statusID
7539 , dateChange
7540 , userChange
7541 , expirDate
7542 , isCurrent)
7543SELECT
7544 VI.vesselID
7545 , 5
7546 , getdate()
7547 , @userChange
7548 , @expDate
7549 , 1
7550 FROM @vesselInfo VI
7551 WHERE VI.errCode = 0;
7552
7553/* return label stuff */
7554
7555SELECT vesselID, formLotID = @formLotID, errCode
7556FROM @vesselInfo;
7557COMMIT TRANSACTION
7558END TRY
7559BEGIN CATCH
7560ROLLBACK TRANSACTION
7561
7562END CATCH
7563
7564END;
7565')
7566
7567 PRINT N'USP Nutrition.uspAddFormulaVessel created'
7568
7569END TRY
7570BEGIN CATCH
7571 PRINT N'USP Nutrition.uspAddFormulaVessel create failed'
7572 PRINT 'Error Number: ' + str(error_number())
7573 PRINT 'Line Number: ' + str(error_line())
7574 PRINT error_message()
7575END CATCH
7576GO
7577
7578/* Nutrition.uspAddOrigin.storedProcedure.sql
7579To call the SP:
7580EXECUTE NutritionuspAddOrigin @vesselID=1, @targetVesselID=3, @mls=20, @username='admin';
7581 */
7582
7583BEGIN TRY
7584EXECUTE ('-- =============================================
7585-- Author: Lisa Bohm (lbohm@paragondsi.com)
7586-- Create date: 2013-07-22
7587-- Description: Add origin information for a vessel
7588-- Modified: 2013-12-05 add audit tracking call
7589-- 2014-02-20 add volume stuff
7590-- 2014-02-26 Who cares if we randomly keep pouring milk in
7591-- 2014-03-17 add expiration date dealing if what we''re pouring in has sooner exp date
7592-- =============================================
7593CREATE PROCEDURE [Nutrition].[uspAddOrigin]
7594 -- Add the parameters for the stored procedure here
7595@vesselID bigint
7596, @targetVesselID bigint
7597, @mls decimal(7,2) = 1
7598, @username varchar(50)
7599
7600 AS
7601 DECLARE @already bit
7602 , @existsIn bit
7603 , @errCode tinyint
7604 , @oCalsPer decimal(6,2)
7605 , @tCalsPer decimal(6,2)
7606 , @theNewID bigint
7607 , @vExpDate datetime
7608 , @tExpDate datetime
7609 , @expDateHrs smallint
7610 , @newExpDate datetime
7611 , @vHistID bigint
7612 , @prevEntry varchar(1000);
7613
7614BEGIN TRY
7615BEGIN TRANSACTION
7616 -- SET NOCOUNT ON added to prevent extra result sets from
7617 -- interfering with SELECT statements.
7618 SET NOCOUNT ON;
7619
7620SET @errcode = 0;
7621SET @existsIn = COALESCE((SELECT MAX(1)
7622 FROM Nutrition.tVesselOrigin NVO
7623 WHERE NVO.vesselID = @vesselID
7624 AND NVO.targetVesselID = @targetVesselID), 0)
7625
7626/* when container change, reduce expiration to... how many hours? Also, source and exp vessel current exp time*/
7627SET @expDateHrs = (SELECT StS.setValue
7628 FROM SysInfo.tSetting StS
7629 WHERE StS.settingID = 30);
7630
7631IF @expDateHrs > 0
7632 BEGIN
7633 SET @newExpDate = DATEADD(hour, @expDateHrs, getdate());
7634 END
7635ELSE
7636 BEGIN
7637 SET @newExpDate = ''2500-01-01''
7638 END
7639
7640SET @vExpDate = (SELECT NVH.expirDate
7641 FROM Nutrition.tVesselHistory NVH
7642 WHERE NVH.vesselID = @vesselID
7643 AND NVH.isCurrent = 1);
7644
7645SELECT @tExpDate = NVH.expirDate
7646 , @vHistID = NVH.vesselHistoryID
7647 FROM Nutrition.tVesselHistory NVH
7648 WHERE NVH.vesselID = @targetVesselID
7649 AND NVH.isCurrent = 1;
7650
7651/* all root vessel stuff. */
7652IF @existsIn = 0
7653BEGIN
7654 /* is the "target" vessel a "root" vessel? If so, create additional pseudo-target vessel with the old root bottle code */
7655 SET @already = COALESCE((SELECT MAX(1)
7656 FROM Nutrition.tVessel NTV
7657 LEFT OUTER JOIN Nutrition.tMotherVessel NMV ON NtV.vesselID = NMV.vesselID
7658 LEFT OUTER JOIN Nutrition.tDonorVessel NDV ON NtV.vesselID = NDV.vesselID
7659 LEFT OUTER JOIN Nutrition.tFormulaVessel NFV ON NtV.vesselID = NFV.vesselID
7660 INNER JOIN Nutrition.tVesselHistory NVH ON NtV.vesselID = NVH.vesselID
7661 WHERE (NMV.vesselID IS NOT NULL
7662 OR NDV.vesselID IS NOT NULL
7663 OR NFV.vesselID IS NOT NULL)
7664 AND NtV.vesselID = @targetVesselID
7665 AND NVH.isCurrent = 1
7666 AND NVH.statusID > 1), 0);
7667
7668 IF @already = 1
7669 BEGIN
7670 /* add to "empty" location since it will be */
7671 DECLARE @newRtVessel TABLE (vesselID bigint);
7672 DECLARE @newRtID bigint;
7673 /* add new root bottle */
7674 INSERT INTO Nutrition.tVessel
7675 (vesselCode
7676 , nCategoryID
7677 , mls
7678 , gms
7679 , calsPer
7680 , locationID)
7681 OUTPUT Inserted.vesselID into @newRtVessel
7682
7683 SELECT ''RT''
7684 , nCategoryID
7685 , 0
7686 , 0
7687 , calsPer
7688 /* disposed location since it''s already emptied into "orig" target vessel */
7689 , 2
7690
7691 FROM Nutrition.tVessel NtV
7692 WHERE NtV.vesselID = @targetVesselID;
7693 /* update root vessel code */
7694 SELECT @newRtID = vesselID
7695 FROM @newRtVessel;
7696
7697
7698/* check so no duplicate vessel codes */
7699
7700 UPDATE NV
7701 SET NV.vesselCode = CASE WHEN NtV.vesselCode IS NULL THEN NV.vesselCode + CAST(NV.vesselID AS VARCHAR) ELSE NV.vesselCode + ''0'' + CAST(NV.vesselID AS VARCHAR) END
7702 FROM Nutrition.tVessel NV
7703 INNER JOIN @newRtVessel NRV ON NV.vesselID = NRV.vesselID
7704 LEFT OUTER JOIN Nutrition.tVessel NtV ON NtV.vesselCode = NV.vesselCode + CAST(NV.vesselID AS VARCHAR);
7705
7706 /* update root vessel crap: vessel assign, additives issued, vessel history, mother vessel, formula vessel, donor vessel */
7707
7708 SET @prevEntry = ''System-added root vessel, formerly'' + CAST(@targetVesselID AS VARCHAR) + ''. Addtl tables poss. updated for this: One from: tMotherVessel, tDonorVessel, tFormulaVessel; Nutrition.tAdditivesIssued; Nutrition.tVesselHistory; Nutrition.tVesselAssign poss. added to new root.''
7709 /* audit record */
7710 EXECUTE Sysinfo.uspAddDataChange @username=''System'', @auditTypeID=9, @tableName=''Nutrition.tVessel'', @recordID=@newRtID, @previousEntry=@prevEntry;
7711
7712 UPDATE Nutrition.tMotherVessel
7713 SET vesselID = @newRtID
7714 WHERE vesselID = @targetVesselID;
7715
7716 UPDATE Nutrition.tDonorVessel
7717 SET vesselID = @newRtID
7718 WHERE vesselID = @targetVesselID;
7719
7720 UPDATE Nutrition.tFormulaVessel
7721 SET vesselID = @newRtID
7722 WHERE vesselID = @targetVesselID;
7723
7724 /* leave assignment; add to root if needed */
7725 INSERT INTO Nutrition.tVesselAssign
7726 (pSysID, vesselID)
7727 SELECT NVA.pSysID
7728 , @newRtID
7729 FROM Nutrition.tVesselAssign NVA
7730 WHERE vesselID = @targetVesselID;
7731
7732 UPDATE Nutrition.tAdditiveIssue
7733 SET vesselID = @newRtID
7734 WHERE vesselID = @targetVesselID;
7735
7736 /* vessel history */
7737 UPDATE Nutrition.tVesselHistory
7738 SET vesselID = @newRtID
7739 WHERE vesselID = @targetVesselID
7740 AND isCurrent = 0;
7741
7742 /* copy current one and add as not current */
7743 INSERT INTO Nutrition.tVesselHistory
7744 (vesselID, statusID, dateChange, userChange, expirDate, isCurrent)
7745 SELECT @newRtID
7746 , statusID
7747 , dateChange
7748 , userChange
7749 , expirDate
7750 , 0
7751
7752 FROM Nutrition.tVesselHistory
7753 WHERE vesselID = @targetVesselID
7754 AND isCurrent = 1;
7755
7756 /* then insert the root vessel add origin into the table */
7757
7758 INSERT INTO Nutrition.tVesselOrigin
7759 (vesselID
7760 , targetVesselID
7761 , mls)
7762 SELECT NtV.vesselID
7763 , @targetVesselID
7764 , NtV.mls
7765
7766 FROM Nutrition.tVessel NtV
7767 Where NtV.vesselID = @newRtID;
7768
7769 /* add "empty" status */
7770 INSERT INTO Nutrition.tVesselHistory
7771 (vesselID
7772 , statusID
7773 , dateChange
7774 , userChange
7775 , expirDate
7776 , isCurrent)
7777 VALUES (@newRtID
7778 , 16
7779 , getdate()
7780 , ''makeRoot''
7781 , getdate()
7782 , 1);
7783
7784 /* END root vessel stuff. */
7785 END
7786
7787 /* okay, insert */
7788 INSERT INTO Nutrition.tVesselOrigin
7789 (vesselID
7790 , targetVesselID
7791 , mls)
7792 VALUES
7793 (@vesselID
7794 , @targetVesselID
7795 , @mls);
7796
7797 SET @theNewID = SCOPE_IDENTITY();
7798
7799 EXECUTE Sysinfo.uspAddDataChange @username=@username, @auditTypeID=9, @tableName=''Nutrition.tVesselOrigin'', @recordID=@theNewID, @previousEntry=''N/A'';
7800
7801 /* pass along assignment if there isn''t one already */
7802SET @already = COALESCE((SELECT 1
7803 FROM Nutrition.tVesselAssign NVA
7804 WHERE NVA.vesselID = @targetvesselID), 0)
7805
7806IF @already = 0
7807 BEGIN
7808 /* pass along assignment */
7809 INSERT INTO Nutrition.tVesselAssign
7810 (pSysID, vesselID)
7811 SELECT NVA.pSysID
7812 , @targetVesselID
7813 FROM Nutrition.tVesselAssign NVA
7814 WHERE vesselID = @vesselID;
7815
7816 SET @theNewID = SCOPE_IDENTITY();
7817
7818 EXECUTE Sysinfo.uspAddDataChange @username=''System'', @auditTypeID=9, @tableName=''Nutrition.tVesselAssign'', @recordID=@theNewID, @previousEntry=''Assignment passed with combine procedure.'';
7819
7820 END
7821END
7822/* tweakie the volumie.... */
7823/* subtract from the old one */
7824 SELECT @prevEntry = ''mls = '' + CAST(NtV.mls AS VARCHAR)
7825 FROM Nutrition.tVessel NtV
7826 WHERE vesselID = @vesselID;
7827
7828 UPDATE NtV
7829 SET mls = CASE WHEN COALESCE(NtV.mls,0) < @mls THEN 0 ELSE COALESCE(NtV.mls,0) - @mls END
7830 FROM Nutrition.tVessel NtV
7831 WHERE vesselID = @vesselID;
7832
7833 EXECUTE Sysinfo.uspAddDataChange @username=''System'', @auditTypeID=10, @tableName=''Nutrition.tVessel'', @recordID=@vesselID, @previousEntry=@prevEntry;
7834
7835 /* add to the new */
7836 SELECT @prevEntry = ''mls = '' + CAST(NtV.mls AS VARCHAR)
7837 FROM Nutrition.tVessel NtV
7838 WHERE vesselID = @targetVesselID;
7839
7840 UPDATE NtV
7841 SET mls = COALESCE(NtV.mls,0) + @mls
7842 FROM Nutrition.tVessel NtV
7843 WHERE vesselID = @targetVesselID;
7844
7845 EXECUTE Sysinfo.uspAddDataChange @username=''System'', @auditTypeID=10, @tableName=''Nutrition.tVessel'', @recordID=@targetVesselID, @previousEntry=@prevEntry;
7846
7847/* update the calsPer on the target vessel if vessel cals are higher. */
7848 SELECT @oCalsPer = NtVO.calsPer
7849 FROM Nutrition.tVessel NtVO
7850 WHERE NtVO.vesselID = @vesselID;
7851
7852 SELECT @tCalsPer = NtVT.calsPer
7853 FROM Nutrition.tVessel NtVT
7854 WHERE NtVT.vesselID = @targetVesselID;
7855
7856IF @oCalsPer > @tCalsPer
7857 BEGIN
7858 UPDATE Nutrition.tVessel
7859 SET calsPer = @oCalsPer
7860 WHERE vesselID = @targetVesselID;
7861
7862 SET @prevEntry = ''Cals per = '' + CAST(@oCalsPer AS VARCHAR);
7863
7864 EXECUTE Sysinfo.uspAddDataChange @username=''System'', @auditTypeID=10, @tableName=''Nutrition.tVessel'', @recordID=@targetVesselID, @previousEntry=@prevEntry;
7865
7866 END
7867
7868/* NOW - expiration dates. */
7869IF @vExpDate < @tExpDate
7870/* though - if the source vessel expiration date is less than the target vessel expiration date, change target vessel expiration date */
7871 BEGIN
7872 IF @newExpDate < @vExpDate
7873 /* but if the container change expir date is less than all of them...! */
7874 BEGIN
7875 INSERT INTO Nutrition.tVesselHistory (vesselID, statusID, dateChange, userChange, expirDate, isCurrent)
7876 SELECT vesselID, statusID, getdate(), ''System'', @newExpDate, 1
7877 FROM Nutrition.tVesselHistory
7878 WHERE vesselHistoryID = @vHistID;
7879
7880 UPDATE Nutrition.tVesselHistory
7881 SET isCurrent = 0
7882 WHERE vesselHistoryID = @vHistID;
7883
7884 END
7885 ELSE
7886 BEGIN
7887 INSERT INTO Nutrition.tVesselHistory (vesselID, statusID, dateChange, userChange, expirDate, isCurrent)
7888 SELECT vesselID, statusID, getdate(), ''System'', @vExpDate, 1
7889 FROM Nutrition.tVesselHistory
7890 WHERE vesselHistoryID = @vHistID;
7891
7892 UPDATE Nutrition.tVesselHistory
7893 SET isCurrent = 0
7894 WHERE vesselHistoryID = @vHistID;
7895
7896 END
7897 END
7898ELSE IF @vExpDate >= @tExpDate AND @newExpDate < @tExpDate
7899/* if the source is not less, but the new one is... */
7900 BEGIN
7901 INSERT INTO Nutrition.tVesselHistory (vesselID, statusID, dateChange, userChange, expirDate, isCurrent)
7902 SELECT vesselID, statusID, getdate(), ''System'', @newExpDate, 1
7903 FROM Nutrition.tVesselHistory
7904 WHERE vesselHistoryID = @vHistID;
7905
7906 UPDATE Nutrition.tVesselHistory
7907 SET isCurrent = 0
7908 WHERE vesselHistoryID = @vHistID;
7909 END
7910
7911/* k done for now */
7912
7913 SET @errCode = 0
7914 SELECT errCode = @errCode;
7915COMMIT TRANSACTION
7916
7917END TRY
7918BEGIN CATCH
7919ROLLBACK TRANSACTION
7920 SET @errCode = 1
7921 SELECT errCode = @errCode;
7922END CATCH;
7923')
7924
7925 PRINT N'USP Nutrition.uspAddOrigin created'
7926
7927END TRY
7928BEGIN CATCH
7929 PRINT N'USP Nutrition.uspAddOrigin create failed'
7930 PRINT 'Error Number: ' + str(error_number())
7931 PRINT 'Line Number: ' + str(error_line())
7932 PRINT error_message()
7933END CATCH
7934GO
7935
7936/* PTInfo.uspPatientByID.storedProcedure.sql
7937To call the SP:
7938EXECUTE PTinfo.usPatientByID @pSysID=1, @username='admin';
7939 */
7940
7941BEGIN TRY
7942EXECUTE ('-- =============================================
7943-- Author: Lisa Bohm (lbohm@paragondsi.com)
7944-- Create date: 2013-07-29
7945-- Description: Returns all patient data for a primary ID
7946-- Modified: 2013-11-21 to handle the HL7MsgField tbl add
7947-- 2013-12-07 add audit tracking call
7948-- =============================================
7949CREATE PROCEDURE [PTinfo].[uspPatientByID]
7950 -- Add the parameters for the stored procedure here
7951@pSysID bigint
7952, @username varchar(50)
7953
7954 AS
7955 DECLARE
7956 @priPID tinyint
7957
7958 BEGIN
7959 -- SET NOCOUNT ON added to prevent extra result sets from
7960 -- interfering with SELECT statements.
7961 SET NOCOUNT ON;
7962
7963SET @priPID = PTinfo.udfGetPIDtype();
7964
7965/* get the patient info with that ID */
7966SELECT PtP.pSysID
7967 , primaryID = PtI.IDValue
7968 , PtP.firstName
7969 , PtP.lastname
7970 , PtP.midInit
7971 , PtP.dob
7972 , currentStatus = PST.pStatTypeName
7973
7974
7975FROM PTinfo.tPatient PtP
7976 INNER JOIN PtInfo.tStatus PtS ON PtP.pSysID = PtS.pSysID
7977 INNER JOIN PtInfo.tStatusType PST ON PST.pStatTypeID = PtS.pStatTypeID
7978 INNER JOIN PtInfo.tIdentity PtI ON PtI.pSysID = PtP.pSysID
7979
7980WHERE PtP.pSysID = @pSysID
7981 AND PtS.isCurrent = 1
7982 AND PtI.identTypeID = @priPID
7983 ;
7984/* audit */
7985EXECUTE SysInfo.uspAddRecordView @username=@username, @auditTypeID=7, @pSysID=@pSysID;
7986
7987END;
7988')
7989
7990 PRINT N'USP PTinfo.uspPatientByID created'
7991
7992END TRY
7993BEGIN CATCH
7994 PRINT N'USP PTinfo.uspPatientByID create failed'
7995 PRINT 'Error Number: ' + str(error_number())
7996 PRINT 'Line Number: ' + str(error_line())
7997 PRINT error_message()
7998END CATCH
7999GO
8000
8001/* PTinfo.uspMotherByID.storedProcedure.sql
8002To call the SP:
8003EXECUTE PTinfo.uspMotherByID @pSysID=2, @username='admin';
8004 */
8005
8006BEGIN TRY
8007EXECUTE ('-- =============================================
8008-- Author: Lisa Bohm (lbohm@paragondsi.com)
8009-- Create date: 2013-07-29
8010-- Description: Returns mother data for a mother''s primary ID
8011-- Modified: 2013-12-07 add audit tracking call
8012-- =============================================
8013CREATE PROCEDURE [PTinfo].[uspMotherByID]
8014 -- Add the parameters for the stored procedure here
8015@pSysID bigint
8016, @username varchar(50)
8017 AS
8018 BEGIN
8019 -- SET NOCOUNT ON added to prevent extra result sets from
8020 -- interfering with SELECT statements.
8021 SET NOCOUNT ON;
8022
8023/* get the patient info with that ID */
8024SELECT PMI.pSysID
8025 , PMI.calsPer
8026
8027FROM PTinfo.tMomInfo PMI
8028WHERE PMI.pSysID = @pSysID;
8029
8030EXECUTE SysInfo.uspAddRecordView @username=@username, @auditTypeID=7, @pSysID=@pSysID;
8031
8032END;
8033')
8034
8035 PRINT N'USP PTinfo.uspMotherByID created'
8036
8037END TRY
8038BEGIN CATCH
8039 PRINT N'USP PTinfo.uspMotherByID create failed'
8040 PRINT 'Error Number: ' + str(error_number())
8041 PRINT 'Line Number: ' + str(error_line())
8042 PRINT error_message()
8043END CATCH
8044GO
8045
8046/* PTinfo.uspChildByID.storedProcedure.sql
8047To call the SP:
8048EXECUTE PTinfo.uspChildByID @pSysID=2, @username='admin';
8049 */
8050
8051BEGIN TRY
8052EXECUTE ('-- =============================================
8053-- Author: Lisa Bohm (lbohm@paragondsi.com)
8054-- Create date: 2013-07-29
8055-- Description: Returns child data for a pSysID
8056-- Modified: 2013-11-21 to handle the HL7MsgField tbl add
8057-- 2013-12-05 add audit tracking call
8058-- =============================================
8059CREATE PROCEDURE [PTinfo].[uspChildByID]
8060 -- Add the parameters for the stored procedure here
8061 @pSysID bigint
8062 , @username varchar(50)
8063
8064 AS
8065 DECLARE
8066 @priPID tinyint
8067
8068 BEGIN
8069 -- SET NOCOUNT ON added to prevent extra result sets from
8070 -- interfering with SELECT statements.
8071 SET NOCOUNT ON;
8072
8073SET @priPID = PTinfo.udfGetPIDtype();
8074
8075/* get the patient info with that ID */
8076SELECT PCL.pSysID
8077 , PCL.motherSysID
8078 , motherPrimaryID = PIM.IDvalue
8079 , PCL.multiple
8080 , pCL.gender
8081 , PCL.allowFormula
8082 , pCL.allowBottle
8083 , allowDonor = MAX(CASE WHEN PDO.orderTypeID = 1 THEN 1 ELSE 0 END)
8084 , needsFortification = MAX(CASE WHEN PDO.orderTypeID = 2 THEN 1 ELSE 0 END)
8085 , pCL.location
8086
8087
8088FROM PTinfo.tChildLink PCL
8089 LEFT OUTER JOIN PTinfo.tIdentity PIM ON PCL.motherSysID = PIM.pSysID
8090 AND PIM.identTypeID = @priPID
8091 LEFT OUTER JOIN PTinfo.tDocOrder PDO ON PCL.pSysID = PDO.pSysID
8092 AND PDO.orderEnd > getdate()
8093
8094WHERE PCL.pSysID = @pSysID
8095
8096GROUP BY PCL.pSysID
8097 , PCL.motherSysID
8098 , PIM.IDvalue
8099 , PCL.multiple
8100 , pCL.gender
8101 , PCL.allowFormula
8102 , pCL.allowBottle
8103 , pCL.location;
8104
8105 EXECUTE SysInfo.uspAddRecordView @username=@username, @auditTypeID=7, @pSysID=@pSysID;
8106
8107
8108END;
8109')
8110
8111 PRINT N'USP PTinfo.uspChildByID created'
8112
8113END TRY
8114BEGIN CATCH
8115 PRINT N'USP PTinfo.uspChildByID create failed'
8116 PRINT 'Error Number: ' + str(error_number())
8117 PRINT 'Line Number: ' + str(error_line())
8118 PRINT error_message()
8119END CATCH
8120GO
8121
8122/* Storage.uspAllLocations.storedProcedure.sql
8123To call the SP:
8124EXECUTE Storage.uspAllLocations @username='admin';
8125 */
8126
8127BEGIN TRY
8128EXECUTE ('-- =============================================
8129-- Author: Lisa Bohm (lbohm@paragondsi.com)
8130-- Create date: 2013-07-29
8131-- Description: list of all locations
8132-- Modified: 2013-11-21 to handle the HL7MsgField tbl add
8133-- 2013-12-09 add audit tracking call
8134-- =============================================
8135CREATE PROCEDURE [Storage].[uspAllLocations]
8136 -- Add the parameters for the stored procedure here
8137@username varchar(50)
8138
8139AS
8140 DECLARE
8141 @priPID tinyint
8142
8143BEGIN
8144 -- SET NOCOUNT ON added to prevent extra result sets from
8145 -- interfering with SELECT statements.
8146 SET NOCOUNT ON;
8147
8148SET @priPID = PTinfo.udfGetPIDtype();
8149
8150SELECT StL.locationID
8151 , StL.locationCode
8152 , StL.locationName
8153 , StL.locatedInID
8154 , StL.locTypeID
8155 , StT.locTypeName
8156 , PtP.pSysID
8157 , primaryID = PtI.IDvalue
8158 , PtP.firstName
8159 , PtP.midInit
8160 , PtP.lastName
8161 , chillType = CASE WHEN StL.locTypeID = 1 THEN ''Non-Storage'' ELSE
8162 CASE WHEN StL.locTypeID = 2 THEN ''Fridge'' ELSE
8163 CASE WHEN StL.locTypeID = 3 THEN ''Freezer'' ELSE
8164 CASE WHEN StP.locTypeID = 1 THEN ''Non-Storage'' ELSE
8165 CASE WHEN StP.locTypeID = 2 THEN ''Fridge'' ELSE
8166 CASE WHEN StP.locTypeID = 3 THEN ''Freezer'' ELSE '''' END
8167 END
8168 END
8169 END
8170 END
8171 END
8172
8173FROM Storage.tLocation StL
8174 INNER JOIN Storage.tLocationType StT ON StL.locTypeID = StT.locTypeID
8175 LEFT OUTER JOIN (Storage.tLocationAssign SLA
8176 INNER JOIN PTinfo.tPatient PtP ON SLA.pSysID = PtP.pSysID
8177 INNER JOIN PtInfo.tIdentity PtI ON PtP.pSysID = PtI.pSysID
8178 AND PtI.identTypeID = @priPID
8179 ) ON StL.locationID = SLA.locationID
8180 INNER JOIN Storage.tLocation STP ON StL.locatedInID = StP.locationID
8181;
8182
8183/* audit records. Multiple so use code insert */
8184INSERT INTO SysInfo.tRecordView
8185 (viewDate, userName, auditTypeID, pSysID)
8186
8187SELECT DISTINCT getdate()
8188 , @userName
8189 , 7
8190 , SLA.pSysID
8191 FROM Storage.tLocation StL
8192 INNER JOIN Storage.tLocationAssign SLA ON StL.locationID = SLA.locationID
8193;
8194
8195END;
8196')
8197
8198 PRINT N'USP Storage.uspAllLocations created'
8199
8200END TRY
8201BEGIN CATCH
8202 PRINT N'USP Storage.uspAllLocations create failed'
8203 PRINT 'Error Number: ' + str(error_number())
8204 PRINT 'Line Number: ' + str(error_line())
8205 PRINT error_message()
8206END CATCH
8207GO
8208
8209/* Storage.uspLocationByCode.storedProcedure.sql
8210To call the SP:
8211EXECUTE Storage.uspLocationByCode @locationCode='L0003', @username='admin';
8212 */
8213
8214BEGIN TRY
8215EXECUTE ('-- =============================================
8216-- Author: Lisa Bohm (lbohm@paragondsi.com)
8217-- Create date: 2013-07-29
8218-- Description: location information by location code
8219-- Modified: 2013-11-21 to handle the HL7MsgField tbl add
8220-- 2013-12-09 add audit tracking call
8221-- =============================================
8222CREATE PROCEDURE [Storage].[uspLocationByCode]
8223 -- Add the parameters for the stored procedure here
8224@locationCode varchar(20)
8225, @username varchar(50)
8226
8227AS
8228 DECLARE
8229 @priPID tinyint
8230 , @chillType varchar(12)
8231 , @locTypeID tinyint
8232 , @locatedInID bigint
8233 , @theNewID bigint;
8234
8235BEGIN
8236 -- SET NOCOUNT ON added to prevent extra result sets from
8237 -- interfering with SELECT statements.
8238 SET NOCOUNT ON;
8239
8240/* pass along the chill type. ''Cause, well, we''ll need it. */
8241
8242SELECT @locTypeID = STL.locTypeID
8243 , @locatedInID = STL.locatedInID
8244
8245 FROM Storage.tlocation STL
8246 WHERE StL.locationCode = @locationCode;
8247
8248IF @locTypeID = 4
8249 BEGIN
8250 SELECT @chillType = CASE WHEN StL.locTypeID = 2 THEN ''Fridge'' ELSE
8251 CASE WHEN StL.locTypeID = 3 THEN ''Freezer'' ELSE
8252 CASE WHEN StL.locTypeID = 1 THEN ''Non-Storage'' ELSE '''' END
8253 END
8254 END
8255 FROM Storage.tLocation STL
8256 WHERE StL.locationID = @locatedInID
8257
8258 END
8259ELSE
8260 BEGIN
8261 SET @chillType = CASE WHEN @locTypeID = 2 THEN ''Fridge'' ELSE
8262 CASE WHEN @locTypeID = 3 THEN ''Freezer'' ELSE
8263 CASE WHEN @locTypeID = 1 THEN ''Non-Storage'' ELSE '''' END
8264 END
8265 END
8266 END
8267
8268SET @priPID = PTinfo.udfGetPIDtype();
8269
8270
8271SELECT StL.locationID
8272 , StL.locationCode
8273 , StL.locationName
8274 , StL.locatedInID
8275 , StL.locTypeID
8276 , StT.locTypeName
8277 , PtP.pSysID
8278 , primaryID = PtI.IDvalue
8279 , PtP.firstName
8280 , PtP.midInit
8281 , PtP.lastName
8282 , chillType = @chillType
8283
8284FROM Storage.tLocation StL
8285 INNER JOIN Storage.tLocationType StT ON StL.locTypeID = StT.locTypeID
8286 LEFT OUTER JOIN (Storage.tLocationAssign SLA
8287 INNER JOIN PTinfo.tPatient PtP ON SLA.pSysID = PtP.pSysID
8288 INNER JOIN PtInfo.tIdentity PtI ON PtP.pSysID = PtI.pSysID
8289 AND PtI.identTypeID = @priPID
8290 ) ON StL.locationID = SLA.locationID
8291
8292WHERE StL.locationCode = @locationCode;
8293
8294 /* if assigned, shows pt plus ID so audit */
8295 SET @theNewID = COALESCE((SELECT SLA.pSysID
8296 FROM Storage.tLocation StL
8297 INNER JOIN Storage.tLocationAssign SLA ON StL.locationID = SLA.locationID
8298 WHERE StL.locationCode = @locationCode),0);
8299
8300 IF @theNewID > 0
8301 BEGIN
8302 /* audit stuff */
8303 EXECUTE SysInfo.uspAddRecordView @username=@username, @auditTypeID=7, @pSysID=@theNewID;
8304 END
8305END ;
8306')
8307
8308 PRINT N'USP Storage.uspLocationByCode created'
8309
8310END TRY
8311BEGIN CATCH
8312 PRINT N'USP Storage.uspLocationByCode create failed'
8313 PRINT 'Error Number: ' + str(error_number())
8314 PRINT 'Line Number: ' + str(error_line())
8315 PRINT error_message()
8316END CATCH
8317GO
8318
8319/* PTinfo.uspPrimaryToSysID.storedProcedure.sql
8320To call the SP:
8321EXECUTE PTinfo.uspPrimaryToSysID @primaryID='8279284';
8322 */
8323
8324BEGIN TRY
8325EXECUTE ('/****** Object: StoredProcedure [PTinfo].[uspPrimaryToSysID] Script Date: 07/30/2013 11:26:19 ******/
8326-- =============================================
8327-- Author: Lisa Bohm (lbohm@paragondsi.com)
8328-- Create date: 2013-07-30
8329-- Description: translates a primaryID to a pSysID
8330-- Modified: 2013-11-21 to handle the HL7MsgField tbl add
8331-- =============================================
8332CREATE PROCEDURE [PTinfo].[uspPrimaryToSysID]
8333 -- Add the parameters for the stored procedure here
8334@primaryID varchar(50)
8335
8336 AS
8337 DECLARE
8338 @priPID tinyint
8339
8340 BEGIN
8341 -- SET NOCOUNT ON added to prevent extra result sets from
8342 -- interfering with SELECT statements.
8343 SET NOCOUNT ON;
8344
8345SET @priPID = PTinfo.udfGetPIDtype();
8346
8347/* get the patient info with that ID */
8348SELECT PtP.pSysID
8349 , primaryID = PtI.IDValue
8350
8351FROM PTinfo.tPatient PtP
8352 INNER JOIN PtInfo.tIdentity PtI ON PtI.pSysID = PtP.pSysID
8353
8354WHERE PtI.IDValue = @primaryID
8355 AND PtI.identTypeID = @priPID
8356 ;
8357END;
8358')
8359
8360 PRINT N'USP PTinfo.uspPrimaryToSysID created'
8361
8362END TRY
8363BEGIN CATCH
8364 PRINT N'USP PTinfo.uspPrimaryToSysID create failed'
8365 PRINT 'Error Number: ' + str(error_number())
8366 PRINT 'Line Number: ' + str(error_line())
8367 PRINT error_message()
8368END CATCH
8369GO
8370
8371/* PTinfo.uspAddEditPStatusByID.storedProcedure.sql
8372To call the SP:
8373EXECUTE PTinfo.uspAddEditPStatusByID @pSysID=2, @pStatTypeID=2, @username='admin';
8374 */
8375
8376BEGIN TRY
8377EXECUTE ('/****** Object: StoredProcedure [PTinfo].[uspAddEditPStatusByID] Script Date: 12/07/2012 11:26:19 ******/
8378
8379-- =============================================
8380-- Author: Lisa Bohm (lbohm@paragondsi.com)
8381-- Create date: 2013-07-29
8382-- Description: Edit Patient Status
8383-- Modified: 2013-12-05 add audit tracking cal
8384-- =============================================
8385CREATE PROCEDURE [PTinfo].[uspAddEditPStatusByID]
8386 -- Add the parameters for the stored procedure here
8387@pSysID bigint
8388, @pStatTypeID tinyint = 1
8389, @username varchar(50)
8390
8391AS
8392DECLARE @theNewID bigint
8393BEGIN
8394 -- SET NOCOUNT ON added to prevent extra result sets from
8395 -- interfering with SELECT statements.
8396 SET NOCOUNT ON;
8397
8398 BEGIN TRY
8399 BEGIN TRANSACTION
8400 SET @theNewID = COALESCE((SELECT PtS.pStatID
8401 FROM PTinfo.tStatus PtS
8402 WHERE PtS.pSysID = @pSysID
8403 AND PtS.isCurrent = 1),0);
8404
8405 IF @theNewID > 0
8406 BEGIN
8407 /* update existing statuses to "not current" */
8408 UPDATE PTinfo.tStatus
8409 SET isCurrent = 0
8410 WHERE pStatID = @theNewID;
8411
8412 EXECUTE Sysinfo.uspAddDataChange @username=@username, @auditTypeID=10, @tableName=''PTinfo.tStatus'', @recordID=@theNewID, @previousEntry=''isCurrent=1'';
8413 END
8414 /* then insert current one */
8415 INSERT INTO PTinfo.tStatus (pSysID
8416 , pStatTypeID
8417 , statDate
8418 , isCurrent)
8419 VALUES
8420 (@pSysID
8421 , @pStatTypeID
8422 , getdate()
8423 , 1);
8424
8425 SET @theNewID = SCOPE_IDENTITY();
8426
8427 EXECUTE Sysinfo.uspAddDataChange @username=@username, @auditTypeID=9, @tableName=''PTinfo.tStatus'', @recordID=@theNewID, @previousEntry=''N/A'';
8428
8429 COMMIT TRANSACTION
8430 END TRY
8431
8432 BEGIN CATCH
8433 ROLLBACK TRANSACTION
8434 PRINT N''Err: Status not successfully updated.''
8435 END CATCH
8436
8437END;
8438')
8439
8440 PRINT N'USP PTinfo.uspAddEditPStatusByID created'
8441
8442END TRY
8443BEGIN CATCH
8444 PRINT N'USP PTinfo.uspAddEditPStatusByID create failed'
8445 PRINT 'Error Number: ' + str(error_number())
8446 PRINT 'Line Number: ' + str(error_line())
8447 PRINT error_message()
8448END CATCH
8449GO
8450
8451/* PTinfo.uspPatientByMotherID.storedProcedure.sql
8452To call the SP:
8453EXECUTE PTinfo.uspPatientByMotherID @motherSysID=4;
8454 */
8455
8456BEGIN TRY
8457EXECUTE ('-- =============================================
8458-- Author: Lisa Bohm (lbohm@paragondsi.com)
8459-- Create date: 2013-07-30
8460-- Description: Returns child/Patient data for a mother''s MRN
8461-- Modified: 2013-11-21 to handle the HL7MsgField tbl add
8462-- 2013-12-07 add audit tracking call
8463-- =============================================
8464CREATE PROCEDURE [PTinfo].[uspPatientByMotherID]
8465 -- Add the parameters for the stored procedure here
8466@motherSysID bigint
8467, @username varchar(50)
8468
8469 AS
8470 DECLARE
8471 @priPID tinyint
8472 BEGIN
8473 -- SET NOCOUNT ON added to prevent extra result sets from
8474 -- interfering with SELECT statements.
8475 SET NOCOUNT ON;
8476
8477SET @priPID = PTinfo.udfGetPIDtype();
8478
8479/* get the patient info with that mommy */
8480SELECT PtP.pSysID
8481 , primaryID = PtI.IDvalue
8482 , PtP.firstName
8483 , PtP.lastName
8484 , PtP.dob
8485 , PST.pStatTypeName
8486 , PCL.multiple
8487
8488FROM PTinfo.tChildLink PCL
8489 INNER JOIN PTinfo.tPatient PtP ON PCL.pSysID = PtP.pSysID
8490 INNER JOIN PTinfo.tIdentity PtI ON PtP.pSysID = PtI.pSysID
8491 INNER JOIn PTinfo.tStatus PTS ON PtP.pSysID = PTS.pSysID
8492 INNER JOIN PTinfo.tStatusType PST ON PTS.pStatTypeID = PST.pStatTypeID
8493
8494
8495WHERE PCL.motherSysID = @motherSysID
8496 AND PtI.identTypeID = @priPID
8497 AND PTS.isCurrent = 1;
8498
8499/* audit - use direct for multiple records/no RBAR */
8500 INSERT INTO SysInfo.tRecordView
8501 (viewDate, userName, auditTypeID, pSysID)
8502
8503SELECT getdate()
8504 , @userName
8505 , 7
8506 , PCL.pSysID
8507
8508FROM PTinfo.tChildLink PCL
8509WHERE PCL.motherSysID = @motherSysID;
8510
8511 END;
8512')
8513
8514 PRINT N'USP PTinfo.uspPatientByMotherID created'
8515
8516END TRY
8517BEGIN CATCH
8518 PRINT N'USP PTinfo.uspPatientByMotherID create failed'
8519 PRINT 'Error Number: ' + str(error_number())
8520 PRINT 'Line Number: ' + str(error_line())
8521 PRINT error_message()
8522END CATCH
8523GO
8524
8525/* Nutrition.uspAddEditAdditive.storedProcedure.sql
8526To call the SP:
8527EXECUTE Nutrition.uspAddEditAdditive @additiveID=0, @additiveName='Wonder Supplement', @unitOfMeas='scp', @username='admin';
8528Use @additiveID=0 for add, actual @additiveID for edit.
8529 */
8530
8531BEGIN TRY
8532EXECUTE ('/****** Object: StoredProcedure [Nutrition].[uspAddEditAdditive] Script Date: 8/12/2013 11:26:19 ******/
8533
8534-- =============================================
8535-- Author: Lisa Bohm (lbohm@paragondsi.com)
8536-- Create date: 2013-08-12
8537-- Description: add additive to list.
8538-- Modified: 2013-12-05 add audit tracking call
8539-- =============================================
8540CREATE PROCEDURE [Nutrition].[uspAddEditAdditive]
8541 -- Add the parameters for the stored procedure here
8542 @additiveID smallint
8543, @additiveName varchar(50)
8544, @unitOfMeas varchar(15)
8545, @username varchar(50)
8546
8547 AS
8548 DECLARE @already smallint
8549 , @isActive bit
8550 , @errCode tinyint
8551 , @theNewID bigint
8552 , @prevEntry varchar(1000);
8553
8554 BEGIN
8555 -- SET NOCOUNT ON added to prevent extra result sets from
8556 -- interfering with SELECT statements.
8557 SET NOCOUNT ON;
8558SET @already = COALESCE((SELECT NAL.additiveID
8559 FROM Nutrition.tAdditiveList NAL
8560 WHERE NAL.additiveID <> @additiveID
8561 AND NAL.additiveName = @additiveName),0)
8562IF @already > 0
8563 BEGIN
8564 /* if inactive, activate */
8565 SELECT @isActive = NAL.isActive
8566 FROM Nutrition.tAdditiveList NAL
8567 WHERE NAL.additiveID = @already;
8568
8569 IF @isActive = 0
8570 BEGIN
8571 UPDATE Nutrition.tAdditiveList
8572 SET isActive = 1
8573 WHERE additiveID = @already;
8574
8575 EXECUTE Sysinfo.uspAddDataChange @username=@username, @auditTypeID=10, @tableName=''Nutrition.tAdditiveList'', @recordID=@already, @previousEntry=''isActive=0'';
8576
8577 SET @errCode = 0
8578 SELECT errCode = @errCode;
8579 END
8580 ELSE
8581 BEGIN
8582 SET @errCode = 20
8583 SELECT errCode = @errCode;
8584 END
8585 END
8586
8587ELSE
8588 BEGIN
8589 IF @additiveID = 0
8590 BEGIN
8591 INSERT INTO Nutrition.tAdditiveList
8592 ([additiveName]
8593 ,[unitOfMeas])
8594 VALUES
8595 (@additiveName
8596 , @unitOfMeas);
8597
8598 SET @theNewID = SCOPE_IDENTITY();
8599
8600 EXECUTE Sysinfo.uspAddDataChange @username=@username, @auditTypeID=9, @tableName=''Nutrition.tAdditiveList'', @recordID=@theNewID, @previousEntry=''N/A'';
8601
8602 SET @errCode = 0
8603 SELECT errCode = @errCode;
8604 END
8605 ELSE
8606 BEGIN
8607 /* set up audit previous record */
8608 SELECT @prevEntry = NAL.additiveName + ''; '' + NAL.unitOfMeas + ''; '' + CAST(NAL.isActive AS VARCHAR) + ''.''
8609 FROM Nutrition.tAdditiveList NAL
8610 WHERE NAL.additiveID = @additiveID;
8611
8612 UPDATE Nutrition.tAdditiveList
8613 SET additiveName = @additiveName
8614 , unitofMeas = @unitofMeas
8615 WHERE additiveID = @additiveID;
8616
8617 EXECUTE Sysinfo.uspAddDataChange @username=@username, @auditTypeID=10, @tableName=''Nutrition.tAdditiveList'', @recordID=@additiveID, @previousEntry=@prevEntry;
8618
8619 SET @errCode = 0
8620 SELECT errCode = @errCode;
8621 END
8622 END
8623
8624END;
8625')
8626
8627 PRINT N'USP Nutrition.uspAddEditAdditive created'
8628
8629END TRY
8630BEGIN CATCH
8631 PRINT N'USP Nutrition.uspAddEditAdditive create failed'
8632 PRINT 'Error Number: ' + str(error_number())
8633 PRINT 'Line Number: ' + str(error_line())
8634 PRINT error_message()
8635END CATCH
8636GO
8637
8638/* Nutrition.uspAddAdditive.storedProcedure.sql
8639To call the SP:
8640EXECUTE Nutrition.uspAddAdditive @vesselID=123, @additiveID=1, @doses=2, @vesselHistoryID=12, @username='admin';
8641 */
8642
8643BEGIN TRY
8644EXECUTE ('/****** Object: StoredProcedure [Nutrition].[uspAddAdditive] Script Date: 6/28/2013 11:26:19 ******/
8645-- =============================================
8646-- Author: Lisa Bohm (lbohm@paragondsi.com)
8647-- Create date: 2013-06-28
8648-- Description: add additive to vessel. Precede with adding a fortify status.
8649-- Modified: 2013-12-05 add audit tracking call
8650-- =============================================
8651CREATE PROCEDURE [Nutrition].[uspAddAdditive]
8652 -- Add the parameters for the stored procedure here
8653@vesselID bigint
8654, @additiveID smallint
8655, @doses decimal(4,2)
8656, @vesselHistoryID bigint
8657, @username varchar(50)
8658
8659 AS
8660 DECLARE @theNewID bigint
8661 , @errCode tinyint;
8662BEGIN TRY
8663 -- SET NOCOUNT ON added to prevent extra result sets from
8664 -- interfering with SELECT statements.
8665 SET NOCOUNT ON;
8666
8667BEGIN TRANSACTION
8668
8669INSERT INTO Nutrition.tAdditiveIssue
8670 ([vesselID]
8671 ,[additiveID]
8672 ,[Doses]
8673 ,[vesselHistoryID])
8674 VALUES
8675 (@vesselID
8676 , @additiveID
8677 , @doses
8678 , @vesselHistoryID);
8679
8680 SET @theNewID = SCOPE_IDENTITY();
8681
8682 /* add data change record */
8683 EXECUTE Sysinfo.uspAddDataChange @username=@username, @auditTypeID=9, @tableName=''Nutrition.tAdditiveIssue'', @recordID=@theNewID, @previousEntry=''N/A'';
8684 COMMIT TRANSACTION
8685 SET @errCode = 0;
8686 SELECT errCode = @errCode;
8687END TRY
8688BEGIN CATCH
8689ROLLBACK TRANSACTION
8690
8691 SET @errCode = 1;
8692 SELECT errCode = @errCode;
8693END CATCH;
8694')
8695
8696 PRINT N'USP Nutrition.uspAddAdditive created'
8697
8698END TRY
8699BEGIN CATCH
8700 PRINT N'USP Nutrition.uspAddAdditive create failed'
8701 PRINT 'Error Number: ' + str(error_number())
8702 PRINT 'Line Number: ' + str(error_line())
8703 PRINT error_message()
8704END CATCH
8705GO
8706
8707/* Nutrition.uspVesselCodeToID.storedProcedure.sql
8708To call the SP:
8709EXECUTE Nutrition.uspVesselCodeToID @vesselCode='B123';
8710 */
8711
8712BEGIN TRY
8713EXECUTE ('/****** Object: StoredProcedure [Nutrition].[uspVesselCodeToID] Script Date: 07/30/2013 11:26:19 ******/
8714-- =============================================
8715-- Author: Lisa Bohm (lbohm@paragondsi.com)
8716-- Create date: 2013-07-30
8717-- Description: translates a vesselCode to a vesselID
8718--
8719-- =============================================
8720CREATE PROCEDURE [Nutrition].[uspVesselCodeToID]
8721 -- Add the parameters for the stored procedure here
8722@vesselCode varchar(20)
8723
8724 AS
8725
8726 BEGIN
8727 -- SET NOCOUNT ON added to prevent extra result sets from
8728 -- interfering with SELECT statements.
8729 SET NOCOUNT ON;
8730
8731/* get the patient info with that ID */
8732SELECT NtV.vesselID
8733 , NtV.vesselCode
8734
8735FROM Nutrition.tVessel NtV
8736
8737WHERE NtV.vesselCode = @vesselCode;
8738END;
8739')
8740
8741 PRINT N'USP Nutrition.uspVesselCodeToID created'
8742
8743END TRY
8744BEGIN CATCH
8745 PRINT N'USP Nutrition.uspVesselCodeToID create failed'
8746 PRINT 'Error Number: ' + str(error_number())
8747 PRINT 'Line Number: ' + str(error_line())
8748 PRINT error_message()
8749END CATCH
8750GO
8751
8752/* PTinfo.uspAssignAdditive.storedProcedure.sql
8753To call the SP:
8754EXECUTE PTinfo.uspAssignAdditive @pSysID=12, @additiveID=1, @dosage=2, @username='admin';
8755 */
8756
8757BEGIN TRY
8758EXECUTE ('/****** Object: StoredProcedure [PTinfo].[uspAssignAdditive] Script Date: 6/28/2013 11:26:19 ******/
8759
8760-- =============================================
8761-- Author: Lisa Bohm (lbohm@paragondsi.com)
8762-- Create date: 2013-08-01
8763-- Description: Assign Additive to patient
8764-- Modified: 2013-12-05 add audit tracking call
8765-- =============================================
8766CREATE PROCEDURE [PTinfo].[uspAssignAdditive]
8767 -- Add the parameters for the stored procedure here
8768@pSysID bigint
8769, @additiveID smallint
8770, @dosage decimal(4,2)
8771, @username varchar(50)
8772
8773AS
8774DECLARE @already bigint
8775 , @errCode tinyint
8776 , @theNewID bigint
8777 , @prevEntry varchar(1000);
8778BEGIN
8779 -- SET NOCOUNT ON added to prevent extra result sets from
8780 -- interfering with SELECT statements.
8781 SET NOCOUNT ON;
8782
8783/* does patient already have a doc order for additives? if not, add one at this time. Eventually remove when done through Doc orders */
8784SET @already = COALESCE((SELECT COUNT(PDO.docOrderID)
8785 FROM PTinfo.tDocOrder PDO
8786 WHERE PDO.pSysID = @pSysID
8787 AND PDO.orderTypeID = 2
8788 AND PDO.orderEnd > getdate()), 0)
8789
8790IF @already = 0
8791 -- None exists so add doc order --
8792 BEGIN
8793 INSERT INTO PTinfo.tDocOrder (pSysID, orderNum, orderTypeID, orderNote, orderStart, orderEnd)
8794 VALUES(
8795 @pSysID
8796 , ''0''
8797 , 2
8798 , ''Default add order for additives required''
8799 , getdate()
8800 , ''2500-01-01'')
8801
8802 SET @theNewID = SCOPE_IDENTITY();
8803
8804 EXECUTE Sysinfo.uspAddDataChange @username=@username, @auditTypeID=9, @tableName=''PTinfo.tDocOrder'', @recordID=@theNewID, @previousEntry=''N/A - System entry for additive assigned'';
8805 END
8806
8807/* now, assign additive through the additive patient table */
8808
8809/* is this patient already assigned this additive? If so, update the dosage */
8810SET @already = COALESCE(
8811 (SELECT 1
8812 FROM PTinfo.tAdditivePatient PAP
8813 WHERE PAP.pSysID = @pSysID
8814 AND PAP.additiveID = @additiveID), 0)
8815
8816IF @already = 0
8817 BEGIN
8818 INSERT INTO PTinfo.tAdditivePatient
8819 (additiveID, pSysID, dosage)
8820 VALUES
8821 (@additiveID
8822 , @pSysID
8823 , @dosage);
8824
8825 SET @theNewID = SCOPE_IDENTITY();
8826
8827 EXECUTE Sysinfo.uspAddDataChange @username=@username, @auditTypeID=9, @tableName=''PTinfo.tAdditivePatient'', @recordID=@theNewID, @previousEntry=''N/A'';
8828
8829 END
8830ELSE
8831 BEGIN
8832 SELECT @prevEntry = CAST(PAP.dosage AS varchar) + ''.''
8833 , @theNewID = PAP.addPatientID
8834 FROM PTinfo.tAdditivePatient PAP
8835 WHERE PAP.pSysID = @pSysID
8836 AND PAP.additiveID = @additiveID;
8837
8838 UPDATE PTinfo.tAdditivePatient
8839 SET dosage = @dosage
8840 WHERE pSysID = @pSysID
8841 AND additiveID = @additiveID;
8842 END
8843
8844 EXECUTE Sysinfo.uspAddDataChange @username=@username, @auditTypeID=10, @tableName=''PTinfo.tAdditivePatient'', @recordID=@theNewID, @previousEntry=@prevEntry;
8845
8846 SET @errCode = 0;
8847 SELECT @errCode;
8848/* Now - are there still any active additives for the patient? If not, and there''s a doc order for additives, make the end date for them NOW */
8849
8850SET @already = COALESCE(
8851 (SELECT COUNT(PAP.addPatientID)
8852 FROM PTinfo.tAdditivePatient PAP
8853 WHERE PAP.pSysID = @pSysID
8854 AND PAP.dosage > 0), 0)
8855IF @already = 0
8856 BEGIN
8857 /* data change tracking for audit */
8858 INSERT INTO SysInfo.tDataChange (viewDate, userName, auditTypeID, tableName, recordID, previousEntry)
8859 SELECT getdate()
8860 , @username
8861 , 10
8862 , ''PTinfo.tDocOrder''
8863 , PDO.docOrderID
8864 , ''Order End: '' + CAST(PDO.orderEnd AS VARCHAR) + ''.''
8865
8866 FROM PTinfo.tDocOrder PDO
8867 WHERE pSysID = @pSysID
8868 AND orderTypeID = 2
8869 AND orderEnd > getdate();
8870
8871 /* now end the doc order by updating the end date to now */
8872 UPDATE PTinfo.tDocOrder
8873 SET orderEnd = getdate()
8874 WHERE pSysID = @pSysID
8875 AND orderTypeID = 2
8876 AND orderEnd > getdate();
8877 END
8878END;
8879')
8880
8881 PRINT N'USP PTinfo.uspAssignAdditive created'
8882
8883END TRY
8884BEGIN CATCH
8885 PRINT N'USP PTinfo.uspAssignAdditive create failed'
8886 PRINT 'Error Number: ' + str(error_number())
8887 PRINT 'Line Number: ' + str(error_line())
8888 PRINT error_message()
8889END CATCH
8890GO
8891
8892/* PTinfo.uspAdditiveAssignedList.storedProcedure.sql
8893To call the SP:
8894EXECUTE PTinfo.uspAdditiveAssignedList @pSysID=4, @username='admin';
8895 */
8896
8897BEGIN TRY
8898EXECUTE ('/****** Object: StoredProcedure [PTinfo].[uspAdditiveAssignedList] Script Date: 6/28/2013 11:26:19 ******/
8899
8900-- =============================================
8901-- Author: Lisa Bohm (lbohm@paragondsi.com)
8902-- Create date: 2013-08-01
8903-- Description: Lists additives assigned to a patient
8904-- Modified: 2013-12-05 add audit tracking call
8905-- =============================================
8906CREATE PROCEDURE [PTinfo].[uspAdditiveAssignedList]
8907 -- Add the parameters for the stored procedure here
8908@pSysID bigint
8909, @username varchar(50)
8910
8911 AS
8912BEGIN
8913 -- SET NOCOUNT ON added to prevent extra result sets from
8914 -- interfering with SELECT statements.
8915 SET NOCOUNT ON;
8916
8917SELECT PAP.pSysID
8918 , PAP.additiveID
8919 , NAL.additiveName
8920 , NAL.unitOfMeas
8921 , PAP.dosage
8922
8923FROM PTinfo.tAdditivePatient PAP
8924 INNER JOIN Nutrition.tAdditiveList NAL ON PAP.additiveID = NAL.additiveID
8925
8926 WHERE PAP.pSysID = @pSySID
8927 AND PAP.dosage > 0;
8928
8929/* audit the patient info view */
8930EXECUTE SysInfo.uspAddRecordView @username=@username, @auditTypeID=7, @pSysID=@pSysID;
8931
8932END;
8933')
8934
8935 PRINT N'USP PTinfo.uspAdditiveAssignedList created'
8936
8937END TRY
8938BEGIN CATCH
8939 PRINT N'USP PTinfo.uspAdditiveAssignedList create failed'
8940 PRINT 'Error Number: ' + str(error_number())
8941 PRINT 'Line Number: ' + str(error_line())
8942 PRINT error_message()
8943END CATCH
8944GO
8945
8946/* Nutrition.uspStatusChange.storedProcedure.sql
8947To call the SP:
8948EXECUTE Nutrition.uspStatusChange @vesselID=123, @statusID=3, @userChange='admin';
8949 */
8950
8951BEGIN TRY
8952EXECUTE ('/****** Object: StoredProcedure [Nutrition].[uspStatusChange] Script Date: 6/28/2013 11:26:19 ******/
8953
8954-- =============================================
8955-- Author: Lisa Bohm (lbohm@paragondsi.com)
8956-- Create date: 2013-07-22
8957-- Description: Change status of milk
8958-- Modified: 2013-12-09 add audit tracking call
8959-- 2014-02-24 Not duplicate certain current statuses
8960-- =============================================
8961CREATE PROCEDURE [Nutrition].[uspStatusChange]
8962 -- Add the parameters for the stored procedure here
8963@vesselID bigint
8964, @statusID tinyint
8965, @userChange varchar(50)
8966, @expirDate datetime = ''2500-01-01''
8967, @dateChange datetime = ''1900-01-01''
8968
8969 AS
8970 DECLARE @currStatusID tinyint
8971 , @currExpDate datetime
8972 , @currStatRank tinyint
8973 , @nCategoryID tinyint
8974 , @vesselhistoryID bigint
8975 , @newStatExpHrs smallint
8976 , @newStatRank tinyint
8977 , @errCode tinyint
8978 , @already bit
8979 , @prevEntry varchar(1000);
8980
8981BEGIN
8982 -- SET NOCOUNT ON added to prevent extra result sets from
8983 -- interfering with SELECT statements.
8984 SET NOCOUNT ON;
8985
8986IF @dateChange = ''1900-01-01''
8987 BEGIN
8988 SET @dateChange = getdate()
8989 END
8990
8991SET @errCode = 0;
8992
8993/* firster of all, get the current status of the vessel. */
8994
8995SELECT @currStatusID = NVH.statusID
8996 , @currExpDate = NVH.expirDate
8997 , @vesselHistoryID = NVH.vesselHistoryID
8998 , @currStatRank = NSL.statRank
8999
9000 FROM Nutrition.tVesselHistory NVH
9001 INNER JOIN Nutrition.tStatusList NSL ON NVH.statusID = NSL.statusID
9002 WHERE NVH.vesselID = @vesselID
9003 AND NVH.isCurrent = 1;
9004
9005/* if the "new" status is the same as the current status, well, STOP. Unless it''s an event. or fortified. stat rank of fortified is 3. Um... yeah, I think that''s it. */
9006IF @currStatRank > 3 AND @currStatusID = @statusID
9007BEGIN
9008/* donut dooo anything! */
9009 SET @errCode = 22
9010 SELECT errCode = @errCode
9011 , vesselHistoryID = @vesselHistoryID;
9012END
9013
9014IF @errCode = 0 /* nothing wrong yet! */
9015BEGIN
9016/* first of all, are there even expiration hours associated with this status? */
9017SET @newStatExpHrs = COALESCE((SELECT COUNT(StS.setValue)
9018 FROM Nutrition.tStatusExpir NSE
9019 INNER JOIN SysInfo.tSetting StS ON NSE.settingID = StS.settingID
9020 WHERE NSE.statusID = @statusID
9021 AND StS.setValue > 0),0);
9022
9023/* what''s the status rank of the new status? */
9024SET @newStatRank = (SELECT NSL.statRank
9025 FROM Nutrition.tStatusList NSL
9026 WHERE NSL.statusID = @statusID);
9027
9028/* if there are, then do all the finding of the correct stuff */
9029IF @newStatExpHrs > 0
9030/*what exactly is IN this vessel? We need to know to assign expiration correctly */
9031BEGIN
9032/* temp table for contents */
9033
9034DECLARE @contents TABLE (oVID bigint
9035 , vesselID bigint
9036 , nCatID tinyint)
9037
9038SET @already = COALESCE((SELECT CASE WHEN COUNT(NVO.vesselID) > 0 THEN 1 ELSE 0 END
9039 FROM Nutrition.tVesselOrigin NVO
9040 WHERE NVO.targetVesselID = @vesselID), 0)
9041
9042IF @already = 0
9043 BEGIN
9044 INSERT INTO @contents
9045 (oVID
9046 , vesselID
9047 , nCatID)
9048
9049 SELECT NTVi.vesselID
9050 , NTVi.vesselID
9051 , NTVi.nCategoryID
9052
9053 FROM Nutrition.tVessel NTVi
9054 WHERE NtVi.vesselID = @vesselID;
9055 END
9056ELSE
9057 BEGIN
9058 /* get contents of root bottles */
9059 WITH cteBuildPath AS
9060 (-- the anchor part now is just for the original bottle
9061 SELECT oVessel = NVOr.vesselID
9062 , tgtVessel = NVOr.targetVesselID
9063 , hLevel = 1
9064
9065 FROM Nutrition.tVesselOrigin NVOr
9066 WHERE NVOr.targetVesselID = @vesselID
9067
9068 /* the recursive part */
9069 UNION ALL
9070
9071 SELECT oVessel = NVOl.vesselID
9072 , tgtVessel = NVOl.targetVesselID
9073 , hLevel = cte.hLevel + 1
9074
9075 FROM Nutrition.tVesselOrigin NVOl
9076 INNER JOIN cteBuildPath AS cte ON cte.oVessel = NVOl.targetVesselID
9077 )
9078
9079
9080 INSERT INTO @contents
9081 (oVID
9082 , vesselID
9083 , nCatID)
9084
9085 SELECT @vesselID
9086 , RTv.oVessel
9087 , NtV.nCategoryID
9088
9089 FROM cteBuildPath RTv
9090 LEFT OUTER JOIN Nutrition.tVesselOrigin NVO ON RTv.oVessel = NVO.targetVesselID
9091 INNER JOIN Nutrition.tVessel NtV ON RTv.oVessel = NtV.vesselID
9092
9093 WHERE NVO.vesselID IS NULL;
9094 END
9095
9096
9097SET @nCategoryID = COALESCE((SELECT MIN(CT.nCatID)
9098 FROM @contents CT
9099 WHERE CT.nCatID > 0
9100 ),0)
9101
9102SET @newStatExpHrs = COALESCE((SELECT StS.setValue
9103 FROM Nutrition.tStatusExpir NSE
9104 INNER JOIN SysInfo.tSetting StS ON NSE.settingID = StS.settingID
9105 WHERE NSE.statusID = @statusID
9106 AND NSE.nCategoryID = @nCategoryID),9999);
9107 END
9108ELSE
9109 BEGIN
9110 SET @newStatExpHrs = 9999;
9111 END
9112
9113/* trying to change to the same status? Waaaaal... don''t bother! */
9114
9115 BEGIN TRY
9116/* yeah - that last status? shouldn''t be current any longer... maybe
9117-- If the new status rank = 0, then doesn''t override the new current one. IF the new status rank > 0
9118-- but is less than the current status rank, it''s not allowed.
9119 */
9120
9121IF @newStatRank > 0
9122 BEGIN
9123 IF @newStatRank < @currStatRank OR @currStatusID = @statusID
9124 BEGIN
9125
9126 UPDATE Nutrition.tVesselHistory
9127 SET isCurrent = 0
9128 WHERE vesselHistoryID = @vesselHistoryID;
9129
9130 /* audit record */
9131 EXECUTE Sysinfo.uspAddDataChange @username=@userChange, @auditTypeID=10, @tableName=''Nutrition.tVesselHistory'', @recordID=@vesselHistoryID, @previousEntry=''isCurrent = 1'';
9132
9133/* if "now" plus the exp hours for the setting is GREATER than the current expiration date, use the current exp date. If not, use the "new" expiration date. */
9134 SET @currExpDate = CASE WHEN @expirDate < @currExpDate THEN @expirDate ELSE
9135 CASE WHEN @currExpDate < DATEADD(HOUR,@newStatExpHrs,@dateChange) THEN
9136 CASE WHEN @statusID = 3 THEN DATEADD(HOUR,@newStatExpHrs,@dateChange) ELSE @currExpDate END
9137 ELSE DATEADD(HOUR,@newStatExpHrs,@dateChange) END
9138 END;
9139
9140 INSERT INTO Nutrition.tVesselHistory
9141 (vesselID
9142 , statusID
9143 , dateChange
9144 , userChange
9145 , expirDate
9146 , isCurrent)
9147 VALUES
9148 (@vesselID
9149 , @statusID
9150 , @dateChange
9151 , @userChange
9152 , @currExpDate
9153 , 1)
9154
9155 SET @vesselHistoryID = (SELECT SCOPE_IDENTITY());
9156
9157 SET @errCode = 0
9158 SELECT errCode = @errCode
9159 , vesselHistoryID = @vesselHistoryID;
9160 END
9161 ELSE
9162 BEGIN
9163 SET @errCode = 1
9164 SET @vesselHistoryID = 0
9165 SELECT errCode = @errCode
9166 , vesselHistoryID = @vesselHistoryID;
9167 END
9168 END
9169
9170ELSE
9171/* add status event (status Rank = 0). Make sure NOT to set as "isCurrent" */
9172
9173/* if "now" plus the exp hours for the setting is GREATER than the current expiration date, use the current exp date. If not, use the "new" expiration date. */
9174 BEGIN
9175 SET @currExpDate = CASE WHEN @expirDate < @currExpDate THEN @expirDate ELSE
9176 CASE WHEN @currExpDate < DATEADD(HOUR,@newStatExpHrs,@dateChange) THEN
9177 CASE WHEN @statusID = 3 THEN DATEADD(HOUR,@newStatExpHrs,@dateChange) ELSE @currExpDate END
9178 ELSE DATEADD(HOUR,@newStatExpHrs,@dateChange) END
9179 END;
9180
9181 INSERT INTO Nutrition.tVesselHistory
9182 (vesselID
9183 , statusID
9184 , dateChange
9185 , userChange
9186 , expirDate
9187 , isCurrent)
9188 VALUES
9189 (@vesselID
9190 , @statusID
9191 , @dateChange
9192 , @userChange
9193 , @currExpDate
9194 , 0)
9195
9196 SET @vesselHistoryID = (SELECT SCOPE_IDENTITY());
9197 SET @errCode = 0
9198 SELECT errCode = @errCode
9199 , vesselHistoryID = @vesselHistoryID;
9200 END
9201END TRY
9202BEGIN CATCH
9203SET @errCode = 1
9204SET @vesselHistoryID = 0
9205
9206SELECT errCode = @errCode
9207 , vesselHistoryID = @vesselHistoryID;;
9208
9209END CATCH
9210END
9211END;
9212')
9213
9214 PRINT N'USP Nutrition.uspStatusChange created'
9215
9216END TRY
9217BEGIN CATCH
9218 PRINT N'USP Nutrition.uspStatusChange create failed'
9219 PRINT 'Error Number: ' + str(error_number())
9220 PRINT 'Line Number: ' + str(error_line())
9221 PRINT error_message()
9222END CATCH
9223GO
9224
9225/* Nutrition.uspDelAdditive.storedProcedure.sql
9226To call the SP:
9227EXECUTE Nutrition.uspDelAdditive @vesselID=123, @additiveID=1;
9228 */
9229
9230BEGIN TRY
9231EXECUTE ('/****** Object: StoredProcedure [Nutrition].[uspDelAdditive] Script Date: 6/28/2013 11:26:19 ******/
9232-- =============================================
9233-- Author: Lisa Bohm (lbohm@paragondsi.com)
9234-- Create date: 2013-06-28
9235-- Description: remove additive to milk
9236-- Modified: 2014-02-25 check if is current, make sure a current history thingy
9237-- =============================================
9238CREATE PROCEDURE [Nutrition].[uspDelAdditive]
9239 -- Add the parameters for the stored procedure here
9240@vesselID bigint
9241, @additiveID bigint
9242, @username varchar(40)
9243
9244 AS
9245 DECLARE @addIssueID bigint
9246 , @vesselHistoryID bigint
9247 , @already tinyint
9248 , @errCode tinyint
9249 , @isCurrent bit
9250 , @prevEntry varchar(1000);
9251
9252BEGIN
9253 -- SET NOCOUNT ON added to prevent extra result sets from
9254 -- interfering with SELECT statements.
9255 SET NOCOUNT ON;
9256
9257/* remove the history record as well, if only for this additive */
9258 SET @vesselHistoryID = COALESCE((SELECT vesselHistoryID
9259 FROM Nutrition.tAdditiveIssue
9260 WHERE additiveID = @additiveID
9261 AND vesselID = @vesselID),0);
9262 SET @addIssueID = COALESCE((SELECT addIssueID
9263 FROM Nutrition.tAdditiveIssue
9264 WHERE additiveID = @additiveID
9265 AND vesselID = @vesselID),0);
9266
9267IF @addIssueID > 0
9268 BEGIN TRY
9269 BEGIN TRANSACTION
9270 /* auditing... */
9271 SELECT @prevEntry = CAST(NAI.additiveID AS VARCHAR) + ''; '' + CAST(NAI.vesselID AS VARCHAR) + ''; '' + CAST(NAI.doses AS VARCHAR) + ''; '' + CAST(NAI.vesselHistoryID AS VARCHAR) + ''.''
9272 FROM Nutrition.tAdditiveIssue NAI
9273 WHERE NAI.addIssueID = @addIssueID
9274
9275 DELETE
9276 FROM Nutrition.tAdditiveIssue
9277 WHERE addIssueID = @addIssueID;
9278
9279 EXECUTE Sysinfo.uspAddDataChange @username=@username, @auditTypeID=11, @tableName=''Nutrition.tAdditiveIssue'', @recordID=@addIssueID, @previousEntry=@prevEntry;
9280
9281 /* hey any other additives issued using this history ID? */
9282 SELECT @already = COUNT(NAI.addIssueID)
9283 FROM Nutrition.tAdditiveIssue NAI
9284 WHERE NAI.vesselHistoryID = @vesselHistoryID;
9285
9286 /* only delete if none are */
9287 IF @already = 0
9288 BEGIN
9289 SELECT @isCurrent = NVH.isCurrent
9290 FROM Nutrition.tVesselHistory NVH
9291 WHERE NVH.vesselHistoryID = @vesselHistoryID;
9292
9293 SELECT @prevEntry = CAST(NVH.vesselID AS VARCHAR) + ''; '' + CAST(NVH.statusID AS VARCHAR) + ''; '' + CAST(NVH.dateChange AS VARCHAR) + ''; '' + NVH.userChange + ''; '' + CAST(NVH.expirDate AS VARCHAR) + ''; '' + CAST(NVH.isCurrent AS VARCHAR) + ''.''
9294 FROM Nutrition.tVesselHistory NVH
9295 WHERE NVH.vesselHistoryID = @vesselHistoryID;
9296
9297 DELETE
9298 FROM Nutrition.tVesselHistory
9299 WHERE vesselHistoryID = @vesselHistoryID
9300
9301 EXECUTE Sysinfo.uspAddDataChange @username=@username, @auditTypeID=11, @tableName=''Nutrition.tVesselHistory'', @recordID=@vesselHistoryID, @previousEntry=@prevEntry;
9302
9303
9304 IF @isCurrent = 1
9305 BEGIN
9306 /* find most recent status that''s not an event, set that as isCurrent... */
9307 SELECT @vesselHistoryID = MAX(NvH.vesselHistoryID)
9308 FROM Nutrition.tVesselHistory NvH
9309 INNER JOIN Nutrition.tStatusList NSL ON NvH.statusID = NSL.statusID
9310 WHERE NvH.vesselID = @vesselID
9311 AND NSL.statCatID = 2;
9312
9313 UPDATE Nutrition.tVesselHistory
9314 SET isCurrent = 1
9315 WHERE vesselHistoryID = @vesselHistoryID;
9316
9317 SET @prevEntry = ''isCurrent = 0.''
9318
9319 EXECUTE Sysinfo.uspAddDataChange @username=@username, @auditTypeID=10, @tableName=''Nutrition.tVesselHistory'', @recordID=@vesselHistoryID, @previousEntry=@prevEntry;
9320
9321 END
9322 END
9323 COMMIT TRANSACTION
9324
9325 SET @errCode=0;
9326 SELECT errCode = @errCode;
9327
9328END TRY
9329BEGIN CATCH
9330ROLLBACK TRANSACTION
9331 SET @errCode=1;
9332 SELECT errCode = @errCode;
9333
9334END CATCH
9335END;
9336')
9337
9338 PRINT N'USP Nutrition.uspDelAdditive created'
9339
9340END TRY
9341BEGIN CATCH
9342 PRINT N'USP Nutrition.uspDelAdditive create failed'
9343 PRINT 'Error Number: ' + str(error_number())
9344 PRINT 'Line Number: ' + str(error_line())
9345 PRINT error_message()
9346END CATCH
9347GO
9348
9349
9350/* PTinfo.uspPatientByStat.storedProcedure.sql
9351To call the SP:
9352EXECUTE PTinfo.uspPatientByStat @isChild=1, @statTypeID=1, @username='admin';
9353 */
9354
9355BEGIN TRY
9356EXECUTE ('-- =============================================
9357-- Author: Lisa Bohm (lbohm@paragondsi.com)
9358-- Create date: 2013-04-28
9359-- Description: Returns child/Patient data for a mother''s MRN/active
9360-- Modified: 2013-11-21 to handle the HL7MsgField tbl add
9361-- 2013-12-09 add audit tracking call
9362-- =============================================
9363CREATE PROCEDURE [PTinfo].[uspPatientByStat]
9364 -- Add the parameters for the stored procedure here
9365@isChild bit = 1
9366, @statTypeID tinyint = 1
9367, @username varchar(50)
9368
9369 AS
9370 DECLARE
9371 @priPID tinyint
9372 BEGIN
9373 -- SET NOCOUNT ON added to prevent extra result sets from
9374 -- interfering with SELECT statements.
9375 SET NOCOUNT ON;
9376
9377SET @priPID = PTinfo.udfGetPIDtype();
9378
9379/* get the patient info */
9380SELECT PtP.pSysID
9381 , PtP.firstName
9382 , PtP.midInit
9383 , PtP.lastName
9384 , PtP.dob
9385 , primaryID = PtI.IDvalue
9386 , isChild = CASE WHEN PCL.pSysID IS NULL then 0 ELSE 1 END
9387
9388FROM PTinfo.tPatient PtP
9389 INNER JOIN PTinfo.tStatus PtS ON PtP.pSysID = PtS.pSysID
9390 INNER JOIN PTinfo.tIdentity PtI On PtP.pSysID = PtI.pSysID
9391 LEFT OUTER JOIN PTinfo.tChildLink PCL ON PtP.pSysID = PCL.pSysID
9392WHERE PtS.isCurrent = 1
9393 AND PtS.pStatTypeID = @statTypeID
9394 AND PtI.identTypeID = @priPID
9395 AND CASE WHEN PCL.pSysID IS NULL then 0 ELSE 1 END = @isChild
9396
9397ORDER BY PtP.lastName
9398 , PtP.firstName
9399 ;
9400
9401/* audit - multiple records so go with insert */
9402INSERT INTO SysInfo.tRecordView
9403 (viewDate, userName, auditTypeID, pSysID)
9404
9405SELECT getdate()
9406 , @userName
9407 , 7
9408 , PtP.pSysID
9409
9410FROM PTinfo.tPatient PtP
9411 INNER JOIN PTinfo.tStatus PtS ON PtP.pSysID = PtS.pSysID
9412 LEFT OUTER JOIN PTinfo.tChildLink PCL ON PtP.pSysID = PCL.pSysID
9413
9414 WHERE PtS.isCurrent = 1
9415 AND PtS.pStatTypeID = @statTypeID
9416 AND CASE WHEN PCL.pSysID IS NULL then 0 ELSE 1 END = @isChild;
9417
9418END;
9419')
9420
9421 PRINT N'USP PTinfo.uspPatientByStat created'
9422
9423END TRY
9424BEGIN CATCH
9425 PRINT N'USP PTinfo.uspPatientByStat create failed'
9426 PRINT 'Error Number: ' + str(error_number())
9427 PRINT 'Line Number: ' + str(error_line())
9428 PRINT error_message()
9429END CATCH
9430GO
9431
9432/* PTinfo.uspPatientStatByLName.storedProcedure.sql
9433To call the SP:
9434EXECUTE PTinfo.uspPatientStatByLName @isChild=1, @statTypeID=1, @lname='Doe', @username='admin';
9435 */
9436
9437BEGIN TRY
9438EXECUTE ('-- =============================================
9439-- Author: Lisa Bohm (lbohm@paragondsi.com)
9440-- Create date: 2013-07-29
9441-- Description: Returns child/Patient data by last name
9442-- Modified: 2013-11-21 to handle the HL7MsgField tbl add
9443-- 2013-12-09 add audit tracking call
9444-- =============================================
9445CREATE PROCEDURE [PTinfo].[uspPatientStatByLName]
9446 -- Add the parameters for the stored procedure here
9447@isChild bit = 1
9448, @statTypeID tinyint = 1
9449, @lName varchar(25)
9450, @username varchar(50)
9451
9452 AS
9453 DECLARE
9454 @priPID tinyint
9455 BEGIN
9456 -- SET NOCOUNT ON added to prevent extra result sets from
9457 -- interfering with SELECT statements.
9458 SET NOCOUNT ON;
9459SET @priPID = PTinfo.udfGetPIDtype();
9460
9461/* get the patient info */
9462SELECT PtP.pSysID
9463 , PtP.firstName
9464 , PtP.midInit
9465 , PtP.lastName
9466 , PtP.dob
9467 , primaryID = PtI.IDvalue
9468 , isChild = CASE WHEN PCL.pSysID IS NULL then 0 ELSE 1 END
9469
9470FROM PTinfo.tPatient PtP
9471 INNER JOIN PTinfo.tStatus PtS ON PtP.pSysID = PtS.pSysID
9472 INNER JOIN PTinfo.tIdentity PtI On PtP.pSysID = PtI.pSysID
9473 LEFT OUTER JOIN PTinfo.tChildLink PCL ON PtP.pSysID = PCL.pSysID
9474
9475 WHERE PtS.isCurrent = 1
9476 AND PtS.pStatTypeID = @statTypeID
9477 AND PtI.identTypeID = @priPID
9478 AND CASE WHEN PCL.pSysID IS NULL then 0 ELSE 1 END = @isChild
9479 AND PtP.lastName LIKE @lName + ''%''
9480
9481ORDER BY PtP.lastName
9482 , PtP.firstName
9483 ;
9484
9485/* audit - multiple records so go with insert */
9486INSERT INTO SysInfo.tRecordView
9487 (viewDate, userName, auditTypeID, pSysID)
9488
9489SELECT getdate()
9490 , @userName
9491 , 7
9492 , PtP.pSysID
9493
9494FROM PTinfo.tPatient PtP
9495 INNER JOIN PTinfo.tStatus PtS ON PtP.pSysID = PtS.pSysID
9496 LEFT OUTER JOIN PTinfo.tChildLink PCL ON PtP.pSysID = PCL.pSysID
9497
9498 WHERE PtS.isCurrent = 1
9499 AND PtS.pStatTypeID = @statTypeID
9500 AND CASE WHEN PCL.pSysID IS NULL then 0 ELSE 1 END = @isChild
9501 AND PtP.lastName LIKE @lName + ''%'';
9502
9503END;
9504')
9505
9506 PRINT N'USP PTinfo.uspPatientStatByLName created'
9507
9508END TRY
9509BEGIN CATCH
9510 PRINT N'USP PTinfo.uspPatientStatByLName create failed'
9511 PRINT 'Error Number: ' + str(error_number())
9512 PRINT 'Line Number: ' + str(error_line())
9513 PRINT error_message()
9514END CATCH
9515GO
9516
9517/* PTinfo.uspEditPrimaryID.storedProcedure.sql
9518To call the SP:
9519EXECUTE PTinfo.uspEditPrimaryID @pSysID=1, @origPID='82749', @newPID='0093820', @username='admin';
9520 */
9521
9522BEGIN TRY
9523EXECUTE ('-- =============================================
9524-- Author: Lisa Bohm (lbohm@paragondsi.com)
9525-- Create date: 2013-07-29
9526-- Description: edits a primary ID
9527-- Modified: 2013-11-21 to handle the HL7MsgField tbl add
9528-- 2013-12-07 add audit tracking call
9529-- =============================================
9530CREATE PROCEDURE [PTinfo].[uspEditPrimaryID]
9531 -- Add the parameters for the stored procedure here
9532@pSysID bigint
9533, @origPID varchar(50)
9534, @newPID varchar(50)
9535, @username varchar(50)
9536AS
9537 DECLARE @priPID tinyint
9538 , @already bit
9539 , @errCode tinyint
9540 , @theNewID bigint
9541 , @prevEntry varchar(1000);
9542 BEGIN
9543 -- SET NOCOUNT ON added to prevent extra result sets from
9544 -- interfering with SELECT statements.
9545 SET NOCOUNT ON;
9546
9547SET @priPID = PTinfo.udfGetPIDtype();
9548
9549/* mothers only here*/
9550/* does the "change it to" MRN already exist? */
9551SET @already = COALESCE((SELECT 1
9552 FROM PTinfo.tIdentity PTI
9553 WHERE PTI.identTypeID = @priPID
9554 AND PTI.IDvalue = @newPID
9555 ), 0)
9556
9557IF @already = 1
9558 BEGIN
9559 SET @errCode = 20
9560 SELECT @errCode;
9561 END
9562
9563ELSE
9564 BEGIN TRY
9565 /* audit stuff */
9566 SELECT @theNewID = PtI.pID
9567 FROM PTinfo.tIdentity PtI
9568 WHERE identTypeID = @priPID
9569 AND pSysID= @pSysID
9570 AND IDvalue = @origPID;
9571
9572 /* create new record for Mother with new MRN */
9573
9574 /* update identity records */
9575 UPDATE PTinfo.tIdentity
9576 SET IDvalue = @newPID
9577 WHERE identTypeID = @priPID
9578 AND pSysID= @pSysID
9579 AND IDvalue = @origPID;
9580
9581 SET @prevEntry = ''primaryID = '' + @origPID
9582 EXECUTE Sysinfo.uspAddDataChange @username=@username, @auditTypeID=10, @tableName=''PTinfo.tIdentity'', @recordID=@theNewID, @previousEntry=@prevEntry;
9583
9584 SET @errCode = 0
9585 SELECT @errCode;
9586
9587END TRY
9588
9589BEGIN CATCH
9590 SET @errCode = 1
9591 SELECT @errCode;
9592END CATCH
9593
9594END;
9595')
9596
9597 PRINT N'USP PTinfo.uspEditPrimaryID created'
9598
9599END TRY
9600BEGIN CATCH
9601 PRINT N'USP PTinfo.uspEditPrimaryID create failed'
9602 PRINT 'Error Number: ' + str(error_number())
9603 PRINT 'Line Number: ' + str(error_line())
9604 PRINT error_message()
9605END CATCH
9606GO
9607
9608/* Nutrition.uspGetAdditiveList.storedProcedure.sql
9609To call the SP:
9610EXECUTE Nutrition.uspGetAdditiveList;
9611 */
9612
9613BEGIN TRY
9614EXECUTE ('
9615/****** Object: StoredProcedure [Nutrition].[uspGetAdditiveList] Script Date: 8/09/2013 11:26:19 ******/
9616
9617-- =============================================
9618-- Author: Lisa Bohm (lbohm@paragondsi.com)
9619-- Create date: 2013-08-09
9620-- Description: gets formula
9621--
9622-- =============================================
9623CREATE PROCEDURE [Nutrition].[uspGetAdditiveList]
9624 -- Add the parameters for the stored procedure here
9625 AS
9626
9627 BEGIN
9628 -- SET NOCOUNT ON added to prevent extra result sets from
9629 -- interfering with SELECT statements.
9630 SET NOCOUNT ON;
9631
9632SELECT NAL.additiveID
9633 , NAL.additiveName
9634 , NAL.unitOfMeas
9635
9636FROM Nutrition.tAdditiveList NAL
9637WHERE NAL.isActive = 1;
9638
9639END;
9640')
9641
9642 PRINT N'USP Nutrition.uspGetAdditiveList created'
9643
9644END TRY
9645BEGIN CATCH
9646 PRINT N'USP Nutrition.uspGetAdditiveList create failed'
9647 PRINT 'Error Number: ' + str(error_number())
9648 PRINT 'Line Number: ' + str(error_line())
9649 PRINT error_message()
9650END CATCH
9651GO
9652
9653/* Nutrition.uspAddDisposalCode.storedProcedure.sql
9654To call the SP:
9655EXECUTE Nutrition.uspAddDisposalCode @statusName='Truly Empty', @username='admin';
9656 */
9657
9658BEGIN TRY
9659EXECUTE ('/****** Object: StoredProcedure [Nutrition].[uspAddDisposalCode] Script Date: 8/12/2013 11:26:19 ******/
9660
9661-- =============================================
9662-- Author: Lisa Bohm (lbohm@paragondsi.com)
9663-- Create date: 2013-08-12
9664-- Description: add custom disposal code.
9665-- Modified: 2013-12-05 add audit tracking call
9666-- =============================================
9667CREATE PROCEDURE [Nutrition].[uspAddDisposalCode]
9668 -- Add the parameters for the stored procedure here
9669@statusName varchar(20)
9670, @username varchar(50)
9671
9672 AS
9673 DECLARE @already tinyint
9674 , @errCode tinyint
9675 , @visible bit
9676 , @theNewID tinyint;
9677
9678 BEGIN
9679 -- SET NOCOUNT ON added to prevent extra result sets from
9680 -- interfering with SELECT statements.
9681 SET NOCOUNT ON;
9682
9683SELECT @already = CASE WHEN NSL.statusID IS NULL THEN 0 ELSE NSL.statusID END
9684 , @visible = COALESCE(NSL.visible, 1)
9685 FROM Nutrition.tStatusList NSL
9686 WHERE NSL.statusName = @statusName
9687 AND NSL.statCatID = 4;
9688IF @already > 1
9689 BEGIN
9690 IF @visible = 0
9691 BEGIN TRY
9692 BEGIN TRANSACTION
9693
9694 UPDATE Nutrition.tStatusList
9695 SET visible = 1
9696 WHERE statCatID = 4
9697 AND canEdit = 1
9698 AND statusName = @statusName;
9699
9700 EXECUTE Sysinfo.uspAddDataChange @username=@username, @auditTypeID=10, @tableName=''Nutrition.tStatusList'', @recordID=@already, @previousEntry=''visible=0'';
9701
9702 COMMIT TRANSACTION
9703 SET @errCode = 0;
9704 SELECT errCode = @errCode;
9705 END TRY
9706 BEGIN CATCH
9707 ROLLBACK TRANSACTION
9708 SET @errCode = 1;
9709 SELECT errCode = @errCode;
9710 END CATCH
9711 ELSE
9712 BEGIN
9713 SET @errCode = 20
9714 SELECT errCode = @errCode;
9715 END
9716 END
9717ELSE
9718 BEGIN TRY
9719 BEGIN TRANSACTION
9720
9721 INSERT INTO Nutrition.tStatusList
9722 ([statCatID]
9723 ,[statusName]
9724 , [canEdit]
9725 , [visible]
9726 , [statRank])
9727 VALUES
9728 (4
9729 , @statusName
9730 , 1
9731 , 1
9732 , 1);
9733
9734 SET @theNewID = SCOPE_IDENTITY();
9735
9736 EXECUTE Sysinfo.uspAddDataChange @username=@username, @auditTypeID=9, @tableName=''Nutrition.tStatusList'', @recordID=@theNewID, @previousEntry=''N/A'';
9737
9738 COMMIT TRANSACTION
9739 SET @errCode = 0;
9740 SELECT errCode = @errCode;
9741 END TRY
9742 BEGIN CATCH
9743 ROLLBACK TRANSACTION
9744 SET @errCode = 1;
9745 SELECT errCode = @errCode;
9746 END CATCH
9747
9748END;
9749')
9750
9751 PRINT N'USP Nutrition.uspAddDisposalCode created'
9752
9753END TRY
9754BEGIN CATCH
9755 PRINT N'USP Nutrition.uspAddDisposalCode create failed'
9756 PRINT 'Error Number: ' + str(error_number())
9757 PRINT 'Line Number: ' + str(error_line())
9758 PRINT error_message()
9759END CATCH
9760GO
9761
9762/* Nutrition.uspDelDisposalCode.storedProcedure.sql
9763To call the SP:
9764EXECUTE Nutrition.uspDelDisposalCode @statusID=20, @username='admin';
9765 */
9766
9767BEGIN TRY
9768EXECUTE ('/****** Object: StoredProcedure [Nutrition].[uspDelDisposalCode] Script Date: 8/12/2013 11:26:19 ******/
9769-- =============================================
9770-- Author: Lisa Bohm (lbohm@paragondsi.com)
9771-- Create date: 2013-08-12
9772-- Description: inactivate custom disposal code.
9773-- Modified: 2013-12-05 add audit tracking call
9774-- =============================================
9775CREATE PROCEDURE [Nutrition].[uspDelDisposalCode]
9776 -- Add the parameters for the stored procedure here
9777 @statusID tinyint
9778 , @username varchar(50)
9779
9780 AS
9781 DECLARE @errCode tinyint;
9782 BEGIN
9783 -- SET NOCOUNT ON added to prevent extra result sets from
9784 -- interfering with SELECT statements.
9785 SET NOCOUNT ON;
9786
9787 UPDATE Nutrition.tStatusList
9788 SET visible = 0
9789 WHERE statCatID = 4
9790 AND canEdit = 1
9791 AND statusID = @statusID;
9792
9793 EXECUTE Sysinfo.uspAddDataChange @username=@username, @auditTypeID=10, @tableName=''Nutrition.tStatusList'', @recordID=@statusID, @previousEntry=''visible=1'';
9794
9795 SET @errCode = 0
9796 SELECT errCode = @errCode;
9797
9798END;
9799')
9800
9801 PRINT N'USP Nutrition.uspDelDisposalCode created'
9802
9803END TRY
9804BEGIN CATCH
9805 PRINT N'USP Nutrition.uspDelDisposalCode create failed'
9806 PRINT 'Error Number: ' + str(error_number())
9807 PRINT 'Line Number: ' + str(error_line())
9808 PRINT error_message()
9809END CATCH
9810GO
9811
9812/* Nutrition.uspGetDisposalCode.storedProcedure.sql
9813To call the SP:
9814EXECUTE Nutrition.uspGetDisposalCode;
9815 */
9816
9817BEGIN TRY
9818EXECUTE ('/****** Object: StoredProcedure [Nutrition].[uspGetDisposalCode] Script Date: 8/09/2013 11:26:19 ******/
9819-- =============================================
9820-- Author: Lisa Bohm (lbohm@paragondsi.com)
9821-- Create date: 2013-08-12
9822-- Description: gets disposal codes
9823--
9824-- =============================================
9825CREATE PROCEDURE [Nutrition].[uspGetDisposalCode]
9826 -- Add the parameters for the stored procedure here
9827 AS
9828
9829 BEGIN
9830 -- SET NOCOUNT ON added to prevent extra result sets from
9831 -- interfering with SELECT statements.
9832 SET NOCOUNT ON;
9833
9834SELECT NSL.statusID
9835 , NSL.statusName
9836
9837FROM Nutrition.tStatusList NSL
9838
9839WHERE NSL.statCatID = 4
9840 AND NSL.visible = 1;
9841
9842END;
9843')
9844
9845 PRINT N'USP Nutrition.uspGetDisposalCode created'
9846
9847END TRY
9848BEGIN CATCH
9849 PRINT N'USP Nutrition.uspGetDisposalCode create failed'
9850 PRINT 'Error Number: ' + str(error_number())
9851 PRINT 'Line Number: ' + str(error_line())
9852 PRINT error_message()
9853END CATCH
9854GO
9855
9856/* Nutrition.uspAddVesselNote.storedProcedure.sql
9857To call the SP:
9858EXECUTE Nutrition.uspAddVesselNote @vesselID=123, @vNote='blah blah blah, Ginger', @vesselHistoryID=12, @username='admin';
9859 */
9860
9861BEGIN TRY
9862EXECUTE ('/****** Object: StoredProcedure [Nutrition].[uspAddVesselNote] Script Date: 6/28/2013 11:26:19 ******/
9863
9864-- =============================================
9865-- Author: Lisa Bohm (lbohm@paragondsi.com)
9866-- Create date: 2013-08-12
9867-- Description: add note to vessel. Precede with adding some status or other.
9868-- Modified: 2013-12-05 add audit tracking call
9869-- =============================================
9870CREATE PROCEDURE [Nutrition].[uspAddVesselNote]
9871 -- Add the parameters for the stored procedure here
9872@vesselID bigint
9873, @vNote varchar(200)
9874, @vesselHistoryID bigint
9875, @username varchar(50)
9876
9877 AS
9878 DECLARE @theNewID bigint
9879 BEGIN
9880 -- SET NOCOUNT ON added to prevent extra result sets from
9881 -- interfering with SELECT statements.
9882 SET NOCOUNT ON;
9883
9884INSERT INTO Nutrition.tvesselNote
9885 ([vesselID]
9886 ,[vNote]
9887 ,[vesselHistoryID])
9888 VALUES
9889 (@vesselID
9890 , @vNote
9891 , @vesselHistoryID);
9892 /* audit */
9893 SET @theNewID = SCOPE_IDENTITY();
9894 EXECUTE Sysinfo.uspAddDataChange @username=@username, @auditTypeID=9, @tableName=''Nutrition.tVesselNote'', @recordID=@theNewID, @previousEntry=''N/A'';
9895
9896END;
9897')
9898
9899 PRINT N'USP Nutrition.uspAddVesselNote created'
9900
9901END TRY
9902BEGIN CATCH
9903 PRINT N'USP Nutrition.uspAddVesselNote create failed'
9904 PRINT 'Error Number: ' + str(error_number())
9905 PRINT 'Line Number: ' + str(error_line())
9906 PRINT error_message()
9907END CATCH
9908GO
9909
9910/* PTinfo.uspNeedFeedNotify.storedProcedure.sql
9911To call the SP:
9912EXECUTE PTinfo.uspNeedFeedNotify @username='admin';
9913 */
9914
9915BEGIN TRY
9916EXECUTE ('/****** Object: StoredProcedure [PTinfo].[uspNeedFeedNotify] Script Date: 11/29/2012 11:26:19 ******/
9917
9918-- =============================================
9919-- Author: Lisa Bohm (lbohm@paragondsi.com)
9920-- Create date: 2013-08-13
9921-- Description: Returns data for patients not fed within specified hour marker
9922-- Modified: 2013-11-21 to handle the HL7MsgField tbl add
9923-- 2013-12-07 add audit tracking call
9924-- =============================================
9925CREATE PROCEDURE [PTinfo].[uspNeedFeedNotify]
9926 -- Add the parameters for the stored procedure here
9927@username varchar(50)
9928
9929AS
9930DECLARE @feedWithin tinyint
9931 , @targetTime dateTime
9932 , @priPID tinyint
9933
9934 SELECT @feedWithin= ABS(CAST(SS.[setValue] as INT))
9935 FROM SysInfo.tSetting SS
9936 WHERE SS.settingID = 12;
9937SET @targetTime=DATEADD(hour,-1*@feedWithin, getdate())
9938
9939
9940 BEGIN
9941 -- SET NOCOUNT ON added to prevent extra result sets from
9942 -- interfering with SELECT statements.
9943 SET NOCOUNT ON;
9944IF @feedWithin <> 0
9945BEGIN
9946SET @priPID = PTinfo.udfGetPIDtype();
9947
9948SELECT MRF.primaryID
9949 , MRF.patientName
9950 , fedWhen = CASE WHEN MRF.fedWhen = ''1900-01-01'' THEN NULL ELSE MRF.fedWhen END
9951 , MRF.fedBy
9952
9953FROM (
9954 SELECT PtP.pSysID
9955 , primaryID = PtI.IDvalue
9956 , patientName = PtP.lastName + '', '' + PtP.firstName + '' '' +
9957 CASE WHEN COALESCE(PCL.multiple,''0'') = ''0'' THEN ''''
9958 ELSE CASE WHEN LEN(LTRIM(RTRIM(PCL.multiple))) > 0 THEN '' ('' + PCL.multiple + '')''
9959 ELSE '''' END
9960 END
9961 , fedWhen = CASE WHEN NVH.dateChange IS NULL THEN ''1900-01-01'' ELSE NVH.dateChange END
9962 , fedBy = NVH.userChange
9963 , Rank() OVER (Partition BY PtI.IDvalue ORDER BY CASE WHEN NVH.dateChange IS NULL THEN ''1900-01-01'' ELSE NVH.dateChange END DESC) N
9964
9965 FROM PTinfo.tPatient PtP
9966 INNER JOIN PtInfo.tChildLink PCL ON PtP.pSysID = PCL.pSysID
9967 INNER JOIN PtInfo.tStatus PTS ON PtP.pSysID = PTS.pSysID
9968 INNER JOIN PTinfo.tIdentity PtI ON PtP.pSysID = PtI.pSysID
9969
9970 LEFT OUTER JOIN (Nutrition.tVesselAssign NVA
9971 INNER JOIN Nutrition.tVesselHistory NVH ON NVA.vesselID = NVH.vesselID
9972 ) ON PtP.pSysID = NVA.pSysID
9973 AND NVH.statusID = 7
9974
9975/* active */
9976 WHERE PTS.isCurrent = 1
9977 AND PtS.pStatTypeID = 1
9978/* primary ID */
9979 AND PtI.identTypeID = @priPID
9980
9981 ) MRF
9982 LEFT OUTER JOIN PTinfo.tDocOrder PDO ON MRF.pSysID = PDO.pSysID
9983 AND PDO.orderTypeID = 4
9984 AND PDO.orderEnd < getdate()
9985
9986WHERE MRF.N = 1
9987 AND MRF.fedWhen < @targetTime
9988 AND PDO.orderNum IS NULL
9989
9990ORDER BY MRF.fedWhen ASC
9991;
9992
9993/* add auditing */
9994INSERT INTO SysInfo.tRecordView
9995 (viewDate, userName, auditTypeID, pSysID)
9996
9997SELECT getdate()
9998 , @userName
9999 , 7
10000 , MRF.pSysID
10001
10002FROM (
10003 SELECT PtP.pSysID
10004 , fedWhen = CASE WHEN NVH.dateChange IS NULL THEN ''1900-01-01'' ELSE NVH.dateChange END
10005 , Rank() OVER (Partition BY PtI.IDvalue ORDER BY CASE WHEN NVH.dateChange IS NULL THEN ''1900-01-01'' ELSE NVH.dateChange END DESC) N
10006
10007 FROM PTinfo.tPatient PtP
10008 INNER JOIN PtInfo.tChildLink PCL ON PtP.pSysID = PCL.pSysID
10009 INNER JOIN PtInfo.tStatus PTS ON PtP.pSysID = PTS.pSysID
10010 INNER JOIN PTinfo.tIdentity PtI ON PtP.pSysID = PtI.pSysID
10011
10012 LEFT OUTER JOIN (Nutrition.tVesselAssign NVA
10013 INNER JOIN Nutrition.tVesselHistory NVH ON NVA.vesselID = NVH.vesselID
10014 ) ON PtP.pSysID = NVA.pSysID
10015 AND NVH.statusID = 7
10016
10017/* active */
10018 WHERE PTS.isCurrent = 1
10019 AND PtS.pStatTypeID = 1
10020/* primary ID */
10021 AND PtI.identTypeID = @priPID
10022
10023 ) MRF
10024 LEFT OUTER JOIN PTinfo.tDocOrder PDO ON MRF.pSysID = PDO.pSysID
10025 AND PDO.orderTypeID = 4
10026 AND PDO.orderEnd < getdate()
10027
10028WHERE MRF.N = 1
10029 AND MRF.fedWhen < @targetTime
10030 AND PDO.orderNum IS NULL
10031
10032ORDER BY MRF.fedWhen ASC
10033;
10034END
10035END;
10036')
10037
10038 PRINT N'USP PTinfo.uspNeedFeedNotify created'
10039
10040END TRY
10041BEGIN CATCH
10042 PRINT N'USP PTinfo.uspNeedFeedNotify create failed'
10043 PRINT 'Error Number: ' + str(error_number())
10044 PRINT 'Line Number: ' + str(error_line())
10045 PRINT error_message()
10046END CATCH
10047GO
10048
10049/* PTinfo.uspActiveLastFed.storedProcedure.sql
10050To call the SP:
10051EXECUTE PTinfo.uspActiveLastFed @username='admin';
10052 */
10053
10054BEGIN TRY
10055EXECUTE ('-- =============================================
10056-- Author: Lisa Bohm (lbohm@paragondsi.com)
10057-- Create date: 2013-12-09
10058-- Description: Returns last time all active patients were fed
10059-- Modified:
10060-- =============================================
10061CREATE PROCEDURE [PTinfo].[uspActiveLastFed]
10062 -- Add the parameters for the stored procedure here
10063@username varchar(50)
10064
10065AS
10066DECLARE @priPID tinyint
10067
10068 BEGIN
10069 -- SET NOCOUNT ON added to prevent extra result sets from
10070 -- interfering with SELECT statements.
10071 SET NOCOUNT ON;
10072
10073 SET @priPID = PTinfo.udfGetPIDtype();
10074
10075 SELECT MRF.primaryID
10076 , MRF.patientName
10077 , fedWhen = CASE WHEN PDO.docOrderID IS NOT NULL THEN ''NPO'' ELSE
10078 CASE WHEN MRF.fedWhen = ''1900-01-01'' THEN NULL ELSE CAST(MRF.fedWhen AS VARCHAR) END
10079 END
10080 , MRF.fedBy
10081
10082FROM (
10083 SELECT PtP.pSysID
10084 , primaryID = PtI.IDvalue
10085 , patientName = PtP.lastName + '', '' + PtP.firstName + '' '' +
10086 CASE WHEN COALESCE(PCL.multiple,''0'') = ''0'' THEN ''''
10087 ELSE CASE WHEN LEN(LTRIM(RTRIM(PCL.multiple))) > 0 THEN '' ('' + PCL.multiple + '')''
10088 ELSE '''' END
10089 END
10090 , fedWhen = CASE WHEN NVH.dateChange IS NULL THEN ''1900-01-01'' ELSE NVH.dateChange END
10091 , fedBy = NVH.userChange
10092 , Rank() OVER (Partition BY PtI.IDvalue ORDER BY CASE WHEN NVH.dateChange IS NULL THEN ''1900-01-01'' ELSE NVH.dateChange END DESC) N
10093
10094 FROM PTinfo.tPatient PtP
10095 INNER JOIN PtInfo.tChildLink PCL ON PtP.pSysID = PCL.pSysID
10096 INNER JOIN PtInfo.tStatus PTS ON PtP.pSysID = PTS.pSysID
10097 INNER JOIN PTinfo.tIdentity PtI ON PtP.pSysID = PtI.pSysID
10098
10099 LEFT OUTER JOIN (Nutrition.tVesselAssign NVA
10100 INNER JOIN Nutrition.tVesselHistory NVH ON NVA.vesselID = NVH.vesselID
10101 ) ON PtP.pSysID = NVA.pSysID
10102 AND NVH.statusID = 7
10103
10104/* active */
10105 WHERE PTS.isCurrent = 1
10106 AND PtS.pStatTypeID = 1
10107/* primary ID */
10108 AND PtI.identTypeID = @priPID
10109
10110 ) MRF
10111 LEFT OUTER JOIN PTinfo.tDocOrder PDO ON MRF.pSysID = PDO.pSysID
10112 AND PDO.orderTypeID = 4
10113 AND PDO.orderEnd > getdate()
10114
10115WHERE MRF.N = 1
10116
10117ORDER BY MRF.fedWhen ASC
10118;
10119
10120/* add auditing */
10121INSERT INTO SysInfo.tRecordView
10122 (viewDate, userName, auditTypeID, pSysID)
10123
10124SELECT getdate()
10125 , @userName
10126 , 7
10127 , MRF.pSysID
10128
10129FROM (
10130 SELECT PtP.pSysID
10131 , fedWhen = CASE WHEN NVH.dateChange IS NULL THEN ''1900-01-01'' ELSE NVH.dateChange END
10132 , Rank() OVER (Partition BY PtI.IDvalue ORDER BY CASE WHEN NVH.dateChange IS NULL THEN ''1900-01-01'' ELSE NVH.dateChange END DESC) N
10133
10134 FROM PTinfo.tPatient PtP
10135 INNER JOIN PtInfo.tChildLink PCL ON PtP.pSysID = PCL.pSysID
10136 INNER JOIN PtInfo.tStatus PTS ON PtP.pSysID = PTS.pSysID
10137 INNER JOIN PTinfo.tIdentity PtI ON PtP.pSysID = PtI.pSysID
10138
10139 LEFT OUTER JOIN (Nutrition.tVesselAssign NVA
10140 INNER JOIN Nutrition.tVesselHistory NVH ON NVA.vesselID = NVH.vesselID
10141 ) ON PtP.pSysID = NVA.pSysID
10142 AND NVH.statusID = 7
10143
10144/* active */
10145 WHERE PTS.isCurrent = 1
10146 AND PtS.pStatTypeID = 1
10147/* primary ID */
10148 AND PtI.identTypeID = @priPID
10149
10150 ) MRF
10151WHERE MRF.N = 1
10152
10153ORDER BY MRF.fedWhen ASC;
10154END;
10155')
10156
10157 PRINT N'USP PTinfo.uspActiveLastFed created'
10158
10159END TRY
10160BEGIN CATCH
10161 PRINT N'USP PTinfo.uspActiveLastFed create failed'
10162 PRINT 'Error Number: ' + str(error_number())
10163 PRINT 'Line Number: ' + str(error_line())
10164 PRINT error_message()
10165END CATCH
10166GO
10167
10168/* Nutrition.uspAvailVesselCtByMother.storedProcedure.sql
10169To call the SP:
10170EXECUTE Nutrition.uspAvailVesselCtByMother @pSysID=12;
10171 */
10172
10173BEGIN TRY
10174EXECUTE ('/****** Object: StoredProcedure [Nutrition].[uspAvailVesselCtByMother] Script Date: 6/28/2013 11:26:19 ******/
10175
10176-- =============================================
10177-- Author: Lisa Bohm (lbohm@paragondsi.com)
10178-- Create date: 2013-08-13
10179-- Description: count of available vessels for a mother.
10180-- Modified: 2014-03-18 add expired but not disposed milk
10181-- =============================================
10182CREATE PROCEDURE [Nutrition].[uspAvailVesselCtByMother]
10183 -- Add the parameters for the stored procedure here
10184@pSysID bigint
10185
10186 AS
10187 BEGIN
10188 -- SET NOCOUNT ON added to prevent extra result sets from
10189 -- interfering with SELECT statements.
10190 SET NOCOUNT ON;
10191
10192WITH cteContents AS
10193( -- anchor
10194SELECT oVessel = NtV.vesselID
10195 , tgtVessel = NtV.vesselID
10196 , NSL.statCatID
10197
10198FROM Nutrition.tVessel NtV
10199 LEFT OUTER JOIN Nutrition.tMotherVessel NMV ON NtV.vesselID = NMV.vesselID
10200 INNER JOIN Nutrition.tVesselHistory NVH ON NtV.vesselID = NVH.vesselID
10201 INNER JOIN Nutrition.tStatusList NSL ON NVH.statusID = NSL.statusID
10202
10203WHERE NvH.isCurrent = 1
10204 AND NMV.pSysID = @pSysID
10205
10206UNION ALL
10207 SELECT oVessel = cte.oVessel
10208 , tgtVessel = NVOl.targetVesselID
10209 , NSLl.statCatID
10210
10211 FROM Nutrition.tVesselOrigin NVOl
10212 INNER JOIN cteContents AS cte ON cte.tgtVessel = NVOl.vesselID
10213 INNER JOIN Nutrition.tVesselHistory NVHl ON NVOl.targetvesselID = NVHl.vesselID
10214 INNER JOIN Nutrition.tStatusList NSLl ON NVHl.statusID = NSLl.statusID
10215WHERE NVHl.isCurrent = 1
10216)
10217
10218SELECT numVessels = COUNT(DISTINCT theList.theVessel)
10219
10220FROM (SELECT theVessel = cte.tgtVessel
10221FROM cteContents cte
10222WHERE cte.statCatID >= 2
10223 AND cte.statCatID <=3
10224
10225UNION ALL
10226
10227SELECT theVessel = NtV.vesselID
10228
10229FROM Nutrition.tVessel NtV
10230 INNER JOIN Nutrition.tVesselAssign NVA ON NtV.vesselID = NVA.vesselID
10231 INNER JOIN PtInfo.tChildLink PCL ON NVA.pSysID = PCL.pSysID
10232 INNER JOIN Nutrition.tVesselHistory NVH ON NtV.vesselID = NVH.vesselID
10233 INNER JOIN Nutrition.tStatusList NSL ON NVH.statusID = NSL.statusID
10234
10235 WHERE NSL.statCatID >= 2
10236 AND NSL.statCatID <= 3
10237 AND NVH.isCurrent = 1
10238 AND PCL.motherSysID = @pSysID) theList;
10239
10240
10241 END;
10242')
10243
10244 PRINT N'USP Nutrition.uspAvailVesselCtByMother created'
10245
10246END TRY
10247BEGIN CATCH
10248 PRINT N'USP Nutrition.uspAvailVesselCtByMother create failed'
10249 PRINT 'Error Number: ' + str(error_number())
10250 PRINT 'Line Number: ' + str(error_line())
10251 PRINT error_message()
10252END CATCH
10253GO
10254
10255/* Nutrition.uspAvailVesselCtByChild.storedProcedure.sql
10256To call the SP:
10257EXECUTE Nutrition.uspAvailVesselCtByChild @pSysID=1;
10258 */
10259
10260BEGIN TRY
10261EXECUTE ('/****** Object: StoredProcedure [Nutrition].[uspAvailVesselCtByChild] Script Date: 6/28/2013 11:26:19 ******/
10262
10263-- =============================================
10264-- Author: Lisa Bohm (lbohm@paragondsi.com)
10265-- Create date: 2013-08-13
10266-- Description: count of available vessels for a Child
10267-- Modified: 2014-03-18 add expired but not disposed milk
10268-- =============================================
10269CREATE PROCEDURE [Nutrition].[uspAvailVesselCtByChild]
10270 -- Add the parameters for the stored procedure here
10271@pSysID bigint
10272
10273 AS
10274 BEGIN
10275 -- SET NOCOUNT ON added to prevent extra result sets from
10276 -- interfering with SELECT statements.
10277 SET NOCOUNT ON;
10278
10279SELECT numVessels = COUNT(DISTINCT NtV.vesselID)
10280FROM Nutrition.tVessel NtV
10281 INNER JOIN Nutrition.tVesselAssign NVA ON NtV.vesselID = NVA.vesselID
10282 INNER JOIN Nutrition.tVesselHistory NVH ON NtV.vesselID = NVH.vesselID
10283 INNER JOIN Nutrition.tStatusList NSL ON NVH.statusID = NSL.statusID
10284
10285WHERE NvH.isCurrent = 1
10286 AND NVA.pSysID = @pSysID
10287 AND NSL.statCatID >= 2
10288 AND NSL.statCatID <=3;
10289
10290 END;
10291')
10292
10293 PRINT N'USP Nutrition.uspAvailVesselCtByChild created'
10294
10295END TRY
10296BEGIN CATCH
10297 PRINT N'USP Nutrition.uspAvailVesselCtByChild create failed'
10298 PRINT 'Error Number: ' + str(error_number())
10299 PRINT 'Line Number: ' + str(error_line())
10300 PRINT error_message()
10301END CATCH
10302GO
10303
10304/* Nutrition.uspSetMotherVesselLost.storedProcedure.sql
10305To call the SP:
10306EXECUTE Nutrition.uspSetMotherVesselLost @pSysID=12, @userChange='admin';
10307 */
10308
10309BEGIN TRY
10310EXECUTE ('/****** Object: StoredProcedure [Nutrition].[uspSetMotherVesselLost] Script Date: 6/28/2013 11:26:19 ******/
10311
10312-- =============================================
10313-- Author: Lisa Bohm (lbohm@paragondsi.com)
10314-- Create date: 2013-08-13
10315-- Description: set available vessels lost for a mother.
10316-- Modified: 2014-02-12 to also set lost child assigned donor milk/formula
10317-- 2014-03-18 add expired but not disposed milk
10318-- =============================================
10319CREATE PROCEDURE [Nutrition].[uspSetMotherVesselLost]
10320 -- Add the parameters for the stored procedure here
10321@pSysID bigint
10322, @userChange varchar(25)
10323
10324 AS
10325DECLARE @contents TABLE (oVessel bigint, tgtVessel bigint, statCatID tinyint, expirDate datetime);
10326
10327 BEGIN
10328 -- SET NOCOUNT ON added to prevent extra result sets from
10329 -- interfering with SELECT statements.
10330 SET NOCOUNT ON;
10331
10332
10333WITH cteContents AS
10334( -- anchor
10335SELECT oVessel = NtV.vesselID
10336 , tgtVessel = NtV.vesselID
10337 , NSL.statCatID
10338 , NvH.expirDate
10339
10340
10341FROM Nutrition.tVessel NtV
10342 LEFT OUTER JOIN Nutrition.tMotherVessel NMV ON NtV.vesselID = NMV.vesselID
10343 INNER JOIN Nutrition.tVesselHistory NVH ON NtV.vesselID = NVH.vesselID
10344 INNER JOIN Nutrition.tStatusList NSL ON NVH.statusID = NSL.statusID
10345
10346WHERE NvH.isCurrent = 1
10347 AND NMV.pSysID = @pSysID
10348
10349UNION ALL
10350 SELECT oVessel = cte.oVessel
10351 , tgtVessel = NVOl.targetVesselID
10352 , NSLl.statCatID
10353 , NvHl.expirDate
10354
10355 FROM Nutrition.tVesselOrigin NVOl
10356 INNER JOIN cteContents AS cte ON cte.tgtVessel = NVOl.vesselID
10357 INNER JOIN Nutrition.tVesselHistory NVHl ON NVOl.targetvesselID = NVHl.vesselID
10358 INNER JOIN Nutrition.tStatusList NSLl ON NVHl.statusID = NSLl.statusID
10359WHERE NVHl.isCurrent = 1
10360)
10361
10362INSERT INTO @contents (oVessel, tgtVessel, statCatID, expirDate)
10363SELECT cte.oVessel
10364 , cte.tgtVessel
10365 , cte.statCatID
10366 , cte.expirDate
10367FROM cteContents cte
10368WHERE cte.statCatID >= 2
10369 AND cte.statCatID <= 3;
10370
10371/* what about other containers that have been assigned to any child of the mother? */
10372INSERT INTO @contents (oVessel, tgtVessel, statCatID, expirDate)
10373
10374SELECT NtV.vesselID
10375 , NtV.vesselID
10376 , NSL.statCatID
10377 , NVH.expirDate
10378
10379FROM Nutrition.tVessel NtV
10380 INNER JOIN Nutrition.tVesselAssign NVA ON NtV.vesselID = NVA.vesselID
10381 INNER JOIN PtInfo.tChildLink PCL ON NVA.pSysID = PCL.pSysID
10382 INNER JOIN Nutrition.tVesselHistory NVH ON NtV.vesselID = NVH.vesselID
10383 INNER JOIN Nutrition.tStatusList NSL ON NVH.statusID = NSL.statusID
10384
10385 WHERE NSL.statCatID >= 2
10386 AND NSL.statCatID <= 3
10387 AND NVH.isCurrent = 1
10388 AND PCL.motherSysID = @pSysID
10389 AND NOT EXISTS (SELECT CT.tgtVessel
10390 FROM @contents CT
10391 WHERE CT.tgtVessel = NtV.vesselID);
10392
10393/* now that we got ''em, we need to set the current isCurrent to zero */
10394
10395UPDATE NTVH
10396SET NTVH.isCurrent = 0
10397FROM Nutrition.tVesselHistory NtVH
10398 INNER JOIN @contents cte ON NtVH.vesselID = cte.tgtVessel
10399WHERE NtVH.isCurrent = 1
10400
10401/* wait wait WAIT! move to disposed location */
10402
10403UPDATE NtV
10404SET NtV.locationID = 2
10405FROM Nutrition.tVessel NtV
10406INNER JOIN @contents cte ON NtV.vesselID = cte.tgtVessel
10407WHERE cte.statCatID >= 2
10408 AND cte.statCatID <= 3;
10409
10410/* add new "lost" status */
10411INSERT INTO Nutrition.tVesselHistory
10412 (vesselID, statusID, dateChange, userChange, expirDate, isCurrent)
10413
10414SELECT cte.tgtVessel
10415 , 11
10416 , getdate()
10417 , @userChange
10418 , cte.expirDate
10419 , 1
10420
10421FROM @contents cte
10422WHERE cte.statCatID >= 2
10423 AND cte.statCatID <= 3;
10424
10425
10426 END;
10427')
10428
10429 PRINT N'USP Nutrition.uspSetMotherVesselLost created'
10430
10431END TRY
10432BEGIN CATCH
10433 PRINT N'USP Nutrition.uspSetMotherVesselLost create failed'
10434 PRINT 'Error Number: ' + str(error_number())
10435 PRINT 'Line Number: ' + str(error_line())
10436 PRINT error_message()
10437END CATCH
10438GO
10439
10440/* Nutrition.uspSetChildVesselLost.storedProcedure.sql
10441To call the SP:
10442EXECUTE Nutrition.uspSetChildVesselLost @pSysID=4, @userChange='admin';
10443 */
10444
10445BEGIN TRY
10446EXECUTE ('/****** Object: StoredProcedure [Nutrition].[uspSetChildVesselLost] Script Date: 6/28/2013 11:26:19 ******/
10447
10448-- =============================================
10449-- Author: Lisa Bohm (lbohm@paragondsi.com)
10450-- Create date: 2013-08-13
10451-- Description: set available vessels lost for a mother.
10452--
10453-- =============================================
10454CREATE PROCEDURE [Nutrition].[uspSetChildVesselLost]
10455 -- Add the parameters for the stored procedure here
10456@pSysID bigint
10457, @userChange varchar(25)
10458
10459 AS
10460DECLARE @contents TABLE (vesselID bigint, statCatID tinyint, expirDate datetime);
10461
10462 BEGIN
10463 -- SET NOCOUNT ON added to prevent extra result sets from
10464 -- interfering with SELECT statements.
10465 SET NOCOUNT ON;
10466
10467INSERT INTO @contents (vesselID, statCatID, expirDate)
10468SELECT NtV.vesselID
10469 , NSL.statCatID
10470 , NVH.expirDate
10471
10472FROM Nutrition.tVessel NtV
10473 INNER JOIN Nutrition.tVesselAssign NVA ON NtV.vesselID = NVA.vesselID
10474 INNER JOIN Nutrition.tVesselHistory NVH ON NtV.vesselID = NVH.vesselID
10475 INNER JOIN Nutrition.tStatusList NSL ON NVH.statusID = NSL.statusID
10476WHERE NvH.isCurrent = 1
10477 AND NVA.pSysID = @pSysID
10478 AND NSL.statCatID = 2;
10479
10480/* now that we got ''em, we need to set the current isCurrent to zero */
10481UPDATE NTVH
10482SET NTVH.isCurrent = 0
10483FROM Nutrition.tVesselHistory NtVH
10484 INNER JOIN @contents cte ON NtVH.vesselID = cte.vesselID
10485WHERE NtVH.isCurrent = 1
10486
10487/* wait wait WAIT! move to disposed location */
10488
10489UPDATE NtV
10490SET NtV.locationID = 2
10491FROM Nutrition.tVessel NtV
10492INNER JOIN @contents cte ON NtV.vesselID = cte.vesselID;
10493
10494/* add new "lost" status */
10495INSERT INTO Nutrition.tVesselHistory
10496 (vesselID, statusID, dateChange, userChange, expirDate, isCurrent)
10497
10498SELECT cte.vesselID
10499 , 11
10500 , getdate()
10501 , @userChange
10502 , cte.expirDate
10503 , 1
10504
10505FROM @contents cte;
10506
10507END;
10508')
10509
10510 PRINT N'USP Nutrition.uspSetChildVesselLost created'
10511
10512END TRY
10513BEGIN CATCH
10514 PRINT N'USP Nutrition.uspSetChildVesselLost create failed'
10515 PRINT 'Error Number: ' + str(error_number())
10516 PRINT 'Line Number: ' + str(error_line())
10517 PRINT error_message()
10518END CATCH
10519GO
10520
10521/* PTinfo.uspEditPatientStatus.storedProcedure.sql
10522To call the SP:
10523EXECUTE PTinfo.uspEditPatientStatus @pSysID=12, @pStatTypeID=1, @username='admin';
10524 */
10525
10526BEGIN TRY
10527EXECUTE ('/****** Object: StoredProcedure [PTinfo].[uspEditPatientStatus] Script Date: 8/15/2013 11:26:19 ******/
10528
10529-- =============================================
10530-- Author: Lisa Bohm (lbohm@paragondsi.com)
10531-- Create date: 2013-08-15
10532-- Description: Edits patient status
10533-- Modified: 2013-12-05 add audit tracking call
10534-- =============================================
10535CREATE PROCEDURE [PTinfo].[uspEditPatientStatus]
10536 -- Add the parameters for the stored procedure here
10537@pSysID bigint
10538, @pStatTypeID tinyint
10539, @username varchar(50)
10540
10541AS
10542DECLARE @mother bit
10543 , @errCode tinyint
10544 , @theNewID bigint;
10545
10546DECLARE @bins TABLE (locationID bigint);
10547
10548BEGIN
10549 -- SET NOCOUNT ON added to prevent extra result sets from
10550 -- interfering with SELECT statements.
10551 SET NOCOUNT ON;
10552
10553BEGIN TRY
10554BEGIN TRANSACTION
10555
10556/* if this is the discharged status */
10557IF @pStatTypeID = 2
10558 BEGIN
10559 SET @mother = COALESCE((SELECT MAX(1)
10560 FROM PTinfo.tMomInfo PMI
10561 WHERE PMI.pSysID = @pSysID),0);
10562
10563/* if this is a MOTHER to move to the discharged status... */
10564 IF @mother = 1
10565 BEGIN
10566 /* delete all assigned bins, but just unassign all fridges/freezers */
10567
10568 INSERT INTO @bins
10569 (locationID)
10570 SELECT SLA.locationID
10571 FROM Storage.tLocationAssign SLA
10572 INNER JOIN Storage.tLocation STL ON STL.locationID = SLA.locationID
10573 WHERE SLA.pSysID = @pSysID
10574 AND STL.locTypeID = 4;
10575
10576 /* audit info for assignment delete */
10577 INSERT INTO SysInfo.tDataChange
10578 (viewDate, userName, auditTypeID, tableName, recordID, previousEntry)
10579 SELECT getdate()
10580 , @username
10581 , 11
10582 , ''Storage.tLocationAssign''
10583 , SLA.locAssignID
10584 , CAST(SLA.locationID AS varchar) + ''; '' + CAST(SLA.pSysID AS VARCHAR) + ''.''
10585 FROM Storage.tlocationAssign SLA
10586 WHERE pSysID = @pSysID;
10587
10588 DELETE
10589 FROM Storage.tlocationAssign
10590 WHERE pSysID = @pSysID;
10591
10592 /* audit info for bin delete */
10593 INSERT INTO SysInfo.tDataChange
10594 (viewDate, userName, auditTypeID, tableName, recordID, previousEntry)
10595 SELECT getdate()
10596 , @username
10597 , 11
10598 , ''Storage.tLocation''
10599 , StL.locationID
10600 , StL.locationName + ''; '' + StL.locationCode + ''; '' + CAST(StL.locatedInID AS VARCHAR) + ''; '' + CAST(locTypeID AS VARCHAR) + ''.''
10601 FROM Storage.tLocation STL
10602 INNER JOIN @bins BN ON STL.locationID = BN.locationID
10603 WHERE STL.locationID = Bn.locationID;
10604
10605 DELETE STL
10606 FROM Storage.tLocation STL
10607 INNER JOIN @bins BN ON STL.locationID = BN.locationID
10608 WHERE STL.locationID = Bn.locationID;
10609
10610 /* set all children to discharged - first set current pStatType record to not iscurrent */
10611
10612 /* audit info for status update */
10613 INSERT INTO SysInfo.tDataChange
10614 (viewDate, userName, auditTypeID, tableName, recordID, previousEntry)
10615 SELECT getdate()
10616 , @username
10617 , 10
10618 , ''PTinfo.tStatus''
10619 , PTS.pStatID
10620 , ''isCurrent = 1''
10621 FROM PTinfo.tStatus PTS
10622 INNER JOIN PtInfo.tChildLink PCL ON PTS.pSysID = PCL.pSysID
10623 WHERE PCL.motherSysID = @pSysID
10624 AND PTS.isCurrent = 1;
10625
10626 UPDATE PTS
10627 SET PTS.isCurrent = 0
10628 FROM PTinfo.tStatus PTS
10629 INNER JOIN PtInfo.tChildLink PCL ON PTS.pSysID = PCL.pSysID
10630 WHERE PCL.motherSysID = @pSysID
10631 AND PTS.isCurrent = 1;
10632
10633 /* add the discharged status for the kiddos */
10634 INSERT INTO PTInfo.tStatus
10635 (pSysID, pStatTypeID, statDate, isCurrent)
10636 SELECT
10637 PCL.pSysID
10638 , @pStatTypeID
10639 , getdate()
10640 , 1
10641 FROM PTinfo.tChildLink PCL
10642 WHERE PCL.motherSysID = @pSysID;
10643
10644 INSERT INTO SysInfo.tDataChange
10645 (viewDate, userName, auditTypeID, tableName, recordID, previousEntry)
10646 SELECT getdate()
10647 , @username
10648 , 9
10649 , ''PTinfo.tStatus''
10650 , PTS.pStatID
10651 , ''N/A''
10652 FROM PTinfo.tStatus PTS
10653 INNER JOIN PtInfo.tChildLink PCL ON PTS.pSysID = PCL.pSysID
10654 WHERE PCL.motherSysID = @pSysID
10655 AND PTS.isCurrent = 1;
10656 END
10657
10658 END
10659
10660/* okay just change the status on everyone else. First set current status as not current */
10661 SET @theNewID = COALESCE((SELECT COUNT(PtS.pStatID)
10662 FROM PTinfo.tStatus PtS
10663 WHERE pSysID = @pSysID
10664 AND isCurrent = 1),0);
10665
10666 IF @theNewID > 0
10667 BEGIN
10668 UPDATE PTinfo.tStatus
10669 SET isCurrent = 0
10670 WHERE pSysID = @pSysID
10671 AND isCurrent = 1;
10672
10673 EXECUTE Sysinfo.uspAddDataChange @username=@username, @auditTypeID=10, @tableName=''PTinfo.tStatus'', @recordID=@theNewID, @previousEntry=''isCurrent=1'';
10674 END
10675
10676 INSERT INTO PTInfo.tStatus
10677 (pSysID, pStatTypeID, statDate, isCurrent)
10678 VALUES
10679 (@pSysID
10680 , @pStatTypeID
10681 , getdate()
10682 , 1);
10683
10684 SET @theNewID = SCOPE_IDENTITY();
10685
10686 EXECUTE Sysinfo.uspAddDataChange @username=@username, @auditTypeID=9, @tableName=''PTinfo.tStatus'', @recordID=@theNewID, @previousEntry=''N/A'';
10687
10688SET @errCode = 0;
10689SELECT errCode = @errCode;
10690
10691COMMIT TRANSACTION
10692END TRY
10693BEGIN CATCH
10694 SET @errCode = 1;
10695 SELECT errCode = @errCode;
10696 ROLLBACK TRANSACTION
10697END CATCH
10698
10699END;
10700')
10701
10702 PRINT N'USP PTinfo.uspEditPatientStatus created'
10703
10704END TRY
10705BEGIN CATCH
10706 PRINT N'USP PTinfo.uspEditPatientStatus create failed'
10707 PRINT 'Error Number: ' + str(error_number())
10708 PRINT 'Line Number: ' + str(error_line())
10709 PRINT error_message()
10710END CATCH
10711GO
10712
10713/* Storage.uspDeleteLocation.storedProcedure.sql
10714To call the SP:
10715EXECUTE Storage.uspDeleteLocation @locationID=3, @username='admin';
10716 */
10717
10718BEGIN TRY
10719EXECUTE ('/****** Object: StoredProcedure [Storage].[uspDeleteLocation] Script Date: 8/20/2013 11:26:19 ******/
10720
10721-- =============================================
10722-- Author: Lisa Bohm (lbohm@paragondsi.com)
10723-- Create date: 2013-08-20
10724-- Description: delete after checking locations
10725-- Modified: 2013-12-09 add audit tracking call
10726-- =============================================
10727CREATE PROCEDURE [Storage].[uspDeleteLocation]
10728 -- Add the parameters for the stored procedure here
10729@locationID bigint
10730, @username varchar(50)
10731
10732AS
10733DECLARE @errCode tinyint
10734 , @gotMilk smallint
10735 , @prevEntry varchar(1000)
10736 , @theNewID bigint;
10737
10738BEGIN
10739 -- SET NOCOUNT ON added to prevent extra result sets from
10740 -- interfering with SELECT statements.
10741 SET NOCOUNT ON;
10742
10743/* okay, remove location. Don''t allow the three initial locationIDs to be removed */
10744IF @locationID > 2
10745 BEGIN
10746/* now, for that recursion thing - what''s all in these locations? */
10747 DECLARE @contents TABLE (origLocID bigint
10748 , locationID bigint
10749 , locationCode varchar(20)
10750);
10751
10752WITH cteLocTree AS
10753 (-- the anchor part now is just for the original bottle
10754 SELECT origLocID = STL.locationID
10755 , parentLoc = STL.locatedInID
10756 , locCode = StL.locationCode
10757-- , hLevel = 1
10758
10759 FROM Storage.tLocation STL
10760 WHERE STL.locationID = @locationID
10761
10762 /* the recursive part */
10763 UNION ALL
10764
10765 SELECT origLocID = STLo.locationID
10766 , parentLoc = STLo.locatedInID
10767 , locCode = STLo.locationCode
10768-- , hLevel = cte.hLevel + 1
10769
10770 FROM Storage.tLocation STLo
10771 INNER JOIN cteLocTree cte ON cte.origLocID = STLo.locatedInID
10772 )
10773
10774 INSERT INTO @contents
10775 (origLocID
10776 , locationID
10777 , locationCode)
10778
10779 SELECT @locationID
10780 , CLT.origLocID
10781 , CLT.locCode
10782
10783 FROM ctelocTree CLT;
10784
10785
10786 SELECT @gotMilk = COALESCE(COUNT(DISTINCT NtV.vesselID), 0)
10787 FROM @contents CLT
10788 LEFT OUTER JOIN Nutrition.tVessel NTV ON CLT.locationID = NTV.locationID
10789 ;
10790
10791 IF @gotMilk > 0
10792 BEGIN
10793 SET @errCode = 6
10794 SELECT errCode = @errCode;
10795 END
10796 ELSE
10797 BEGIN
10798 /* delete location and all within */
10799 /* audit info */
10800 SELECT @theNewID = SLA.locAssignID
10801 , @prevEntry = CAST(SLA.locationID AS VARCHAR) + ''; '' + CAST(SLA.pSysID AS VARCHAR) + ''.''
10802 FROM Storage.tLocationAssign SLA
10803 WHERE locationID = @locationID;
10804
10805 DELETE SLA
10806 FROM Storage.tLocationAssign SLA
10807 INNER JOIN @contents CT ON SLA.locationID = CT.locationID;
10808
10809 EXECUTE Sysinfo.uspAddDataChange @username=@username, @auditTypeID=11, @tableName=''Storage.tLocationAssign'', @recordID=@theNewID, @previousEntry=@prevEntry;
10810
10811 SELECT @prevEntry = StL.locationName + ''; '' + StL.locationCode + ''; '' + CAST(StL.locatedInID AS VARCHAR) + ''; '' + CAST(StL.locTypeID AS VARCHAR) + ''.''
10812 FROM Storage.tLocation StL
10813 WHERE StL.locationID = @locationID;
10814
10815 DELETE SLD
10816 FROM Storage.tLocation SLD
10817 INNER JOIN @contents CT ON SLD.locationID = CT.locationID;
10818
10819 EXECUTE Sysinfo.uspAddDataChange @username=@username, @auditTypeID=11, @tableName=''Storage.tLocation'', @recordID=@locationID, @previousEntry=@prevEntry;
10820
10821 SET @errCode = 0
10822 SELECT errCode = @errCode;
10823
10824 END
10825 END
10826
10827
10828ELSE
10829 BEGIN
10830 SET @errCode = 7
10831 SELECT errCode = @errCode;
10832 END
10833
10834END ;
10835')
10836
10837 PRINT N'USP Storage.uspDeleteLocation created'
10838
10839END TRY
10840BEGIN CATCH
10841 PRINT N'USP Storage.uspDeleteLocation create failed'
10842 PRINT 'Error Number: ' + str(error_number())
10843 PRINT 'Line Number: ' + str(error_line())
10844 PRINT error_message()
10845END CATCH
10846GO
10847
10848/* Nutrition.uspMoveMilk.storedProcedure.sql
10849To call the SP:
10850EXECUTE Nutrition.uspMoveMilk @vesselID=123, @newlocCode='L0005', @userChange='admin';
10851 */
10852
10853BEGIN TRY
10854EXECUTE ('/****** Object: StoredProcedure [Nutrition].[uspMoveMilk] Script Date: 8/21/2013 11:26:19 ******/
10855
10856-- =============================================
10857-- Author: Lisa Bohm (lbohm@paragondsi.com)
10858-- Create date: 2013-08-21
10859-- Description: Move milk to another location
10860-- Modified: 2013-12-09 add audit tracking call
10861-- =============================================
10862CREATE PROCEDURE [Nutrition].[uspMoveMilk]
10863 -- Add the parameters for the stored procedure here
10864 @vesselID bigint
10865 , @newlocCode varchar(20)
10866 , @userChange varchar(20)
10867 , @dateChange datetime = ''1900-01-01''
10868
10869/* move milk. So we need a few key facts:
10870-- Like, is autoRec on?
10871-- what is the current status of the vessel?
10872-- what is IN the vessel?
10873-- Where is the vessel currently?
10874-- what type of location are we going to put the vessel in?
10875-- who is that location assigned to?
10876-- is the VESSEL assigned?
10877-- what are our outcome choices?
10878-- autoreceive
10879-- error message
10880-- move the milk
10881
10882 */
10883 AS
10884 DECLARE @autoReceive bit
10885 , @motherSysID bigint
10886 , @motherStatus tinyint
10887 , @currStatus tinyint
10888 , @currStatusRank tinyint
10889 , @currChillType tinyint
10890 , @newLocAssign bigint
10891 , @newChillType tinyint
10892 , @newStatus tinyint
10893 , @assignedToSysID bigint
10894 , @already smallint
10895 , @errCode tinyint
10896 , @expHrs smallint
10897 , @donor tinyint
10898 , @formula tinyint
10899 , @childDonor tinyint
10900 , @childFormula tinyint
10901 , @expRecd bit
10902 , @prevEntry varchar(1000);
10903
10904IF @dateChange = ''1900-01-01''
10905 BEGIN
10906 SET @dateChange = getdate()
10907 END
10908
10909BEGIN TRY
10910BEGIN TRANSACTION
10911 -- SET NOCOUNT ON added to prevent extra result sets from
10912 -- interfering with SELECT statements.
10913 SET NOCOUNT ON;
10914
10915/* get contents and mother of discharged */
10916DECLARE @contents TABLE (oVID bigint
10917 , vesselID bigint
10918 , motherSysID bigint
10919 , otherType varchar(7)
10920 , motherStatus tinyint)
10921
10922SET @already = COALESCE((SELECT COUNT(NVO.vesselOriginID)
10923 FROM Nutrition.tVesselOrigin NVO
10924 WHERE NVO.targetVesselID = @vesselID), 0)
10925
10926IF @already = 0
10927 BEGIN
10928 INSERT INTO @contents
10929 (oVID
10930 , vesselID
10931 , motherSysID
10932 , otherType
10933 , motherStatus)
10934
10935 SELECT NTVi.vesselID
10936 , NTVi.vesselID
10937 , COALESCE(NMV.pSysID,0)
10938 , CASE WHEN COALESCE(NDV.lotID,0) > 0 THEN 2 ELSE
10939 CASE WHEN COALESCE(NFV.formLotID,0) > 0 THEN 3 ELSE 0 END
10940 END
10941 , COALESCE(PtS.pStatTypeID, 0)
10942
10943
10944 FROM Nutrition.tVessel NTVi
10945 LEFT OUTER JOIN (Nutrition.tMotherVessel NMV
10946 INNER JOIN PTinfo.tStatus PtS ON NMV.pSysID = PtS.pSysID
10947 ) ON NtVI.vesselID = NMV.vesselID
10948 AND PtS.isCurrent = 1
10949 LEFT OUTER JOIn Nutrition.tDonorVessel NDV ON NTVi.vesselID = NDV.vesselID
10950 LEFT OUTER JOIn Nutrition.tFormulaVessel NFV ON NTVi.vesselID = NFV.vesselID
10951
10952 WHERE NtVi.vesselID = @vesselID;
10953 END
10954ELSE
10955 BEGIN
10956
10957 WITH cteBuildPath AS
10958 (-- the anchor part now is just for the original bottle
10959 SELECT oVessel = NVOr.vesselID
10960 , tgtVessel = NVOr.targetVesselID
10961 , hLevel = 1
10962
10963 FROM Nutrition.tVesselOrigin NVOr
10964 WHERE NVOr.targetVesselID = @vesselID
10965
10966 /* the recursive part */
10967 UNION ALL
10968
10969 SELECT oVessel = NVOl.vesselID
10970 , tgtVessel = NVOl.targetVesselID
10971 , hLevel = cte.hLevel + 1
10972
10973 FROM Nutrition.tVesselOrigin NVOl
10974 INNER JOIN cteBuildPath AS cte ON cte.oVessel = NVOl.targetVesselID
10975 )
10976
10977
10978 INSERT INTO @contents
10979 (oVID
10980 , vesselID
10981 , motherSysID
10982 , otherType
10983 , motherStatus)
10984
10985 SELECT @vesselID
10986 , NTVo.vesselID
10987 , COALESCE(NMV.pSysID,0)
10988 , CASE WHEN COALESCE(NDV.lotID,0) > 0 THEN 2 ELSE
10989 CASE WHEN COALESCE(NFV.formLotID,0) > 0 THEN 3 ELSE 0 END
10990 END
10991 , COALESCE(PtS.pStatTypeID, 0)
10992
10993
10994 FROM cteBuildPath RTv
10995 LEFT OUTER JOIN Nutrition.tVesselOrigin NVO ON RTv.oVessel = NVO.targetVesselID
10996 INNER JOIN Nutrition.tVessel NTVo ON RTv.oVessel = NTVo.vesselID
10997 LEFT OUTER JOIN (Nutrition.tMotherVessel NMV
10998 INNER JOIN PTinfo.tStatus PtS ON NMV.pSysID = PtS.pSysID
10999 ) ON NTVo.vesselID = NMV.vesselID
11000 AND PtS.isCurrent = 1
11001 LEFT OUTER JOIn Nutrition.tDonorVessel NDV ON NTVo.vesselID = NDV.vesselID
11002 LEFT OUTER JOIn Nutrition.tFormulaVessel NFV ON NTVo.vesselID = NFV.vesselID
11003
11004 WHERE NVO.vesselID IS NULL;
11005 END
11006
11007SELECT @motherStatus = COALESCE(CT.motherStatus, 0)
11008 , @motherSysID = COALESCE(CT.motherSysID, 0)
11009FROM @contents CT
11010WHERE CT.motherSysID > 0;
11011
11012SELECT @donor = MAX(CASE WHEN otherType = 2 THEN 1 ELSE 0 END)
11013 , @formula = MAX(CASE WHEN otherType = 3 THEN 1 ELSE 0 END)
11014FROM @contents CT;
11015
11016/* set default error code to not have an entirely horribly heinous flow */
11017SET @errCode = 0;
11018
11019IF @motherStatus = 2
11020 BEGIN
11021 SET @errCode = 10
11022 END
11023
11024--SELECT errCode = @errCode;
11025
11026IF @errCode = 0 /* no known errors ...yet */
11027BEGIN
11028/* is autoreceive on? */
11029 SELECT @autoReceive = CASE WHEN STS.setValue = ''false'' then 0 ELSE 1 END
11030 FROM SysInfo.tSetting STS
11031 WHERE STS.settingID = 10;
11032
11033/* get the current location, current status ID/rank of vessel */
11034 SELECT @assignedToSysID = COALESCE(NVA.pSysID, 0)
11035 , @currChillType = CASE WHEN NVH.statusID = 3 THEN 3 ELSE 2 END
11036 , @currStatus = NVH.statusID
11037 , @currStatusRank = NSL.statRank
11038
11039 FROM Nutrition.tVessel NtV
11040 INNER JOIN Nutrition.tVesselHistory NVH ON NtV.vesselID = NVH.vesselID
11041 INNER JOIN Nutrition.tStatusList NSL ON NVH.statusID = NSL.statusID
11042 LEFT OUTER JOIn Nutrition.tVesselAssign NVA ON NtV.vesselID = NVA.vesselID
11043 LEFT OUTER JOIn (Storage.tLocation STL
11044 LEFT OUTER JOIn Storage.tLocation BPT ON StL.locatedInID = BPT.locationID
11045 ) ON NtV.locationID = STL.locationID
11046 WHERE NtV.vesselID = @vesselID
11047 AND NVH.isCurrent = 1;
11048
11049 SELECT @newChillType = CASE WHEN STL.locTypeID < 4 THEN STL.locTypeID ELSE COALESCE(BPT.locTypeID, 0) END
11050 FROM Storage.tLocation STL
11051 LEFT OUTER JOIn Storage.tLocation BPT ON StL.locatedInID = BPT.locationID
11052 WHERE STL.locationCode = @newLocCode;
11053
11054IF @newChillType = 1
11055 BEGIN
11056 SET @errCode = 8
11057 END
11058END
11059--SELECT errCode = @errCode;
11060
11061
11062IF @errCode = 0 /* no known errors ...yet */
11063 BEGIN
11064 IF @autoReceive = 1 AND @currStatus = 1
11065 BEGIN
11066
11067 /* Start with the fresh setting for the received event */
11068 SELECT @expHrs = StS.setValue
11069 FROM SysInfo.tSetting StS
11070 WHERE STS.settingID = 19;
11071
11072 /* add received event - oh yeah, and pumped event */
11073
11074 SELECT @expRecd = CASE WHEN StS.setValue = ''False'' THEN 1 ELSE 0 END
11075 FROM SysInfo.tSetting StS
11076 WHERE STS.settingID = 22;
11077
11078 INSERT INTO Nutrition.tVesselHistory
11079 (vesselID
11080 , statusID
11081 , dateChange
11082 , userChange
11083 , expirDate
11084 , isCurrent)
11085 VALUES
11086 (@vesselID
11087 , 14
11088 , @dateChange
11089 , @userChange
11090 , DATEADD(hour, @expHrs, @dateChange)
11091 , 0);
11092
11093 INSERT INTO Nutrition.tVesselHistory
11094 (vesselID
11095 , statusID
11096 , dateChange
11097 , userChange
11098 , expirDate
11099 , isCurrent)
11100 VALUES
11101 (@vesselID
11102 , 17
11103 , getdate()
11104 , @userChange
11105 , DATEADD(hour, @expHrs, getdate())
11106 , 0);
11107
11108
11109 /* set not recd status to not current */
11110
11111 /* testing
11112 PRINT N''uncurrented not received status'';
11113 -- */
11114
11115 UPDATE Nutrition.tVesselHistory
11116 SET isCurrent = 0
11117 WHERE vesselID = @vesselID
11118 AND statusID = 1;
11119
11120 /* is this going to be fresh or frozen? what are the exp hours params for this? */
11121
11122 SELECT @expHrs = StS.setValue
11123 FROM Nutrition.tStatusExpir NSE
11124 INNER JOIN SysInfo.tSetting StS ON NSE.settingID = StS.settingID
11125 WHERE NSE.statusID = CASE WHEN @newChillType = 3 THEN 3 ELSE 2 END
11126 AND NSE.nCategoryID = 1;
11127
11128
11129 /* add fresh or frozen status */
11130 INSERT INTO Nutrition.tVesselHistory
11131 (vesselID
11132 , statusID
11133 , dateChange
11134 , userChange
11135 , expirDate
11136 , isCurrent)
11137 VALUES
11138 (@vesselID
11139 , CASE WHEN @newChillType = 3 THEN 3 ELSE 2 END
11140 , getdate()
11141 , @userChange
11142 , CASE WHEN @expRecd = 1 THEN DATEADD(hour, @expHrs, @dateChange) ELSE DATEADD(hour, @expHrs, getdate()) END
11143 , 1);
11144
11145
11146 SET @currStatus = CASE WHEN @newChillType = 3 THEN 3 ELSE 2 END;
11147 SET @currStatusRank = CASE WHEN @newChillType = 3 THEN 5 ELSE 6 END;
11148 SET @currChillType = @newChillType;
11149
11150 /* testing
11151 PRINT N''Added status of '' + CASE WHEN @newChillType = 3 THEN ''3'' ELSE ''2'' END + '' and expiration date of '' + CAST(DATEADD(hour, @expHrs, getdate()) AS varchar) + ''.'';
11152-- */
11153
11154 END
11155END
11156--SELECT errCode = @errCode;
11157
11158
11159IF @errCode = 0 /* no known errors ...yet */
11160 BEGIN
11161 IF @currStatusRank <=2 OR @currStatus = 1
11162 BEGIN
11163 SET @errCode = 9
11164 END
11165 END
11166--SELECT errCode = @errCode;
11167
11168IF @errCode = 0 /* no known errors ...yet */
11169 BEGIN /* check the storage type first perhaps.... YEAH.... */
11170 /* if milk is thawed or fortified it can''t be refrozen */
11171 IF @newChillType = 3 AND @currStatusRank < 5
11172 BEGIN
11173 SET @errCode = 4
11174 END
11175 END
11176--SELECT errCode = @errCode;
11177
11178IF @errCode = 0 /* no known errors ...yet */
11179 BEGIN
11180 SELECT @newLocAssign = COALESCE(pSysID, 0)
11181 FROM Storage.tLocation StL
11182 LEFT OUTER JOIN Storage.tLocationAssign SLA ON StL.locationID = SLA.locationID
11183 WHERE StL.locationCode = @newLocCode;
11184 /* location assigned? to someone else? */
11185 IF @newLocAssign > 0
11186 BEGIN
11187 /* if mother''s milk but bin assigned to diff mother */
11188 IF @motherSysID > 0 AND @motherSysID <> @newLocAssign
11189 BEGIN
11190 SET @errCode = 21;
11191 END
11192 ELSE
11193 BEGIN
11194 /* completely not mother''s milk. Check to see if assigned */
11195 IF @assignedToSysID > 0
11196 /* is the assigned to child the child of this mother to whom the bin is assigned to? If already assigned, have to assume the child can have this milk. */
11197 BEGIN
11198 SET @already = COALESCE((SELECT COUNT(PCL.childLinkID)
11199 FROM PtInfo.tChildLink PCL
11200 WHERE PCL.pSysID = @assignedToSysID
11201 AND PCL.motherSysID = @newLocAssign),0);
11202 IF @already = 0
11203 BEGIN
11204 SET @errCode = 21
11205 END
11206 END
11207 END
11208 END
11209 END
11210
11211--SELECT errCode = @errCode;
11212
11213
11214IF @errCode = 0 /* no known errors ...yet */
11215 BEGIN
11216 IF @newLocAssign > 0 /* back to the bin being assigned. This should just have flowed to bottles that are not assigned. But we can always check. */
11217 BEGIN
11218 IF @assignedToSysID = 0
11219 BEGIN
11220 /* if donor or formula, can child have donor or formula? OK. Need to find the child. */
11221 SELECT @childFormula = MAX(CASE WHEN PCL.allowFormula = 0 THEN 0 ELSE 1 END)
11222 , @childDonor = MAX(CASE WHEN PDO.orderNum IS NOT NULL THEN 1 ELSE 0 END)
11223 FROM PTinfo.tChildLink PCL
11224 LEFT OUTER JOIN PTinfo.tDocOrder PDO ON PCL.pSysID = PDO.pSysID
11225 AND PDO.orderEnd > getdate()
11226 AND PDO.orderTypeID = 1
11227 WHERE PCL.motherSysID = @newLocAssign;
11228
11229 IF @childFormula = 0 AND @formula = 1
11230 BEGIN
11231 SET @errCode = 15;
11232 END
11233 ELSE IF @childDonor = 0 and @donor = 1
11234 BEGIN
11235 SET @errCode = 14;
11236 END
11237 END
11238 END
11239 END
11240--SELECT errCode = @errCode;
11241
11242/* okay, I don''t care what other errors. If it''s moving to either of these places, do it anyway. */
11243IF @newLocCode = ''L002'' OR @newLocCode = ''L003''
11244 BEGIN
11245 SET @errCode = 0
11246 END
11247
11248IF @errCode = 0 /* no known errors ...yet */
11249 BEGIN
11250 /* okay, mooooove the milk. Do the stuff. */
11251 /* status change? */
11252 IF @currChillType = 3 AND @newChillType < 3
11253 BEGIN
11254 /* add thawed status */
11255 EXECUTE Nutrition.uspStatusChange @vesselID
11256 , 4
11257 , @userChange;
11258
11259 END
11260 ELSE IF @currChillType < 3 and @newChillType = 3
11261 BEGIN
11262 /* add frozen status */
11263 EXECUTE Nutrition.uspStatusChange @vesselID
11264 , 3
11265 , @userChange;
11266 END
11267 /* add the new location */
11268 /* auditing info */
11269 SELECT @prevEntry = ''Location: '' + CAST(NtV.locationID AS VARCHAR) + ''.''
11270 FROM Nutrition.tVessel NtV
11271 WHERE NtV.vesselID = @vesselID;
11272
11273 UPDATE Nutrition.tVessel
11274 SET locationID = (SELECT sTL.locationID
11275 FROM Storage.tLocation STL
11276 WHERE STL.locationCode = @newlocCode)
11277 WHERE vesselID = @vesselID;
11278
11279 EXECUTE Sysinfo.uspAddDataChange @username=@userChange, @auditTypeID=10, @tableName=''Nutrition.tVessel'', @recordID=@vesselID, @previousEntry=@prevEntry;
11280
11281 END
11282
11283SELECT errCode = @errCode;
11284
11285
11286COMMIT TRANSACTION
11287END TRY
11288BEGIN CATCH
11289ROLLBACK TRANSACTION
11290 SET @errCode = 1
11291 SELECT errCode = @errCode;
11292
11293END CATCH;
11294')
11295
11296 PRINT N'USP Nutrition.uspMoveMilk created'
11297
11298END TRY
11299BEGIN CATCH
11300 PRINT N'USP Nutrition.uspMoveMilk create failed'
11301 PRINT 'Error Number: ' + str(error_number())
11302 PRINT 'Line Number: ' + str(error_line())
11303 PRINT error_message()
11304END CATCH
11305GO
11306
11307
11308/* Storage.uspVesselNotStored.storedProcedure.sql
11309To call the SP:
11310EXECUTE Storage.uspVesselNotStored;
11311 */
11312
11313BEGIN TRY
11314EXECUTE ('/****** Object: StoredProcedure [Storage].[uspVesselNotStored] Script Date: 8/22/2013 11:26:19 ******/
11315
11316-- =============================================
11317-- Author: Lisa Bohm (lbohm@paragondsi.com)
11318-- Create date: 2013-08-22
11319-- Description: list of all "not stored" vessels
11320--
11321-- =============================================
11322CREATE PROCEDURE [Storage].[uspVesselNotStored]
11323 -- Add the parameters for the stored procedure here
11324
11325AS
11326BEGIN
11327 -- SET NOCOUNT ON added to prevent extra result sets from
11328 -- interfering with SELECT statements.
11329 SET NOCOUNT ON;
11330
11331SELECT TOP 250 NtV.vesselID
11332 , NtV.vesselCode
11333 , NSL.statusName
11334 , dateRecd = NVRc.dateChange
11335
11336FROM Nutrition.tVessel NtV
11337 INNER JOIn Nutrition.tVesselHistory NVH ON NtV.vesselID = NVH.vesselID
11338 INNER JOIn Nutrition.tStatusList NSL ON NVH.statusID = NSL.statusID
11339 /* date received */
11340 LEFT OUTER JOIn Nutrition.tVesselHistory NVRc ON NtV.vesselID = NVRc.vesselID
11341 AND NVRc.statusID = 17
11342
11343WHERE NVH.isCurrent = 1
11344 AND NSL.statCatID = 2
11345 AND NtV.locationID < 10
11346
11347ORDER BY NVRc.dateChange DESC;
11348
11349END;
11350')
11351
11352 PRINT N'USP Storage.uspVesselNotStored created'
11353
11354END TRY
11355BEGIN CATCH
11356 PRINT N'USP Storage.uspVesselNotStored create failed'
11357 PRINT 'Error Number: ' + str(error_number())
11358 PRINT 'Line Number: ' + str(error_line())
11359 PRINT error_message()
11360END CATCH
11361GO
11362
11363/* Nutrition.uspInactiveAdditive.storedProcedure.sql
11364To call the SP:
11365EXECUTE Nutrition.uspInactiveAdditive @additiveID=1, @username='admin';
11366 */
11367
11368BEGIN TRY
11369EXECUTE ('/****** Object: StoredProcedure [Nutrition].[uspInactiveAdditive] Script Date: 8/27/2013 11:26:19 ******/
11370
11371-- =============================================
11372-- Author: Lisa Bohm (lbohm@paragondsi.com)
11373-- Create date: 2013-08-27
11374-- Description: inactivate additive.
11375-- Modified: 2013-12-05 add audit tracking call
11376-- =============================================
11377CREATE PROCEDURE [Nutrition].[uspInactiveAdditive]
11378 -- Add the parameters for the stored procedure here
11379 @additiveID smallint
11380 , @username varchar(50)
11381
11382 AS
11383 DECLARE @errCode tinyint;
11384 BEGIN
11385 -- SET NOCOUNT ON added to prevent extra result sets from
11386 -- interfering with SELECT statements.
11387 SET NOCOUNT ON;
11388
11389 UPDATE Nutrition.tAdditiveList
11390 SET isActive = 0
11391 WHERE additiveID = @additiveID;
11392
11393 EXECUTE Sysinfo.uspAddDataChange @username=@username, @auditTypeID=10, @tableName=''Nutrition.tAdditiveList'', @recordID=@additiveID, @previousEntry=''isActive=1'';
11394
11395 SET @errCode = 0
11396 SELECT errCode = @errCode;
11397
11398END;
11399')
11400
11401 PRINT N'USP Nutrition.uspInactiveAdditive created'
11402
11403END TRY
11404BEGIN CATCH
11405 PRINT N'USP Nutrition.uspInactiveAdditive create failed'
11406 PRINT 'Error Number: ' + str(error_number())
11407 PRINT 'Line Number: ' + str(error_line())
11408 PRINT error_message()
11409END CATCH
11410GO
11411
11412/* Nutrition.uspGetBankList.storedProcedure.sql
11413To call the SP:
11414EXECUTE Nutrition.uspGetBankList;
11415 */
11416
11417BEGIN TRY
11418EXECUTE ('/****** Object: StoredProcedure [Nutrition].[uspGetBankList] Script Date: 8/27/2013 11:26:19 ******/
11419
11420-- =============================================
11421-- Author: Lisa Bohm (lbohm@paragondsi.com)
11422-- Create date: 2013-08-27
11423-- Description: gets donor bank list
11424--
11425-- =============================================
11426CREATE PROCEDURE [Nutrition].[uspGetBankList]
11427 -- Add the parameters for the stored procedure here
11428 AS
11429
11430 BEGIN
11431 -- SET NOCOUNT ON added to prevent extra result sets from
11432 -- interfering with SELECT statements.
11433 SET NOCOUNT ON;
11434
11435SELECT NDB.donorBankID
11436 , NDB.bankName
11437
11438FROM Nutrition.tDonorBank NDB
11439
11440WHERE NDB.isActive = 1;
11441
11442END;
11443')
11444
11445 PRINT N'USP Nutrition.uspGetBankList created'
11446
11447END TRY
11448BEGIN CATCH
11449 PRINT N'USP Nutrition.uspGetBankList create failed'
11450 PRINT 'Error Number: ' + str(error_number())
11451 PRINT 'Line Number: ' + str(error_line())
11452 PRINT error_message()
11453END CATCH
11454GO
11455
11456/* Nutrition.uspAddEditDonorBank.storedProcedure.sql
11457To call the SP:
11458EXECUTE Nutrition.uspAddEditDonorBank @donorBankID=0, @bankName='Donor Bank Extraordinaire', @username='admin';
11459Please use @donorBankID=0 for new, actual @donorBankID for edit.
11460 */
11461
11462BEGIN TRY
11463EXECUTE ('/****** Object: StoredProcedure [Nutrition].[uspAddEditDonorBank] Script Date: 8/12/2013 11:26:19 ******/
11464
11465-- =============================================
11466-- Author: Lisa Bohm (lbohm@paragondsi.com)
11467-- Create date: 2013-08-27
11468-- Description: add donorBank to list.
11469-- Modified: 2013-12-05 add audit tracking call
11470-- =============================================
11471CREATE PROCEDURE [Nutrition].[uspAddEditDonorBank]
11472 -- Add the parameters for the stored procedure here
11473 @donorBankID tinyint
11474, @bankName varchar(50)
11475, @username varchar(50)
11476
11477 AS
11478 DECLARE @already smallint
11479 , @isActive bit
11480 , @errCode Tinyint
11481 , @theNewID smallint
11482 , @prevEntry varchar(1000);
11483
11484 BEGIN
11485 -- SET NOCOUNT ON added to prevent extra result sets from
11486 -- interfering with SELECT statements.
11487 SET NOCOUNT ON;
11488SET @already = COALESCE((SELECT NDB.donorBankID
11489 FROM Nutrition.tDonorBank NDB
11490 WHERE NDB.donorBankID <> @donorBankID
11491 AND NDB.bankName = @bankName),0)
11492IF @already > 0
11493 BEGIN
11494 /* if inactive, activate */
11495 SELECT @isActive = NDB.isActive
11496 FROM Nutrition.tDonorBank NDB
11497 WHERE NDB.donorBankID = @already;
11498
11499 IF @isActive = 0
11500 BEGIN
11501 UPDATE Nutrition.tDonorBank
11502 SET isActive = 1
11503 WHERE donorBankID = @already;
11504
11505 EXECUTE Sysinfo.uspAddDataChange @username=@username, @auditTypeID=10, @tableName=''Nutrition.tDonorBank'', @recordID=@already, @previousEntry=''isActive=0'';
11506
11507 SET @errCode = 0
11508 SELECT errCode = @errCode;
11509 END
11510 ELSE
11511 BEGIN
11512 SET @errCode = 20
11513 SELECT errCode = @errCode;
11514 END
11515 END
11516ELSE
11517 BEGIN
11518 IF @donorBankID = 0
11519 BEGIN
11520 INSERT INTO Nutrition.tDonorBank
11521 ([bankName]
11522 ,[isActive])
11523 VALUES
11524 (@bankName
11525 , 1);
11526
11527 SET @theNewID = SCOPE_IDENTITY();
11528
11529 EXECUTE Sysinfo.uspAddDataChange @username=@username, @auditTypeID=9, @tableName=''Nutrition.tDonorBank'', @recordID=@theNewID, @previousEntry=''N/A'';
11530
11531 SET @errCode = 0
11532 SELECT errCode = @errCode;
11533 END
11534 ELSE
11535 BEGIN
11536 /* set up audit previous record */
11537 SELECT @prevEntry = NDB.bankName + ''; '' + CAST(NDB.isActive AS VARCHAR) + ''.''
11538 FROM Nutrition.tDonorBank NDB
11539 WHERE NDB.donorBankID = @donorBankID;
11540
11541 UPDATE Nutrition.tDonorBank
11542 SET bankName = @bankName
11543 WHERE donorBankID = @donorBankID;
11544
11545 EXECUTE Sysinfo.uspAddDataChange @username=@username, @auditTypeID=10, @tableName=''Nutrition.tDonorBank'', @recordID=@donorBankID, @previousEntry=@prevEntry;
11546
11547 SET @errCode = 0
11548 SELECT errCode = @errCode;
11549 END
11550 END
11551
11552END;
11553')
11554
11555 PRINT N'USP Nutrition.uspAddEditDonorBank created'
11556
11557END TRY
11558BEGIN CATCH
11559 PRINT N'USP Nutrition.uspAddEditDonorBank create failed'
11560 PRINT 'Error Number: ' + str(error_number())
11561 PRINT 'Line Number: ' + str(error_line())
11562 PRINT error_message()
11563END CATCH
11564GO
11565
11566/* Nutrition.uspInactiveBank.storedProcedure.sql
11567To call the SP:
11568EXECUTE Nutrition.uspInactiveBank @donorBankID=2, @username='admin';
11569 */
11570
11571BEGIN TRY
11572EXECUTE ('/****** Object: StoredProcedure [Nutrition].[uspInactiveBank] Script Date: 8/27/2013 11:26:19 ******/
11573
11574-- =============================================
11575-- Author: Lisa Bohm (lbohm@paragondsi.com)
11576-- Create date: 2013-08-27
11577-- Description: inactivate donor bank.
11578-- Modified: 2013-12-05 add audit tracking call
11579-- =============================================
11580CREATE PROCEDURE [Nutrition].[uspInactiveBank]
11581 -- Add the parameters for the stored procedure here
11582 @donorBankID smallint
11583 , @username varchar(50)
11584 AS
11585 DECLARE @errCode tinyint;
11586 BEGIN
11587 -- SET NOCOUNT ON added to prevent extra result sets from
11588 -- interfering with SELECT statements.
11589 SET NOCOUNT ON;
11590
11591 UPDATE Nutrition.tDonorBank
11592 SET isActive = 0
11593 WHERE donorBankID = @donorBankID;
11594
11595 EXECUTE Sysinfo.uspAddDataChange @username=@username, @auditTypeID=10, @tableName=''Nutrition.tDonorBank'', @recordID=@donorBankID, @previousEntry=''isActive=1'';
11596
11597 SET @errCode = 0
11598 SELECT errCode = @errCode;
11599END;')
11600
11601 PRINT N'USP Nutrition.uspInactiveBank created'
11602
11603END TRY
11604BEGIN CATCH
11605 PRINT N'USP Nutrition.uspInactiveBank create failed'
11606 PRINT 'Error Number: ' + str(error_number())
11607 PRINT 'Line Number: ' + str(error_line())
11608 PRINT error_message()
11609END CATCH
11610GO
11611
11612 /* Nutrition.uspAddEditDonorLot.storedProcedure.sql
11613To call the SP:
11614EXECUTE Nutrition.uspAddEditDonorLot @lotID=0, @donorBankID=1, @lotCode='BC131009', @username='admin';
11615Please use @lotID=0 for new, actual @lotID for edit.
11616 */
11617
11618BEGIN TRY
11619EXECUTE ('/****** Object: StoredProcedure [Nutrition].[uspAddEditDonorLot] Script Date: 8/12/2013 11:26:19 ******/
11620
11621-- =============================================
11622-- Author: Lisa Bohm (lbohm@paragondsi.com)
11623-- Create date: 2013-08-27
11624-- Description: add/edit donor lot to list.
11625-- Modified: 2013-12-05 add audit tracking call
11626-- =============================================
11627CREATE PROCEDURE [Nutrition].[uspAddEditDonorLot]
11628 -- Add the parameters for the stored procedure here
11629@lotID int
11630, @donorBankID tinyint
11631, @lotCode varchar(25)
11632, @username varchar(50)
11633
11634 AS
11635 DECLARE @already int
11636 , @errCode tinyint
11637 , @newLotID int
11638 , @prevEntry varchar(1000);
11639
11640 BEGIN
11641 -- SET NOCOUNT ON added to prevent extra result sets from
11642 -- interfering with SELECT statements.
11643 SET NOCOUNT ON;
11644SET @already = COALESCE((SELECT NDL.lotID
11645 FROM Nutrition.tDonorLot NDL
11646 WHERE NDL.lotID <> @lotID
11647 AND NDL.donorBankID = @donorBankID
11648 AND NDL.lotCode = @lotCode),0)
11649IF @already > 0
11650 BEGIN
11651 SET @errCode = 20
11652 SELECT errCode = @errCode
11653 , lotID = @lotID;
11654 END
11655ELSE
11656 BEGIN
11657 IF @lotID = 0
11658 BEGIN
11659 INSERT INTO Nutrition.tDonorLot
11660 ([donorBankID]
11661 ,[lotCode])
11662 VALUES
11663 (@donorBankID
11664 , @lotCode);
11665
11666 SET @newLotID = SCOPE_IDENTITY()
11667
11668 EXECUTE Sysinfo.uspAddDataChange @username=@username, @auditTypeID=9, @tableName=''Nutrition.tDonorLot'', @recordID=@newLotID, @previousEntry=''N/A'';
11669
11670 SET @errCode = 0
11671 SELECT errCode = @errCode
11672 , lotID = @newLotID;
11673 END
11674 ELSE
11675 BEGIN
11676 /* set up audit previous record */
11677 SELECT @prevEntry = ''lot code: '' + NDL.lotCode + ''.''
11678 FROM Nutrition.tDonorLot NDL
11679 WHERE NDL.lotID = @lotID;
11680
11681 UPDATE Nutrition.tDonorLot
11682 SET lotCode = @lotCode
11683 WHERE lotID = @lotID;
11684
11685 EXECUTE Sysinfo.uspAddDataChange @username=@username, @auditTypeID=10, @tableName=''Nutrition.tDonorLot'', @recordID=@lotID, @previousEntry=@prevEntry;
11686 SET @newLotID = @lotID;
11687 SET @errCode = 0
11688 SELECT errCode = @errCode
11689 , lotID = @newLotID;
11690 END
11691 END
11692
11693END;
11694')
11695
11696 PRINT N'USP Nutrition.uspAddEditDonorLot created'
11697
11698END TRY
11699BEGIN CATCH
11700 PRINT N'USP Nutrition.uspAddEditDonorLot create failed'
11701 PRINT 'Error Number: ' + str(error_number())
11702 PRINT 'Line Number: ' + str(error_line())
11703 PRINT error_message()
11704END CATCH
11705GO
11706
11707/* PTinfo.uspReceiveHL7msg.storedProcedure.sql
11708To call the SP:
11709EXECUTE PTinfo.uspReceiveHL7msg @adtXML='';
11710See beginning section of SP for XML format.
11711 */
11712
11713BEGIN TRY
11714EXECUTE ('/****** Object: StoredProcedure [PTinfo].[uspReceiveHL7msg] Script Date: 12/07/2012 11:26:19 ******/
11715-- =============================================
11716-- Author: Lisa Bohm (lbohm@paragondsi.com)
11717-- Create date: 2013-08-29
11718-- Description: Add/Edit HL7 message, test for matches
11719-- Modified: 2013-11-22 updated for HL7MsgField tbl add
11720-- 2013-12-07 add audit tracking call
11721-- 2014-04-07 explicit transaction, watch for null string concat
11722-- 2014-04-14 deal with times in the birthdate
11723-- Incoming XML things example:
11724--<message type = "1">
11725--<section code = "PID" fieldnum = "0" subfieldnum = "1" content = "PID"/>
11726--<section code = "PID" fieldnum = "1" subfieldnum = "1" content = "0001"/>
11727--<section code = "PID" fieldnum = "2" subfieldnum = "1" content = "13537830"/>
11728--<section code = "PID" fieldnum = "3" subfieldnum = "1" content = "75590419"/>
11729--<section code = "PID" fieldnum = "3" subfieldnum = "4" content = "SH"/>
11730--<section code = "PID" fieldnum = "5" subfieldnum = "1" content = "CNRMOTHER"/>
11731--<section code = "PID" fieldnum = "5" subfieldnum = "2" content = "ONE"/>
11732--<section code = "PID" fieldnum = "5" subfieldnum = "3" content = "MABLE"/>
11733--<section code = "PID" fieldnum = "7" subfieldnum = "1" content = "19780618"/>
11734--<section code = "PID" fieldnum = "8" subfieldnum = "1" content = "F"/>
11735--<section code = "PID" fieldnum = "18" subfieldnum = "1" content = "135378303248"/>
11736--</message>
11737-- =============================================
11738CREATE PROCEDURE [PTinfo].[uspReceiveHL7msg]
11739 -- Add the parameters for the stored procedure here
11740@adtXML XML
11741
11742AS
11743DECLARE @matchMother tinyint
11744 , @primaryField tinyint
11745 , @motherField tinyint
11746 , @allowFormdef bit
11747 , @allowBottleDef bit
11748 , @autoActivate bit
11749 , @isMatch bit
11750 , @addRcd bit
11751 , @already bit
11752 , @pSysID bigint
11753 , @matchSysID bigint
11754 , @matchPriID varchar(50)
11755 , @errCode tinyint
11756 , @firstName varchar(50)
11757 , @lastName varchar(50)
11758 , @mInit varchar(1)
11759 , @dob datetime
11760 , @priID varchar(50)
11761 , @gender varchar(15)
11762 , @location varchar(50)
11763 , @isChild bit
11764 , @matchFName varchar(50)
11765 , @matchLName varchar(50)
11766 , @matchmInit varchar(1)
11767 , @matchdob datetime
11768 , @matchgender varchar(15)
11769 , @matchlocation varchar(50)
11770 , @messageID bigint
11771 , @theNewID bigint = 0
11772 , @prevEntry varchar(1000)
11773 , @passedDOB datetime;
11774
11775
11776BEGIN TRY
11777BEGIN TRANSACTION
11778 -- SET NOCOUNT ON added to prevent extra result sets from
11779 -- interfering with SELECT statements.
11780 SET NOCOUNT ON;
11781
11782DECLARE @ADTinfo TABLE (fieldID tinyint NOT NULL
11783 , sect char(3) NOT NULL
11784 , fieldNum tinyint NOT NULL
11785 , subFieldNum tinyint NOT NULL
11786 , content varchar(50) NOT NULL);
11787
11788SET @isMatch = 0
11789SET @addRcd = 1
11790
11791/* dump message info into the temporary table; join with fieldID from the PHF table */
11792INSERT INTO @ADTinfo (fieldID, sect, fieldNum, subFieldNum, content)
11793 SELECT PHF.fieldID
11794 , PHF.sect
11795 , PHF.fieldNum
11796 , PHF.subFieldNum
11797 , COALESCE(adtData.value(''@content'', ''varchar(50)''),'''')
11798
11799 FROM PTinfo.tHL7MsgField PHF
11800 LEFT OUTER JOIN @adtXML.nodes(''./message/section'') as data ( adtData ) ON PHF.sect = adtData.value(''@code'', ''char(3)'')
11801 AND PHF.fieldNum = adtData.value(''@fieldnum'', ''tinyint'')
11802 AND PHF.subFieldNum = adtData.value(''@subfieldnum'', ''tinyint'')
11803WHERE PHF.msgTypeID=1;
11804
11805/* get the passed info from the dob field */
11806SELECT @passedDOB = CASE WHEN len(rTrim(ltrim(ADT.content))) = 8 THEN convert(datetime, (ADT.content + '' 00:00:00''),101)
11807 WHEN len(rTrim(ltrim(ADT.content))) > 8 THEN convert(datetime,stuff(stuff(stuff(ADT.content, 9, 0, '' ''), 12, 0, '':''), 15, 0, '':''),101)
11808 ELSE ''1900-01-01'' END
11809 FROM @ADTinfo ADT
11810 WHERE ADT.sect = ''PID''
11811 AND ADT.fieldNum = 7;
11812
11813/* now I need to know what identifiers I need to keep, and of course the primary ID. And the mother match. So - field ID of the settings info */
11814/* first, IS the primary ID what we match the mother ID with?*/
11815
11816SELECT @motherField = MAX(CASE WHEN StS.settingID = 33 THEN PHF.fieldID ELSE 0 END)
11817 , @matchMother = MAX(CASE WHEN StS.settingID = 69 THEN PHF.fieldID ELSE 0 END)
11818 , @primaryField = MAX(CASE WHEN StS.settingID = 32 THEN PHF.fieldID ELSE 0 END)
11819 , @allowFormDef = MAX(CASE WHEN StS.settingID = 14 THEN
11820 CASE WHEN StS.setValue = ''Y'' THEN 1 ELSE 0 END
11821 ELSE 0 END)
11822 , @allowBottleDef = MAX(CASE WHEN StS.settingID = 13 THEN
11823 CASE WHEN StS.setValue = ''Y'' THEN 1 ELSE 0 END
11824 ELSE 0 END)
11825 , @autoActivate = MAX(CASE WHEN StS.settingID = 39 THEN StS.setValue ELSE 0 END)
11826
11827FROM SysInfo.tSetting StS
11828 LEFT OUTER JOIN PTinfo.tHL7MsgField PHF ON StS.setValue = CAST(PHF.fieldNum AS VARCHAR)
11829 AND PHF.sect = ''PID''
11830;
11831
11832/* If the mother is discharged, reactivate her. Mother pSysID here. So this would be for a child, whose mother is in the system but inactive. */
11833
11834SET @matchSysID = COALESCE((SELECT PtP.pSysID
11835 FROM PTinfo.tPatient PtP
11836 INNER JOIN PTinfo.tIdentity PTI ON PtP.pSysID = PTI.pSysID
11837 INNER JOIN PTinfo.tIdentityType PIT ON PtI.identTypeID = PIT.identTypeID
11838 INNER JOIN PTinfo.tStatus PtS ON PtP.pSysID = PtS.pSysID
11839 INNER JOIN @ADTinfo ADT ON ADT.fieldID = PIT.fieldID
11840 WHERE PIT.fieldID = @matchMother
11841 AND PTI.IDvalue = ADT.content
11842 AND PtS.isCurrent = 1
11843 AND PtS.pStatTypeID <> 1),0);
11844
11845IF @pSysID > 0
11846 BEGIN
11847 /* activate */
11848 EXECUTE PTinfo.uspAddEditPStatusByID @pSysID=@matchSysID, @username=''System'';
11849
11850 /* testing
11851 PRINT N''Mother is discharged. reactivate.''
11852 -- */
11853 END
11854
11855/* is this record even in the PTinfo.tPatient table? So now we''re going to get the "main" pSysID. */
11856SET @pSysID = COALESCE((SELECT PtP.pSysID
11857 FROM PTinfo.tPatient PtP
11858 INNER JOIN PTinfo.tIdentity PTI ON PtP.pSysID = PTI.pSysID
11859 INNER JOIN PTinfo.tIdentityType PIT ON PtI.identTypeID = PIT.identTypeID
11860 INNER JOIN @ADTinfo ADT ON PIT.fieldID = ADT.fieldID
11861 WHERE PIT.fieldID = @primaryField
11862 AND PTI.IDvalue = ADT.content),0);
11863
11864/* if so, update information */
11865IF @pSysID > 0
11866 BEGIN
11867 SET @isMatch = 1
11868 SET @addRcd = 0
11869
11870 /* testing
11871 PRINT N''In db.''
11872 -- */
11873
11874 /* if patient/mother is discharged, "reactivate" */
11875 SET @already = COALESCE((SELECT 1
11876 FROM PTinfo.tStatus PtS
11877 WHERE PtS.pSysID = @pSysID
11878 AND PtS.isCurrent = 1
11879 AND PtS.pStatTypeID <> 1),0)
11880
11881 IF @already = 1 AND @autoActivate > 0
11882 /* add status change record */
11883 BEGIN
11884 EXECUTE PTinfo.uspAddEditPStatusByID @pSysID=@pSysID, @username=''System'';
11885 /* testing
11886 PRINT N''Already in system. Reactivating''
11887 -- */
11888 END
11889
11890 /* now update the info */
11891
11892 /* what does cross-apply do? note the group by doesn''t work. CTE? */
11893 /* audit */
11894 SELECT @prevEntry = COALESCE(PtP.firstName,''No FN entered'') + ''; '' + COALESCE(PtP.lastName,''No LN entered'') + ''; '' + COALESCE(PtP.midInit,''No MI entered'') + ''; '' + COALESCE(CAST(PtP.dob AS VARCHAR),''No DOB entered'') + ''.''
11895 FROM PTinfo.tPatient PtP
11896 WHERE PtP.pSysID = @pSysID;
11897
11898 UPDATE PtP
11899 SET PtP.firstName = CASE WHEN ADTI.fName IS NULL THEN PtP.firstName ELSE ADTI.fName END
11900 , PtP.lastName = CASE WHEN ADTI.lName IS NULL THEN PtP.lastName ELSE ADTI.lName END
11901 , PtP.midInit =CASE WHEN ADTI.mInit IS NULL THEN PtP.midInit ELSE ADTI.mInit END
11902 , PtP.dob = CASE WHEN ADTI.dob = ''1900-01-01'' THEN PtP.dob ELSE ADTI.dob END
11903
11904 FROM PTinfo.tPatient PtP
11905 CROSS APPLY (SELECT fName = MAX(CASE WHEN ADT.fieldID = 4 THEN ADT.content ELSE NULL END)
11906 , lName = MAX(CASE WHEN ADT.fieldID = 3 THEN ADT.content ELSE NULL END)
11907 , mInit = MAX(CASE WHEN ADT.fieldID = 5 THEN LEFT(ADT.content,1) ELSE NULL END)
11908 , dob = MAX(@passedDOB)
11909 FROM @ADTinfo ADT
11910 WHERE ADT.sect = ''PID''
11911 GROUP BY ADT.sect) ADTI
11912 WHERE PtP.pSysID = @pSysID;
11913
11914 /* audit record */
11915 EXECUTE Sysinfo.uspAddDataChange @username=''System'', @auditTypeID=10, @tableName=''PTinfo.tPatient'', @recordID=@pSysID, @previousEntry=@prevEntry;
11916
11917 /* nothing should update if not a child anyway. But I suppose I should find out and do it the right way */
11918
11919 SET @theNewID = COALESCE((SELECT PCL.childLinkID
11920 FROM PTinfo.tChildLink PCL
11921 WHERE PCL.pSysID = @pSysID),0);
11922
11923 IF @theNewID > 0
11924 BEGIN
11925 SELECT @prevEntry = ''location: '' + COALESCE(PCL.location, ''none'') + ''.''
11926 FROM PTinfo.tChildLink PCL
11927 WHERE PCL.pSysID = @pSysID;
11928
11929 UPDATE PCL
11930 SET PCL.location = CASE WHEN ADTI.loc IS NULL THEN COALESCE(PCL.location, ''None specified'') ELSE ADTI.loc END
11931 FROM PTinfo.tChildLink PCL
11932 CROSS APPLY (SELECT loc = MAX(CASE WHEN ADT.fieldID = 13 THEN ADT.content ELSE NULL END)
11933 FROM @ADTinfo ADT
11934 WHERE ADT.sect = ''PV1''
11935 GROUP BY ADT.sect) ADTI
11936 WHERE childLinkID = @theNewID;
11937
11938 /* audit record */
11939 EXECUTE Sysinfo.uspAddDataChange @username=''System'', @auditTypeID=10, @tableName=''PTinfo.tChildLink'', @recordID=@theNewID, @previousEntry=@prevEntry;
11940
11941 SET @errCode = 0
11942 SELECT errCode = @errCode;
11943 END
11944 /* testing
11945 PRINT N''Update person in the PTinfo.Patient table.''
11946 -- */
11947 END
11948
11949/* if not, check to see if the mother/child match thing happens. */
11950ELSE
11951 BEGIN
11952
11953 /* mother or child? */
11954 SELECT @isChild = CASE WHEN
11955 CASE
11956 WHEN len(rTrim(ltrim(ADT.content))) = 8 THEN convert(datetime, (ADT.content + '' 00:00:00''),101)
11957 WHEN len(rTrim(ltrim(ADT.content))) > 8 THEN convert(datetime,stuff(stuff(stuff(ADT.content, 9, 0, '' ''), 12, 0, '':''), 15, 0, '':''),101)
11958 ELSE ''1900-01-01'' END > DATEADD(year, -10, getdate()) THEN 1 ELSE 0 END
11959 FROM @ADTinfo ADT
11960 WHERE ADT.fieldID = 7
11961 IF @isChild = 1
11962
11963 BEGIN
11964 /* testing
11965 PRINT N''Is Child''
11966 -- */
11967 /* if a child, match any mother in the PT.patient table? find mother pSysID */
11968 SET @matchSysID = COALESCE((SELECT PtP.pSysID
11969 FROM PTinfo.tPatient PtP
11970 INNER JOIN PTinfo.tIdentity PTI ON PtP.pSysID = PTI.pSysID
11971 INNER JOIN PTinfo.tIdentityType PIT ON PtI.identTypeID = PIT.identTypeID
11972 INNER JOIN @ADTinfo ADT ON PTI.IDvalue = ADT.content
11973 WHERE PIT.fieldID = @matchMother
11974 AND ADT.fieldID = @motherField),0);
11975
11976 IF @matchSysID > 0
11977 BEGIN
11978 SET @isMatch = 1
11979 /* testing
11980 PRINT N''child match mother in PtP''
11981 -- */
11982 END
11983 ELSE
11984 BEGIN
11985
11986 /* how about any match to a mother in the HL7 table? */
11987 SELECT @messageID = CASE WHEN PHM.messageID IS NOT NULL THEN PHM.messageID ELSE 0 END
11988 , @matchfName = PHM.firstName
11989 , @matchlName = PHM.lastname
11990 , @matchmInit = PHM.midInit
11991 , @matchdob = PHM.dob
11992 , @matchPriID = PHpri.IDvalue
11993
11994 FROM PTinfo.tHL7message PHM
11995 INNER JOIN PTinfo.tHL7ID PTI ON PHM.messageID = PTI.messageID
11996 INNER JOIN PTinfo.tIdentityType PIT ON PtI.identTypeID = PIT.identTypeID
11997 INNER JOIN PTinfo.tHL7ID PHpri ON PHM.messageID = PHpri.messageID
11998 INNER JOIN PTinfo.tIdentityType PTpri ON PHpri.identTypeID = PTpri.identTypeID
11999 INNER JOIN @ADTinfo ADT ON PTI.IDvalue = ADT.content
12000
12001
12002 WHERE PIT.fieldID = @matchMother
12003 AND ADT.fieldID = @motherField
12004 AND PTpri.fieldID = @primaryField;
12005
12006 SET @messageID = COALESCE(@messageID, 0);
12007
12008 IF @messageID > 0 AND @autoactivate > 0
12009 /* if match, activate both and remove from HL7 setup */
12010 BEGIN
12011 SET @isMatch = 1
12012 /* deal with adding the mother */
12013 EXECUTE PTinfo.uspAddEditPatient @pSysID = 0, @primaryID=@matchPriID, @fName=@matchfName, @lName=@matchlname, @mInit=@matchmInit, @dob=@matchdob, @returnID=@matchSysID OUTPUT, @errCode=@errCode OUTPUT, @username=''System'';
12014
12015 /* testing
12016 PRINT N''add mother''
12017 -- */
12018
12019 /* add other IDs */
12020 INSERT INTO PTinfo.tIdentity (identTypeID, pSysID, IDvalue)
12021
12022 SELECT PHI.identTypeID
12023 , @matchSysID
12024 , PHI.IDvalue
12025
12026 FROM PTinfo.tHL7ID PHI
12027 INNER JOIN PTinfo.tIdentityType PIT ON PHI.identTypeID = PIT.identTypeID
12028 WHERE PIT.fieldID <> @primaryField
12029 AND PHI.messageID = @messageID
12030 ;
12031
12032 /*audit. Possible multiple records */
12033 INSERT INTO SysInfo.tDataChange
12034 (viewDate, userName, auditTypeID, tableName, recordID, previousEntry)
12035 SELECT
12036 getdate()
12037 , ''System''
12038 , 9
12039 , ''PTinfo.tIdentity''
12040 , PHI.PID
12041 , ''N/A''
12042
12043 FROM PTinfo.tIdentity PHI
12044 INNER JOIN PTinfo.tIdentityType PIT ON PHI.identTypeID = PIT.identTypeID
12045 WHERE PIT.fieldID <> @primaryField
12046 AND PHI.pSysID = @matchSysID
12047 ;
12048 /* testing
12049 PRINT N''Add mother identities.''
12050 -- */
12051 /* add mother record */
12052 EXECUTE PTinfo.uspAddEditMomInfo @pSysID=@matchSysID, @calsPer = 20, @username=''System'';
12053
12054 /* add status */
12055 EXECUTE PTinfo.uspAddEditPStatusByID @pSysID=@matchSysID, @username=''System'';
12056 /* testing
12057 PRINT N''add mother record.''
12058 -- */
12059 /* removing the HL7 message */
12060 DELETE
12061 FROM PTinfo.tHL7ID
12062 WHERE messageID = @messageID;
12063
12064 DELETE
12065 FROM PTinfo.tHL7message
12066 WHERE messageID = @messageID;
12067 /* testing
12068 PRINT N''remove mother''''s HL7 message''
12069 -- */
12070 END
12071
12072 END
12073 END
12074 ELSE
12075 BEGIN
12076 /* this is a mother!!! does the mother match a child in the HL7 table? Check what IDfield the mother is supposed to match */
12077 SELECT @messageID = CASE WHEN PHM.messageID IS NOT NULL THEN PHM.messageID ELSE 0 END
12078 , @matchfName = PHM.firstName
12079 , @matchlName = PHM.lastname
12080 , @matchmInit = PHM.midInit
12081 , @matchdob = PHM.dob
12082 , @matchgender = PHM.gender
12083 , @matchPriID = PHpri.IDvalue
12084 , @matchlocation = PHM.location
12085
12086 FROM PTinfo.tHL7message PHM
12087 INNER JOIN PTinfo.tHL7ID PHpri ON PHM.messageID = PHpri.messageID
12088 INNER JOIN PTinfo.tIdentityType PIT ON PHpri.identTypeID = PIT.identTypeID
12089 INNER JOIN @ADTinfo ADT ON PHM.momPrimaryID = ADT.content
12090
12091 WHERE PIT.fieldID = @primaryField
12092 AND ADT.fieldID = @matchMother;
12093
12094 SET @messageID = COALESCE(@messageID, 0);
12095/* check to here */
12096
12097 /* If so, add the child and delete the message */
12098 IF @messageID > 0 AND @autoActivate > 0
12099 BEGIN
12100 SET @isMatch = 1
12101
12102 /* testing
12103 PRINT N''Is mother, matches child''
12104 -- */
12105
12106 /* deal with adding the child, the @motherSysID is doubling for the child''s primaryID in this case. BUUUT the mother doesn''t have a sysID at this time. */
12107
12108 /* so what goes horribly wrong if we add the mother''s patient record here?*/
12109/* HERE */
12110 SELECT @firstName = MAX(CASE WHEN ADT.fieldID = 4 THEN ADT.content ELSE NULL END)
12111 , @lastName = MAX(CASE WHEN ADT.fieldID = 3 THEN ADT.content ELSE NULL END)
12112 , @mInit = MAX(CASE WHEN ADT.fieldID = 5 THEN LEFT(ADT.content,1) ELSE NULL END)
12113 , @dob = MAX(CASE WHEN @passedDOB = ''1900-01-01'' THEN getdate() ELSE @passedDOB END)
12114 , @priID = MAX(CASE WHEN ADT.fieldID = @primaryField THEN ADT.content ELSE NULL END)
12115 FROM @ADTinfo ADT
12116 WHERE ADT.sect = ''PID''
12117 GROUP BY ADT.sect
12118
12119 /* add the mother record from the current hl7 message */
12120 EXECUTE PTinfo.uspAddEditPatient @pSysID = 0, @primaryID=@priID, @fName=@firstName, @lName=@lastName, @mInit=@mInit, @dob=@dob, @returnID=@pSysID OUTPUT, @errCode=@errCode OUTPUT, @username=''System'';
12121
12122 /* add mother''s other identities */
12123 INSERT INTO PTinfo.tIdentity (identTypeID, pSysID, IDvalue)
12124
12125 SELECT PIT.identTypeID
12126 , @pSysID
12127 , ADT.content
12128
12129 FROM PTinfo.tIdentityType PIT
12130 INNER JOIN @ADTinfo ADT ON PIT.fieldID = ADT.fieldID
12131 WHERE PIT.fieldID <> @primaryField;
12132
12133 /*audit. Possible multiple records */
12134 INSERT INTO SysInfo.tDataChange
12135 (viewDate, userName, auditTypeID, tableName, recordID, previousEntry)
12136 SELECT
12137 getdate()
12138 , ''System''
12139 , 9
12140 , ''PTinfo.tIdentity''
12141 , PHI.PID
12142 , ''N/A''
12143
12144 FROM PTinfo.tIdentity PHI
12145 INNER JOIN PTinfo.tIdentityType PIT ON PHI.identTypeID = PIT.identTypeID
12146 WHERE PIT.fieldID <> @primaryField
12147 AND PHI.pSysID = @pSysID;
12148
12149 /* add mother status */
12150 EXECUTE PTinfo.uspAddEditPStatusByID @pSysID=@pSysID, @username=''System'';
12151 /* add mother info */
12152 EXECUTE PTinfo.uspAddEditMomInfo @pSysID=@pSysID, @calsPer = 20, @username=''System'';
12153
12154 /* add the child from the existing HL7 message */
12155 EXECUTE PTinfo.uspAddEditPatient @pSysID = 0, @primaryID=@matchPriID, @fName=@matchfName, @lName=@matchlname, @mInit=@matchmInit, @dob=@matchdob, @returnID=@matchSysID OUTPUT, @errCode=@errCode OUTPUT, @username=''System'';
12156
12157 /* add child''s other IDs */
12158 INSERT INTO PTinfo.tIdentity (identTypeID, pSysID, IDvalue)
12159
12160 SELECT PHI.identTypeID
12161 , @matchSysID
12162 , PHI.IDvalue
12163
12164 FROM PTinfo.tHL7ID PHI
12165 INNER JOIN PTinfo.tIdentityType PIT ON PHI.identTypeID = PIT.identTypeID
12166
12167 WHERE PIT.fieldID <> @primaryField
12168 AND PHI.messageID = @messageID
12169 ;
12170 /*audit. Possible multiple records */
12171 INSERT INTO SysInfo.tDataChange
12172 (viewDate, userName, auditTypeID, tableName, recordID, previousEntry)
12173 SELECT
12174 getdate()
12175 , ''System''
12176 , 9
12177 , ''PTinfo.tIdentity''
12178 , PHI.PID
12179 , ''N/A''
12180
12181 FROM PTinfo.tIdentity PHI
12182 INNER JOIN PTinfo.tIdentityType PIT ON PHI.identTypeID = PIT.identTypeID
12183 WHERE PIT.fieldID <> @primaryField
12184 AND PHI.pSysID = @matchSysID
12185 ;
12186
12187 /* add child record */
12188 EXECUTE PTinfo.uspAddEditChildLink @pSysID=@matchSysID, @motherSysID = @pSysID, @multiple=0, @gender=@matchgender, @allowFormula=@allowFormDef, @allowBottle=@allowBottleDef, @location=@matchlocation, @username=''System'';
12189
12190 /* add child''s status */
12191 EXECUTE PTinfo.uspAddEditPStatusByID @pSysID=@matchSysID, @username=''System'';
12192 /* removing the HL7 message */
12193 DELETE
12194 FROM PTinfo.tHL7ID
12195 WHERE messageID = @messageID;
12196
12197 DELETE
12198 FROM PTinfo.tHL7message
12199 WHERE messageID = @messageID;
12200
12201 END
12202 END
12203
12204IF @isMatch > 0
12205 BEGIN
12206 IF @addRcd = 1 AND @autoActivate > 0
12207 BEGIN
12208
12209 SELECT @firstName = MAX(CASE WHEN ADT.fieldID = 4 THEN ADT.content ELSE NULL END)
12210 , @lastName = MAX(CASE WHEN ADT.fieldID = 3 THEN ADT.content ELSE NULL END)
12211 , @mInit = MAX(CASE WHEN ADT.fieldID = 5 THEN ADT.content ELSE NULL END)
12212 , @dob = MAX(CASE WHEN @passedDOB = ''1900-01-01'' THEN getdate() ELSE @passedDOB END)
12213 , @priID = MAX(CASE WHEN ADT.fieldID = @primaryField THEN ADT.content ELSE NULL END)
12214
12215 FROM @ADTinfo ADT
12216 WHERE ADT.sect = ''PID''
12217 GROUP BY ADT.sect;
12218
12219 /* add PTP record */
12220 EXECUTE PTinfo.uspAddEditPatient @pSysID = @pSysID, @primaryID=@priID, @fName=@firstName, @lName=@lastname, @mInit=@mInit, @dob=@dob, @returnID=@pSysID OUTPUT, @errCode=@errCode OUTPUT, @username=''System'';
12221
12222 /* add other IDs */
12223 INSERT INTO PTinfo.tIdentity (identTypeID, pSysID, IDvalue)
12224
12225 SELECT PIT.identTypeID
12226 , @pSysID
12227 , ADT.content
12228
12229 FROM PTinfo.tIdentityType PIT
12230 INNER JOIN @ADTinfo ADT ON PIT.fieldID = ADT.fieldID
12231 WHERE PIT.fieldID <> @primaryField;
12232
12233 /*audit. Possible multiple records */
12234 INSERT INTO SysInfo.tDataChange
12235 (viewDate, userName, auditTypeID, tableName, recordID, previousEntry)
12236 SELECT
12237 getdate()
12238 , ''System''
12239 , 9
12240 , ''PTinfo.tIdentity''
12241 , PHI.PID
12242 , ''N/A''
12243
12244 FROM PTinfo.tIdentity PHI
12245 INNER JOIN PTinfo.tIdentityType PIT ON PHI.identTypeID = PIT.identTypeID
12246 WHERE PIT.fieldID <> @primaryField
12247 AND PHI.pSysID = @pSysID;
12248
12249 /* add status */
12250 EXECUTE PTinfo.uspAddEditPStatusByID @pSysID=@pSysID, @username=''System'';
12251
12252 IF @isChild = 1
12253 BEGIN
12254 /* add child record */
12255
12256 /* testing
12257 PRINT N''Child link added ''
12258 -- */
12259 SELECT @gender = ADT.content
12260 FROM @ADTinfo ADT
12261 WHERE ADT.fieldID = 8;
12262
12263 SELECT @location = ADT.content
12264 FROM @ADTinfo ADT
12265 WHERE ADT.fieldID = 13;
12266
12267 EXECUTE PTinfo.uspAddEditChildLink @pSysID=@pSysID, @motherSysID = @matchSysID, @multiple=0, @gender=@gender, @allowFormula=@allowFormDef, @allowBottle=@allowBottleDef, @location=@location, @username=''System'';
12268 END
12269 ELSE
12270 BEGIN
12271 /* testing
12272 PRINT N''Mom link added''
12273 -- */
12274 EXECUTE PTinfo.uspAddEditMomInfo @pSysID=@pSysID, @calsPer = 20, @username=''System'';
12275 END
12276 END
12277 END
12278 ELSE
12279 BEGIN
12280 /* how about just need to update hl7 record in the table */
12281 /* but wait. IF @autoactivate is zero, it''s possible the wrongggggg message can get updated. */
12282 SET @messageID = 0;
12283
12284 SELECT @priID = ADT.content
12285 FROM @ADTinfo ADT
12286 WHERE ADT.fieldID = @primaryField;
12287
12288 SELECT @messageID = CASE WHEN PHM.messageID IS NOT NULL THEN PHM.messageID ELSE 0 END
12289
12290 FROM PTinfo.tHL7message PHM
12291 INNER JOIN PTinfo.tHL7ID PTI ON PHM.messageID = PTI.messageID
12292 INNER JOIN PTinfo.tIdentityType PIT ON PtI.identTypeID = PIT.identTypeID
12293
12294 WHERE PIT.fieldID = @primaryField
12295 AND PTI.IDvalue = @priID;
12296
12297 IF @messageID > 0
12298 BEGIN
12299 /* testing
12300 PRINT N''updating record in the HL7 message table.''
12301 -- */
12302 /* audit */
12303 SELECT @prevEntry = COALESCE(PHM.firstName,''No FN entered'') + ''; '' + COALESCE(PHM.lastName,''No LN entered'') + ''; '' + COALESCE(PHM.midInit,''No MI entered'') + ''; '' + COALESCE(CAST(PHM.dob AS VARCHAR),''No DOB entered'') + ''; '' + COALESCE(PHM.gender,''No gender entered'') + ''; '' + COALESCE(PHM.location,''No loc entered'') + ''.''
12304 FROM PTinfo.tHL7Message PHM
12305 WHERE PHM.messageID = @messageID;
12306 /* do the update for the PID fields */
12307 UPDATE PHM
12308 SET PHM.firstName = CASE WHEN ADTI.fName IS NULL THEN PHM.firstName ELSE ADTI.fName END
12309 , PHM.lastName = CASE WHEN ADTI.lName IS NULL THEN PHM.lastName ELSE ADTI.lName END
12310 , PHM.midInit =CASE WHEN ADTI.mInit IS NULL THEN PHM.midInit ELSE ADTI.mInit END
12311 , PHM.dob = CASE WHEN ADTI.dob = ''1900-01-01'' THEN PHM.dob ELSE ADTI.dob END
12312 , PHM.gender = CASE WHEN ADTI.gender IS NULL THEN PHM.gender ELSE ADTI.gender END
12313
12314 FROM PTinfo.tHL7Message PHM
12315 CROSS APPLY (SELECT fName = MAX(CASE WHEN ADT.fieldID = 4 THEN ADT.content ELSE NULL END)
12316 , lName = MAX(CASE WHEN ADT.fieldID = 3 THEN ADT.content ELSE NULL END)
12317 , mInit = MAX(CASE WHEN ADT.fieldID = 5 THEN LEFT(ADT.content,1) ELSE NULL END)
12318 , dob = MAX(@passedDOB)
12319 , gender = MAX(CASE WHEN ADT.fieldID = 8 THEN ADT.content ELSE NULL END)
12320 FROM @ADTinfo ADT
12321 WHERE ADT.sect = ''PID''
12322 GROUP BY ADT.sect) ADTI
12323 WHERE PHM.messageID = @messageID;
12324 /* audit record */
12325 EXECUTE Sysinfo.uspAddDataChange @username=''System'', @auditTypeID=10, @tableName=''PTinfo.tHL7Message'', @recordID=@messageID, @previousEntry=@prevEntry;
12326 /* then update the location .. */
12327
12328 UPDATE PHM
12329 SET PHM.location = CASE WHEN ADTI.content IS NULL THEN
12330 CASE WHEN PHM.location IS NULL THEN '''' ELSE PHM.location END
12331 ELSE ADTI.content END
12332
12333 FROM PTinfo.tHL7Message PHM
12334 OUTER APPLY (SELECT ADT.content
12335 FROM @ADTinfo ADT
12336 WHERE ADT.fieldID = 13) ADTI
12337 WHERE messageID = @messageID;
12338
12339 END
12340 ELSE
12341 BEGIN
12342 /* no match EVAH (@isMatch = 0 still), put in HL7 table */
12343
12344 /* testing
12345 PRINT N''No match at all. Insert into HL7 tables.''
12346 -- */
12347 SELECT @location = ADT.content
12348 FROM @ADTinfo ADT
12349 WHERE ADT.fieldID = 13;
12350
12351 INSERT INTO PTinfo.tHL7message
12352 (recdDate, firstName, lastName, midInit, dob, momPrimaryID, gender, location)
12353 SELECT getdate()
12354 , fName = MAX(CASE WHEN ADT.fieldID = 4 THEN ADT.content ELSE NULL END)
12355 , lName = MAX(CASE WHEN ADT.fieldID = 3 THEN ADT.content ELSE NULL END)
12356 , mInit = MAX(CASE WHEN ADT.fieldID = 5 THEN LEFT(ADT.content,1) ELSE NULL END)
12357 , dob = MAX(CASE WHEN @passedDOB = ''1900-01-01'' THEN getdate() ELSE @passedDOB END)
12358 , priID = MAX(CASE WHEN ADT.fieldID = @motherField THEN ADT.content ELSE NULL END)
12359 , gender = MAX(CASE WHEN ADT.fieldID = 8 THEN ADT.content ELSE NULL END)
12360 , @location
12361
12362 FROM @ADTinfo ADT
12363 WHERE ADT.sect = ''PID''
12364 GROUP BY ADT.sect;
12365
12366 SELECT @messageID = SCOPE_IDENTITY();
12367
12368 /* audit record */
12369 EXECUTE Sysinfo.uspAddDataChange @username=''System'', @auditTypeID=9, @tableName=''PTinfo.tHL7Message'', @recordID=@messageID, @previousEntry=''N/A'';
12370
12371 /* identity insert */
12372 INSERT INTO PTinfo.tHL7ID (messageID, identTypeID, IDvalue)
12373
12374 SELECT @messageID
12375 , PIT.identTypeID
12376 , ADT.content
12377
12378 FROM PTinfo.tIdentityType PIT
12379 INNER JOIN @ADTinfo ADT ON PIT.fieldID = ADT.fieldID
12380 WHERE ADT.content IS NOT NULL
12381 AND len(RTRIM(LTRIM(ADT.content))) > 0;
12382
12383 /*audit. Possible multiple records */
12384 INSERT INTO SysInfo.tDataChange
12385 (viewDate, userName, auditTypeID, tableName, recordID, previousEntry)
12386 SELECT
12387 getdate()
12388 , ''System''
12389 , 9
12390 , ''PTinfo.tHL7ID''
12391 , PHI.hID
12392 , ''N/A''
12393
12394 FROM PTinfo.tHL7ID PHI
12395 INNER JOIN PTinfo.tIdentityType PIT ON PHI.identTypeID = PIT.identTypeID
12396 WHERE PIT.fieldID <> @primaryField
12397 AND PHI.messageID = @messageID;
12398
12399 END
12400 END
12401 END
12402
12403SET @errCode = 0
12404SELECT errCode = @errCode;
12405
12406COMMIT TRANSACTION
12407END TRY
12408BEGIN CATCH
12409ROLLBACK TRANSACTION
12410 SET @errCode = 1
12411 SELECT errCode = @errCode;
12412
12413END CATCH;
12414')
12415
12416 PRINT N'USP PTinfo.uspReceiveHL7msg created'
12417
12418END TRY
12419BEGIN CATCH
12420 PRINT N'USP PTinfo.uspReceiveHL7msg create failed'
12421 PRINT 'Error Number: ' + str(error_number())
12422 PRINT 'Line Number: ' + str(error_line())
12423 PRINT error_message()
12424END CATCH
12425GO
12426
12427/* PTinfo.uspManualHL7activate.storedProcedure.sql
12428To call the SP:
12429EXECUTE PTinfo.uspManualHL7activate @messageID=5, @motherSysID=12, @username='admin';
12430 */
12431
12432BEGIN TRY
12433EXECUTE ('/****** Object: StoredProcedure [PTinfo].[uspManualHL7activate] Script Date: 12/07/2012 11:26:19 ******/
12434
12435-- =============================================
12436-- Author: Lisa Bohm (lbohm@paragondsi.com)
12437-- Create date: 2013-08-29
12438-- Description: Activate HL7 message (manual)
12439-- Modified: 2013-12-07 add audit tracking call
12440--
12441-- =============================================
12442CREATE PROCEDURE [PTinfo].[uspManualHL7activate]
12443 -- Add the parameters for the stored procedure here
12444@messageID bigint
12445 , @motherSysID bigint
12446 , @username varchar(50)
12447
12448AS
12449DECLARE @newSysID bigint
12450 , @allowFormdef bit
12451 , @allowBottleDef bit
12452 , @errCode tinyint
12453 , @already bit
12454 , @priPID tinyint
12455 , @primaryID varchar(50)
12456 , @theNewID bigint
12457
12458BEGIN TRY
12459BEGIN TRANSACTION
12460 -- SET NOCOUNT ON added to prevent extra result sets from
12461 -- interfering with SELECT statements.
12462 SET NOCOUNT ON;
12463
12464SET @priPID = PTinfo.udfGetPIDtype();
12465 /* does primary ID already exist? */
12466/* what is the primary ID...? */
12467SET @primaryID = (SELECT PvH.primaryID
12468 FROM PTinfo.vHL7 pVH
12469 WHERE PvH.messageID = @messageID);
12470
12471SET @already = COALESCE(
12472 (SELECT 1
12473 FROM PTinfo.tPatient PtP
12474 INNER JOIN PTinfo.tIdentity PtI ON PtP.pSysID = PtI.pSysID
12475 WHERE PtI.IDvalue = @primaryID
12476 AND PtI.identTypeID = @priPID), 0)
12477
12478IF @already = 1
12479 -- patient ID exists but not this patient --
12480 BEGIN
12481 SET @errCode = 23
12482 SELECT errCode = @errCode;
12483 END
12484ELSE
12485 BEGIN
12486
12487 /* transfer the hl7 message table info to the PTInfo table */
12488 INSERT INTO PTinfo.tPatient
12489 (firstName, lastname, midInit, dob, recordAdded)
12490
12491 SELECT PH7.firstName
12492 , PH7.lastName
12493 , PH7.midInit
12494 , PH7.dob
12495 , getdate()
12496
12497 FROM PTinfo.tHL7message PH7
12498 WHERE PH7.messageID = @messageID;
12499
12500 SET @newSysID= SCOPE_IDENTITY();
12501
12502 /* add audit record */
12503 EXECUTE Sysinfo.uspAddDataChange @username=@username, @auditTypeID=9, @tableName=''PTinfo.tPatient'', @recordID=@newSysID, @previousEntry=''Manually added from HL7 holding table.'';
12504
12505
12506 /* add the ID table stuff */
12507
12508 INSERT INTO PTinfo.tIdentity
12509 (pSysID, identTypeID, IDvalue)
12510
12511 SELECT @newSysID
12512 , PHI.identTypeID
12513 , PHI.IDvalue
12514
12515 FROM PTinfo.tHL7ID PHI
12516 WHERE PHI.messageID = @messageID;
12517
12518 /* auditing */
12519 INSERT INTO SysInfo.tDataChange
12520 (viewDate, userName, auditTypeID, tableName, recordID, previousEntry)
12521
12522 SELECT getdate()
12523 , @userName
12524 , 9
12525 , ''PTinfo.tIdentity''
12526 , PtI.pID
12527 , ''Manually added from HL7 holding table.''
12528
12529 FROM Ptinfo.tIdentity PtI
12530 WHERE PtI.pSysID = @newSysID;
12531
12532 /* add the active status */
12533 EXECUTE PTinfo.uspEditPatientStatus @pSysID=@newSysID, @pStatTypeID=1, @username=@username;
12534
12535
12536/* add either the child or mother table link */
12537 IF @motherSysID = 0
12538 BEGIN
12539/* add mother info */
12540 INSERT INTO PTinfo.tMomInfo (pSysID, calsPer)
12541 VALUES (@newSysID, 20);
12542
12543 /* add audit record */
12544 SET @theNewID = SCOPE_IDENTITY();
12545 EXECUTE Sysinfo.uspAddDataChange @username=@username, @auditTypeID=9, @tableName=''PTinfo.tMomInfo'', @recordID=@theNewID, @previousEntry=''Manually added from HL7 holding table.'';
12546
12547 END
12548
12549 ELSE
12550
12551 BEGIN
12552
12553 /* add childlink info */
12554 /* get bottle/formula default values */
12555 SELECT @allowFormDef = MAX(CASE WHEN StS.settingID = 14 THEN
12556 CASE WHEN StS.setValue = ''Y'' THEN 1 ELSE 0 END
12557 ELSE 0 END)
12558 , @allowBottleDef = MAX(CASE WHEN StS.settingID = 13 THEN
12559 CASE WHEN StS.setValue = ''Y'' THEN 1 ELSE 0 END
12560 ELSE 0 END)
12561 FROM SysInfo.tSetting StS;
12562
12563 INSERT INTO PTinfo.tChildLink (pSysID, motherSysID, multiple, gender, location, allowFormula, allowBottle)
12564
12565 SELECT @newSysID
12566 , @motherSysID
12567 , 0
12568 , PHM.gender
12569 , PHM.location
12570 , @allowFormDef
12571 , @allowBottleDef
12572
12573 FROM PTinfo.tHL7message PHM
12574
12575 WHERE PHM.messageID = @messageID;
12576 /* add audit record */
12577 SET @theNewID = SCOPE_IDENTITY();
12578
12579 EXECUTE Sysinfo.uspAddDataChange @username=@username, @auditTypeID=9, @tableName=''PTinfo.tChildLink'', @recordID=@theNewID, @previousEntry=''Manually added from HL7 holding table.'';
12580 END
12581
12582 --/* remove records from HL7 table */
12583 DELETE
12584 FROM PTinfo.tHL7ID
12585 WHERE messageID = @messageID;
12586
12587 DELETE FROM PTinfo.tHL7message
12588 WHERE messageID = @messageID;
12589
12590 SET @errCode = 0;
12591 SELECT errCode = @errCode;
12592
12593 END
12594 COMMIT TRANSACTION
12595
12596END TRY
12597BEGIN CATCH
12598ROLLBACK TRANSACTION
12599 SET @errCode = 1;
12600 SELECT errCode = @errCode;
12601END CATCH;
12602')
12603
12604 PRINT N'USP PTinfo.uspManualHL7activate created'
12605
12606END TRY
12607BEGIN CATCH
12608 PRINT N'USP PTinfo.uspManualHL7activate create failed'
12609 PRINT 'Error Number: ' + str(error_number())
12610 PRINT 'Line Number: ' + str(error_line())
12611 PRINT error_message()
12612END CATCH
12613GO
12614
12615/* PTinfo.uspHL7list.storedProcedure.sql
12616To call the SP:
12617EXECUTE PTinfo.uspHL7list @username='admin';
12618 */
12619
12620BEGIN TRY
12621EXECUTE ('/****** Object: StoredProcedure [PTinfo].[uspHL7list] Script Date: 12/07/2012 11:26:19 ******/
12622
12623-- =============================================
12624-- Author: Lisa Bohm (lbohm@paragondsi.com)
12625-- Create date: 2013-09-01
12626-- Description: List of not-activated-yet HL7 messages
12627-- Modified: 2013-12-09 add audit tracking call
12628-- =============================================
12629CREATE PROCEDURE [PTinfo].[uspHL7list]
12630 -- Add the parameters for the stored procedure here
12631@username varchar(50)
12632
12633 AS
12634BEGIN
12635 -- SET NOCOUNT ON added to prevent extra result sets from
12636 -- interfering with SELECT statements.
12637 SET NOCOUNT ON;
12638
12639SELECT PvH.messageID
12640 , PvH.firstName
12641 , PvH.lastName
12642 , PvH.midInit
12643 , PvH.primaryID
12644 , PvH.dob
12645 , PvH.gender
12646 , PvH.location
12647 , PvH.motherID
12648
12649
12650FROM PTinfo.vHL7 PvH;
12651
12652/* audit - multiple records so go with insert */
12653INSERT INTO SysInfo.tRecordView
12654 (viewDate, userName, auditTypeID, pSysID)
12655
12656SELECT getdate()
12657 , @userName
12658 , 13
12659 , PvH.messageID
12660FROM PTinfo.vHL7 PvH;
12661
12662END;
12663')
12664
12665 PRINT N'USP PTinfo.uspHL7list created'
12666
12667END TRY
12668BEGIN CATCH
12669 PRINT N'USP PTinfo.uspHL7list create failed'
12670 PRINT 'Error Number: ' + str(error_number())
12671 PRINT 'Line Number: ' + str(error_line())
12672 PRINT error_message()
12673END CATCH
12674GO
12675
12676/* SysInfo.uspDashInfo.storedProcedure.sql
12677To call the SP:
12678EXECUTE SysInfo.uspDashInfo;
12679 */
12680
12681BEGIN TRY
12682EXECUTE ('/****** Object: StoredProcedure [SysInfo].[uspDashInfo] Script Date: 3/22/2013 11:26:19 ******/
12683-- =============================================
12684-- Author: Lisa Bohm (lbohm@paragondsi.com)
12685-- Create date: 2013-03-22
12686-- Description: Dashboard info
12687-- Modified: 2013-09-05 for 2.0 upgrade
12688-- =============================================
12689CREATE PROCEDURE [SysInfo].[uspDashInfo]
12690 -- Add the parameters for the stored procedure here
12691AS
12692
12693DECLARE @today date
12694DECLARE @tomorrow date
12695
12696 BEGIN
12697 -- SET NOCOUNT ON added to prevent extra result sets from
12698 -- interfering with SELECT statements.
12699 SET NOCOUNT ON;
12700
12701/* today and tomorrow */
12702SET @today = DATEADD(day,0,datediff(day,0,getdate()))
12703SET @tomorrow = DATEADD(day,1,@today)
12704
12705SELECT
12706 infoID = 1
12707 , infoDescript = ''Active Mothers''
12708 , infoTally = COUNT(DISTINCT PvM.pSysID)
12709 FROM PTinfo.vMother PvM
12710
12711UNION ALL
12712
12713 SELECT
12714 infoID = 2
12715 , infoDescript = ''Active Patients''
12716 , infoTally = COUNT(PvC.pSysID)
12717 FROM PTinfo.vChild PvC
12718
12719UNION ALL
12720
12721 SELECT
12722 infoID = 3
12723 , infoDescript = ''Available Containers In Inventory''
12724 , infoTally = COUNT(NtV.vesselID)
12725 FROM Nutrition.tVessel NtV
12726 INNER JOIN Nutrition.tVesselHistory NVH ON NtV.vesselID = NVH.vesselID
12727 INNER JOIN Nutrition.tStatusList NSL ON NVH.statusID = NSL.statusID
12728 WHERE NvH.isCurrent = 1
12729 AND NSL.statCatID = 2
12730 AND NtV.nCategoryID < 4
12731 AND NVH.expirDate > getdate()
12732
12733UNION ALL
12734
12735SELECT
12736 infoID = 4,
12737 infoDescript = ''Feedings Today''
12738 , infoTally = COUNT(NtV.vesselID)
12739
12740 FROM Nutrition.tVessel NtV
12741 INNER JOIN Nutrition.tVesselHistory NVH ON NtV.vesselID = NVH.vesselID
12742 WHERE NvH.isCurrent = 1
12743 AND NvH.dateChange > @today
12744 AND NvH.dateChange < @tomorrow
12745 AND NvH.statusID = 7
12746/*
12747UNION ALL
12748
12749 SELECT
12750 infoID = 5
12751 , infoDescript = ''Expired Containers In Inventory''
12752 , infoTally = COUNT(NtV.vesselID)
12753 FROM Nutrition.tVessel NtV
12754 INNER JOIN Nutrition.tVesselHistory NVH ON NtV.vesselID = NVH.vesselID
12755 INNER JOIN Nutrition.tStatusList NSL ON NVH.statusID = NSL.statusID
12756 WHERE NvH.isCurrent = 1
12757 AND ((NSL.statCatID = 2
12758 AND NVH.expirDate < getdate())
12759 OR NSL.statCatID = 3)
12760 AND NtV.nCategoryID < 3*/
12761 ;
12762END;
12763
12764')
12765
12766 PRINT N'USP SysInfo.uspDashInfo created'
12767
12768END TRY
12769BEGIN CATCH
12770 PRINT N'USP SysInfo.uspDashInfo create failed'
12771 PRINT 'Error Number: ' + str(error_number())
12772 PRINT 'Line Number: ' + str(error_line())
12773 PRINT error_message()
12774END CATCH
12775GO
12776
12777/* PTinfo.uspAddEditDocOrder.storedProcedure.sql
12778To call the SP:
12779EXECUTE PTinfo.uspAddEditDocOrder @docOrderID=0, @pSysID=4, @orderNum='ORD131009', @orderTypeID=1, @orderNote='More Information here', @orderStart='20131119', @orderEnd='20131125', @username='admin';
12780Please use @docOrderID=0 for new, actual @docOrderID for edit.
12781 */
12782
12783BEGIN TRY
12784EXECUTE ('/****** Object: StoredProcedure [PTinfo].[uspAddEditDocOrder] Script Date: 09/05/2013 11:26:19 ******/
12785-- =============================================
12786-- Author: Lisa Bohm (lbohm@paragondsi.com)
12787-- Create date: 2013-09-05
12788-- Description: Add/Edit a mother''s addtl info
12789-- Modified: 2013-12-05 add audit tracking call
12790-- =============================================
12791CREATE PROCEDURE [PTinfo].[uspAddEditDocOrder]
12792 -- Add the parameters for the stored procedure here
12793@docOrderID bigint = 0
12794, @pSysID bigint
12795, @orderNum varchar(50)
12796, @orderTypeID tinyint
12797, @orderNote varchar(500)
12798, @orderStart datetime = ''1900-01-01''
12799, @orderEnd datetime= ''2500-01-01''
12800, @username varchar(50)
12801
12802AS
12803 DECLARE
12804 @already bit
12805 , @errCode tinyint
12806 , @prevEntry varchar(1000);
12807BEGIN
12808 -- SET NOCOUNT ON added to prevent extra result sets from
12809 -- interfering with SELECT statements.
12810 SET NOCOUNT ON;
12811
12812IF @orderStart = ''19000101''
12813 BEGIN
12814 SET @orderStart = getdate();
12815 END
12816
12817/* CANNOT do merge! Does doc order info already exist? */
12818SET @already = COALESCE(
12819 (SELECT 1
12820 FROM PTinfo.tDocOrder PDO
12821 WHERE PDO.docOrderID = @docOrderID), 0)
12822
12823IF @already = 1
12824 -- doc order info exists so edit. --
12825 BEGIN
12826 /* audit information */
12827 SELECT @prevEntry = PDO.orderNum + ''; '' + CAST(PDO.orderTypeID AS VARCHAR) + ''; '' + PDO.orderNote + ''; '' + CAST(PDO.orderEnd AS VARCHAR) + ''.''
12828 FROM PTinfo.tDocOrder PDO
12829 WHERE PDO.docOrderID = @docOrderID;
12830
12831 UPDATE PTinfo.tDocOrder
12832 SET orderNote = @orderNote
12833 , orderEnd = @orderEnd
12834 WHERE docOrderID = @docOrderID;
12835
12836 EXECUTE Sysinfo.uspAddDataChange @username=@username, @auditTypeID=10, @tableName=''PTinfo.tDocOrder'', @recordID=@docOrderID, @previousEntry=@prevEntry;
12837
12838 SET @errCode = 0
12839 SELECT errCode = @errCode;
12840 END
12841ELSE
12842 BEGIN
12843 -- mom info doesn''t exist so add --
12844 BEGIN TRY
12845 INSERT INTO PTinfo.tDocOrder
12846 (pSysID
12847 , orderNum
12848 , orderTypeID
12849 , orderNote
12850 , orderStart
12851 , orderEnd
12852 )
12853 VALUES (@pSysID
12854 , @orderNum
12855 , @orderTypeID
12856 , @orderNote
12857 , @orderStart
12858 , @orderEnd
12859 );
12860
12861 SET @docOrderID = SCOPE_IDENTITY();
12862
12863 EXECUTE Sysinfo.uspAddDataChange @username=@username, @auditTypeID=9, @tableName=''PTinfo.tDocOrder'', @recordID=@docOrderID, @previousEntry=''N/A'';
12864
12865 SET @errCode = 0
12866 SELECT errCode = @errCode;
12867 END TRY
12868
12869 BEGIN CATCH
12870 SET @errCode = 1
12871 SELECT errCode = @errCode;
12872 PRINT N''Err: Data not successfully added or updated.''
12873 END CATCH
12874 END
12875END;
12876')
12877
12878 PRINT N'USP PTinfo.uspAddEditDocOrder created'
12879
12880END TRY
12881BEGIN CATCH
12882 PRINT N'USP PTinfo.uspAddEditDocOrder create failed'
12883 PRINT 'Error Number: ' + str(error_number())
12884 PRINT 'Line Number: ' + str(error_line())
12885 PRINT error_message()
12886END CATCH
12887GO
12888
12889/* PTinfo.uspDocOrderByPT.storedProcedure.sql
12890To call the SP:
12891EXECUTE PTinfo.uspDocOrderByPT @pSysID=4, @username='admin';
12892 */
12893
12894BEGIN TRY
12895EXECUTE ('/****** Object: StoredProcedure [PTinfo].[uspDocOrderByPT] Script Date: 09/05/2013 11:26:19 ******/
12896
12897-- =============================================
12898-- Author: Lisa Bohm (lbohm@paragondsi.com)
12899-- Create date: 2013-09-05
12900-- Description: Current, in effect doc orders for a patient
12901-- Modified: 2013-12-05 add audit tracking call
12902-- =============================================
12903CREATE PROCEDURE [PTinfo].[uspDocOrderByPT]
12904 -- Add the parameters for the stored procedure here
12905 @pSysID bigint
12906 , @username varchar(50)
12907
12908AS
12909BEGIN
12910 -- SET NOCOUNT ON added to prevent extra result sets from
12911 -- interfering with SELECT statements.
12912 SET NOCOUNT ON;
12913
12914SELECT PDO.docOrderID
12915 , PDO.pSysID
12916 , PDO.orderNum
12917 , PDO.orderTypeID
12918 , POT.orderTypeName
12919 , PDO.orderNote
12920 , PDO.orderStart
12921 , PDO.orderEnd
12922
12923FROM PTinfo.tDocOrder PDO
12924 INNER JOIN PTinfo.tOrderType POT ON PDO.orderTypeID = POT.orderTypeID
12925WHERE PDO.pSysID = @pSysID
12926 AND PDO.orderStart <= getdate()
12927 AND PDO.orderEnd > getdate();
12928
12929 EXECUTE SysInfo.uspAddRecordView @username=@username, @auditTypeID=7, @pSysID=@pSysID;
12930END;
12931')
12932
12933 PRINT N'USP PTinfo.uspDocOrderByPT created'
12934
12935END TRY
12936BEGIN CATCH
12937 PRINT N'USP PTinfo.uspDocOrderByPT create failed'
12938 PRINT 'Error Number: ' + str(error_number())
12939 PRINT 'Line Number: ' + str(error_line())
12940 PRINT error_message()
12941END CATCH
12942GO
12943
12944/* PTinfo.uspMotherNoBin.storedProcedure.sql
12945To call the SP:
12946EXECUTE PTinfo.uspMotherNoBin @username='admin';
12947 */
12948
12949BEGIN TRY
12950EXECUTE ('/****** Object: StoredProcedure [PTinfo].[uspMotherNoBin] Script Date: 04/28/2013 11:26:19 ******/
12951
12952-- =============================================
12953-- Author: Lisa Bohm (lbohm@paragondsi.com)
12954-- Create date: 2013-04-28
12955-- Description: Returns Mothers with no bin assigned
12956-- Modified: 2013-09-18 Updated for Q3 2013 upgrade
12957-- 2013-12-09 add audit tracking call
12958-- =============================================
12959CREATE PROCEDURE [PTinfo].[uspMotherNoBin]
12960 -- Add the parameters for the stored procedure here
12961@username varchar(50)
12962
12963 AS
12964 BEGIN
12965 -- SET NOCOUNT ON added to prevent extra result sets from
12966 -- interfering with SELECT statements.
12967 SET NOCOUNT ON;
12968
12969
12970/* get the mothers without bins */
12971SELECT DISTINCT PvM.pSysID
12972 , PvM.primaryID
12973 , PvM.firstName
12974 , PvM.lastName
12975 , PvM.recordAdded
12976
12977FROM PTinfo.vMother PvM
12978WHERE NOT EXISTS (SELECT SLA.pSysID
12979 FROM Storage.tLocationAssign SLA
12980 WHERE PvM.pSysID = SLA.pSysID);
12981
12982/* audit - multiple records so go with insert */
12983INSERT INTO SysInfo.tRecordView
12984 (viewDate, userName, auditTypeID, pSysID)
12985
12986SELECT DISTINCT getdate()
12987 , @userName
12988 , 7
12989 , PvM.pSysID
12990
12991FROM PTinfo.vMother PvM
12992WHERE NOT EXISTS (SELECT SLA.pSysID
12993 FROM Storage.tLocationAssign SLA
12994 WHERE PvM.pSysID = SLA.pSysID);
12995END;
12996')
12997
12998 PRINT N'USP PTinfo.uspMotherNoBin created'
12999
13000END TRY
13001BEGIN CATCH
13002 PRINT N'USP PTinfo.uspMotherNoBin create failed'
13003 PRINT 'Error Number: ' + str(error_number())
13004 PRINT 'Line Number: ' + str(error_line())
13005 PRINT error_message()
13006END CATCH
13007GO
13008
13009/* Nutrition.uspInactiveFormula.storedProcedure.sql
13010To call the SP:
13011EXECUTE Nutrition.uspInactiveFormula @formulaID=2, @username='admin';
13012 */
13013
13014BEGIN TRY
13015EXECUTE ('/****** Object: StoredProcedure [Nutrition].[uspInactiveFormula] Script Date: 8/27/2013 11:26:19 ******/
13016-- =============================================
13017-- Author: Lisa Bohm (lbohm@paragondsi.com)
13018-- Create date: 2013-10-01
13019-- Description: inactivate formula.
13020-- Modified: 2013-12-05 add audit tracking call
13021-- =============================================
13022CREATE PROCEDURE [Nutrition].[uspInactiveFormula]
13023 -- Add the parameters for the stored procedure here
13024 @formulaID smallint
13025 , @username varchar(50)
13026 AS
13027 DECLARE @errCode tinyint;
13028 BEGIN
13029 -- SET NOCOUNT ON added to prevent extra result sets from
13030 -- interfering with SELECT statements.
13031 SET NOCOUNT ON;
13032
13033 UPDATE Nutrition.tFormula
13034 SET isActive = 0
13035 WHERE formulaID = @formulaID;
13036
13037 EXECUTE Sysinfo.uspAddDataChange @username=@username, @auditTypeID=10, @tableName=''Nutrition.tFormula'', @recordID=@formulaID, @previousEntry=''isActive=1'';
13038
13039 SET @errCode = 0
13040 SELECT errCode = @errCode;
13041
13042END;
13043')
13044
13045 PRINT N'USP Nutrition.uspInactiveFormula created'
13046
13047END TRY
13048BEGIN CATCH
13049 PRINT N'USP Nutrition.uspInactiveFormula create failed'
13050 PRINT 'Error Number: ' + str(error_number())
13051 PRINT 'Line Number: ' + str(error_line())
13052 PRINT error_message()
13053END CATCH
13054GO
13055
13056/* PTinfo.uspDelHL7byDate.storedProcedure.sql
13057To call the SP:
13058EXECUTE PTinfo.uspDelHL7byDate @daysAgo=3;
13059 */
13060
13061BEGIN TRY
13062EXECUTE ('/****** Object: StoredProcedure [PTinfo].[uspDelHL7byDate] Script Date: 12/07/2012 11:26:19 ******/
13063
13064-- =============================================
13065-- Author: Lisa Bohm (lbohm@paragondsi.com)
13066-- Create date: 2013-10-09
13067-- Description: Delete unmatched HL7 messages by date
13068--
13069-- =============================================
13070CREATE PROCEDURE [PTinfo].[uspDelHL7byDate]
13071 -- Add the parameters for the stored procedure here
13072@messageID bigint
13073
13074AS
13075DECLARE @errCode tinyint;
13076
13077BEGIN
13078 -- SET NOCOUNT ON added to prevent extra result sets from
13079 -- interfering with SELECT statements.
13080 SET NOCOUNT ON;
13081
13082 BEGIN TRY
13083
13084 DELETE
13085 FROM PTinfo.tHL7ID
13086 WHERE messageID = @messageID;
13087
13088 DELETE
13089 FROM PTinfo.tHL7message
13090 WHERE messageID = @messageID;
13091
13092 SET @errCode = 0
13093 SELECT errCode = @errCode;
13094
13095
13096 END TRY
13097
13098 BEGIN CATCH
13099 SET @errCode = 1
13100 SELECT errCode = @errCode;
13101 PRINT N''Err: Record not successfully deleted.''
13102 END CATCH
13103
13104END;
13105')
13106
13107 PRINT N'USP PTinfo.uspDelHL7byDate created'
13108
13109END TRY
13110BEGIN CATCH
13111 PRINT N'USP PTinfo.uspDelHL7byDate create failed'
13112 PRINT 'Error Number: ' + str(error_number())
13113 PRINT 'Line Number: ' + str(error_line())
13114 PRINT error_message()
13115END CATCH
13116GO
13117
13118/* PTinfo.uspDelHL7byID.storedProcedure.sql
13119To call the SP:
13120EXECUTE PTinfo.uspDelHL7byID @messageID=3;
13121 */
13122
13123BEGIN TRY
13124EXECUTE ('/****** Object: StoredProcedure [PTinfo].[uspDelHL7byID] Script Date: 12/07/2012 11:26:19 ******/
13125
13126-- =============================================
13127-- Author: Lisa Bohm (lbohm@paragondsi.com)
13128-- Create date: 2013-10-09
13129-- Description: Delete unmatched HL7 messages by date
13130--
13131-- =============================================
13132CREATE PROCEDURE [PTinfo].[uspDelHL7byID]
13133 -- Add the parameters for the stored procedure here
13134@messageID bigint
13135
13136AS
13137DECLARE @errCode tinyint;
13138
13139BEGIN
13140 -- SET NOCOUNT ON added to prevent extra result sets from
13141 -- interfering with SELECT statements.
13142 SET NOCOUNT ON;
13143
13144 BEGIN TRY
13145
13146 DELETE
13147 FROM PTinfo.tHL7ID
13148 WHERE messageID = @messageID;
13149
13150 DELETE
13151 FROM PTinfo.tHL7message
13152 WHERE messageID = @messageID;
13153
13154 SET @errCode = 0
13155 SELECT errCode = @errCode;
13156
13157
13158 END TRY
13159
13160 BEGIN CATCH
13161 SET @errCode = 1
13162 SELECT errCode = @errCode;
13163 PRINT N''Err: Record not successfully deleted.''
13164 END CATCH
13165
13166END;
13167')
13168
13169 PRINT N'USP PTinfo.uspDelHL7byID created'
13170
13171END TRY
13172BEGIN CATCH
13173 PRINT N'USP PTinfo.uspDelHL7byID create failed'
13174 PRINT 'Error Number: ' + str(error_number())
13175 PRINT 'Line Number: ' + str(error_line())
13176 PRINT error_message()
13177END CATCH
13178GO
13179
13180/* Nutrition.uspParentVessel.storedProcedure.sql
13181To call the SP:
13182EXECUTE Nutrition.uspParentVessel @vesselCode='B131';
13183 */
13184
13185BEGIN TRY
13186EXECUTE ('/****** Object: StoredProcedure [Nutrition].[uspParentVessel] Script Date: 7/16/2013 10:40:17 AM ******/
13187-- =============================================
13188-- Author:Lisa Bohm (lbohm@paragondsi.com)
13189-- Create date: 2013-11-13
13190-- Description:Returns list of parent vessels; avoid looping
13191-- Modified: 2013-12-16 errorMsg to errCode
13192-- =============================================
13193CREATE PROCEDURE [Nutrition].[uspParentVessel]
13194-- Add the parameters for the stored procedure here
13195@vesselCode varchar(50)
13196
13197AS
13198DECLARE @auditBottle bigint
13199
13200BEGIN
13201-- SET NOCOUNT ON added to prevent extra result sets from
13202-- interfering with SELECT statements.
13203SET NOCOUNT ON;
13204
13205SET @auditBottle = COALESCE((SELECT NtV.vesselID
13206 FROM Nutrition.tVessel NtV
13207 WHERE NtV.vesselCode = @vesselCode),0);
13208
13209IF @auditBottle > 0
13210BEGIN
13211
13212WITH cteBuildPath AS
13213 (-- the anchor part now is just for the original bottle
13214 SELECT oVessel = @auditBottle
13215 , tgtVessel = NtV.vesselID
13216 FROM Nutrition.tVessel NtV
13217 WHERE NtV.vesselID = @auditBottle
13218
13219 /* the recursive part */
13220 UNION ALL
13221
13222 SELECT oVessel = NVOl.vesselID
13223 , tgtVessel = NVOl.targetVesselID
13224 FROM Nutrition.tVesselOrigin NVOl
13225 INNER JOIN cteBuildPath AS cte ON cte.oVessel = NVOl.targetVesselID
13226 )
13227
13228SELECT parentVessel = oVessel
13229FROM cteBuildPath;
13230
13231END
13232END;
13233')
13234
13235 PRINT N'USP Nutrition.uspParentVessel created'
13236
13237END TRY
13238BEGIN CATCH
13239 PRINT N'USP Nutrition.uspParentVessel create failed'
13240 PRINT 'Error Number: ' + str(error_number())
13241 PRINT 'Line Number: ' + str(error_line())
13242 PRINT error_message()
13243END CATCH
13244GO
13245
13246
13247/* Nutrition.uspAddEditFormulaLot.storedProcedure.sql
13248To call the SP:
13249EXECUTE Nutrition.uspAddEditFormulaLot @formlotID=0, @formulaID=1, @formlotCode='FL131009', @username='admin';
13250Please use @formlotID=0 for new, actual @formlotID for edit.
13251 */
13252
13253BEGIN TRY
13254EXECUTE ('/****** Object: StoredProcedure [Nutrition].[uspAddEditFormulaLot] Script Date: 8/12/2013 11:26:19 ******/
13255-- =============================================
13256-- Author: Lisa Bohm (lbohm@paragondsi.com)
13257-- Create date: 2013-10-30
13258-- Description: add/edit Formula lot to list.
13259-- Modified: 2013-12-05 add audit tracking call
13260-- =============================================
13261CREATE PROCEDURE [Nutrition].[uspAddEditFormulaLot]
13262 -- Add the parameters for the stored procedure here
13263@formlotID int
13264, @formulaID tinyint
13265, @formLotCode varchar(25)
13266, @username varchar(50)
13267
13268 AS
13269 DECLARE @already int
13270 , @errCode tinyint
13271 , @newFormLotID int
13272 , @prevEntry varchar(1000);
13273
13274 BEGIN
13275 -- SET NOCOUNT ON added to prevent extra result sets from
13276 -- interfering with SELECT statements.
13277 SET NOCOUNT ON;
13278SET @already = COALESCE((SELECT NFL.formLotID
13279 FROM Nutrition.tFormulaLot NFL
13280 WHERE NFL.formLotID <> @formLotID
13281 AND NFL.formulaID = @formulaID
13282 AND NFL.formLotCode= @formlotCode),0)
13283IF @already > 0
13284 BEGIN
13285 /* there is already a lot for this formula with this lot code */
13286 SET @errCode = 0
13287 SELECT errCode = @errCode
13288 , formLotID = @already;
13289 END
13290ELSE
13291 BEGIN
13292 IF @formLotID = 0
13293 BEGIN
13294 IF LEN(RTRIM(LTRIM(@formLotCode))) = 0
13295 BEGIN
13296 SELECT @newFormLotID = NFL.formLotID
13297 FROM Nutrition.tFormulaLot NFL
13298 WHERE NFL.formulaID = @formulaID
13299 AND NFL.formLotCode = ''Unspecified Lot'';
13300
13301 SET @errCode = 0
13302 SELECT errCode = @errCode
13303 , formLotID = @newformLotID;
13304
13305 END
13306 ELSE
13307 BEGIN
13308 INSERT INTO Nutrition.tFormulaLot
13309 ([formulaID]
13310 ,[formLotCode])
13311 VALUES
13312 (@formulaID
13313 , @formLotCode);
13314
13315 SET @newformLotID = SCOPE_IDENTITY()
13316
13317 EXECUTE Sysinfo.uspAddDataChange @username=@username, @auditTypeID=9, @tableName=''Nutrition.tFormulaLot'', @recordID=@newformLotID, @previousEntry=''N/A'';
13318
13319 SET @errCode = 0
13320 SELECT errCode = @errCode
13321 , formLotID = @newformLotID;
13322 END
13323 END
13324 ELSE
13325 BEGIN
13326 /* set up audit previous record */
13327 SELECT @prevEntry = NFL.formLotcode + ''.''
13328 FROM Nutrition.tFormulaLot NFL
13329 WHERE NFL.formLotID = @formLotID;
13330
13331 UPDATE Nutrition.tFormulaLot
13332 SET formLotCode= @formlotCode
13333 WHERE formLotID = @formLotID;
13334
13335 EXECUTE Sysinfo.uspAddDataChange @username=@username, @auditTypeID=10, @tableName=''Nutrition.tFormulaLot'', @recordID=@formLotID, @previousEntry=@prevEntry;
13336
13337 SET @newformLotID = @formLotID;
13338 SET @errCode = 0;
13339 SELECT errCode = @errCode
13340 , formLotID = @newformLotID;
13341 END
13342 END
13343
13344END;
13345')
13346
13347 PRINT N'USP Nutrition.uspAddEditFormulaLot created'
13348
13349END TRY
13350BEGIN CATCH
13351 PRINT N'USP Nutrition.uspAddEditFormulaLot create failed'
13352 PRINT 'Error Number: ' + str(error_number())
13353 PRINT 'Line Number: ' + str(error_line())
13354 PRINT error_message()
13355END CATCH
13356GO
13357
13358/* report SPs */
13359/* Rpt.uspDates.storedProcedure.sql
13360To call the SP:
13361EXECUTE Rpt.uspDates @startDate='20131101', @endDate='20131130';
13362 */
13363
13364BEGIN TRY
13365EXECUTE ('/****** Object: StoredProcedure [Rpt].[uspDates] Script Date: 8/27/2013 11:26:19 ******/
13366
13367-- =============================================
13368-- Author: Lisa Bohm (lbohm@paragondsi.com)
13369-- Create date: 2013-10-21
13370-- Description: return date parameters as recordset
13371--
13372-- =============================================
13373CREATE PROCEDURE [Rpt].[uspDates]
13374 -- Add the parameters for the stored procedure here
13375@startDate date
13376, @endDate date
13377
13378 AS
13379 BEGIN
13380 -- SET NOCOUNT ON added to prevent extra result sets from
13381 -- interfering with SELECT statements.
13382 SET NOCOUNT ON;
13383
13384SELECT startDate = @startDate
13385 , endDate = @endDate;
13386
13387END;
13388')
13389
13390 PRINT N'USP Rpt.uspDates created'
13391
13392END TRY
13393BEGIN CATCH
13394 PRINT N'USP Rpt.uspDates create failed'
13395 PRINT 'Error Number: ' + str(error_number())
13396 PRINT 'Line Number: ' + str(error_line())
13397 PRINT error_message()
13398END CATCH
13399GO
13400/* Rpt.uspAvailEBM.storedProcedure.sql
13401To call the SP:
13402EXECUTE Rpt.uspAvailEBM @username='admin';
13403 */
13404
13405BEGIN TRY
13406EXECUTE ('-- =============================================
13407-- Author: Lisa Bohm (lbohm@paragondsi.com)
13408-- Create date: 2013-10-09
13409-- Description: Returns milk inventory
13410-- Modified: 2013-11-21 to handle the HL7MsgField tbl add
13411-- 2013-12-09 add audit tracking call
13412-- 2014-02-24 recd date fixing....
13413-- 2014-03-19 added formula
13414-- =============================================
13415CREATE PROCEDURE [Rpt].[uspAvailEBM]
13416 -- Add the parameters for the stored procedure here
13417 @username varchar(50)
13418 AS
13419 DECLARE @priPID tinyint
13420 BEGIN
13421 -- SET NOCOUNT ON added to prevent extra result sets from
13422 -- interfering with SELECT statements.
13423 SET NOCOUNT ON;
13424
13425/* find primary ID */
13426SET @priPID = PTinfo.udfGetPIDtype();
13427
13428WITH cteContents AS
13429( -- anchor
13430SELECT contVessel = NtV.vesselID
13431 , oVessel = NtV.vesselID
13432 , NtV.vesselCode
13433 , expDate = NVH.expirDate
13434 , NvH.statusID
13435 , NSL.statCatID
13436
13437FROM Nutrition.tVessel NtV
13438 INNER JOIN Nutrition.tVesselHistory NVH ON NtV.vesselID = NVH.vesselID
13439 INNER JOIN Nutrition.tStatusList NSL ON NVH.statusID = NSL.statusID
13440
13441WHERE NvH.isCurrent = 1
13442 AND NSL.statCatID = 2
13443 AND NvH.expirDate > getdate()
13444
13445UNION ALL
13446 SELECT contVessel = NVOl.vesselID
13447 , oVessel = cte.oVessel
13448 , vesselCode = cte.vesselCode
13449 , expDate = cte.expDate
13450 , statusID = cte.statusID
13451 , statCatID = cte.statCatID
13452
13453
13454 FROM Nutrition.tVesselOrigin NVOl
13455 INNER JOIN cteContents AS cte ON cte.contVessel = NVOl.targetVesselID
13456)
13457
13458SELECT vesselID = cte.oVessel
13459 , cte.vesselCode
13460 , vesselCont = NtC.nCategoryAbbrev
13461 , cte.expDate
13462 , NSL.statusName
13463 , NtV.mls
13464 , NtV.gms
13465 , locationCode = StL.locationName
13466 , PtP.firstName
13467 , PtP.lastName
13468 , PtP.midInit
13469 , primaryID = PtI.IDvalue
13470 , lotCode = CASE WHEN NDL.lotCode IS NOT NULL THEN
13471 CASE WHEN LEN(RTRIM(LTRIM(NDL.lotCode))) > 0 THEN NDL.lotCode ELSE ''Unspecified lot'' END
13472 ELSE
13473 CASE WHEN NFL.formLotCode IS NOT NULL THEN NFL.formLotCode ELSE NULL END
13474 END
13475 , bankName = CASE WHEN NDL.lotCode IS NOT NULL THEN NDB.bankName ELSE
13476 CASE WHEN NFL.formLotCode IS NOT NULL THEN NF.formulaName ELSE NULL END
13477 END
13478 , recdDate = MAX(CASE WHEN NVH.dateChange IS NULL THEN ''19000101'' ELSE NVH.dateChange END)
13479
13480FROM cteContents cte
13481 INNER JOIN Nutrition.tVessel NtV ON cte.oVessel = NtV.vesselID
13482 INNER JOIN Nutrition.tVessel NtVC ON cte.contVessel = NtVC.vesselID
13483 INNER JOIN Storage.tLocation StL ON NtV.locationID = StL.locationID
13484 INNER JOIN Nutrition.tCategory NtC ON NtVC.nCategoryID = NtC.nCategoryID
13485 INNER JOIN Nutrition.tStatusList NSL ON cte.statusID = NSL.statusID
13486 LEFT OUTER JOIN Nutrition.tVesselHistory NVH ON cte.contVessel = NVH.vesselID
13487 AND NvH.statusID = 17
13488 LEFT OUTER JOIN (Nutrition.tMotherVessel NMV
13489 INNER JOIN PTinfo.tPatient PtP ON PtP.pSysID = NmV.pSysID
13490 INNER JOIN PTinfo.tIdentity PtI ON PtP.pSysID = PtI.pSysID
13491 AND PtI.identTypeID = @priPID
13492 ) ON cte.contVessel = NMV.vesselID
13493 LEFT OUTER JOIN (Nutrition.tDonorVessel NDV
13494 INNER JOIN Nutrition.tDonorLot NDL ON NDV.lotID = NDL.lotID
13495 INNER JOIN Nutrition.tDonorBank NDB ON NDL.donorBankID = NDB.donorBankID
13496 ) ON cte.contVessel = NDV.vesselID
13497 LEFT OUTER JOIN (Nutrition.tFormulaVessel NFV
13498 INNER JOIN Nutrition.tFormulaLot NFL ON NFV.formLotID = NFL.formLotID
13499 INNER JOIN Nutrition.tFormula NF ON NFL.formulaID = NF.formulaID
13500 ) ON cte.contVessel = NFV.vesselID
13501
13502WHERE (NMV.vesselID IS NOT NULL
13503 OR NDV.vesselID IS NOT NULL
13504 OR NFV.vesselID IS NOT NULL)
13505
13506GROUP BY cte.oVessel
13507 , cte.vesselCode
13508 , NtC.nCategoryAbbrev
13509 , cte.expDate
13510 , NSL.statusName
13511 , NtV.mls
13512 , NtV.gms
13513 , StL.locationName
13514 , PtP.firstName
13515 , PtP.lastName
13516 , PtP.midInit
13517 , PtI.IDvalue
13518 , lotCode
13519 , bankName
13520 , NFL.formLotCode
13521 , NF.formulaName;
13522
13523EXECUTE Sysinfo.uspAddReportView @username=@username, @auditTypeID=8, @reportName=''Available EBM'', @startDate=''19000101'', @endDate=''25000101'', @addtlParamValue=''N/A'';
13524
13525END;
13526')
13527
13528 PRINT N'USP Rpt.uspAvailEBM created'
13529
13530END TRY
13531BEGIN CATCH
13532 PRINT N'USP Rpt.uspAvailEBM create failed'
13533 PRINT 'Error Number: ' + str(error_number())
13534 PRINT 'Line Number: ' + str(error_line())
13535 PRINT error_message()
13536END CATCH
13537GO
13538
13539/* Rpt.uspAvailEBMByID.storedProcedure.sql
13540To call the SP:
13541EXECUTE Rpt.uspAvailEBMByID @pSysID=12, @username='admin';
13542 */
13543
13544BEGIN TRY
13545EXECUTE ('/****** Object: StoredProcedure [Rpt].[uspAvailEBMByID] Script Date: 10/09/2013 11:26:19 ******/
13546-- =============================================
13547-- Author: Lisa Bohm (lbohm@paragondsi.com)
13548-- Create date: 2013-10-09
13549-- Description: Returns milk inventory by mother
13550-- Modified: 2013-11-21 to handle the HL7MsgField tbl add
13551-- 2013-12-09 add audit tracking call
13552-- 2014-02-13 add donor/formula vessels assigned to child
13553-- 2014-03-18 add expired but not disposed milk
13554-- =============================================
13555CREATE PROCEDURE [Rpt].[uspAvailEBMByID]
13556 -- Add the parameters for the stored procedure here
13557@pSysID bigint
13558, @username varchar(50)
13559 AS
13560 DECLARE @priPID tinyint
13561 DECLARE @contents TABLE (vesselID bigint, vesselCode varchar(25), expirDate datetime, recdDate datetime, statusID tinyint, statCatID tinyint, pSysID bigint);
13562 BEGIN
13563 -- SET NOCOUNT ON added to prevent extra result sets from
13564 -- interfering with SELECT statements.
13565 SET NOCOUNT ON;
13566
13567/* find primary ID */
13568SET @priPID = PTinfo.udfGetPIDtype();
13569
13570WITH cteContents AS
13571( -- anchor
13572SELECT rtVessel = NtV.vesselID
13573 , cVessel = NtV.vesselID
13574 , NtV.vesselCode
13575 , NVH.expirDate
13576 , recdDate = NvR.dateChange
13577 , NVH.statusID
13578 , NSL.statCatID
13579 , NMV.pSysID
13580
13581FROM Nutrition.tVessel NtV
13582 INNER JOIN Nutrition.tVesselHistory NVH ON NtV.vesselID = NVH.vesselID
13583 INNER JOIN Nutrition.tStatusList NSL ON NVH.statusID = NSL.statusID
13584 INNER JOIN Nutrition.tMotherVessel NMV ON NtV.vesselID = NMV.vesselID
13585 LEFT OUTER JOIN Nutrition.tVesselHistory NvR ON NtV.vesselID = NvR.vesselID
13586 AND NVR.statusID = 17
13587
13588WHERE NvH.isCurrent = 1
13589 AND NMV.pSysID = @pSysID
13590
13591UNION ALL
13592 SELECT rtVessel = cte.rtVessel
13593 , cVessel = NVOl.targetVesselID
13594 , NtVl.vesselCode
13595 , NvHl.expirDate
13596 , cte.recdDate
13597 , NvHl.statusID
13598 , NSLl.statCatID
13599 , cte.pSysID
13600
13601 FROM Nutrition.tVesselOrigin NVOl
13602 INNER JOIN Nutrition.tVessel NtVl ON NVOl.targetVesselID = NtVl.vesselID
13603 INNER JOIN Nutrition.tVesselHistory NVHl ON NtVL.vesselID = NVHl.vesselID
13604 INNER JOIN Nutrition.tStatusList NSLl ON NVHl.statusID = NSLl.statusID
13605 INNER JOIN cteContents AS cte ON cte.cVessel = NVOl.vesselID
13606
13607 WHERE NvHl.isCurrent = 1
13608
13609)
13610
13611INSERT INTO @contents (vesselID, vesselCode, expirDate, recdDate, statusID, statCatID, pSysID)
13612
13613SELECT vesselID = cte.cVessel
13614 , cte.vesselCode
13615 , cte.expirDate
13616 , cte.recdDate
13617 , cte.statusID
13618 , cte.statCatID
13619 , cte.pSysID
13620FROM cteContents cte
13621 WHERE cte.statCatID >= 2
13622 AND cte.statCatID <= 3;
13623
13624/* what about other containers that have been assigned to any child of the mother? */
13625INSERT INTO @contents (vesselID, vesselCode, expirDate, recdDate, statusID, statCatID, pSysID)
13626
13627SELECT NtV.vesselID
13628 , NtV.vesselCode
13629 , NVH.expirDate
13630 , NvR.dateChange
13631 , NVH.statusID
13632 , NSL.statCatID
13633 , 0
13634
13635FROM Nutrition.tVessel NtV
13636 INNER JOIN Nutrition.tVesselAssign NVA ON NtV.vesselID = NVA.vesselID
13637 INNER JOIN PtInfo.tChildLink PCL ON NVA.pSysID = PCL.pSysID
13638 INNER JOIN Nutrition.tVesselHistory NVH ON NtV.vesselID = NVH.vesselID
13639 INNER JOIN Nutrition.tStatusList NSL ON NVH.statusID = NSL.statusID
13640 LEFT OUTER JOIN Nutrition.tVesselHistory NvR ON NtV.vesselID = NvR.vesselID
13641 AND NVR.statusID = 17
13642
13643 WHERE NSL.statCatID >= 2
13644 AND NSL.statCatID <= 3
13645 AND NVH.isCurrent = 1
13646 AND PCL.motherSysID = @pSysID
13647 AND NOT EXISTS (SELECT CT.vesselID
13648 FROM @contents CT
13649 WHERE CT.vesselID = NtV.vesselID);
13650
13651SELECT ct.vesselID
13652 , ct.vesselCode
13653 , vesselCont = NtC.nCategoryAbbrev
13654 , expDate = CT.expirDate
13655 , NSL.statusName
13656 , NtV.mls
13657 , NtV.gms
13658 , locationCode = StL.locationName
13659 , PtP.firstName
13660 , PtP.lastName
13661 , PtP.midInit
13662 , primaryID = PtI.IDvalue
13663 , recdDate = MAX(CASE WHEN NVH.dateChange IS NULL THEN CT.recdDate ELSE NVH.dateChange END)
13664
13665FROM @contents CT
13666 INNER JOIN Nutrition.tVessel NtV ON CT.vesselID = NtV.vesselID
13667 INNER JOIN Storage.tLocation StL ON NtV.locationID = StL.locationID
13668 INNER JOIN Nutrition.tCategory NtC ON NtV.nCategoryID = NtC.nCategoryID
13669 LEFT OUTER JOIN Nutrition.tVesselHistory NVH ON NtV.vesselID = NVH.vesselID
13670 AND NVH.statusID = 17
13671 INNER JOIN Nutrition.tStatusList NSL ON CT.statusID = NSL.statusID
13672 LEFT OUTER JOIN (Nutrition.tMotherVessel NMV
13673 INNER JOIN PTinfo.tPatient PtP ON PtP.pSysID = NmV.pSysID
13674 INNER JOIN PTinfo.tIdentity PtI ON PtP.pSysID = PtI.pSysID
13675 ) ON CT.pSysID = NMV.pSysID
13676 AND PtI.identTypeID = @priPID
13677
13678GROUP BY ct.vesselID
13679 , ct.vesselCode
13680 , NtC.nCategoryAbbrev
13681 , CT.expirDate
13682 , NSL.statusName
13683 , NtV.mls
13684 , NtV.gms
13685 , StL.locationName
13686 , PtP.firstName
13687 , PtP.lastName
13688 , PtP.midInit
13689 , PtI.IDvalue;
13690
13691EXECUTE Sysinfo.uspAddReportView @username=@username, @auditTypeID=8, @reportName=''Available EBM By Mother'', @startDate=''19000101'', @endDate=''25000101'', @addtlParamValue=@pSysID;
13692
13693END;
13694')
13695
13696 PRINT N'USP Rpt.uspAvailEBMByID created'
13697
13698END TRY
13699BEGIN CATCH
13700 PRINT N'USP Rpt.uspAvailEBMByID create failed'
13701 PRINT 'Error Number: ' + str(error_number())
13702 PRINT 'Line Number: ' + str(error_line())
13703 PRINT error_message()
13704END CATCH
13705GO
13706
13707/* Rpt.uspExpEBM.storedProcedure.sql
13708To call the SP:
13709EXECUTE Rpt.uspExpEBM @username='admin';
13710 */
13711
13712BEGIN TRY
13713EXECUTE ('/****** Object: StoredProcedure [Rpt].[uspExpEBM] Script Date: 10/09/2013 11:26:19 ******/
13714
13715-- =============================================
13716-- Author: Lisa Bohm (lbohm@paragondsi.com)
13717-- Create date: 2013-10-09
13718-- Description: Returns milk inventory by mother
13719-- Modified: 2013-11-21 to handle the HL7MsgField tbl add
13720-- 2013-12-09 add audit tracking call
13721-- =============================================
13722CREATE PROCEDURE [Rpt].[uspExpEBM]
13723 -- Add the parameters for the stored procedure here
13724@username varchar(50)
13725 AS
13726 DECLARE @priPID tinyint
13727 BEGIN
13728 -- SET NOCOUNT ON added to prevent extra result sets from
13729 -- interfering with SELECT statements.
13730 SET NOCOUNT ON;
13731
13732/* find primary ID */
13733SET @priPID = PTinfo.udfGetPIDtype();
13734
13735WITH cteContents AS
13736( -- anchor
13737SELECT oVessel = NtV.vesselID
13738 , tgtVessel = NtV.vesselID
13739 , NtV.vesselCode
13740 , expDate = NVH.expirDate
13741 , NSL.statCatID
13742 , NSL.statusName
13743 , NtV.mls
13744 , NtV.gms
13745 , NtV.locationID
13746 , NtV.nCategoryID
13747
13748FROM Nutrition.tVessel NtV
13749 INNER JOIN Nutrition.tVesselHistory NVH ON NtV.vesselID = NVH.vesselID
13750 INNER JOIN Nutrition.tStatusList NSL ON NVH.statusID = NSL.statusID
13751
13752WHERE NvH.isCurrent = 1
13753 AND NSL.statCatID >= 2
13754 AND NSL.statCatID <= 3
13755 AND Nvh.expirDate < getdate()
13756
13757UNION ALL
13758 SELECT oVessel = NVOl.vesselID
13759 , tgtVessel = cte.tgtVessel
13760 , cte.vesselCode
13761 , expDate = cte.expDate
13762 , cte.statCatID
13763 , cte.statusName
13764 , cte.mls
13765 , cte.gms
13766 , cte.locationID
13767 , cte.nCategoryID
13768
13769 FROM Nutrition.tVesselOrigin NVOl
13770 INNER JOIN cteContents AS cte ON cte.oVessel = NVOl.targetVesselID
13771)
13772
13773SELECT DISTINCT vesselID = cte.tgtVessel
13774 , cte.vesselCode
13775 , vesselCont = NtC.nCategoryAbbrev
13776 , cte.expDate
13777 , cte.statusName
13778 , cte.mls
13779 , cte.gms
13780 , locationCode = StL.locationName
13781 , PtP.firstName
13782 , PtP.lastName
13783 , PtP.midInit
13784 , primaryID = PtI.IDvalue
13785 , NDL.lotCode
13786 , NDB.bankName
13787
13788FROM cteContents cte
13789 INNER JOIN Storage.tLocation StL ON cte.locationID = StL.locationID
13790 INNER JOIN Nutrition.tCategory NtC ON cte.nCategoryID = NtC.nCategoryID
13791LEFT OUTER JOIN (Nutrition.tMotherVessel NMV
13792 INNER JOIN PTinfo.tPatient PtP ON PtP.pSysID = NmV.pSysID
13793 INNER JOIN PTinfo.tIdentity PtI ON PtP.pSysID = PtI.pSysID
13794 AND PtI.identTypeID = @priPID
13795 ) ON cte.oVessel = NMV.vesselID
13796 LEFT OUTER JOIN (Nutrition.tDonorVessel NDV
13797 INNER JOIN Nutrition.tDonorLot NDL ON NDV.lotID = NDL.lotID
13798 INNER JOIN Nutrition.tDonorBank NDB ON NDL.donorBankID = NDB.donorBankID
13799 ) ON cte.oVessel = NDV.vesselID
13800
13801WHERE cte.statCatID >= 2
13802 AND cte.statCatID <= 3
13803 AND cte.expDate < getdate();
13804
13805 /* audit info */
13806 EXECUTE Sysinfo.uspAddReportView @username=@username, @auditTypeID=8, @reportName=''Expired EBM'', @startDate=''19000101'', @endDate=''25000101'', @addtlParamValue=''N/A'';
13807END;
13808')
13809
13810 PRINT N'USP Rpt.uspExpEBM created'
13811
13812END TRY
13813BEGIN CATCH
13814 PRINT N'USP Rpt.uspExpEBM create failed'
13815 PRINT 'Error Number: ' + str(error_number())
13816 PRINT 'Line Number: ' + str(error_line())
13817 PRINT error_message()
13818END CATCH
13819GO
13820
13821/* Rpt.uspExpEBMByID.storedProcedure.sql
13822To call the SP:
13823EXECUTE Rpt.uspExpEBMByID @pSysID=20, @username='admin';
13824 */
13825
13826BEGIN TRY
13827EXECUTE ('/****** Object: StoredProcedure [Rpt].[uspExpEBMByID] Script Date: 10/09/2013 11:26:19 ******/
13828
13829-- =============================================
13830-- Author: Lisa Bohm (lbohm@paragondsi.com)
13831-- Create date: 2013-10-09
13832-- Description: Returns milk inventory by mother
13833-- Modified: 2013-11-21 to handle the HL7MsgField tbl add
13834-- 2013-12-09 add audit tracking call
13835-- =============================================
13836CREATE PROCEDURE [Rpt].[uspExpEBMByID]
13837 -- Add the parameters for the stored procedure here
13838 @pSysID bigint
13839 , @username varchar(50)
13840 AS
13841 DECLARE @priPID tinyint
13842 BEGIN
13843 -- SET NOCOUNT ON added to prevent extra result sets from
13844 -- interfering with SELECT statements.
13845 SET NOCOUNT ON;
13846
13847/* find primary ID */
13848SET @priPID = PTinfo.udfGetPIDtype();
13849
13850WITH cteContents AS
13851( -- anchor
13852SELECT oVessel = NtV.vesselID
13853 , tgtVessel = NtV.vesselID
13854 , NtV.vesselCode
13855 , expDate = NVH.expirDate
13856 , NSL.statCatID
13857 , NSL.statusName
13858 , NtV.mls
13859 , NtV.gms
13860 , NtV.locationID
13861 , NtV.nCategoryID
13862
13863FROM Nutrition.tVessel NtV
13864 INNER JOIN Nutrition.tVesselHistory NVH ON NtV.vesselID = NVH.vesselID
13865 INNER JOIN Nutrition.tStatusList NSL ON NVH.statusID = NSL.statusID
13866
13867WHERE NvH.isCurrent = 1
13868 AND NSL.statCatID >= 2
13869 AND NSL.statCatID <= 3
13870 AND Nvh.expirDate < getdate()
13871
13872UNION ALL
13873 SELECT oVessel = NVOl.vesselID
13874 , tgtVessel = cte.tgtVessel
13875 , cte.vesselCode
13876 , expDate = cte.expDate
13877 , cte.statCatID
13878 , cte.statusName
13879 , cte.mls
13880 , cte.gms
13881 , cte.locationID
13882 , cte.nCategoryID
13883
13884 FROM Nutrition.tVesselOrigin NVOl
13885 INNER JOIN cteContents AS cte ON cte.oVessel = NVOl.targetVesselID
13886)
13887
13888SELECT vesselID = cte.tgtVessel
13889 , cte.vesselCode
13890 , vesselCont = NtC.nCategoryAbbrev
13891 , cte.expDate
13892 , cte.statusName
13893 , cte.mls
13894 , cte.gms
13895 , StL.locationName
13896 , PtP.firstName
13897 , PtP.lastName
13898 , PtP.midInit
13899 , primaryID = PtI.IDvalue
13900
13901FROM cteContents cte
13902 INNER JOIN Storage.tLocation StL ON cte.locationID = StL.locationID
13903 INNER JOIN Nutrition.tCategory NtC ON cte.nCategoryID = NtC.nCategoryID
13904 INNER JOIN Nutrition.tMotherVessel NMV ON cte.oVessel = NMV.vesselID
13905 INNER JOIN PTinfo.tPatient PtP ON PtP.pSysID = NMV.pSysID
13906 INNER JOIN PTinfo.tIdentity PtI ON PtP.pSysID = PtI.pSysID
13907
13908WHERE PtI.identTypeID = @priPID
13909 AND NMV.pSysID = @pSysID;
13910
13911 /* audit info */
13912 EXECUTE Sysinfo.uspAddReportView @username=@username, @auditTypeID=8, @reportName=''Expired EBM By Mother'', @startDate=''19000101'', @endDate=''25000101'', @addtlParamValue=@pSysID;
13913END
13914;
13915')
13916
13917 PRINT N'USP Rpt.uspExpEBMByID created'
13918
13919END TRY
13920BEGIN CATCH
13921 PRINT N'USP Rpt.uspExpEBMByID create failed'
13922 PRINT 'Error Number: ' + str(error_number())
13923 PRINT 'Line Number: ' + str(error_line())
13924 PRINT error_message()
13925END CATCH
13926GO
13927
13928/* Rpt.uspFeedByAdmin.storedProcedure.sql
13929To call the SP:
13930EXECUTE Rpt.uspFeedByAdmin @startDate='20131101', @endDate='20131130', @feedAdmin='admin', @username='admin';
13931 */
13932
13933BEGIN TRY
13934EXECUTE ('/****** Object: StoredProcedure [Rpt].[uspFeedByAdmin] Script Date: 10/09/2013 11:26:19 ******/
13935-- =============================================
13936-- Author: Lisa Bohm (lbohm@paragondsi.com)
13937-- Create date: 2013-10-09
13938-- Description: Returns data for feedings done by an admin by date range
13939-- Modified: 2013-11-21 to handle the HL7MsgField tbl add
13940-- 2013-12-09 add audit tracking call
13941-- =============================================
13942CREATE PROCEDURE [Rpt].[uspFeedByAdmin]
13943 -- Add the parameters for the stored procedure here
13944@startDate date
13945, @endDate date
13946, @feedAdmin varchar(50)
13947, @username varchar(50)
13948 AS
13949 DECLARE @priPID tinyint
13950 BEGIN
13951 -- SET NOCOUNT ON added to prevent extra result sets from
13952 -- interfering with SELECT statements.
13953 SET NOCOUNT ON;
13954
13955SET @endDate = DATEADD(day,1,@endDate);
13956
13957/* find primary ID */
13958SET @priPID = PTinfo.udfGetPIDtype();
13959
13960/* now get yer the data */
13961
13962SELECT NtV.vesselID
13963 , NtV.vesselCode
13964 , NtV.mls
13965 , NtV.gms
13966 , NtV.calsPer
13967 , feedType = CASE WHEN NVH.statusID = 18 THEN ''Swab'' ELSE NtC.nCategoryAbbrev END
13968 , pSysID = PtI.pSysID
13969 , primaryID = PtI.IDvalue
13970 , PtP.firstName
13971 , PtP.lastName
13972 , PtP.midInit
13973 , dateFed = NVH.dateChange
13974 , feedAdmin = NvH.userChange
13975
13976 FROM Nutrition.tVessel NtV
13977 INNER JOIN Nutrition.tVesselAssign NVA ON NtV.vesselID = NVA.vesselID
13978 INNER JOIN Nutrition.tCategory NtC ON NtV.nCategoryID = NtC.nCategoryID
13979 INNER JOIN PtInfo.tPatient PtP ON NvA.pSysID = PtP.pSysID
13980 INNER JOIN PtInfo.tIdentity PtI ON PtP.pSysID = PtI.pSysID
13981 INNER JOIN Nutrition.tVesselHistory NVH ON NtV.vesselID = NVH.vesselID
13982
13983 WHERE PtI.identTypeID = @priPID
13984 AND (NVH.statusID = 7
13985 OR NVH.statusID = 18)
13986 AND NVH.dateChange >= @startDate
13987 AND NVH.dateChange < @endDate
13988 AND NVH.userChange = @feedAdmin;
13989
13990EXECUTE Sysinfo.uspAddReportView @username=@username, @auditTypeID=8, @reportName=''Feeds by Admin'', @startDate=@startDate, @endDate=@endDate, @addtlParamValue=@feedAdmin;
13991END;
13992')
13993
13994 PRINT N'USP Rpt.uspFeedByAdmin created'
13995
13996END TRY
13997BEGIN CATCH
13998 PRINT N'USP Rpt.uspFeedByAdmin create failed'
13999 PRINT 'Error Number: ' + str(error_number())
14000 PRINT 'Line Number: ' + str(error_line())
14001 PRINT error_message()
14002END CATCH
14003GO
14004
14005/* Rpt.uspFeedByID.storedProcedure.sql
14006To call the SP:
14007EXECUTE Rpt.uspFeedByID @startDate='20131101', @endDate='20131130', @pSysID=4, @username='admin';
14008 */
14009
14010BEGIN TRY
14011EXECUTE ('/****** Object: StoredProcedure [Rpt].[uspFeedByID] Script Date: 10/09/2013 11:26:19 ******/
14012
14013-- =============================================
14014-- Author: Lisa Bohm (lbohm@paragondsi.com)
14015-- Create date: 2013-10-09
14016-- Description: Returns data for feedings for a patient by date range
14017-- Modified: 2013-11-21 to handle the HL7MsgField tbl add
14018-- 2013-12-09 add audit tracking call
14019-- =============================================
14020CREATE PROCEDURE [Rpt].[uspFeedByID]
14021 -- Add the parameters for the stored procedure here
14022@startDate date
14023, @endDate date
14024, @pSysID bigint
14025, @username varchar(50)
14026 AS
14027 DECLARE @priPID tinyint
14028 BEGIN
14029 -- SET NOCOUNT ON added to prevent extra result sets from
14030 -- interfering with SELECT statements.
14031 SET NOCOUNT ON;
14032
14033/* find primary ID */
14034SET @priPID = PTinfo.udfGetPIDtype();
14035SET @endDate = DATEADD(day,1,@endDate);
14036
14037/* now get yer the data */
14038
14039SELECT NtV.vesselID
14040 , NtV.vesselCode
14041 , NtV.mls
14042 , NtV.gms
14043 , NtV.calsPer
14044 , feedType = CASE WHEN NVH.statusID = 18 THEN ''Swab'' ELSE NtC.nCategoryAbbrev END
14045 , pSysID = PtI.pSysID
14046 , primaryID = PtI.IDvalue
14047 , PtP.firstName
14048 , PtP.lastName
14049 , PtP.midInit
14050 , dateFed = NVH.dateChange
14051 , feedAdmin = NvH.userChange
14052
14053 FROM Nutrition.tVessel NtV
14054 INNER JOIN Nutrition.tVesselAssign NVA ON NtV.vesselID = NVA.vesselID
14055 INNER JOIN Nutrition.tCategory NtC ON NtV.nCategoryID = NtC.nCategoryID
14056 INNER JOIN PtInfo.tPatient PtP ON NvA.pSysID = PtP.pSysID
14057 INNER JOIN PtInfo.tIdentity PtI ON PtP.pSysID = PtI.pSysID
14058 INNER JOIN Nutrition.tVesselHistory NVH ON NtV.vesselID = NVH.vesselID
14059
14060 WHERE PtI.identTypeID = @priPID
14061 AND (NVH.statusID = 7
14062 OR NVH.statusID = 18)
14063 AND NVH.dateChange >= @startDate
14064 AND NVH.dateChange < @endDate
14065 AND NVA.pSysID = @pSysID
14066UNION ALL
14067
14068SELECT vesselID = PDO.docOrderID
14069 , vesselCode = ''NPO''
14070 , mls = 0
14071 , gms = 0
14072 , calsPer = 0
14073 , feedType = ''NPO''
14074 , PDO.pSysID
14075 , primaryID = PtI.IDvalue
14076 , PtP.firstName
14077 , PtP.lastName
14078 , PtP.midInit
14079 , dateFed = PDO.orderStart
14080 , feedAdmin = ''None''
14081
14082FROM PTinfo.tDocOrder PDO
14083 INNER JOIN PtInfo.tPatient PtP ON PDO.pSysID = PtP.pSysID
14084 INNER JOIN PtInfo.tIdentity PtI ON PtP.pSysID = PtI.pSysID
14085WHERE PDO.pSysID = @pSysID
14086 AND PDO.orderTypeID = 4
14087 AND PtI.identTypeID = @priPID;
14088/* audit info */
14089EXECUTE Sysinfo.uspAddReportView @username=@username, @auditTypeID=8, @reportName=''Feeds By Patient'', @startDate=@startDate, @endDate=@endDate, @addtlParamValue=@pSysID;
14090END;
14091')
14092
14093 PRINT N'USP Rpt.uspFeedByID created'
14094
14095END TRY
14096BEGIN CATCH
14097 PRINT N'USP Rpt.uspFeedByID create failed'
14098 PRINT 'Error Number: ' + str(error_number())
14099 PRINT 'Line Number: ' + str(error_line())
14100 PRINT error_message()
14101END CATCH
14102GO
14103
14104/* Rpt.uspFeedAvert.storedProcedure.sql
14105To call the SP:
14106EXECUTE Rpt.uspFeedAvert @startDate='20131101', @endDate='20131130', @username='admin';
14107 */
14108
14109BEGIN TRY
14110EXECUTE ('-- =============================================
14111-- Author: Lisa Bohm (lbohm@paragondsi.com)
14112-- Create date: 2013-10-09
14113-- Description: Returns near miss data
14114-- Modified:
14115-- =============================================
14116CREATE PROCEDURE [Rpt].[uspFeedAvert]
14117 -- Add the parameters for the stored procedure here
14118@startDate date
14119, @endDate date
14120 , @username varchar(50)
14121 AS
14122 DECLARE @priPID tinyint
14123 BEGIN
14124 -- SET NOCOUNT ON added to prevent extra result sets from
14125 -- interfering with SELECT statements.
14126 SET NOCOUNT ON;
14127
14128SET @endDate = DATEADD(day,1,@endDate);
14129/* find primary ID */
14130SET @priPID = PTinfo.udfGetPIDtype();
14131
14132/* now get yer the data. We need the whoops data as well as the next feed data for that patient. */
14133
14134SELECT SAL.avertDate
14135 , primaryID = SAL.secondScan
14136 , vesselCode = SAL.firstScan
14137 , feedType = NtC.nCategoryAbbrev
14138 , SRC.reason
14139 , SAL.username
14140 , gdFd.pSysID
14141 , GFvesselCode = gdFd.vesselCode
14142 , GFfeedType = gdFd.nCategoryAbbrev
14143 , GFdateFed = gdFd.dateChange
14144 , GFadmin = gdFd.userChange
14145
14146FROM SysInfo.tAvertLog SAL
14147 INNER JOIN SysInfo.tReasonCode SRC ON SAL.reasonCodeID = SRC.reasonCodeID
14148 LEFT OUTER JOIN PTinfo.tIdentity PtI ON SAL.secondScan = PtI.IDValue
14149 AND PtI.identTypeID = @priPID
14150 LEFT OUTER JOIN (Nutrition.tVessel NtVO
14151 INNER JOIN Nutrition.tCategory NtC ON NtVO.nCategoryID = NtC.nCategoryID
14152 ) ON SAL.secondScan = NtVO.vesselCode
14153
14154 LEFT OUTER JOIN (SELECT NvA.pSysID
14155 , NvH.dateChange
14156 , NvH.userChange
14157 , NtV.vesselCode
14158 , NtCG.nCategoryAbbrev
14159 FROM Nutrition.tVesselAssign NvA
14160 INNER JOIN Nutrition.tVesselHistory NvH ON NvA.vesselID = NvH.vesselID
14161 INNER JOIN Nutrition.tVessel NtV ON NvA.vesselID = NtV.vesselID
14162 INNER JOIN Nutrition.tCategory NtCG ON NtV.nCategoryID = NtCG.nCategoryID
14163 WHERE NvH.statusID = 7
14164 ) gdFd ON PtI.pSysID = gdFd.pSysID
14165 AND GdFd.dateChange < DATEADD(hour, 1, SAL.avertDate)
14166 AND GdFd.dateChange > SAL.avertDate
14167
14168WHERE SAL.avertDate >=@startDate
14169 AND SAL.avertDate < @endDate
14170 AND SAL.auditTypeID = 5;
14171
14172/* audit info */
14173EXECUTE Sysinfo.uspAddReportView @username=@username, @auditTypeID=8, @reportName=''Feed Avert'', @startDate=@startDate, @endDate=@endDate, @addtlParamValue=''N/A'';
14174END;
14175')
14176
14177 PRINT N'USP Rpt.uspFeedAvert created'
14178
14179END TRY
14180BEGIN CATCH
14181 PRINT N'USP Rpt.uspFeedAvert create failed'
14182 PRINT 'Error Number: ' + str(error_number())
14183 PRINT 'Line Number: ' + str(error_line())
14184 PRINT error_message()
14185END CATCH
14186GO
14187
14188/* Rpt.uspAdminFeedByDate.storedProcedure.sql
14189To call the SP:
14190EXECUTE Rpt.uspAdminFeedByDate @startDate='20131101', @endDate='20131130';
14191 */
14192
14193BEGIN TRY
14194EXECUTE ('/****** Object: StoredProcedure [Rpt].[uspAdminFeedByDate] Script Date: 8/27/2013 11:26:19 ******/
14195
14196-- =============================================
14197-- Author: Lisa Bohm (lbohm@paragondsi.com)
14198-- Create date: 2013-10-21
14199-- Description: for a date range, administer-ers who have administered feeds
14200--
14201-- =============================================
14202CREATE PROCEDURE [Rpt].[uspAdminFeedByDate]
14203 -- Add the parameters for the stored procedure here
14204@startDate date
14205, @endDate date
14206
14207 AS
14208 BEGIN
14209 -- SET NOCOUNT ON added to prevent extra result sets from
14210 -- interfering with SELECT statements.
14211 SET NOCOUNT ON;
14212
14213SET @endDate = DATEADD(day,1,@endDate);
14214
14215SELECT DISTINCT FED.userChange
14216FROM
14217(SELECT NVH.userChange
14218FROM Nutrition.tVesselHistory NVH
14219WHERE NVH.statusID = 7
14220 AND NVH.dateChange >= @startDate
14221 AND NVH.dateChange < @endDate
14222
14223UNION ALL
14224
14225SELECT userChange = SAL.userName
14226FROM SysInfo.tAvertLog SAL
14227WHERE SAL.avertDate >= @startDate
14228 AND SAL.avertDate < @endDate
14229 AND SAL.auditTypeID = 5) FED;
14230
14231END;
14232')
14233
14234 PRINT N'USP Rpt.uspAdminFeedByDate created'
14235
14236END TRY
14237BEGIN CATCH
14238 PRINT N'USP Rpt.uspAdminFeedByDate create failed'
14239 PRINT 'Error Number: ' + str(error_number())
14240 PRINT 'Line Number: ' + str(error_line())
14241 PRINT error_message()
14242END CATCH
14243GO
14244
14245/* Rpt.uspFeedByDate.storedProcedure.sql
14246To call the SP:
14247EXECUTE Rpt.uspFeedByDate @startDate='20131101', @endDate='20131130', @username='admin';
14248 */
14249
14250BEGIN TRY
14251EXECUTE ('/****** Object: StoredProcedure [Rpt].[uspFeedByDate] Script Date: 8/27/2013 11:26:19 ******/
14252-- =============================================
14253-- Author: Lisa Bohm (lbohm@paragondsi.com)
14254-- Create date: 2013-10-09
14255-- Description: Returns data for feedings done by an admin by date range
14256-- Modified: 2013-11-21 to handle the HL7MsgField tbl add
14257-- 2013-12-09 add audit tracking call
14258-- =============================================
14259CREATE PROCEDURE [Rpt].[uspFeedByDate]
14260 -- Add the parameters for the stored procedure here
14261@startDate date
14262, @endDate date
14263, @username varchar(50)
14264 AS
14265 DECLARE @priPID tinyint
14266 BEGIN
14267 -- SET NOCOUNT ON added to prevent extra result sets from
14268 -- interfering with SELECT statements.
14269 SET NOCOUNT ON;
14270
14271SET @endDate = DATEADD(day,1,@endDate);
14272
14273/* find primary ID */
14274SET @priPID = PTinfo.udfGetPIDtype();
14275
14276/* now get yer the data */
14277
14278SELECT NtV.vesselID
14279 , NtV.vesselCode
14280 , NtV.mls
14281 , NtV.gms
14282 , NtV.calsPer
14283 , feedType = CASE WHEN NVH.statusID = 18 THEN ''Swab'' ELSE NtC.nCategoryAbbrev END
14284 , pSysID = PtI.pSysID
14285 , primaryID = PtI.IDvalue
14286 , PtP.firstName
14287 , PtP.lastName
14288 , PtP.midInit
14289 , dateFed = NVH.dateChange
14290 , feedAdmin = NvH.userChange
14291
14292 FROM Nutrition.tVessel NtV
14293 INNER JOIN Nutrition.tVesselAssign NVA ON NtV.vesselID = NVA.vesselID
14294 INNER JOIN Nutrition.tCategory NtC ON NtV.nCategoryID = NtC.nCategoryID
14295 INNER JOIN PtInfo.tPatient PtP ON NvA.pSysID = PtP.pSysID
14296 INNER JOIN PtInfo.tIdentity PtI ON PtP.pSysID = PtI.pSysID
14297 INNER JOIN Nutrition.tVesselHistory NVH ON NtV.vesselID = NVH.vesselID
14298
14299 WHERE PtI.identTypeID = @priPID
14300 AND (NVH.statusID = 7
14301 OR NVH.statusID = 18)
14302 AND NVH.dateChange >= @startDate
14303 AND NVH.dateChange < @endDate;
14304
14305EXECUTE Sysinfo.uspAddReportView @username=@username, @auditTypeID=8, @reportName=''Feeds by Date'', @startDate=@startDate, @endDate=@endDate, @addtlParamValue=''N/A'';
14306END;
14307')
14308
14309 PRINT N'USP Rpt.uspFeedByDate created'
14310
14311END TRY
14312BEGIN CATCH
14313 PRINT N'USP Rpt.uspFeedByDate create failed'
14314 PRINT 'Error Number: ' + str(error_number())
14315 PRINT 'Line Number: ' + str(error_line())
14316 PRINT error_message()
14317END CATCH
14318GO
14319
14320/* Rpt.uspActivePtList.storedProcedure.sql
14321To call the SP:
14322EXECUTE Rpt.uspActivePtList @username='admin';
14323 */
14324
14325BEGIN TRY
14326EXECUTE ('/****** Object: StoredProcedure [Rpt].[uspActivePtList] Script Date: 11/1/2013 3:05:09 PM ******/
14327-- =============================================
14328-- Author: Lisa Bohm (lbohm@paragondsi.com)
14329-- Create date: 2013-10-09
14330-- Description: Returns milk inventory
14331-- Modified: 2013-11-21 to handle the HL7MsgField tbl add
14332-- 2013-12-09 add audit tracking call
14333-- =============================================
14334CREATE PROCEDURE [Rpt].[uspActivePtList]
14335 -- Add the parameters for the stored procedure here
14336 @username varchar(50)
14337
14338 AS
14339 DECLARE @priPID tinyint
14340 BEGIN
14341 -- SET NOCOUNT ON added to prevent extra result sets from
14342 -- interfering with SELECT statements.
14343 SET NOCOUNT ON;
14344
14345/* find primary ID */
14346SET @priPID = PTinfo.udfGetPIDtype();
14347
14348
14349SELECT PtP.pSysID
14350 , PtP.firstName
14351 , PtP.midInit
14352 , PtP.lastName
14353 , PtP.dob
14354 , PCL.multiple
14355 , primaryID = PtI.IDvalue
14356 , motherFName = PtM.firstName
14357 , motherLName = PtM.lastName
14358 , motherMid = PtM.midInit
14359 , motherPID = PtMI.IDvalue
14360
14361FROM PTinfo.tPatient PtP
14362 INNER JOIN PTinfo.tStatus PtS ON PtP.pSysID = PtS.pSysID
14363 INNER JOIN PTinfo.tIdentity PtI On PtP.pSysID = PtI.pSysID
14364 INNER JOIN PTinfo.tChildLink PCL ON PtP.pSysID = PCL.pSysID
14365 LEFT OUTER JOIN (PTinfo.tPatient PtM
14366 INNER JOIN PTinfo.tIdentity PtMI ON PtM.pSysID = PtMI.pSysID
14367 AND PtMI.identTypeID = @priPID
14368 ) ON PtM.pSysID = PCL.motherSysID
14369
14370
14371WHERE PtS.isCurrent = 1
14372 AND PtS.pStatTypeID = 1
14373 AND PtI.identTypeID = @priPID
14374
14375 ;
14376
14377EXECUTE Sysinfo.uspAddReportView @username=@username, @auditTypeID=8, @reportName=''Active Patient List'', @startDate=''19000101'', @endDate=''25000101'', @addtlParamValue=''N/A'';
14378
14379END;
14380')
14381
14382 PRINT N'USP Rpt.uspActivePtList created'
14383
14384END TRY
14385BEGIN CATCH
14386 PRINT N'USP Rpt.uspActivePtList create failed'
14387 PRINT 'Error Number: ' + str(error_number())
14388 PRINT 'Line Number: ' + str(error_line())
14389 PRINT error_message()
14390END CATCH
14391GO
14392
14393/* Rpt.uspRecdByDate.storedProcedure.sql
14394To call the SP:
14395EXECUTE Rpt.uspRecdByDate @startDate='20131101', @endDate='20131130', @username='admin';
14396 */
14397
14398BEGIN TRY
14399EXECUTE ('/****** Object: StoredProcedure [Rpt].[uspRecdByDate] Script Date: 10/09/2013 11:26:19 ******/
14400-- =============================================
14401-- Author: Lisa Bohm (lbohm@paragondsi.com)
14402-- Create date: 2013-10-09
14403-- Description: Returns data for bottle history
14404-- Modified: 2013-11-21 to handle the HL7MsgField tbl add
14405-- 2013-12-09 add audit tracking call
14406-- 2014-03-06 alter for root code vessels
14407-- =============================================
14408CREATE PROCEDURE [Rpt].[uspRecdByDate]
14409 -- Add the parameters for the stored procedure here
14410@startDate date
14411, @endDate date
14412, @username varchar(50)
14413 AS
14414 DECLARE @priPID tinyint
14415 BEGIN
14416 -- SET NOCOUNT ON added to prevent extra result sets from
14417 -- interfering with SELECT statements.
14418 SET NOCOUNT ON;
14419
14420SET @endDate = DATEADD(day,1,@endDate);
14421
14422/* find primary ID */
14423SET @priPID = PTinfo.udfGetPIDtype();
14424
14425/* now get yer the data */
14426
14427SELECT NtV.vesselID
14428 , vesselCode = CASE WHEN NVRT.vesselCode IS NULL THEN NtV.vesselCode ELSE NVRT.vesselCode END
14429 , NtV.mls
14430 , NtV.gms
14431 , NtV.calsPer
14432 , NtC.nCategoryAbbrev
14433 , motherSysID = PtI.pSysID
14434 , contents = CASE WHEN PtI.pSysID IS NULL THEN NDB.bankName + '': '' + NDL.lotCode ELSE PtP.firstName + '' '' + PtP.lastName + '' ('' + PtI.IDvalue + '')'' END
14435 , dateRecd = DateAdd(dd,0, DateDiff(dd,0,NVH.dateChange))
14436
14437 FROM Nutrition.tVessel NtV
14438 LEFT OUTER JOIN (Nutrition.tVesselOrigin NVO
14439 INNER JOIN Nutrition.tVessel NVRT ON NVO.targetVesselID = NVRT.vesselID
14440 ) ON NtV.vesselID = NVO.vesselID
14441 AND LEFT(NtV.vesselCode,2) = ''RT''
14442 LEFT OUTER JOIN (Nutrition.tMotherVessel NMV
14443 INNER JOIN PtInfo.tPatient PtP ON NMV.pSysID = PtP.pSysID
14444 INNER JOIN PtInfo.tIdentity PtI ON PtP.pSysID = PtI.pSysID
14445 AND PtI.identTypeID = @priPID
14446 ) ON NtV.vesselID = NMV.vesselID
14447 LEFT OUTER JOIN (Nutrition.tDonorVessel NDV
14448 INNER JOIN Nutrition.tDonorLot NDL ON NDV.lotID = NDL.lotID
14449 INNER JOIN Nutrition.tDonorBank NDB ON NDL.donorBankID = NDB.donorBankID
14450 ) ON NtV.vesselID = NDV.vesselID
14451 INNER JOIN Nutrition.tCategory NtC ON NtV.nCategoryID = NtC.nCategoryID
14452 INNER JOIN Nutrition.tVesselHistory NVH ON NtV.vesselID = NVH.vesselID
14453
14454 WHERE NVH.statusID = 17
14455 AND NtV.nCategoryID >=1
14456 AND NtV.nCategoryID <=2
14457 AND (PtI.pSysID IS NOT NULL
14458 OR NDL.lotCode IS NOT NULL)
14459 AND NVH.dateChange >= @startDate
14460 AND NVH.dateChange < @endDate;
14461/* audit info */
14462EXECUTE Sysinfo.uspAddReportView @username=@username, @auditTypeID=8, @reportName=''Milk Received By Date'', @startDate=@startDate, @endDate=@endDate, @addtlParamValue=''N/A'';
14463
14464END;
14465')
14466
14467 PRINT N'USP Rpt.uspRecdByDate created'
14468
14469END TRY
14470BEGIN CATCH
14471 PRINT N'USP Rpt.uspRecdByDate create failed'
14472 PRINT 'Error Number: ' + str(error_number())
14473 PRINT 'Line Number: ' + str(error_line())
14474 PRINT error_message()
14475END CATCH
14476GO
14477
14478/* Rpt.uspDispByDate.storedProcedure.sql
14479To call the SP:
14480EXECUTE Rpt.uspDispByDate @startDate='20131101', @endDate='20131130', @username='admin';
14481 */
14482
14483BEGIN TRY
14484EXECUTE ('/****** Object: StoredProcedure [Rpt].[uspDispByDate] Script Date: 10/09/2013 11:26:19 ******/
14485-- =============================================
14486-- Author: Lisa Bohm (lbohm@paragondsi.com)
14487-- Create date: 2013-11-05
14488-- Description: Returns data for bottles disposed in a date range
14489-- Modified: 2013-11-21 to handle the HL7MsgField tbl add
14490-- 2013-12-09 add audit tracking call
14491-- 2014-03-06 add better content gathering
14492-- =============================================
14493CREATE PROCEDURE [Rpt].[uspDispByDate]
14494 -- Add the parameters for the stored procedure here
14495@startDate date
14496, @endDate date
14497, @username varchar(50)
14498
14499 AS
14500 DECLARE @priPID tinyint
14501 BEGIN
14502 -- SET NOCOUNT ON added to prevent extra result sets from
14503 -- interfering with SELECT statements.
14504 SET NOCOUNT ON;
14505
14506SET @endDate = DATEADD(day,1,@endDate);
14507
14508/* find primary ID */
14509SET @priPID = PTinfo.udfGetPIDtype();
14510
14511WITH cteContents AS
14512( -- anchor
14513SELECT contVessel = NtV.vesselID
14514 , oVessel = NtV.vesselID
14515 , NtV.vesselCode
14516 , NvH.statusID
14517 , NSL.statCatID
14518
14519FROM Nutrition.tVessel NtV
14520 INNER JOIN Nutrition.tVesselHistory NVH ON NtV.vesselID = NVH.vesselID
14521 INNER JOIN Nutrition.tStatusList NSL ON NVH.statusID = NSL.statusID
14522
14523WHERE NvH.isCurrent = 1
14524 AND NSL.statCatID = 4
14525 AND NvH.statusID <> 7
14526 AND LEFT(NtV.vesselCode,2) <> ''RT''
14527
14528UNION ALL
14529 SELECT contVessel = NVOl.vesselID
14530 , oVessel = cte.oVessel
14531 , vesselCode = cte.vesselCode
14532 , statusID = cte.statusID
14533 , statCatID = cte.statCatID
14534
14535 FROM Nutrition.tVesselOrigin NVOl
14536 INNER JOIN cteContents AS cte ON cte.contVessel = NVOl.targetVesselID
14537)
14538
14539/* now get yer the data */
14540SELECT NtV.vesselID
14541 , NtV.vesselCode
14542 , NtV.mls
14543 , NtV.gms
14544 , NSL.statusName
14545 , NtV.calsPer
14546 , Nvh.userChange
14547 , NtC.nCategoryAbbrev
14548 , motherSysID = CASE WHEN cont.pSysID = 0 THEN
14549 CASE WHEN cont.lotID = 0 THEN cont.formLotID ELSE cont.lotID END
14550 ELSE cont.pSysID END
14551 , contents = CASE WHEN cont.pSysID = 0 THEN
14552 CASE WHEN cont.lotID = 0 THEN
14553 NtF.formulaName + '': '' + NFL.formlotCode
14554 ELSE NDB.bankName + '': '' + NDL.lotCode END
14555 ELSE PtP.firstName + '' '' + PtP.lastName + '' ('' + PtI.IDvalue + '')'' END
14556 , dateDisp = DateAdd(dd,0, DateDiff(dd,0,NVH.dateChange))
14557
14558 FROM Nutrition.tVessel NtV
14559 LEFT OUTER JOIN ((SELECT ct.oVessel
14560 , ct.vesselCode
14561 , ct.statusID
14562 , ct.statCatID
14563 , pSysID = MAX(COALESCE(NMV.pSysID,0))
14564 , lotID = MAX(COALESCE(NDV.lotID,0))
14565 , formLotID = MAX(COALESCE(NFV.formlotID,0))
14566 FROM cteContents ct
14567 LEFT OUTER JOIN Nutrition.tMotherVessel NMV ON ct.contvessel = NMV.vesselID
14568 LEFT OUTER JOIN Nutrition.tDonorVessel NDV ON ct.contvessel = NDV.vesselID
14569 LEFT OUTER JOIN Nutrition.tFormulaVessel NFV ON ct.contvessel = NFV.vesselID
14570
14571 GROUP BY ct.oVessel
14572 , ct.vesselCode
14573 , ct.statusID
14574 , ct.statCatID) cont
14575 LEFT OUTER JOIN (PtInfo.tPatient PtP
14576 INNER JOIN PtInfo.tIdentity PtI ON PtP.pSysID = PtI.pSysID
14577 AND PtI.identTypeID = @priPID
14578 ) ON cont.pSysID = PtP.pSysID
14579 LEFT OUTER JOIN (Nutrition.tDonorLot NDL
14580 INNER JOIN Nutrition.tDonorBank NDB ON NDL.donorBankID = NDB.donorBankID
14581 ) ON cont.lotID = NDL.lotID
14582 LEFT OUTER JOIN (Nutrition.tFormulaLot NFL
14583 INNER JOIN Nutrition.tFormula NtF ON NFL.formulaID = NtF.formulaID
14584 ) ON cont.formlotID = NFL.formlotID
14585 ) ON NtV.vesselID = cont.oVessel
14586 INNER JOIN Nutrition.tCategory NtC ON NtV.nCategoryID = NtC.nCategoryID
14587 INNER JOIN Nutrition.tVesselHistory NVH ON NtV.vesselID = NVH.vesselID
14588 INNER JOIN Nutrition.tStatusList NSL ON NVH.statusID = NSL.statusID
14589
14590/* current status is a disposed status, but not fed */
14591 WHERE NVH.isCurrent = 1
14592 AND NVH.statusID <> 7
14593 AND NSL.statCatID = 4
14594 AND LEFT(NtV.vesselCode,2)<>''RT''
14595 AND NVH.dateChange >= @startDate
14596 AND NVH.dateChange < @endDate;
14597
14598 /* audit info */
14599 EXECUTE Sysinfo.uspAddReportView @username=@username, @auditTypeID=8, @reportName=''Milk Disposed By Date'', @startDate=@startDate, @endDate=@endDate, @addtlParamValue=''N/A'';
14600
14601END;
14602')
14603
14604 PRINT N'USP Rpt.uspDispByDate created'
14605
14606END TRY
14607BEGIN CATCH
14608 PRINT N'USP Rpt.uspDispByDate create failed'
14609 PRINT 'Error Number: ' + str(error_number())
14610 PRINT 'Line Number: ' + str(error_line())
14611 PRINT error_message()
14612END CATCH
14613GO
14614
14615/* Rpt.uspDischargePackSlip.storedProcedure.sql
14616To call the SP:
14617EXECUTE Rpt.uspDischargePackSlip @pSysID=20, @username='admin';
14618 */
14619
14620BEGIN TRY
14621EXECUTE ('/****** Object: StoredProcedure [Rpt].[uspDischargePackSlip] Script Date: 10/09/2013 11:26:19 ******/
14622
14623-- =============================================
14624-- Author: Lisa Bohm (lbohm@paragondsi.com)
14625-- Create date: 2013-11-05
14626-- Description: Returns data for bottles disposed to discharge for a mother
14627-- Modified: 2013-11-21 to handle the HL7MsgField tbl add
14628-- 2013-12-09 add audit tracking call
14629-- 2014-02-18 add donor/formula vessels assigned to child
14630-- =============================================
14631CREATE PROCEDURE [Rpt].[uspDischargePackSlip]
14632 -- Add the parameters for the stored procedure here
14633@pSysID bigint
14634, @username varchar(50)
14635
14636 AS
14637 DECLARE @priPID tinyint
14638 DECLARE @contents TABLE (vesselID bigint, vesselCode varchar(25), expDate datetime, statusID tinyint, statCatID tinyint, pSysID bigint);
14639 BEGIN
14640 -- SET NOCOUNT ON added to prevent extra result sets from
14641 -- interfering with SELECT statements.
14642 SET NOCOUNT ON;
14643
14644/* find primary ID */
14645SET @priPID = PTinfo.udfGetPIDtype();
14646
14647WITH cteContents AS
14648( -- anchor
14649SELECT rtVessel = NtV.vesselID
14650 , cVessel = NtV.vesselID
14651 , NtV.vesselCode
14652 , NVH.expirDate
14653 , NVH.statusID
14654 , NSL.statCatID
14655 , NMV.pSysID
14656
14657FROM Nutrition.tVessel NtV
14658 INNER JOIN Nutrition.tVesselHistory NVH ON NtV.vesselID = NVH.vesselID
14659 INNER JOIN Nutrition.tStatusList NSL ON NVH.statusID = NSL.statusID
14660 INNER JOIN Nutrition.tMotherVessel NMV ON NtV.vesselID = NMV.vesselID
14661
14662WHERE NvH.isCurrent = 1
14663 AND NMV.pSysID = @pSysID
14664
14665UNION ALL
14666 SELECT rtVessel = cte.rtVessel
14667 , cVessel = NVOl.targetVesselID
14668 , NtVl.vesselCode
14669 , NvHl.expirDate
14670 , NvHl.statusID
14671 , NSLl.statCatID
14672 , cte.pSysID
14673
14674 FROM Nutrition.tVesselOrigin NVOl
14675 INNER JOIN Nutrition.tVessel NtVl ON NVOl.targetVesselID = NtVl.vesselID
14676 INNER JOIN Nutrition.tVesselHistory NVHl ON NtVL.vesselID = NVHl.vesselID
14677 INNER JOIN Nutrition.tStatusList NSLl ON NVHl.statusID = NSLl.statusID
14678 INNER JOIN cteContents AS cte ON cte.cVessel = NVOl.vesselID
14679 WHERE NvHl.isCurrent = 1
14680)
14681INSERT INTO @contents (vesselID, vesselCode, expDate, statusID, statCatID, pSysID)
14682
14683SELECT vesselID = cte.cVessel
14684 , cte.vesselCode
14685 , cte.expirDate
14686 , cte.statusID
14687 , cte.statCatID
14688 , cte.pSysID
14689FROM cteContents cte
14690 WHERE cte.statusID = 8;
14691
14692/* what about other containers that have been assigned to any child of the mother? */
14693INSERT INTO @contents (vesselID, vesselCode, expDate, statusID, statCatID, pSysID)
14694
14695SELECT NtV.vesselID
14696 , NtV.vesselCode
14697 , NVH.expirDate
14698 , NVH.statusID
14699 , NSL.statCatID
14700 , 0
14701
14702FROM Nutrition.tVessel NtV
14703 INNER JOIN Nutrition.tVesselAssign NVA ON NtV.vesselID = NVA.vesselID
14704 INNER JOIN PtInfo.tChildLink PCL ON NVA.pSysID = PCL.pSysID
14705 INNER JOIN Nutrition.tVesselHistory NVH ON NtV.vesselID = NVH.vesselID
14706 INNER JOIN Nutrition.tStatusList NSL ON NVH.statusID = NSL.statusID
14707
14708 WHERE NSL.statusID = 8
14709 AND NVH.isCurrent = 1
14710 AND PCL.motherSysID = @pSysID
14711 AND NOT EXISTS (SELECT CT.vesselID
14712 FROM @contents CT
14713 WHERE CT.vesselID = NtV.vesselID);
14714
14715SELECT DISTINCT vesselID = CT.vesselID
14716 , CT.vesselCode
14717 , vesselCont = NtC.nCategoryAbbrev
14718 , CT.expDate
14719 , NSL.statusName
14720 , PtP.firstName
14721 , PtP.lastName
14722 , PtP.midInit
14723 , primaryID = PtI.IDvalue
14724 , discharged = NVH.dateChange
14725
14726FROM @contents CT
14727 INNER JOIN Nutrition.tVessel NtV ON CT.vesselID = NtV.vesselID
14728 INNER JOIN Storage.tLocation StL ON NtV.locationID = StL.locationID
14729 INNER JOIN Nutrition.tCategory NtC ON NtV.nCategoryID = NtC.nCategoryID
14730 LEFT OUTER JOIN Nutrition.tVesselHistory NVH ON NtV.vesselID = NVH.vesselID
14731 AND NVH.statusID = 8
14732 AND NVH.isCurrent = 1
14733 INNER JOIN Nutrition.tStatusList NSL ON CT.statusID = NSL.statusID
14734 LEFT OUTER JOIN (Nutrition.tMotherVessel NMV
14735 INNER JOIN PTinfo.tPatient PtP ON PtP.pSysID = NmV.pSysID
14736 INNER JOIN PTinfo.tIdentity PtI ON PtP.pSysID = PtI.pSysID
14737 ) ON CT.pSysID = NMV.pSysID
14738 AND PtI.identTypeID = @priPID;
14739
14740 /* audit info */
14741 EXECUTE Sysinfo.uspAddReportView @username=@username, @auditTypeID=8, @reportName=''Discharge Packing Slip'', @startDate=''19000101'', @endDate=''25000101'', @addtlParamValue=@pSysID;
14742
14743END;
14744')
14745
14746 PRINT N'USP Rpt.uspDischargePackSlip created'
14747
14748END TRY
14749BEGIN CATCH
14750 PRINT N'USP Rpt.uspDischargePackSlip create failed'
14751 PRINT 'Error Number: ' + str(error_number())
14752 PRINT 'Line Number: ' + str(error_line())
14753 PRINT error_message()
14754END CATCH
14755GO
14756
14757
14758/* Rpt.uspFortificationByDate.storedProcedure.sql
14759To call the SP:
14760EXECUTE Rpt.uspDates @startDate='20131101', @endDate='20131130', @username='admin';
14761 */
14762
14763BEGIN TRY
14764EXECUTE ('/****** Object: StoredProcedure [Rpt].[uspFortificationByDate] Script Date: 10/09/2013 11:26:19 ******/
14765-- =============================================
14766-- Author: Lisa Bohm (lbohm@paragondsi.com)
14767-- Create date: 2013-11-05
14768-- Description: Returns data for fortifications used per patient over a date range
14769-- Modified: 2013-11-21 to handle the HL7MsgField tbl add
14770-- 2013-12-09 add audit tracking call
14771-- =============================================
14772CREATE PROCEDURE [Rpt].[uspFortificationByDate]
14773 -- Add the parameters for the stored procedure here
14774@startDate date
14775 , @endDate date
14776 , @username varchar(50)
14777 AS
14778 DECLARE @priPID tinyint
14779 BEGIN
14780 -- SET NOCOUNT ON added to prevent extra result sets from
14781 -- interfering with SELECT statements.
14782 SET NOCOUNT ON;
14783
14784SET @endDate = DATEADD(day,1,@endDate);
14785
14786/* find primary ID */
14787SET @priPID = PTinfo.udfGetPIDtype();
14788
14789/* so we need any vessels that were fortified and then ... assigned? fed? Let''s go with assigned. We shouldn''t need recursion here... */
14790
14791SELECT PtP.firstName
14792 , PtP.lastName
14793 , PtP.dob
14794 , primaryID = PtI.IDvalue
14795 , NAL.additiveName
14796 , NAL.unitOfMeas
14797 , NAI.doses
14798 , prepDate = NVH.dateChange
14799 , dischargeDate = CASE WHEN PtS.pStatTypeID = 2 THEN PtS.statDate ELSE '''' END
14800
14801FROM Nutrition.tVessel NtV
14802INNER JOIN Nutrition.tAdditiveIssue NAI ON NtV.vesselID = NAI.vesselID
14803INNER JOIN Nutrition.tAdditiveList NAL ON NAI.additiveID = NAL.additiveID
14804INNER JOIN Nutrition.tVesselHistory NVH ON NAI.vesselHistoryID = NVH.vesselHistoryID
14805INNER JOIN Nutrition.tVesselAssign NVA ON NtV.vesselID = NVA.vesselID
14806 INNER JOIN PTinfo.tPatient PtP ON NVA.pSysID = PtP.pSysID
14807 INNER JOIN PTinfo.tIdentity PtI ON PtP.pSysID = PtI.pSysID
14808 INNER JOIN PTinfo.tStatus PtS ON PtP.pSysID = PtS.pSysID
14809
14810WHERE PtI.identTypeID = @priPID
14811 AND PtS.isCurrent = 1
14812 AND NVH.dateChange >= @startDate
14813 AND NVH.dateChange < @endDate;
14814
14815/* audit info */
14816EXECUTE Sysinfo.uspAddReportView @username=@username, @auditTypeID=8, @reportName=''Fortify by Date'', @startDate=@startDate, @endDate=@endDate, @addtlParamValue=''N/A'';
14817END;
14818')
14819
14820 PRINT N'USP Rpt.uspFortificationByDate created'
14821
14822END TRY
14823BEGIN CATCH
14824 PRINT N'USP Rpt.uspFortificationByDate create failed'
14825 PRINT 'Error Number: ' + str(error_number())
14826 PRINT 'Line Number: ' + str(error_line())
14827 PRINT error_message()
14828END CATCH
14829GO
14830
14831/* Rpt.uspFeedAvertByAdmin.storedProcedure.sql
14832To call the SP:
14833EXECUTE Rpt.uspFeedAvertByAdmin @startDate='20131101', @endDate='20131130', @feedAdmin='admin', @username='admin';
14834 */
14835
14836BEGIN TRY
14837EXECUTE ('/****** Object: StoredProcedure [Rpt].[uspFeedAvertByAdmin] Script Date: 12/16/2013 11:26:19 ******/
14838
14839-- =============================================
14840-- Author: Lisa Bohm (lbohm@paragondsi.com)
14841-- Create date: 2013-12-16
14842-- Description: Returns near miss data
14843-- Modified:
14844-- =============================================
14845CREATE PROCEDURE [Rpt].[uspFeedAvertByAdmin]
14846 -- Add the parameters for the stored procedure here
14847@startDate date
14848, @endDate date
14849, @feedAdmin varchar(50)
14850 , @username varchar(50)
14851 AS
14852 DECLARE @priPID tinyint
14853 BEGIN
14854 -- SET NOCOUNT ON added to prevent extra result sets from
14855 -- interfering with SELECT statements.
14856 SET NOCOUNT ON;
14857
14858SET @endDate = DATEADD(day,1,@endDate);
14859/* find primary ID */
14860SET @priPID = PTinfo.udfGetPIDtype();
14861
14862/* now get yer the data. We need the whoops data as well as the next feed data for that patient. */
14863
14864SELECT SAL.avertDate
14865 , primaryID = SAL.secondScan
14866 , vesselCode = SAL.firstScan
14867 , feedType = NtC.nCategoryAbbrev
14868 , SRC.reason
14869 , SAL.username
14870 , gdFd.pSysID
14871 , GFvesselCode = gdFd.vesselCode
14872 , GFfeedType = gdFd.nCategoryAbbrev
14873 , GFdateFed = gdFd.dateChange
14874 , GFadmin = gdFd.userChange
14875
14876FROM SysInfo.tAvertLog SAL
14877 INNER JOIN SysInfo.tReasonCode SRC ON SAL.reasonCodeID = SRC.reasonCodeID
14878 LEFT OUTER JOIN PTinfo.tIdentity PtI ON SAL.secondScan = PtI.IDValue
14879 AND PtI.identTypeID = @priPID
14880 LEFT OUTER JOIN (Nutrition.tVessel NtVO
14881 INNER JOIN Nutrition.tCategory NtC ON NtVO.nCategoryID = NtC.nCategoryID
14882 ) ON SAL.secondScan = NtVO.vesselCode
14883
14884 LEFT OUTER JOIN (SELECT NvA.pSysID
14885 , NvH.dateChange
14886 , NvH.userChange
14887 , NtV.vesselCode
14888 , NtCG.nCategoryAbbrev
14889 FROM Nutrition.tVesselAssign NvA
14890 INNER JOIN Nutrition.tVesselHistory NvH ON NvA.vesselID = NvH.vesselID
14891 INNER JOIN Nutrition.tVessel NtV ON NvA.vesselID = NtV.vesselID
14892 INNER JOIN Nutrition.tCategory NtCG ON NtV.nCategoryID = NtCG.nCategoryID
14893 WHERE NvH.statusID = 7
14894 ) gdFd ON PtI.pSysID = gdFd.pSysID
14895 AND GdFd.dateChange < DATEADD(hour, 1, SAL.avertDate)
14896 AND GdFd.dateChange > SAL.avertDate
14897
14898WHERE SAL.avertDate >=@startDate
14899 AND SAL.avertDate < @endDate
14900 AND SAL.userName = @feedAdmin
14901 AND SAL.auditTypeID = 5;
14902
14903/* audit info */
14904EXECUTE Sysinfo.uspAddReportView @username=@username, @auditTypeID=8, @reportName=''Feed Avert By Admin'', @startDate=@startDate, @endDate=@endDate, @addtlParamValue=@feedAdmin;
14905END;
14906')
14907
14908 PRINT N'USP Rpt.uspFeedAvertByAdmin created'
14909
14910END TRY
14911BEGIN CATCH
14912 PRINT N'USP Rpt.uspFeedAvertByAdmin create failed'
14913 PRINT 'Error Number: ' + str(error_number())
14914 PRINT 'Line Number: ' + str(error_line())
14915 PRINT error_message()
14916END CATCH
14917GO
14918
14919/* Rpt.uspPercentEBM.storedProcedure.sql
14920To call the SP:
14921EXECUTE Rpt.uspPercentEBM @startDate='20131101', @endDate='20131130';
14922 */
14923
14924BEGIN TRY
14925EXECUTE ('/****** Object: StoredProcedure [Rpt].[uspPercentEBM] Script Date: 10/09/2013 11:26:19 ******/
14926-- =============================================
14927-- Author: Lisa Bohm (lbohm@paragondsi.com)
14928-- Create date: 2013-10-09
14929-- Description: Returns percent EBM data
14930--
14931-- =============================================
14932CREATE PROCEDURE [Rpt].[uspPercentEBM]
14933 -- Add the parameters for the stored procedure here
14934@startDate date
14935, @endDate date
14936
14937 AS
14938 BEGIN
14939 -- SET NOCOUNT ON added to prevent extra result sets from
14940 -- interfering with SELECT statements.
14941 SET NOCOUNT ON;
14942
14943SET @endDate = DATEADD(day,1,@endDate);
14944
14945WITH cteContents AS
14946( -- anchor
14947SELECT contVessel = NtV.vesselID
14948 , oVessel = NtV.vesselID
14949 , NtV.vesselCode
14950 , admNurse = NVH.userChange
14951 , fed = NVH.dateChange
14952 , NVH.statusID
14953
14954FROM Nutrition.tVessel NtV
14955 INNER JOIN Nutrition.tVesselHistory NVH ON NtV.vesselID = NVH.vesselID
14956 INNER JOIN Nutrition.tStatusList NSL ON NVH.statusID = NSL.statusID
14957
14958
14959WHERE NvH.isCurrent = 1
14960 AND NSL.statusID = 7
14961 AND NvH.dateChange >=@startDate
14962 AND NvH.dateChange < @endDate
14963
14964 UNION ALL
14965 SELECT contVessel = NVOl.vesselID
14966 , oVessel = cte.oVessel
14967 , vesselCode = cte.vesselCode
14968 , admNurse = cte.admNurse
14969 , fed = cte.fed
14970 , statusID = cte.statusID
14971
14972
14973 FROM Nutrition.tVesselOrigin NVOl
14974 INNER JOIN cteContents AS cte ON cte.contVessel = NVOl.targetVesselID
14975)
14976
14977
14978SELECT DISTINCT vesselID = cte.oVessel
14979 , cte.vesselCode
14980 , NtC.nCategoryAbbrev
14981 , cte.admNurse
14982 , cte.fed
14983 , NtV.mls
14984 , NtV.gms
14985 , containsFormula = CASE WHEN NFV.vesselID IS NOT NULL THEN 1 ELSE 0 END
14986
14987FROM cteContents cte
14988 INNER JOiN Nutrition.tVessel NtV ON cte.oVessel = NtV.vesselID
14989 INNER JOIN Nutrition.tCategory NtC ON NtV.nCategoryID = NtC.nCategoryID
14990 LEFT OUTER JOIN Nutrition.tFormulaVessel NFV ON cte.contvessel = NFV.vesselID;
14991
14992END;
14993')
14994
14995 PRINT N'USP Rpt.uspPercentEBM created'
14996
14997END TRY
14998BEGIN CATCH
14999 PRINT N'USP Rpt.uspPercentEBM create failed'
15000 PRINT 'Error Number: ' + str(error_number())
15001 PRINT 'Line Number: ' + str(error_line())
15002 PRINT error_message()
15003END CATCH
15004GO
15005
15006/* Rpt.uspVesselHistory.storedProcedure.sql
15007To call the SP:
15008EXECUTE Rpt.uspVesselHistory @VesselCode='B201', @username='admin';
15009 */
15010
15011BEGIN TRY
15012EXECUTE ('/****** Object: StoredProcedure [Rpt].[uspVesselHistory] Script Date: 11/6/2013 7:45:07 AM ******/
15013-- =============================================
15014-- Author: Lisa Bohm (lbohm@paragondsi.com)
15015-- Create date: 2013-10-09
15016-- Description: Returns data for bottle history
15017-- Modified: 2013-11-21 to handle the HL7MsgField tbl add
15018-- 2013-12-09 add audit tracking call
15019-- 2014-03-03 rework for 1 level back and 1 level forward
15020-- =============================================
15021CREATE PROCEDURE [Rpt].[uspVesselHistory]
15022 -- Add the parameters for the stored procedure here
15023@vesselCode varchar(20)
15024, @username varchar(50)
15025AS
15026 DECLARE @priPID tinyint
15027 , @vesselID bigint
15028 , @isRoot bit;
15029BEGIN
15030 -- SET NOCOUNT ON added to prevent extra result sets from
15031 -- interfering with SELECT statements.
15032 SET NOCOUNT ON;
15033
15034/* find primary ID */
15035SET @priPID = PTinfo.udfGetPIDtype();
15036
15037 /* contents of orig vessel */
15038DECLARE @contents table
15039 (reqVessel bigint
15040 , parentVessel bigint
15041 , contVessel bigint
15042 , vesselCode varchar(20)
15043 , content varchar(100)
15044 );
15045
15046
15047/* yeah what''s the ID? */
15048SELECT @vesselID = NtV.vesselID
15049FROM Nutrition.tVessel NtV
15050WHERE NtV.vesselCode = @vesselCode;
15051
15052SET @isRoot = COALESCE((SELECT MAX(1)
15053 FROM Nutrition.tVesselOrigin NVO
15054 WHERE NVO.targetVesselID = @vesselID),0);
15055
15056IF @isRoot > 0
15057BEGIN
15058/* immediate predecessors */
15059
15060 WITH cteContents AS
15061 ( -- anchor
15062
15063 SELECT reqVessel = @vesselID
15064 , parentVessel = NVO.vesselID
15065 , ipVessel = NVO.vesselID
15066 , contVessel = NVO.vesselID
15067 FROM Nutrition.tVesselOrigin NVO
15068 WHERE NVO.targetVesselID = @vesselID
15069
15070 UNION ALL
15071 SELECT reqVessel = cte.reqVessel
15072 , parentVessel = cte.parentvessel
15073 , ipVessel = cte.contVessel
15074 , contVessel = NVOL.vesselID
15075
15076 FROM Nutrition.tVesselOrigin NVOl
15077 INNER JOIN cteContents cte ON cte.ipVessel = NVOl.targetVesselID
15078 )
15079
15080
15081 INSERT INTO @contents (reqVessel, parentVessel, contVessel)
15082 SELECT DISTINCT CtC.reqVessel
15083 , CtC.parentVessel
15084 , CtC.contVessel
15085 FROM cteContents CtC;
15086
15087 END
15088
15089 INSERT INTO @contents (reqVessel, parentVessel, contVessel)
15090 VALUES (@vesselID, @vesselID, @vesselID);
15091
15092UPDATE cC
15093SET cC.vesselCode = NtV.vesselCode
15094 , cC.content = CASE WHEN NMV.vesselID IS NOT NULL THEN PtP.firstName + '' '' + PtP.lastName + '' ('' + PtI.IDValue + '')'' ELSE
15095 CASE WHEN NDV.vesselID IS NOT NULL THEN NDB.bankName + '' ('' + NDL.lotCode + '')'' ELSE
15096 CASE WHEN NFV.vesselID IS NOT NULL THEN NF.formulaName + '' ('' + NFL.formLotCode + '')'' ELSE
15097 NULL
15098 END
15099 END
15100 END
15101 , contVessel = cC.parentVessel
15102
15103FROM Nutrition.tVessel NtV
15104 INNER JOIN @contents cC ON NtV.vesselID = cC.parentVessel
15105 LEFT OUTER JOIN (Nutrition.tMotherVessel NMV
15106 INNER JOIN PTinfo.tPatient PtP ON PtP.pSysID = NmV.pSysID
15107 INNER JOIN PTinfo.tIdentity PtI ON PtP.pSysID = PtI.pSysID
15108 AND PtI.identTypeID = @priPID
15109 ) ON cC.contVessel = NMV.vesselID
15110 LEFT OUTER JOIN (Nutrition.tDonorVessel NDV
15111 INNER JOIN Nutrition.tDonorLot NDL ON NDV.lotID = NDL.lotID
15112 INNER JOIN Nutrition.tDonorBank NDB ON NDL.donorBankID = NDB.donorBankID
15113 ) ON cC.contVessel = NDV.vesselID
15114 LEFT OUTER JOIN (Nutrition.tFormulaVessel NFV
15115 INNER JOIN Nutrition.tFormulaLot NFL ON NFV.formlotID = NFL.formlotID
15116 INNER JOIN Nutrition.tFormula NF ON NFL.formulaID = NF.formulaID
15117 ) ON cC.contVessel = NFV.vesselID;
15118
15119/* child vessels */
15120INSERT INTO @contents (reqVessel, parentVessel, contVessel, vesselCode)
15121
15122SELECT @vesselID
15123 , @vesselID
15124 , NtV.vesselID
15125 , NtV.vesselCode
15126
15127FROM Nutrition.tVesselOrigin NVO
15128 INNER JOIN Nutrition.tVessel NtV ON NVO.targetVesselID = NTV.vesselID
15129WHERE NVO.vesselID = @vesselID;
15130
15131
15132SELECT oVesselCode = NVorig.vesselCode
15133 , vesselCode = CASE WHEN LEFT(cC.vesselCode,2) = ''RT'' THEN
15134 CASE WHEN LEFT(NtVPC.vesselCode,2) = ''RT'' THEN @vesselCode ELSE NtVPC.vesselCode END
15135 ELSE cC.vesselCode END
15136 , fType = CASE WHEN cC.parentVessel = cC.contVessel THEN
15137 CASE WHEN cC.contVessel = cC.reqVessel THEN 1 ELSE
15138 CASE WHEN LEFT(cC.vesselCode,2) = ''RT'' THEN 1 ELSE 0 END
15139 END
15140 ELSE 2 END
15141 , contain = CASE WHEN LEFT(cC.vesselCode,2) = ''RT'' THEN COALESCE(substring(REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( contAll.caList, ''&'', ''&'' ), ''''', '''''''' ), ''"'', ''"'' ), ''<'', ''<'' ), ''>'', ''>''),3,75), '''') ELSE
15142 CASE WHEN cC.reqVessel = cC.parentVessel THEN COALESCE(substring(REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( contAll.caList, ''&'', ''&'' ), ''''', '''''''' ), ''"'', ''"'' ), ''<'', ''<'' ), ''>'', ''>''),3,75), '''') ELSE
15143 COALESCE(substring(REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( cont.ctList, ''&'', ''&'' ), ''''', '''''''' ), ''"'', ''"'' ), ''<'', ''<'' ), ''>'', ''>''),3,75), '''') END
15144 END
15145 , NSC.statCatName
15146 , NSL.statusName
15147 , NvH.dateChange
15148 , NvH.userChange
15149 , NvH.expirDate
15150 , assignedTo = COALESCE(CASE WHEN len(PtCp.midInit) > 0 THEN
15151 PtCP. firstName + '' '' + PtCP.midInit + ''. '' + PtCP.lastName
15152 ELSE
15153 PtCP. firstName + '' '' + PtCP.lastName
15154 END
15155 , '''')
15156
15157FROM (SELECT DISTINCT dC.reqVessel, dC.parentVessel, dC.contVessel, dC.vesselCode FROM @contents dC ) cC
15158 INNER JOIN Nutrition.tVessel NVorig ON cC.reqVessel = NVorig.vesselID
15159 INNER JOIN Nutrition.tVesselHistory NVH ON cC.parentVessel = NVH.vesselID
15160 INNER JOIN Nutrition.tStatusList NSL ON NvH.statusID = NSL.statusID
15161 INNER JOIN Nutrition.tStatusCategory NSC ON NSL.statCatID = NSC.statCatID
15162 INNER JOIN Nutrition.tVessel NtVPC ON cC.parentVessel = NtVPC.vesselID
15163 LEFT OUTER JOIN (Nutrition.tVesselAssign NtA
15164 INNER JOIN PTinfo.tPatient PtCP ON NtA.pSysID = PtCP.pSysID
15165 INNER JOIN PTinfo.tIdentity PtC ON NtA.pSysID = PtC.pSysID
15166 AND PtC.identTypeID = @priPID
15167 ) ON cC.contVessel = NtA.vesselID
15168 OUTER APPLY (SELECT DISTINCT '', '' + cT.content
15169 FROM @contents cT
15170 WHERE cT.content IS NOT NULL
15171 AND cT.parentVessel = cC.parentVessel
15172 FOR XML PATH ('''')) cont (ctList)
15173 OUTER APPLY (SELECT DISTINCT '', '' + cT.content
15174 FROM @contents cT
15175 WHERE cT.content IS NOT NULL
15176 FOR XML PATH ('''')) contAll (caList)
15177
15178WHERE ((LEFT(cC.vesselCode,2) = ''RT'' AND NSL.statCatID = 5)
15179 OR (LEFT(cC.vesselCode,2) <> ''RT''))
15180
15181ORDER BY CASE WHEN cC.parentVessel = cC.contVessel THEN
15182 CASE WHEN cC.contVessel = cC.reqVessel THEN 1 ELSE
15183 CASE WHEN LEFT(cC.vesselCode,2) = ''RT'' THEN 1 ELSE 0 END
15184 END
15185 ELSE 2 END
15186 , CASE WHEN cC.reqVessel = cC.contVessel then 1 ELSE
15187 CASE WHEN LEFT(cC.vesselCode,2) = ''RT'' THEN 1 ELSE 0 END
15188 END
15189 , CASE WHEN LEFT(cC.vesselCode,2) = ''RT'' THEN reqVessel ELSE parentVessel END
15190 , CASE WHEN LEFT(cC.vesselCode,2) = ''RT'' THEN reqVessel ELSE contVessel END
15191 , dateChange
15192 , CASE WHEN NSL.statCatID = 5 THEN 0 ELSE 1 END
15193 , NSL.statusID;
15194
15195/* audit info */
15196EXECUTE Sysinfo.uspAddReportView @username=@username, @auditTypeID=8, @reportName=''Vessel History'', @startDate=''19000101'', @endDate=''25000101'', @addtlParamValue=@vesselCode;
15197
15198END;
15199')
15200
15201 PRINT N'USP Rpt.uspVesselHistory created'
15202
15203END TRY
15204BEGIN CATCH
15205 PRINT N'USP Rpt.uspVesselHistory create failed'
15206 PRINT 'Error Number: ' + str(error_number())
15207 PRINT 'Line Number: ' + str(error_line())
15208 PRINT error_message()
15209END CATCH
15210GO
15211
15212/* SysInfo.uspAuditRecordInfo.storedProcedure.sql
15213To call the SP:
15214EXECUTE SysInfo.uspAuditRecordInfo;
15215 */
15216
15217BEGIN TRY
15218EXECUTE ('/****** Object: StoredProcedure [SysInfo].[uspAuditRecordInfo] Script Date: 3/20/2013 11:26:19 ******/
15219
15220-- =============================================
15221-- Author: Lisa Bohm (lbohm@paragondsi.com)
15222-- Create date: 2013-12-13
15223-- Description: Summary of masses of audit info
15224--
15225-- =============================================
15226CREATE PROCEDURE [SysInfo].[uspAuditRecordInfo]
15227 -- Add the parameters for the stored procedure here
15228
15229AS
15230DECLARE @dcRct bigint
15231 , @vrecRct bigint
15232 , @vrptRct bigint
15233 , @dcLast date
15234 , @vrecLast date
15235 , @vrptLast date;
15236
15237BEGIN
15238 -- SET NOCOUNT ON added to prevent extra result sets from
15239 -- interfering with SELECT statements.
15240 SET NOCOUNT ON;
15241
15242SELECT @dcRct = COUNT(sDC.dataChangeID)
15243FROM Sysinfo.tDataChange sDC;
15244
15245SELECT @vRecRct = COUNT(sRV.recordViewID)
15246FROM Sysinfo.tRecordView sRV;
15247
15248SELECT @vRptRct = COUNT(sRpV.reportViewID)
15249FROM Sysinfo.tReportView sRpV;
15250
15251SET @dcLast = (SELECT TOP 1 sDC.viewDate
15252FROM Sysinfo.tDataChange sDC
15253ORDER By sDC.viewDate ASC);
15254
15255SET @vrecLast = (SELECT TOP 1 sRV.viewDate
15256FROM Sysinfo.tRecordView sRV
15257ORDER By sRV.viewDate ASC);
15258
15259SET @vrptLast = (SELECT TOP 1 sRpV.viewDate
15260FROM Sysinfo.tReportView sRpV
15261ORDER By sRpV.viewDate ASC);
15262
15263SELECT dRowCt = @dcRct
15264 , rRowCt = @vrecRct
15265 , pRowCt = @vrptRct
15266 , dSinceDate = @dcLast
15267 , rsinceDate = @vrecLast
15268 , pSinceDate = @vrptLast;
15269
15270END;
15271')
15272
15273 PRINT N'USP SysInfo.uspAuditRecordInfo created'
15274
15275END TRY
15276BEGIN CATCH
15277 PRINT N'USP SysInfo.uspAuditRecordInfo create failed'
15278 PRINT 'Error Number: ' + str(error_number())
15279 PRINT 'Line Number: ' + str(error_line())
15280 PRINT error_message()
15281END CATCH
15282GO
15283
15284/* SysInfo.uspDCarchive.storedProcedure.sql
15285To call the SP:
15286EXECUTE SysInfo.uspDCarchive;
15287 */
15288
15289BEGIN TRY
15290EXECUTE ('/****** Object: StoredProcedure [SysInfo].[uspDCarchive] Script Date: 3/20/2013 11:26:19 ******/
15291
15292-- =============================================
15293-- Author: Lisa Bohm (lbohm@paragondsi.com)
15294-- Create date: 2013-12-13
15295-- Description: Summary of masses of audit info
15296--
15297-- =============================================
15298CREATE PROCEDURE [SysInfo].[uspDCarchive]
15299 -- Add the parameters for the stored procedure here
15300AS
15301DECLARE @keepDays tinyint = 7
15302 , @pullBefore date
15303BEGIN
15304 -- SET NOCOUNT ON added to prevent extra result sets from
15305 -- interfering with SELECT statements.
15306 SET NOCOUNT ON;
15307/* must keep at least 7 days */
15308SELECT @keepDays = CAST(StS.setValue AS INT)
15309 FROM SysInfo.tSetting StS
15310 WHERE StS.settingID=73;
15311/* must keep at least 7 days */
15312IF @keepDays < 7
15313 BEGIN
15314 SET @keepDays = 7;
15315 END
15316
15317SET @pullBefore = DATEADD(day, -1*@keepDays, getdate())
15318
15319SELECT sDC.dataChangeID
15320, sDC.viewDate
15321, sDC.username
15322, sDC.auditTypeID
15323, sDC.tableName
15324, sDC.recordID
15325, sDC.previousEntry
15326
15327FROM SysInfo.tDataChange sDC
15328WHERE sDC.viewDate < @pullBefore;
15329
15330END;
15331')
15332
15333 PRINT N'USP SysInfo.uspDCarchive created'
15334
15335END TRY
15336BEGIN CATCH
15337 PRINT N'USP SysInfo.uspDCarchive create failed'
15338 PRINT 'Error Number: ' + str(error_number())
15339 PRINT 'Line Number: ' + str(error_line())
15340 PRINT error_message()
15341END CATCH
15342GO
15343
15344/* SysInfo.uspRecArchive.storedProcedure.sql
15345To call the SP:
15346EXECUTE SysInfo.uspRecArchive;
15347 */
15348
15349BEGIN TRY
15350EXECUTE ('/****** Object: StoredProcedure [SysInfo].[uspRecArchive] Script Date: 12/13/2013 11:26:19 ******/
15351
15352-- =============================================
15353-- Author: Lisa Bohm (lbohm@paragondsi.com)
15354-- Create date: 2013-12-13
15355-- Description: Record view info for archival
15356--
15357-- =============================================
15358CREATE PROCEDURE [SysInfo].[uspRecArchive]
15359 -- Add the parameters for the stored procedure here
15360AS
15361DECLARE @keepDays tinyint = 7
15362 , @pullBefore date
15363BEGIN
15364 -- SET NOCOUNT ON added to prevent extra result sets from
15365 -- interfering with SELECT statements.
15366 SET NOCOUNT ON;
15367/* must keep at least 7 days */
15368SELECT @keepDays = CAST(StS.setValue AS INT)
15369 FROM SysInfo.tSetting StS
15370 WHERE StS.settingID=73;
15371/* must keep at least 7 days */
15372IF @keepDays < 7
15373 BEGIN
15374 SET @keepDays = 7;
15375 END
15376
15377SET @pullBefore = DATEADD(day, -1*@keepDays, getdate())
15378
15379SELECT sRV.recordViewID
15380, sRV.viewDate
15381, sRV.username
15382, sRV.auditTypeID
15383, sRV.pSysID
15384
15385FROM SysInfo.tRecordView sRV
15386WHERE sRV.viewDate < @pullBefore;
15387
15388END;
15389')
15390
15391 PRINT N'USP SysInfo.uspRecArchive created'
15392
15393END TRY
15394BEGIN CATCH
15395 PRINT N'USP SysInfo.uspRecArchive create failed'
15396 PRINT 'Error Number: ' + str(error_number())
15397 PRINT 'Line Number: ' + str(error_line())
15398 PRINT error_message()
15399END CATCH
15400GO
15401
15402/* SysInfo.uspRptArchive.storedProcedure.sql
15403To call the SP:
15404EXECUTE SysInfo.uspRptArchive;
15405 */
15406
15407BEGIN TRY
15408EXECUTE ('/****** Object: StoredProcedure [SysInfo].[uspRptArchive] Script Date: 12/13/2013 11:26:19 ******/
15409
15410-- =============================================
15411-- Author: Lisa Bohm (lbohm@paragondsi.com)
15412-- Create date: 2013-12-13
15413-- Description: Report view info for archival
15414--
15415-- =============================================
15416CREATE PROCEDURE [SysInfo].[uspRptArchive]
15417 -- Add the parameters for the stored procedure here
15418AS
15419DECLARE @keepDays tinyint = 7
15420 , @pullBefore date
15421BEGIN
15422 -- SET NOCOUNT ON added to prevent extra result sets from
15423 -- interfering with SELECT statements.
15424 SET NOCOUNT ON;
15425/* must keep at least 7 days */
15426SELECT @keepDays = CAST(StS.setValue AS INT)
15427 FROM SysInfo.tSetting StS
15428 WHERE StS.settingID=73;
15429/* must keep at least 7 days */
15430IF @keepDays < 7
15431 BEGIN
15432 SET @keepDays = 7;
15433 END
15434
15435SET @pullBefore = DATEADD(day, -1*@keepDays, getdate())
15436
15437SELECT sRpV.reportViewID
15438, sRpV.viewDate
15439, sRpV.username
15440, sRpV.auditTypeID
15441, sRpV.reportName
15442, sRpV.startDate
15443, sRpV.endDate
15444, sRpV.addtlParamValue
15445
15446FROM SysInfo.tReportView sRpV
15447WHERE sRpV.viewDate < @pullBefore;
15448
15449END;
15450')
15451
15452 PRINT N'USP SysInfo.uspRptArchive created'
15453
15454END TRY
15455BEGIN CATCH
15456 PRINT N'USP SysInfo.uspRptArchive create failed'
15457 PRINT 'Error Number: ' + str(error_number())
15458 PRINT 'Line Number: ' + str(error_line())
15459 PRINT error_message()
15460END CATCH
15461GO
15462
15463/* SysInfo.uspAuditPurge.storedProcedure.sql
15464To call the SP:
15465EXECUTE SysInfo.uspAuditPurge;
15466 */
15467
15468BEGIN TRY
15469EXECUTE ('/****** Object: StoredProcedure [SysInfo].[uspAuditPurge] Script Date: 12/13/2013 11:26:19 ******/
15470
15471-- =============================================
15472-- Author: Lisa Bohm (lbohm@paragondsi.com)
15473-- Create date: 2013-12-13
15474-- Description: Purge records from 3 audit tables
15475--
15476-- =============================================
15477CREATE PROCEDURE [SysInfo].[uspAuditPurge]
15478 -- Add the parameters for the stored procedure here
15479@dDel bit = 0
15480 , @rDel bit = 0
15481 , @pDel bit = 0
15482
15483AS
15484DECLARE @keepDays tinyint = 7
15485 , @delBefore date
15486 , @errCode tinyint
15487BEGIN
15488 -- SET NOCOUNT ON added to prevent extra result sets from
15489 -- interfering with SELECT statements.
15490 SET NOCOUNT ON;
15491/* must keep at least 7 days */
15492SELECT @keepDays = CAST(StS.setValue AS INT)
15493 FROM SysInfo.tSetting StS
15494 WHERE StS.settingID=73;
15495/* must keep at least 7 days */
15496IF @keepDays < 7
15497 BEGIN
15498 SET @keepDays = 7;
15499 END
15500
15501SET @delBefore = DATEADD(day, -1*@keepDays, getdate())
15502
15503BEGIN TRY
15504BEGIN TRANSACTION
15505IF @dDel = 1
15506BEGIN
15507DELETE
15508FROM SysInfo.tDataChange
15509WHERE viewDate < @delBefore;
15510END
15511
15512IF @rDel = 1
15513BEGIN
15514DELETE
15515FROM SysInfo.tRecordView
15516WHERE viewDate < @delBefore;
15517END
15518
15519IF @pDel = 1
15520BEGIN
15521DELETE
15522FROM SysInfo.tReportView
15523WHERE viewDate < @delBefore;
15524END
15525
15526COMMIT TRANSACTION
15527
15528SET @errCode = 0;
15529SELECT errCode = @errCode;
15530END TRY
15531
15532BEGIN CATCH
15533ROLLBACK TRANSACTION
15534SET @errCode = 1;
15535SELECT errCode = @errCode;
15536
15537END CATCH
15538
15539END
15540;
15541')
15542
15543 PRINT N'USP SysInfo.uspAuditPurge created'
15544
15545END TRY
15546BEGIN CATCH
15547 PRINT N'USP SysInfo.uspAuditPurge create failed'
15548 PRINT 'Error Number: ' + str(error_number())
15549 PRINT 'Line Number: ' + str(error_line())
15550 PRINT error_message()
15551END CATCH
15552GO
15553
15554/* Rpt.uspAuditData.storedProcedure.sql
15555To call the SP:
15556EXECUTE Rpt.uspAuditData @startDate = '20131201', @endDate=20131215', @username='admin';
15557 */
15558
15559BEGIN TRY
15560EXECUTE ('/****** Object: StoredProcedure [Rpt].[uspAuditData] Script Date: 10/09/2013 11:26:19 ******/
15561
15562-- =============================================
15563-- Author: Lisa Bohm (lbohm@paragondsi.com)
15564-- Create date: 2013-12-13
15565-- Description: Returns audit info for data change
15566-- Modified:
15567-- =============================================
15568CREATE PROCEDURE [Rpt].[uspAuditData]
15569 -- Add the parameters for the stored procedure here
15570@startDate date
15571, @endDate date
15572, @username varchar(50)
15573
15574 AS
15575 BEGIN
15576 -- SET NOCOUNT ON added to prevent extra result sets from
15577 -- interfering with SELECT statements.
15578 SET NOCOUNT ON;
15579
15580SET @endDate = DATEADD(day, 1, @endDate);
15581
15582SELECT dateChange = sDC.viewDate
15583, sDC.username
15584, sAT.auditTypeName
15585, sDC.tableName
15586, sDC.previousEntry
15587
15588FROM SysInfo.tDataChange sDC
15589 INNER JOIN SysInfo.tAuditType SAT ON sDC.auditTypeID = sAT.auditTypeID
15590 WHERE sDC.viewdate < @endDate
15591 AND sDC.viewDate >= @startDate
15592
15593UNION ALL
15594
15595SELECT NvH.dateChange
15596 , username=NvH.userChange
15597 , auditTypeName = ''add record''
15598 , tableName =''Nutrition.tVesselHistory''
15599 , previousEntry = ''N/A''
15600
15601FROM Nutrition.tVesselHistory NVH
15602 WHERE NvH.dateChange < @endDate
15603 AND NvH.dateChange >= @startDate
15604
15605ORDER BY dateChange;
15606
15607EXECUTE Sysinfo.uspAddReportView @username=@username, @auditTypeID=8, @reportName=''Data Change Audit'', @startDate=@startDate, @endDate=@endDate, @addtlParamValue=''N/A'';
15608
15609END;
15610')
15611
15612 PRINT N'USP Rpt.uspAuditData created'
15613
15614END TRY
15615BEGIN CATCH
15616 PRINT N'USP Rpt.uspAuditData create failed'
15617 PRINT 'Error Number: ' + str(error_number())
15618 PRINT 'Line Number: ' + str(error_line())
15619 PRINT error_message()
15620END CATCH
15621GO
15622
15623/* Rpt.uspLogin.storedProcedure.sql
15624To call the SP:
15625EXECUTE Rpt.uspLogin @startDate = '20131201', @endDate=20131215', @username='admin';
15626 */
15627
15628BEGIN TRY
15629EXECUTE ('/****** Object: StoredProcedure [Rpt].[uspLogin] Script Date: 10/09/2013 11:26:19 ******/
15630
15631-- =============================================
15632-- Author: Lisa Bohm (lbohm@paragondsi.com)
15633-- Create date: 2013-12-16
15634-- Description: Returns login
15635--
15636-- =============================================
15637CREATE PROCEDURE [Rpt].[uspLogin]
15638 -- Add the parameters for the stored procedure here
15639@startDate date
15640, @endDate date
15641, @username varchar(50)
15642 AS
15643 BEGIN
15644 -- SET NOCOUNT ON added to prevent extra result sets from
15645 -- interfering with SELECT statements.
15646 SET NOCOUNT ON;
15647
15648SET @endDate = DATEADD(day,1,@endDate);
15649
15650SELECT SAL.accessDate
15651 , SAL. userName
15652 , SAL.auditTypeID
15653 , SAT.auditTypeName
15654 , SAL.unit
15655
15656 FROM SysInfo.tAccessLog SAL
15657 INNER JOIN Sysinfo.tAuditType SAT ON SAL.auditTypeID = SAT.auditTypeID
15658WHERE SAL.accessDate >=@startDate
15659 AND SAL.accessDate < @endDate;
15660
15661EXECUTE Sysinfo.uspAddReportView @username=@username, @auditTypeID=8, @reportName=''Access Log'', @startDate=@startDate, @endDate=@endDate, @addtlParamValue=''N/A'';
15662
15663END;
15664')
15665
15666 PRINT N'USP Rpt.uspLogin created'
15667
15668END TRY
15669BEGIN CATCH
15670 PRINT N'USP Rpt.uspLogin create failed'
15671 PRINT 'Error Number: ' + str(error_number())
15672 PRINT 'Line Number: ' + str(error_line())
15673 PRINT error_message()
15674END CATCH
15675GO
15676
15677/* Rpt.uspLoginByType.storedProcedure.sql
15678To call the SP:
15679EXECUTE Rpt.uspLoginByType @startDate = '20131201', @endDate=20131215', @auditTypeID=2, @username='admin';
15680 */
15681
15682BEGIN TRY
15683EXECUTE ('/****** Object: StoredProcedure [Rpt].[uspLoginByType] Script Date: 10/09/2013 11:26:19 ******/
15684
15685-- =============================================
15686-- Author: Lisa Bohm (lbohm@paragondsi.com)
15687-- Create date: 2013-12-16
15688-- Description: Returns login info by type
15689--
15690-- =============================================
15691CREATE PROCEDURE [Rpt].[uspLoginByType]
15692 -- Add the parameters for the stored procedure here
15693@startDate date
15694, @endDate date
15695, @auditTypeID tinyint
15696, @username varchar(50)
15697 AS
15698 BEGIN
15699 -- SET NOCOUNT ON added to prevent extra result sets from
15700 -- interfering with SELECT statements.
15701 SET NOCOUNT ON;
15702
15703SET @endDate = DATEADD(day,1,@endDate);
15704
15705SELECT SAL.accessDate
15706 , SAL. userName
15707 , SAL.auditTypeID
15708 , SAt.auditTypeName
15709 , SAL.unit
15710
15711 FROM SysInfo.tAccessLog SAL
15712 INNER JOIN Sysinfo.tAuditType SAT ON SAL.auditTypeID = SAT.auditTypeID
15713WHERE SAL.accessDate >=@startDate
15714 AND SAL.accessDate < @endDate
15715 AND SAL.auditTypeID = @auditTypeID;
15716
15717EXECUTE Sysinfo.uspAddReportView @username=@username, @auditTypeID=8, @reportName=''Access Log'', @startDate=@startDate, @endDate=@endDate, @addtlParamValue=@auditTypeID;
15718
15719END;
15720')
15721
15722 PRINT N'USP Rpt.uspLoginByType created'
15723
15724END TRY
15725BEGIN CATCH
15726 PRINT N'USP Rpt.uspLoginByType create failed'
15727 PRINT 'Error Number: ' + str(error_number())
15728 PRINT 'Line Number: ' + str(error_line())
15729 PRINT error_message()
15730END CATCH
15731GO
15732
15733/* Rpt.uspRecordView.storedProcedure.sql
15734To call the SP:
15735EXECUTE Rpt.uspRecordView @startDate = '20131201', @endDate=20131215', @username='admin';
15736 */
15737
15738BEGIN TRY
15739EXECUTE ('/****** Object: StoredProcedure [Rpt].[uspRecordView] Script Date: 10/09/2013 11:26:19 ******/
15740
15741-- =============================================
15742-- Author: Lisa Bohm (lbohm@paragondsi.com)
15743-- Create date: 2013-12-16
15744-- Description: Returns Record views
15745--
15746-- =============================================
15747CREATE PROCEDURE [Rpt].[uspRecordView]
15748 -- Add the parameters for the stored procedure here
15749@startDate date
15750, @endDate date
15751, @username varchar(50)
15752 AS
15753 DECLARE @priPID tinyint
15754 BEGIN
15755 -- SET NOCOUNT ON added to prevent extra result sets from
15756 -- interfering with SELECT statements.
15757 SET NOCOUNT ON;
15758
15759/* find primary ID */
15760SET @priPID = PTinfo.udfGetPIDtype();
15761SET @endDate = DATEADD(day,1,@endDate);
15762
15763SELECT SRV.viewDate
15764 , SRV. userName
15765 , theName = PTP.firstName + '' '' + PTP.midInit + '' '' + PTP.lastName + '' ('' + PtI.IDvalue + '')''
15766
15767 FROM SysInfo.tRecordView SRV
15768 INNER JOIN PTinfo.tPatient PTP ON SRV.pSysID = PTP.pSysID
15769 INNER JOIN PTinfo.tIdentity PtI ON PtP.pSysID = PtI.pSysID
15770
15771 WHERE SRV.viewDate >=@startDate
15772 AND SRV.viewDate < @endDate
15773 AND PtI.identTypeID = @priPID;
15774
15775EXECUTE Sysinfo.uspAddReportView @username=@username, @auditTypeID=8, @reportName=''Record View'', @startDate=@startDate, @endDate=@endDate, @addtlParamValue=''N/A'';
15776
15777END;
15778')
15779
15780 PRINT N'USP Rpt.uspRecordView created'
15781
15782END TRY
15783BEGIN CATCH
15784 PRINT N'USP Rpt.uspRecordView create failed'
15785 PRINT 'Error Number: ' + str(error_number())
15786 PRINT 'Line Number: ' + str(error_line())
15787 PRINT error_message()
15788END CATCH
15789GO
15790
15791/* Rpt.uspPrepAvert.storedProcedure.sql
15792To call the SP:
15793EXECUTE Rpt.uspPrepAvert @startDate = '20131201', @endDate=20131215', @username='admin';
15794 */
15795
15796BEGIN TRY
15797EXECUTE ('/****** Object: StoredProcedure [Rpt].[uspPrepAvert] Script Date: 12/16/2013 11:26:19 ******/
15798
15799-- =============================================
15800-- Author: Lisa Bohm (lbohm@paragondsi.com)
15801-- Create date: 2013-12-16
15802-- Description: Returns prep error data
15803-- Modified:
15804-- =============================================
15805CREATE PROCEDURE [Rpt].[uspPrepAvert]
15806 -- Add the parameters for the stored procedure here
15807@startDate date
15808, @endDate date
15809 , @username varchar(50)
15810 AS
15811 BEGIN
15812 -- SET NOCOUNT ON added to prevent extra result sets from
15813 -- interfering with SELECT statements.
15814 SET NOCOUNT ON;
15815
15816SET @endDate = DATEADD(day,1,@endDate);
15817
15818/* now get yer the data. We need the whoops data as well as the next feed data for that patient. */
15819
15820SELECT SAL.avertDate
15821 , SAL.username
15822 , firstVessel = SAL.firstScan
15823 , secondVessel = SAL.secondScan
15824 , SRC.reason
15825
15826FROM SysInfo.tAvertLog SAL
15827 INNER JOIN SysInfo.tReasonCode SRC ON SAL.reasonCodeID = SRC.reasonCodeID
15828WHERE SAL.avertDate >=@startDate
15829 AND SAL.avertDate < @endDate
15830 AND SAL.auditTypeID = 6;
15831
15832/* audit info */
15833EXECUTE Sysinfo.uspAddReportView @username=@username, @auditTypeID=8, @reportName=''Prep Avert'', @startDate=@startDate, @endDate=@endDate, @addtlParamValue=''N/A'';
15834END;
15835')
15836
15837 PRINT N'USP Rpt.uspPrepAvert created'
15838
15839END TRY
15840BEGIN CATCH
15841 PRINT N'USP Rpt.uspPrepAvert create failed'
15842 PRINT 'Error Number: ' + str(error_number())
15843 PRINT 'Line Number: ' + str(error_line())
15844 PRINT error_message()
15845END CATCH
15846GO
15847
15848/* Rpt.uspReportView.storedProcedure.sql
15849To call the SP:
15850EXECUTE Rpt.uspReportView @startDate = '20131201', @endDate=20131215', @username='admin';
15851 */
15852
15853BEGIN TRY
15854EXECUTE ('/****** Object: StoredProcedure [Rpt].[uspReportView] Script Date: 10/09/2013 11:26:19 ******/
15855-- =============================================
15856-- Author: Lisa Bohm (lbohm@paragondsi.com)
15857-- Create date: 2013-12-16
15858-- Description: Returns Record views
15859--
15860-- =============================================
15861CREATE PROCEDURE [Rpt].[uspReportView]
15862 -- Add the parameters for the stored procedure here
15863@startDate date
15864, @endDate date
15865, @username varchar(50)
15866 AS
15867 BEGIN
15868 -- SET NOCOUNT ON added to prevent extra result sets from
15869 -- interfering with SELECT statements.
15870 SET NOCOUNT ON;
15871
15872/* find primary ID */
15873SET @endDate = DATEADD(day,1,@endDate);
15874
15875SELECT SRV.viewDate
15876 , SRV. userName
15877 , SRV.reportName
15878 , sDate = SRV.startDate
15879 , eDate = SRV.endDate
15880 , SRV.addtlParamValue
15881
15882 FROM SysInfo.tReportView SRV
15883
15884 WHERE SRV.viewDate >=@startDate
15885 AND SRV.viewDate < @endDate;
15886
15887EXECUTE Sysinfo.uspAddReportView @username=@username, @auditTypeID=8, @reportName=''Report View'', @startDate=@startDate, @endDate=@endDate, @addtlParamValue=''N/A'';
15888END;
15889')
15890
15891 PRINT N'USP Rpt.uspReportView created'
15892
15893END TRY
15894BEGIN CATCH
15895 PRINT N'USP Rpt.uspReportView create failed'
15896 PRINT 'Error Number: ' + str(error_number())
15897 PRINT 'Line Number: ' + str(error_line())
15898 PRINT error_message()
15899END CATCH
15900GO
15901
15902/* Rpt.uspAvailEBMByInfant.storedProcedure.sql
15903To call the SP:
15904EXECUTE Rpt.uspAvailEBMByInfant @pSysID=12, @username='admin';
15905 */
15906
15907BEGIN TRY
15908EXECUTE ('/****** Object: StoredProcedure [Rpt].[uspAvailEBMByInfant] Script Date: 10/09/2013 11:26:19 ******/
15909-- =============================================
15910-- Author: Lisa Bohm (lbohm@paragondsi.com)
15911-- Create date: 2013-12-19
15912-- Description: Returns milk inventory assigned to an infant
15913-- Modified:
15914-- =============================================
15915CREATE PROCEDURE [Rpt].[uspAvailEBMByInfant]
15916 -- Add the parameters for the stored procedure here
15917@pSysID bigint
15918, @username varchar(50)
15919 AS
15920 DECLARE @priPID tinyint
15921 BEGIN
15922 -- SET NOCOUNT ON added to prevent extra result sets from
15923 -- interfering with SELECT statements.
15924 SET NOCOUNT ON;
15925
15926/* find primary ID */
15927SET @priPID = PTinfo.udfGetPIDtype();
15928
15929SELECT NtV.vesselID
15930 , NtV.vesselCode
15931 , vesselCont = NtC.nCategoryAbbrev
15932 , NVHC.expirDate
15933 , NSL.statusName
15934 , NtV.mls
15935 , NtV.gms
15936 , locationCode = StL.locationName
15937 , PtP.firstName
15938 , PtP.lastName
15939 , PtP.midInit
15940 , primaryID = PtI.IDvalue
15941 , recdDate = NVH.dateChange
15942
15943FROM Nutrition.tVessel NtV
15944 INNER JOIN Storage.tLocation StL ON NtV.locationID = StL.locationID
15945 INNER JOIN Nutrition.tCategory NtC ON NtV.nCategoryID = NtC.nCategoryID
15946 LEFT OUTER JOIN Nutrition.tVesselHistory NVH ON NtV.vesselID = NVH.vesselID
15947 AND NVH.statusID = 17
15948 LEFT OUTER JOIN Nutrition.tVesselHistory NVHC ON NtV.vesselID = NVHC.vesselID
15949 AND NVHC.isCurrent = 1
15950 INNER JOIN Nutrition.tStatusList NSL ON NVHC.statusID = NSL.statusID
15951 INNER JOIN Nutrition.tVesselAssign NVA ON NtV.vesselID = NVA.vesselID
15952 INNER JOIN PTinfo.tPatient PtP ON PtP.pSysID = NVA.pSysID
15953 INNER JOIN PTinfo.tIdentity PtI ON PtP.pSysID = PtI.pSysID
15954
15955 WHERE PtI.identTypeID = @priPID
15956 AND NVA.pSysID = @pSysID
15957 AND NSL.statCatID = 2
15958 AND NVHC.expirDate > getdate();
15959
15960EXECUTE Sysinfo.uspAddReportView @username=@username, @auditTypeID=8, @reportName=''Available EBM By Infant'', @startDate=''19000101'', @endDate=''25000101'', @addtlParamValue=@pSysID;
15961
15962END;
15963')
15964
15965 PRINT N'USP Rpt.uspAvailEBMByInfant created'
15966
15967END TRY
15968BEGIN CATCH
15969 PRINT N'USP Rpt.uspAvailEBMByInfant create failed'
15970 PRINT 'Error Number: ' + str(error_number())
15971 PRINT 'Line Number: ' + str(error_line())
15972 PRINT error_message()
15973END CATCH
15974GO
15975
15976/* SysInfo.uspSetStatusExpired.storedProcedure.sql
15977To call the SP:
15978EXECUTE SysInfo.uspSetStatusExpired;
15979 */
15980
15981BEGIN TRY
15982EXECUTE ('/****** Object: StoredProcedure [SysInfo].[uspSetStatusExpired] Script Date: 6/28/2013 11:26:19 ******/
15983
15984-- =============================================
15985-- Author: Lisa Bohm (lbohm@paragondsi.com)
15986-- Create date: 2013-12-23
15987-- Description: Set Status of milk past expir date as expired
15988-- Modified:
15989-- =============================================
15990CREATE PROCEDURE [SysInfo].[uspSetStatusExpired]
15991 -- Add the parameters for the stored procedure here
15992AS
15993DECLARE @errCode tinyint
15994/* So - we need to set up a temp table of all vessels that fit this, because we''ll be using this 8 billion places */
15995DECLARE @expVessel TABLE (vesselID bigint, currVesselHistoryID bigint, expDate datetime)
15996
15997BEGIN
15998 -- SET NOCOUNT ON added to prevent extra result sets from
15999 -- interfering with SELECT statements.
16000 SET NOCOUNT ON;
16001
16002INSERT INTO @expVessel (vesselID, currVesselHistoryID, expDate)
16003
16004SELECT NtV.vesselID
16005 , NvH.vesselHistoryID
16006 , NvH.expirDate
16007
16008FROM Nutrition.tVessel NtV
16009 INNER JOIN Nutrition.tVesselHistory NVH ON NtV.vesselID = NVH.vesselID
16010 INNER JOIN Nutrition.tStatusList NSL ON NVH.statusID = NSL.statusID
16011
16012WHERE NVH.isCurrent = 1 -- current status
16013 AND NSL.statCatID = 2 -- status of available
16014 AND NVH.expirDate < getdate() -- IS expired
16015 ;
16016
16017BEGIN TRY
16018BEGIN TRANSACTION
16019 /* set current vessel status to "not current" */
16020 UPDATE NVH
16021 SET NVH.isCurrent = 0
16022 FROM Nutrition.tVesselHistory NVH
16023 INNER JOIN @expVessel EV ON NVH.vesselHistoryID = EV.currVesselHistoryID;
16024
16025 /* audit record */
16026 /* don''t use the SP to avoid looping here. Direct code. */
16027 INSERT INTO SysInfo.tDataChange
16028 (viewDate, userName, auditTypeID, tableName, recordID, previousEntry)
16029
16030 SELECT getdate()
16031 , ''System''
16032 , 10
16033 , ''Nutrition.tVesselHistory''
16034 , EV.currVesselHistoryID
16035 , ''isCurrent=1''
16036
16037 FROM @expVessel EV;
16038
16039/* Update to expired status */
16040 INSERT INTO Nutrition.tVesselHistory
16041 (vesselID
16042 , statusID
16043 , dateChange
16044 , userChange
16045 , expirDate
16046 , isCurrent)
16047 SELECT EV.vesselID
16048 , 6
16049 , getdate()
16050 , ''System''
16051 , EV.expDate
16052 , 1
16053 FROM @expVessel EV;
16054
16055 COMMIT TRANSACTION
16056 SET @errCode = 0;
16057 SELECT errCode = @errCode;
16058
16059
16060 END TRY
16061 BEGIN CATCH
16062 ROLLBACK TRANSACTION
16063 SET @errCode = 1;
16064 SELECT errCode = @errCode;
16065 END CATCH
16066END;')
16067
16068 PRINT N'USP SysInfo.uspSetStatusExpired created'
16069
16070END TRY
16071BEGIN CATCH
16072 PRINT N'USP SysInfo.uspSetStatusExpired create failed'
16073 PRINT 'Error Number: ' + str(error_number())
16074 PRINT 'Line Number: ' + str(error_line())
16075 PRINT error_message()
16076END CATCH
16077GO
16078
16079/* Rpt.uspAvailDM.storedProcedure.sql
16080To call the SP:
16081EXECUTE Rpt.uspAvailDM @username='admin';
16082 */
16083
16084BEGIN TRY
16085EXECUTE ('
16086/****** Object: StoredProcedure [Rpt].[uspAvailDM] Script Date: 11/1/2013 3:05:09 PM ******/
16087
16088-- =============================================
16089-- Author: Lisa Bohm (lbohm@paragondsi.com)
16090-- Create date: 2013-12-23
16091-- Description: Returns donor milk inventory (available and unassigned)
16092-- Modified:
16093-- =============================================
16094CREATE PROCEDURE [Rpt].[uspAvailDM]
16095 -- Add the parameters for the stored procedure here
16096 @username varchar(50)
16097 AS
16098 DECLARE @priPID tinyint
16099 BEGIN
16100 -- SET NOCOUNT ON added to prevent extra result sets from
16101 -- interfering with SELECT statements.
16102 SET NOCOUNT ON;
16103
16104/* find primary ID */
16105SET @priPID = PTinfo.udfGetPIDtype();
16106
16107SELECT NtV.vesselID
16108 , NtV.vesselCode
16109 , vesselCont = NtC.nCategoryAbbrev
16110 , NVH.expirDate
16111 , NSL.statusName
16112 , NtV.mls
16113 , NtV.gms
16114 , locationCode = StL.locationName
16115 , NDL.lotCode
16116 , NDB.bankName
16117 , recdDate = NRD.dateChange
16118 , isAssigned = CASE WHEN NVA.pSysID IS NULL THEN 0 ELSE 1 END
16119 , PtP.firstName
16120 , PtP.lastName
16121 , primaryID = PtI.IDvalue
16122
16123FROM Nutrition.tVessel NtV
16124 INNER JOIN Storage.tLocation StL ON NtV.locationID = StL.locationID
16125 INNER JOIN Nutrition.tCategory NtC ON NtV.nCategoryID = NtC.nCategoryID
16126 INNER JOIN Nutrition.tVesselHistory NVH ON NtV.vesselID = NVH.vesselID
16127 INNER JOIN Nutrition.tStatusList NSL ON NVH.statusID = NSL.statusID
16128 LEFT OUTER JOIN Nutrition.tVesselHistory NRD ON NtV.vesselID = NRD.vesselID
16129 AND NRD.statusID = 17
16130 INNER JOIN Nutrition.tDonorVessel NDV ON NtV.vesselID = NDV.vesselID
16131 INNER JOIN Nutrition.tDonorLot NDL ON NDV.lotID = NDL.lotID
16132 INNER JOIN Nutrition.tDonorBank NDB ON NDL.donorBankID = NDB.donorBankID
16133 LEFT OUTER JOIN (Nutrition.tVesselAssign NVA
16134 INNER JOIN PTinfo.tPatient PtP ON NVA.pSysID = PtP.pSysID
16135 INNER JOIN PTinfo.tIdentity PtI ON PtP.pSysID = PtI.pSysID
16136 AND PtI.identTypeID = @priPID
16137 ) on NtV.vesselID = NVA.vesselID
16138
16139WHERE NVH.isCurrent = 1 -- current status
16140 AND NSL.statCatID = 2 -- available
16141 AND NVH.expirDate > getdate() -- not expired
16142 ;
16143
16144EXECUTE Sysinfo.uspAddReportView @username=@username, @auditTypeID=8, @reportName=''Available Donor Milk'', @startDate=''19000101'', @endDate=''25000101'', @addtlParamValue=''N/A'';
16145
16146END;')
16147
16148 PRINT N'USP Rpt.uspAvailDM created'
16149
16150END TRY
16151BEGIN CATCH
16152 PRINT N'USP Rpt.uspAvailDM create failed'
16153 PRINT 'Error Number: ' + str(error_number())
16154 PRINT 'Line Number: ' + str(error_line())
16155 PRINT error_message()
16156END CATCH
16157GO
16158
16159/* Nutrition.uspListInMotherBin.storedProcedure.sql
16160To call the SP:
16161EXECUTE Nutrition.uspListInMotherBin @pSysID=5;
16162 */
16163
16164BEGIN TRY
16165EXECUTE ('/****** Object: StoredProcedure [Nutrition].[uspListInMotherBin] Script Date: 02/14/2014 11:26:19 ******/
16166
16167-- =============================================
16168-- Author: Lisa Bohm (lbohm@paragondsi.com)
16169-- Create date: 2014-02-13
16170-- Description: set available vessels lost for a mother.
16171-- Modified:
16172-- =============================================
16173CREATE PROCEDURE [Nutrition].[uspListInMotherBin]
16174 -- Add the parameters for the stored procedure here
16175@pSysID bigint
16176
16177AS
16178
16179 BEGIN
16180 -- SET NOCOUNT ON added to prevent extra result sets from
16181 -- interfering with SELECT statements.
16182 SET NOCOUNT ON;
16183
16184SELECT StL.locationName
16185 , StL.locationCode
16186 , NtV.vesselCode
16187
16188FROM Nutrition.tVessel NtV
16189 INNER JOIN Storage.tLocation StL ON NtV.locationID = StL.locationID
16190 INNER JOIN Storage.tLocationAssign SLA ON StL.locationID = SLA.locationID
16191
16192 WHERE SLA.pSysID = @pSysID;
16193
16194 END;')
16195
16196 PRINT N'USP Nutrition.uspListInMotherBin created'
16197
16198END TRY
16199BEGIN CATCH
16200 PRINT N'USP Nutrition.uspListInMotherBin create failed'
16201 PRINT 'Error Number: ' + str(error_number())
16202 PRINT 'Line Number: ' + str(error_line())
16203 PRINT error_message()
16204END CATCH
16205GO
16206
16207
16208
16209/* Nutrition.uspAFPformVessel.storedProcedure.sql
16210To call the SP:
16211EXECUTE Nutrition.uspAFPformVessel @vesselXML = '<tgtVessel id="116">
16212<formVessel formulaID="" lotID="0" mls="0"/>
16213<formVessel formulaID="" lotID="0" mls="0"/>
16214</tgtVessel>
16215';
16216 */
16217
16218BEGIN TRY
16219EXECUTE ('/****** Object: StoredProcedure [Nutrition].[uspAFPformVessel] Script Date: 3/14/2014 11:26:19 ******/
16220-- =============================================
16221-- Author: Lisa Bohm (lbohm@paragondsi.com)
16222-- Create date: 2014-03-14
16223-- Description: Create AFP vessels in a batch using XML
16224-- Modified:
16225--Testing Data
16226/*DECLARE @vesselXML as XML
16227
16228SET @vesselXML = ''<tgtVessel id="116">
16229<formVessel formulaID="" lotID="0" mls="0"/>
16230<formVessel formulaID="" lotID="0" mls="0"/>
16231</tgtVessel>
16232'' */
16233-- =============================================
16234CREATE PROCEDURE [Nutrition].[uspAFPformVessel]
16235 -- Add the parameters for the stored procedure here
16236@vesselXML XML
16237
16238 AS
16239 DECLARE @expDate datetime
16240 DECLARE @tgtVesselID bigint
16241 DECLARE @vesselInfo TABLE (rowNm tinyint, vesselID bigint, formulaID smallint, lotID int, mls decimal(7,2))
16242 DECLARE @fullVesselInfo TABLE (vesselID bigint, vesselCode varchar(25), lotID int, mls decimal(7,2))
16243
16244SET @expDate = ''2500-01-01'';
16245BEGIN
16246 -- SET NOCOUNT ON added to prevent extra result sets from
16247 -- interfering with SELECT statements.
16248 SET NOCOUNT ON;
16249
16250SET @tgtVesselID = (SELECT tvInfo.value(''@id'', ''bigint'')
16251 FROM @vesselXML.nodes(''./tgtVessel'') as data ( tvInfo ));
16252
16253INSERT INTO @vesselInfo (rowNm, vesselID, formulaId, lotID, mls)
16254 SELECT ROW_NUMBER() OVER (order by rand())
16255 , 0
16256 , vInfo.value(''@formulaID'', ''smallint'') as formulaID
16257 , vInfo.value(''@lotID'', ''int'') as lotID
16258 , vInfo.value(''@mls'', ''decimal(7,2)'') as mls
16259 FROM @vesselXML.nodes(''./tgtVessel/formVessel'') as data ( vInfo );
16260
16261/* is the formula lot ID zero? If so, get the "unspecified lot" lot */
16262UPDATE VI
16263SET lotID = NFL.formLotID
16264FROM @vesselInfo VI
16265 INNER JOIN Nutrition.tFormulaLot NFL ON VI.formulaID = NFL.formulaID
16266WHERE VI.lotID = 0
16267 AND NFL.formLotCode = ''Unspecified lot'';
16268
16269BEGIN TRY
16270BEGIN TRANSACTION
16271/* just go with starting out as the initial disposed location */
16272
16273INSERT INTO Nutrition.tVessel
16274 (vesselCode
16275 , nCategoryID
16276 , mls
16277 , gms
16278 , calsPer
16279 , locationID)
16280OUTPUT Inserted.vesselID, Inserted.vesselCode, 0, Inserted.mls INTO @fullVesselInfo
16281
16282SELECT VI.rowNm
16283 , 3
16284 , 0
16285 , 0
16286 , 0
16287 , 2
16288FROM @vesselInfo VI;
16289
16290UPDATE FVI
16291SET FVI.vesselCode = CASE WHEN NtV.vesselCode IS NULL THEN ''F'' + CAST(FVI.vesselID AS VARCHAR) ELSE ''F'' + ''0'' + CAST(FVI.vesselID AS VARCHAR) END
16292 , FVI.lotID = VI.lotID
16293 , FVI.mls = VI.mls
16294FROM @fullVesselInfo FVI
16295 INNER JOIN @vesselInfo VI ON FVI.vesselCode = VI.rowNm
16296LEFT OUTER JOIN Nutrition.tVessel NtV ON NtV.vesselCode = ''F'' + CAST(FVI.vesselID AS VARCHAR);
16297
16298/* okay, then add the updated vessel code */
16299 UPDATE NtV
16300 SET NtV.vesselCode = FVI.vesselCode
16301 FROM Nutrition.tVessel NtV
16302 INNER JOIN @fullVesselInfo FVI ON NtV.vesselID = FVI.vesselID;
16303
16304 /* insert status "issued" */
16305 INSERT INTO Nutrition.tVesselHistory
16306 (vesselID, statusID, dateChange, userChange, expirDate, isCurrent)
16307 SELECT
16308 FVI.vesselID
16309 , 1
16310 , getdate()
16311 , ''System''
16312 , ''2500-01-01''
16313 , 0
16314 FROM @fullVesselInfo FVI;
16315
16316 /* la la la audit records- do directly to avoid looooooooooping */
16317 INSERT INTO SysInfo.tDataChange
16318 (viewDate, userName, auditTypeID, tableName, recordID, previousEntry)
16319
16320 SELECT getdate()
16321 , ''System''
16322 , 9
16323 , ''Nutrition.tVessel''
16324 , FVI.vesselID
16325 , ''N/A''
16326
16327 FROM @fullVesselInfo FVI;
16328
16329 /* okay, insert */
16330 INSERT INTO Nutrition.tFormulaVessel
16331 (vesselID
16332 , formLotID)
16333 SELECT FVI.vesselID
16334 , FVI.lotID
16335 FROM @fullVesselInfo FVI;
16336;
16337 /* uh huh. Now, audit tracking. No SP to avoid loooooping. */
16338 INSERT INTO SysInfo.tDataChange
16339 (viewDate, userName, auditTypeID, tableName, recordID, previousEntry)
16340
16341SELECT getdate()
16342 , ''System''
16343 , 9
16344 , ''Nutrition.tFormulaVessel''
16345 , NFV.vesselFormulaID
16346 , ''N/A''
16347
16348 FROM @fullVesselInfo FVI
16349 INNER JOIN Nutrition.tFormulaVessel NFV ON FVI.vesselID = NFV.vesselID
16350 WHERE NFV.formLotID = FVI.lotID;
16351
16352/* received status event 17 */
16353INSERT INTO Nutrition.tVesselHistory
16354 (vesselID
16355 , statusID
16356 , dateChange
16357 , userChange
16358 , expirDate
16359 , isCurrent)
16360SELECT FVI.vesselID
16361 , 17
16362 , getdate()
16363 , ''System''
16364 , @expDate
16365 , 0
16366 FROM @fullVesselInfo FVI;
16367
16368/* fortified status 5 */
16369INSERT INTO Nutrition.tVesselHistory
16370 (vesselID
16371 , statusID
16372 , dateChange
16373 , userChange
16374 , expirDate
16375 , isCurrent)
16376SELECT
16377 FVI.vesselID
16378 , 5
16379 , getdate()
16380 , ''System''
16381 , @expDate
16382 , 0
16383 FROM @fullVesselInfo FVI;
16384
16385/* container change event */
16386INSERT INTO Nutrition.tVesselHistory
16387 (vesselID
16388 , statusID
16389 , dateChange
16390 , userChange
16391 , expirDate
16392 , isCurrent)
16393SELECT
16394 FVI.vesselID
16395 , 12
16396 , getdate()
16397 , ''System''
16398 , @expDate
16399 , 0
16400 FROM @fullVesselInfo FVI;
16401
16402
16403/* "pour" into target vessel */
16404INSERT INTO Nutrition.tVesselOrigin (vesselID, targetVesselID, mls)
16405 SELECT FVI.vesselID
16406 , @tgtVesselID
16407 , FVI.mls
16408
16409 FROM @fullVesselInfo FVI;
16410
16411/* update mls in target vessel */
16412UPDATE NtV
16413 SET NtV.mls = NtV.mls + (SELECT SUM(FVI.mls)
16414 FROM @fullVesselInfo FVI)
16415FROM Nutrition.tVessel NtV;
16416
16417/* add empty status */
16418INSERT INTO Nutrition.tVesselHistory
16419 (vesselID
16420 , statusID
16421 , dateChange
16422 , userChange
16423 , expirDate
16424 , isCurrent)
16425SELECT
16426 FVI.vesselID
16427 , 16
16428 , getdate()
16429 , ''System''
16430 , @expDate
16431 , 1
16432 FROM @fullVesselInfo FVI;
16433
16434
16435 COMMIT TRANSACTION
16436END TRY
16437BEGIN CATCH
16438ROLLBACK TRANSACTION
16439
16440END CATCH
16441
16442END;')
16443 DECLARE @errMsg varchar(1000);
16444 SET @errMsg = 'USP Nutrition.uspAFPformVessel created'
16445 SELECT errMsg = @errMsg;
16446
16447END TRY
16448BEGIN CATCH
16449 SET @errMsg = 'USP Nutrition.uspAFPformVessel create failed. Error Number: ' + str(error_number()) + 'Line Number: ' + str(error_line())
16450 SELECT errMsg = @errMsg;
16451END CATCH
16452GO
16453
16454/* verified */
16455/* drop stuff */
16456
16457IF object_id('dbo.tblAdditives') IS NOT NULL
16458BEGIN
16459 DROP TABLE dbo.tblAdditives;
16460END
16461
16462IF object_id('dbo.tblAdditivePairing') IS NOT NULL
16463BEGIN
16464 DROP TABLE dbo.tblAdditivePairing;
16465END
16466
16467IF object_id('dbo.tblAdditivesIssued') IS NOT NULL
16468BEGIN
16469 DROP TABLE dbo.tblAdditivesIssued;
16470END
16471
16472IF object_id('dbo.tblBottles') IS NOT NULL
16473BEGIN
16474 DROP TABLE dbo.tblBottles;
16475END
16476
16477IF object_id('dbo.tblDisposalTypes') IS NOT NULL
16478BEGIN
16479 DROP TABLE dbo.tblDisposalTypes;
16480END
16481
16482IF object_id('dbo.tblDonorBanks') IS NOT NULL
16483BEGIN
16484 DROP TABLE dbo.tblDonorBanks;
16485END
16486
16487IF object_id('dbo.tblFailedLogins') IS NOT NULL
16488BEGIN
16489 DROP TABLE dbo.tblFailedLogins;
16490END
16491
16492IF object_id('dbo.tblFeedings') IS NOT NULL
16493BEGIN
16494 DROP TABLE dbo.tblFeedings;
16495END
16496
16497IF object_id('dbo.tblFillHistory') IS NOT NULL
16498BEGIN
16499 DROP TABLE dbo.tblFillHistory;
16500END
16501
16502IF object_id('dbo.tblFormula') IS NOT NULL
16503BEGIN
16504 DROP TABLE dbo.tblFormula;
16505END
16506
16507IF object_id('dbo.tblHL7') IS NOT NULL
16508BEGIN
16509 DROP TABLE dbo.tblHL7;
16510END
16511
16512IF object_id('dbo.tblLocations') IS NOT NULL
16513BEGIN
16514 DROP TABLE dbo.tblLocations;
16515END
16516
16517IF object_id('dbo.tblMixedBottles') IS NOT NULL
16518BEGIN
16519 DROP TABLE dbo.tblMixedBottles;
16520END
16521
16522IF object_id('dbo.tblNextNumber') IS NOT NULL
16523BEGIN
16524 DROP TABLE dbo.tblNextNumber;
16525END
16526
16527IF object_id('dbo.tblPatients') IS NOT NULL
16528BEGIN
16529 DROP TABLE dbo.tblPatients;
16530END
16531
16532IF object_id('dbo.tblMothers') IS NOT NULL
16533BEGIN
16534 DROP TABLE dbo.tblMothers;
16535END
16536
16537IF object_id('dbo.tblRejectedFeedings') IS NOT NULL
16538BEGIN
16539 DROP TABLE dbo.tblRejectedFeedings;
16540END
16541
16542IF object_id('SysInfo.tFormerSet') IS NOT NULL
16543BEGIN
16544 DROP TABLE SysInfo.tFormerSet;
16545END
16546
16547IF object_id('dbo.tblSystemSettings') IS NOT NULL
16548BEGIN
16549 DROP TABLE dbo.tblSystemSettings;
16550END
16551
16552/* delete old SPs */
16553
16554IF object_id('dbo.FeedingSchedule') IS NOT NULL
16555BEGIN
16556 DROP PROCEDURE dbo.FeedingSchedule
16557END
16558
16559IF object_id('dbo.sp_ExpiredMilk') IS NOT NULL
16560BEGIN
16561 DROP PROCEDURE dbo.sp_ExpiredMilk
16562END
16563
16564IF object_id('dbo.sp_FreshMilk') IS NOT NULL
16565BEGIN
16566 DROP PROCEDURE dbo.sp_FreshMilk
16567END
16568
16569IF object_id('dbo.sp_MilkByMother') IS NOT NULL
16570BEGIN
16571 DROP PROCEDURE sp_MilkByMother
16572END
16573
16574IF object_id('dbo.sp_MilkbyPatient') IS NOT NULL
16575BEGIN
16576 DROP PROCEDURE dbo.sp_MilkbyPatient
16577END
16578
16579IF object_id('dbo.sp_MilkbyStatus') IS NOT NULL
16580BEGIN
16581 DROP PROCEDURE dbo.sp_MilkbyStatus
16582END
16583
16584IF object_id('dbo.sp_SetSystemSettings') IS NOT NULL
16585BEGIN
16586 DROP PROCEDURE dbo.sp_SetSystemSettings
16587END
16588
16589IF object_id('dbo.uspAuditBottle') IS NOT NULL
16590BEGIN
16591 DROP PROCEDURE dbo.uspAuditBottle
16592END
16593
16594IF object_id('dbo.uspBottleByNumber') IS NOT NULL
16595BEGIN
16596 DROP PROCEDURE dbo.uspBottleByNumber
16597END
16598
16599IF object_id('dbo.uspFeederList') IS NOT NULL
16600BEGIN
16601 DROP PROCEDURE dbo.uspFeederList
16602END
16603
16604IF object_id('dbo.uspMotherByMRN') IS NOT NULL
16605BEGIN
16606 DROP PROCEDURE dbo.uspMotherByMRN
16607END
16608
16609IF object_id('dbo.uspNoFeedNotify') IS NOT NULL
16610BEGIN
16611 DROP PROCEDURE dbo.uspNoFeedNotify
16612END
16613
16614IF object_id('dbo.uspPatientByMRN') IS NOT NULL
16615BEGIN
16616 DROP PROCEDURE dbo.uspPatientByMRN
16617END
16618
16619IF object_id('dbo.uspSyringeByNumber') IS NOT NULL
16620BEGIN
16621 DROP PROCEDURE dbo.uspSyringeByNumber
16622END
16623
16624/* complete */