Files
DP44/DataPRO_sql/dbo.sp_DBImportLabDetails.StoredProcedure.sql
2026-04-17 14:55:32 -04:00

132 lines
6.7 KiB
Transact-SQL
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sp_DBImportLabDetails]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sp_DBImportLabDetails]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sp_DBImportLabDetails]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [dbo].[sp_DBImportLabDetails] AS'
END
GO
ALTER PROCEDURE [dbo].[sp_DBImportLabDetails]
@LabDetails xml
,@errorNumber int output
,@errorMessage nvarchar(250) output
AS
BEGIN
set @errorNumber = 0
set @errorMessage = ''
declare @tLabDetails table (
[Name] [nvarchar](255) NULL,
[LabName] [nvarchar](255) NULL,
[LabContactName] [nvarchar](255) NULL,
[LabContactPhone] [nvarchar](255) NULL,
[LabContactFax] [nvarchar](255) NULL,
[LabContactEmail] [nvarchar](255) NULL,
[LabTestRefNumber] [nvarchar](255) NULL,
[LabProjectRefNumber] [nvarchar](255) NULL,
[LastModified] [datetime] NULL,
[LastModifiedBy] [nvarchar](50) NULL,
[LocalOnly] [bit] NULL,
[Version] [int] NULL)
begin try
begin transaction tLabDetails
insert into @tLabDetails
([Name]
,[LabName]
,[LabContactName]
,[LabContactPhone]
,[LabContactFax]
,[LabContactEmail]
,[LabTestRefNumber]
,[LabProjectRefNumber]
,[LastModified]
,[LastModifiedBy]
,[LocalOnly]
,[Version])
select
t.x.value('@Name', 'varchar(255)')
,t.x.value('@LabName', 'varchar(255)')
,t.x.value('@LabContactName', 'varchar(255)')
,t.x.value('@LabContactPhone', 'varchar(255)')
,t.x.value('@LabContactFax', 'varchar(255)')
,t.x.value('@LabContactEmail', 'varchar(255)')
,t.x.value('@LabTestRefNumber', 'varchar(255)')
,t.x.value('@LabProjectRefNumber', 'varchar(255)')
,t.x.value('@LastModified', 'datetime')
,t.x.value('@LastModifiedBy', 'varchar(50)')
,t.x.value('@LocalOnly', 'bit')
,t.x.value('@Version', 'int')
from @LabDetails.nodes('/LabDetails/LabDetail') t(x)
OPTION (OPTIMIZE FOR ( @LabDetails = NULL ))
insert into [dbo].[LabratoryDetails]
([Name]
,[LabratoryName]
,[LabratoryContactName]
,[LabratoryContactPhone]
,[LabratoryContactFax]
,[LabratoryContactEmail]
,[LabratoryTestRefNumber]
,[LabratoryProjectRefNumber]
,[LastModified]
,[LastModifiedBy]
,[LocalOnly]
,[Version])
select
[Name]
,[LabName]
,[LabContactName]
,[LabContactPhone]
,[LabContactFax]
,[LabContactEmail]
,[LabTestRefNumber]
,[LabProjectRefNumber]
,[LastModified]
,[LastModifiedBy]
,[LocalOnly]
,[Version]
from @tLabDetails t where isnull(t.[Name], space(0)) != space(0)
commit transaction tLabDetails
end try
begin catch
set @errorNumber = error_number()
set @errorMessage = error_message()
rollback transaction tLabDetails
end catch;
END
GO