Sie sind auf Seite 1von 6

-----------------------------------------------------------------------------

--SQ09883
-- Add start/finish times to field density test report and Concrete Test Report
-- This script adds some properties to the relative compaction test to allow
-- the field density test report to report start and finish time custom fields
-- from the work order in labs where these are mapped. Similarly, it adds
-- those fields to the Concrete Test Report's Sample Details.
-- After running this script, use QLA to set the Sample Details results fields
-- for the Concrete Test Report to "Default".
-- Database: QESTLab
-- Created By : Krzysztof Kot
-- Created Date : 27 Jan 2017
-- Modified By : Michael Howland
-- Modified Date : 9 Mar 2017
-- Version: 1.2
-- Change Log
--- 1.0 Original Version
--- 1.2 Merge FD and Concrete Scripts
--- 1.4 Add MDE Concrete
-- Repeatability: Safe
-- Re-Run Requirement: After any database upgrade
-----------------------------------------------------------------------------
set nocount on;
--First, helper proc for inserting properties at a given spot assuming that some
properties already exist
if exists(select * from sys.procedures where name = 'qest_temp_AddUpdateObjectPr
opertyAt')
DROP PROCEDURE [dbo].[qest_temp_AddUpdateObjectPropertyAt]
GO
CREATE procedure [dbo].[qest_temp_AddUpdateObjectPropertyAt]
@QestID int,
@PropertyPrefix nvarchar(32),
@Value nvarchar(4000),
@InsertAtPosition int
as
begin
declare @pos int
declare @newProperty nvarchar(32)
declare @lastIdx int
--Check that we're not being passed rubbish
if len(@PropertyPrefix) = 0 or @PropertyPrefix is null
begin
raiserror('Need a non-null, non-empty property prefix, aborting'
,16,1) with nowait;
return;
end
if len(@Value) = 0 or @Value is null
begin
raiserror('Need a non-null, non-empty value, aborting',16,1) wit
h nowait;
return;
end
if @QestID = 0 or @QestID is null
begin
raiserror('Need a non-null, non-zero QESTID, aborting',16,1) wit
h nowait;
return;
end
if @InsertAtPosition = 0 or @InsertAtPosition is null
begin
raiserror('Need a non-null, non-zero position, aborting',16,1) w
ith nowait;
return;
end
--Start actual work
--Find how many of these properties there already exist, if any of the t
ext following our prefix is not numeric,
-- we'll get an exception here too, before attempting to update anything
select @lastIdx = max(convert(int, right(property, len(property)-len(@pr
opertyPrefix))))
from qestObjects
where qestid = @qestid and property like @propertyPrefix+'%'
if @lastIdx is null or @lastIdx = 0
begin
raiserror('Cannot find any properties with prefix %s for QESTID
%d, aborting',16,1,@PropertyPrefix,@QestID) with nowait;
return
end
--if our desired position is at or after the end, set it to the end and
just add it
set @pos = @InsertAtPosition
if (@InsertAtPosition > @lastIdx + 1)
begin
raiserror('Provided position, %d, is larger than expected. Last
property with prefix %s is at %d, adding to the end', 10,1, @InsertAtPosition, @
PropertyPrefix, @lastIdx) with nowait;
set @pos = @lastIdx + 1
end
--if our desired position is somewhere in the middle, move everything el
se down by one
if (@InsertAtPosition < @lastIdx + 1)
begin
update qestobjects
set Property = @PropertyPrefix + convert(nvarchar(4), convert(in
t, right(property,len(property)-len(@PropertyPrefix)))+1)
where qestid = @QestID
and Property like @PropertyPrefix + '%'
and convert(int, right(property,len(property)-len(@PropertyPrefi
x))) >= @pos
end
set @newProperty = @PropertyPrefix + convert(nvarchar(4),@pos)
--Add our property at the desired position
exec qest_AddUpdateObjectProperty @QestID, @newProperty, @Value
end
go
-- Field Density Test Report
--Now put in the options we want
declare @qestid int
declare @propertyPrefix nvarchar(100)
declare @fieldsToAdd table (ID int identity, field nvarchar(100), prompt nvarcha
r(100), position int)
declare @i int
declare @fieldToAdd nvarchar(100)
declare @fieldPrompt nvarchar(100)
declare @pos int
declare @newValue nvarchar(4000)
declare @newProperty nvarchar(32)
declare @nextIdx int
set @qestid = 110244
set @propertyPrefix = 'WOResultsFieldsLong'
--First fix up broken field methods property
begin transaction
select @newProperty = Property, @newValue = value +'|'
from qestobjects where qestid = @qestid and Property like @propertyPrefi
x + '%' and value like '%FieldName=FieldMethods%' and right(value,1) <> '|'
exec qest_AddUpdateObjectProperty @QestID = @qestid, @property = @newPro
perty, @Value = @newValue
commit
insert into @fieldsToAdd
select 'StartTime', 'Start Time', 3
union all
select 'FinishTime', 'Finish Time', 4
--Set up a poor man's cursor
set @i = 1
begin transaction
while exists (select * from @fieldsToAdd where ID = @i)
begin
select @fieldToAdd = field, @fieldPrompt = prompt, @pos = positi
on from @fieldsToAdd where ID = @i
--check if field exists in properties already
if not exists(select * from qestobjects where qestid = @qestid a
nd property like @propertyPrefix+'%' and value like '%FieldName=[_]'+@fieldToAdd
+'%')
begin
set @newProperty = @propertyPrefix + convert(nvarchar(4)
, @nextIdx)
set @newValue = 'IsCommon=T;FieldName=_'+@fieldToAdd+';P
rompt='+@fieldPrompt+';ValueFormat=Short Time;|'
--select @newProperty, @newValue
exec qest_temp_AddUpdateObjectPropertyAt @QestID = @qest
id, @propertyPrefix = @propertyPrefix, @Value = @newValue, @InsertAtPosition = @
pos
end
set @i = @i + 1
end
select * from qestobjects where Property like @propertyPrefix+'%' and qe
stid = @qestid
commit
go

-- Concrete Test Report


declare @qestid int
declare @propertyPrefix nvarchar(100)
declare @fieldsToAdd table (ID int identity, field nvarchar(100), prompt nvarcha
r(100), position int)
declare @i int
declare @fieldToAdd nvarchar(100)
declare @fieldPrompt nvarchar(100)
declare @pos int
declare @newValue nvarchar(4000)
declare @newProperty nvarchar(32)
declare @nextIdx int
set @qestid = 18947
set @propertyPrefix = 'SampleDetailsLong'
insert into @fieldsToAdd
select 'StartTime', 'Start Time', 6
union all
select 'FinishTime', 'Finish Time', 7
--Set up a poor man's cursor
set @i = 1
begin transaction
while exists (select * from @fieldsToAdd where ID = @i)
begin
select @fieldToAdd = field, @fieldPrompt = prompt, @pos = positi
on from @fieldsToAdd where ID = @i
--check if field exists in properties already
if not exists(select * from qestobjects where qestid = @qestid a
nd property like @propertyPrefix+'%' and value like '%FieldName=[_]'+@fieldToAdd
+'%')
begin
set @newProperty = @propertyPrefix + convert(nvarchar(4)
, @nextIdx)
-- Value does not include ;IsLong=T;, so both values sho
uld appear on one line
set @newValue = 'FieldName=_'+@fieldToAdd+';Prompt='+@fi
eldPrompt+';PrintIfNonNull=T;Path=..;ValueFormat=Short Time;|'
--select @newProperty, @newValue
exec qest_temp_AddUpdateObjectPropertyAt @QestID = @qest
id, @propertyPrefix = @propertyPrefix, @Value = @newValue, @InsertAtPosition = @
pos
end
set @i = @i + 1
end
select * from qestobjects where Property like @propertyPrefix+'%' and qe
stid = @qestid order by convert(int, substring(Property, len(@propertyPrefix) +
1, 4)) asc
commit
go

-- MDE Concrete Test Report


declare @qestid int
declare @propertyPrefix nvarchar(100)
declare @fieldsToAdd table (ID int identity, field nvarchar(100), prompt nvarcha
r(100), position int)
declare @i int
declare @fieldToAdd nvarchar(100)
declare @fieldPrompt nvarchar(100)
declare @pos int
declare @newValue nvarchar(4000)
declare @newProperty nvarchar(32)
declare @nextIdx int
set @qestid = 18947
set @propertyPrefix = 'MDESampleDetailsLong'
insert into @fieldsToAdd
select 'StartTime', 'Start Time', 6
union all
select 'FinishTime', 'Finish Time', 7
--Set up a poor man's cursor
set @i = 1
begin transaction
while exists (select * from @fieldsToAdd where ID = @i)
begin
select @fieldToAdd = field, @fieldPrompt = prompt, @pos = positi
on from @fieldsToAdd where ID = @i
--check if field exists in properties already
if not exists(select * from qestobjects where qestid = @qestid a
nd property like @propertyPrefix+'%' and value like '%FieldName=[_]'+@fieldToAdd
+'%')
begin
set @newProperty = @propertyPrefix + convert(nvarchar(4)
, @nextIdx)
-- Value does not include ;IsLong=T;, so both values sho
uld appear on one line
set @newValue = 'FieldName=_'+@fieldToAdd+';Prompt='+@fi
eldPrompt+';PrintIfNonNull=T;Path=..;ValueFormat=Short Time;|'
--select @newProperty, @newValue
exec qest_temp_AddUpdateObjectPropertyAt @QestID = @qest
id, @propertyPrefix = @propertyPrefix, @Value = @newValue, @InsertAtPosition = @
pos
end
set @i = @i + 1
end
select * from qestobjects where Property like @propertyPrefix+'%' and qe
stid = @qestid order by convert(int, substring(Property, len(@propertyPrefix) +
1, 4)) asc
commit
go
--Cleanup
if exists(select * from sys.procedures where name = 'qest_temp_AddUpdateObjectPr
opertyAt')
DROP PROCEDURE [dbo].[qest_temp_AddUpdateObjectPropertyAt]
GO

Das könnte Ihnen auch gefallen