Insert into case statment

can we use insert and update statement in case expression.
like
case emp_id
when emp_id exist then
update....
else
insert....
end;

i like to use that in select query.i know we can use merge query but i am using that in sql server.and merge query is not there in sqlserver.so what is the best alternative for
if exist then update
else insert

Similar Messages

  • Commit for every 1000 records in  Insert into select statment

    Hi I've the following INSERT into SELECT statement .
    The SELECT statement (which has joins ) has around 6 crores fo data . I need to insert that data into another table.
    Please suggest me the best way to do that .
    I'm using the INSERT into SELECT statement , but i want to use commit statement for every 1000 records .
    How can i achieve this ..
    insert into emp_dept_master
    select e.ename ,d.dname ,e.empno ,e.empno ,e.sal
       from emp e , dept d
      where e.deptno = d.deptno       ------ how to use commit for every 1000 records .Thanks

    Smile wrote:
    Hi I've the following INSERT into SELECT statement .
    The SELECT statement (which has joins ) has around 6 crores fo data . I need to insert that data into another table.Does the another table already have records or its empty?
    If its empty then you can drop it and create it as
    create your_another_table
    as
    <your select statement that return 60000000 records>
    Please suggest me the best way to do that .
    I'm using the INSERT into SELECT statement , but i want to use commit statement for every 1000 records .That is not the best way. Frequent commit may lead to ORA-1555 error
    [url http://asktom.oracle.com/pls/apex/f?p=100:11:0::::P11_QUESTION_ID:275215756923]A nice artical from ASKTOM on this one
    How can i achieve this ..
    insert into emp_dept_master
    select e.ename ,d.dname ,e.empno ,e.empno ,e.sal
    from emp e , dept d
    where e.deptno = d.deptno       ------ how to use commit for every 1000 records .
    It depends on the reason behind you wanting to split your transaction into small chunks. Most of the time there is no good reason for that.
    If you are tying to imporve performance by doing so then you are wrong it will only degrade the performance.
    To improve the performance you can use APPEND hint in insert, you can try PARALLEL DML and If you are in 11g and above you can use [url http://docs.oracle.com/cd/E11882_01/appdev.112/e25788/d_parallel_ex.htm#CHDIJACH]DBMS_PARALLEL_EXECUTE to break your insert into chunks and run it in parallel.
    So if you can tell the actual objective we could offer some help.

  • INSERT INTO with SELECT CASE

    Hi all,
    I've this problem: insert into Table_A some values from Table_B... with one derivated/calculated filed.
    Example
    INSERT INTO Table_A (id_part, vendtype, origin, proven, desc)
    SELECT id_p, vendt, origin, proven, <calculate/derivated_field_desc>
    FROM Table_BThe rules for calculate the field desc inserted on Table_A are 1. IF vendt='V' AND origin='N' AND proven!='04' THEN ---> desc='TRANSFER'
    2. IF vendt='M' AND origin='N' AND proven!='04' THEN ---> desc='IMPORTED'
    3. IF proven='04' THEN ---> desc='BAD'So what I use in <calculate/derivated_field_desc> on my INSERT INTO statement??
    Can I use a SELECT CASE WHEN... or only DECODE is a right solution?
    Edited by: trebbia on 15-nov-2010 12.51

    Ok,
    I prefer use select case when... because trasform my rules in DECODE statement is more difficult
    So I write: INSERT INTO Table_A (id_part, vendtype, origin, proven, desc)
    SELECT id_p, vendt, origin, proven, SELECT CASE WHEN vendt='V' AND origin='N' AND proven!='04' THEN 'TRANSFER'
                                                    WHEN vendt='M' AND origin='N' AND proven!='04' THEN 'IMPORTED'
                                                    WHEN proven='04 THEN 'BAD'
                                                    END
    FROM Table_BObtain this error:
    40/56 PL/SQL: ORA-00936: missing expression
    33/1 PL/SQL: SQL Statement ignored
    PS: I use '<' '>' and not '!=' in my code... here not prompted correctly '<''>'
    Edited by: trebbia on 15-nov-2010 13.04
    Edited by: trebbia on 15-nov-2010 13.06

  • Use of insert in case

    Hi,
    Is it possible to use insert statement in CASE.
    For example as follows:
    let create a table as
    create table DECOMPENSATED_BALANCE_TMP (ENDING_ENTERED_BALANCE_DR number, ENDING_ENTERED_BALANCE_CR number);
    select
    CASE
    WHEN (PERIOD_NET_DR+BEGIN_BALANCE_DR-PERIOD_NET_CR-BEGIN_BALANCE_CR) total > 0
    THEN
    INSERT INTO XXGL_DECOMPENSATED_BALANCE_TMP (ENDING_ENTERED_BALANCE_DR) values (total)
    WHEN (PERIOD_NET_DR+BEGIN_BALANCE_DR-PERIOD_NET_CR-BEGIN_BALANCE_CR) <= 0
    THEN
    INSERT INTO XXGL_DECOMPENSATED_BALANCE_TMP (ENDING_ENTERED_BALANCE_CR) values (total)
    else
    NULL
    end
    from gl_balances
    where code_combination_id=109940 and currency_code='GBP';
    Any answers will be highly appreciated ....
    Thanks,
    zaheer

    I don't think that a conditional insert is really needed in this case. THe only part where the insert differse is the else part of your case statment where no insert should be done. THis can be made into a where clause.
    An easy way would be to use a simple INSERT + SELECT statement. Where the value for the dr and cr columns are created by a case or a decode statement.
    untested example
    insert into XXGL_DECOMPENSATED_BALANCE_TMP
      (ENDING_ENTERED_BALANCE_DR, ENDING_ENTERED_BALANCE_CR)
    select case WHEN (PERIOD_NET_DR+BEGIN_BALANCE_DR-PERIOD_NET_CR-BEGIN_BALANCE_CR) > 0  then total
              else null
              end as DR
             ,case WHEN (PERIOD_NET_DR+BEGIN_BALANCE_DR-PERIOD_NET_CR-BEGIN_BALANCE_CR) <= 0  then total
              else null
              end as CR
    from gl_balances
    where code_combination_id=109940
    and currency_code='GBP'
    and (PERIOD_NET_DR+BEGIN_BALANCE_DR-PERIOD_NET_CR-BEGIN_BALANCE_CR) is not null -- else part
    ;The good thing is you could run this insert statement for several code_combination_ids directly in SQL without any pL-loop.
    Just noticed that leonard aready posted almost the same solution.
    Edited by: Sven W. on Aug 24, 2011 3:22 PM

  • Missing Expression Error When Case Statment is used as field

    I have tried to write an expression that will calculate how many days ago a request was submitted and return a user defined value if the calculation falls within the criteria in the case statment. The SQL View for the Query I am using is shown below:
    SELECT A.F_POSN_REQUEST_ID, A.F_REQ_ECP_STATUS, TO_CHAR(A.REQUEST_DTTM,'YYYY-MM-DD-HH24.MI.SS."000000"'), TO_CHAR(A.F_REQ_ST_DT,'YYYY-MM-DD-HH24.MI.SS."000000"'), TO_CHAR(A.F_REQ_ECP_ST_DT,'YYYY-MM-DD-HH24.MI.SS."000000"'), TO_CHAR(A.F_REQ_ECP_PROC_DT,'YYYY-MM-DD-HH24.MI.SS."000000"'), CASE WHEN(sysdate - TO_DATE(SUBSTR( TO_CHAR(A.F_REQ_ST_DT,'YYYY-MM-DD-HH24.MI.SS."000000"'),1,16),'YYYY-MM-DD-HH24.MI'))>60 THEN '>60' ELSE(CASE WHEN(sysdate - TO_DATE(SUBSTR( TO_CHAR(A.F_REQ_ST_DT,'YYYY-MM-DD-HH24.MI.SS."000000"'),1,16),'YYYY-MM-DD-HH24.MI')) >30 AND <=60 THEN '30 - 60' ELSE(CASE WHEN(sysdate - TO_DATE(SUBSTR( TO_CHAR(A.F_REQ_ST_DT,'YYYY-MM-DD-HH24.MI.SS."000000"'),1,16),'YYYY-MM-DD-HH24.MI'))>=15 AND <=30 THEN '15 - 30' ELSE(CASE WHEN(sysdate - TO_DATE(SUBSTR( TO_CHAR(A.F_REQ_ST_DT,'YYYY-MM-DD-HH24.MI.SS."000000"'),1,16),'YYYY-MM-DD-HH24.MI'))<15 THEN 'Less Than 15' ELSE NULL END)END)END)END))
      FROM PS_F_POSN_REQUEST A
      WHERE A.F_REQ_ECP_STATUS IN ('I','O','P')
      GROUP BY  A.F_POSN_REQUEST_ID,  A.F_REQ_ECP_STATUS,  TO_CHAR(A.REQUEST_DTTM,'YYYY-MM-DD-HH24.MI.SS."000000"'),  TO_CHAR(A.F_REQ_ST_DT,'YYYY-MM-DD-HH24.MI.SS."000000"'),  TO_CHAR(A.F_REQ_ECP_ST_DT,'YYYY-MM-DD-HH24.MI.SS."000000"'),  TO_CHAR(A.F_REQ_ECP_PROC_DT,'YYYY-MM-DD-HH24.MI.SS."000000"')When I try to run the query I get Message=ORA-00936: missing expression (50,380)
    Any suggestions you could provide to help me clear this error would be greatly appreciated.
    Edited by: 992737 on Mar 8, 2013 9:07 AM

    Hi,
    Welcome to the forum!
    I think you want something like this:
    CASE
        WHEN  a.f_req_st_dt < TRUNC (SYSDATE) - 60  THEN  '>60'
        WHEN  a.f_req_st_dt < TRUNC (SYSDATE) - 30  THEN  '30.1-60'
        WHEN  a.f_req_st_dt < TRUNC (SYSDATE) - 15  THEN  '15.1-30'
        WHEN  a.f_req_st_dt < TRUNC (SYSDATE)       THEN  '15 or less'
    ENDNested CASE expressions aren't needed very much. Each WHEN clause is evaluated only if all the ealier ones have failed. For example, when testing to see if a row is in the 301.-60 range above (that is, the 2nd WHEN clause), there's no need to see if the difference is greater than or equal to 60; it wouldn't be evaluating that clause if the 1st WHEN condition was TRUE.
    This assumes that a.f_req_st_dt is a TIMESTAMP or DATE; either datatype can be compared with DATEs, such as SYSDATE-60, so there's no need to conevert them to strings, and then convert them back into DATEs.
    I hope this answers your question.
    If not, post a little sample data (CREATE TABLE and INSERT statements, relevant columns only) for all tables involved, and also post the results you want from that data.
    Simplify the problem as much as possible. Show only the parts that you don't already know how to do.
    Explain, using specific examples, how you get those results from that data.
    Always say which version of Oracle you're using (e.g., 11.2.0.2.0).
    See the forum FAQ {message:id=9360002}
    Never write, let alone post, unformatted code. Indent the code to show the exent and structure of clauses (SELECT, FROM, etc.), and complex expressions (such as CASE). The forum FAQ explains how to use \ tags when posting formatted text on this site.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

  • TSQL - Dynamic Query Case statment update issue - to update table by left shifting the rows

    Hi all
    I need an help on my tsql query in 2000 to fix the case statement.
    I have a table like shown below.
    On this table where ever I see PortID as 101 and 105 I need to remove it and shift the rows from right to left.
    ie on First row PortID1 is 101 so I need to remove it and replace it with PortId2 and similarly PortID2 replaced my PortID3 and so on.
    ie like on excel if you delete cell we need to shift the rows to left .
    I wrote an dynamic update statement i,m not sure on the assigning dynamic case statment.
    CREATE TABLE [TravelRank]
    [Destination] [varchar] (50) NULL,
    [PortID1] [int] NULL ,
    [Distance1] [int] NULL ,
    [Rating1] [int] NULL ,
    [PortID2] [int] NULL ,
    [Distance2] [int] NULL ,
    [Rating2] [int] NULL ,
    [PortID3] [int] NULL ,
    [Distance3] [int] NULL ,
    [Rating3] [int] NULL ,
    [PortID4] [int] NULL ,
    [Distance4] [int] NULL ,
    [Rating4] [int] NULL
    INSERT into [TravelRank]
    select 'Virgin Islands','101','10','5','102','20','5','103','31','5','109','41','5'
    Union all
    select 'Guinea','101','15','3','102','22','3','105','32','2','110','45','4'
    Union all
    select 'Benin ','102','12','4','106','28','4','104','33','3','109','48','2'
    Union all
    select 'Ecuador','102','18','5','101','29','5','108','34','1','111','45','5'
    Union all
    select 'Belarus ','103','17','4','105','24','4','108','45','4','112','46','3'
    Union all
    select 'Cook Islands','105','11','2','108','23','2','101','32','2','107','42','4'
    Here is my code to fix
    declare @SQL varchar(4000)
    declare @left varchar(1)
    declare @right varchar(1)
    select @left = '1',@right = '2'
    while @left < 4
    begin
    select @SQL = '
    update t1.PortID' + @left + ' = t2.PortID' + @right + '
    t1.Distance' + @left + ' = t2.Distance' + @right + '
    t1.Rating' + @left + ' = t2.Rating' + @right + '
    'case @left
    when '1' then
    ' t1.PortID' + cast(cast(@left as int)+1 as varchar(1)) + ' = t2.PortID' + cast(cast(@right as int)+1 as varchar(1)) +'
    t1.PortID' + cast(cast(@left as int)+2 as varchar(1)) + ' = t2.PortID' + cast(cast(@right as int)+2 as varchar(1)) +'
    t1.PortID' + cast(cast(@left as int)+3 as varchar(1)) + ' = t2.PortID' + cast(cast(@right as int)+3 as varchar(1)) +'
    t1.Distance' + cast(cast(@left as int)+1 as varchar(1)) + ' = t2.Distance' + cast(cast(@right as int)+1 as varchar(1)) +'
    t1.Distance' + cast(cast(@left as int)+2 as varchar(1)) + ' = t2.Distance' + cast(cast(@right as int)+2 as varchar(1)) +'
    t1.Distance' + cast(cast(@left as int)+3 as varchar(1)) + ' = t2.Distance' + cast(cast(@right as int)+3 as varchar(1)) +'
    t1.Rating' + cast(cast(@left as int)+1 as varchar(1)) + ' = t2.Rating' + cast(cast(@right as int)+1 as varchar(1)) +'
    t1.Rating' + cast(cast(@left as int)+2 as varchar(1)) + ' = t2.Rating' + cast(cast(@right as int)+2 as varchar(1)) +'
    t1.Rating' + cast(cast(@left as int)+3 as varchar(1)) + ' = t2.Rating' + cast(cast(@right as int)+3 as varchar(1)) +'
    ' when '2' then
    ' t1.PortID' + cast(cast(@left as int)+1 as varchar(1)) + ' = t2.PortID' + cast(cast(@right as int)+1 as varchar(1)) +'
    t1.PortID' + cast(cast(@left as int)+2 as varchar(1)) + ' = t2.PortID' + cast(cast(@right as int)+2 as varchar(1)) +'
    t1.Distance' + cast(cast(@left as int)+1 as varchar(1)) + ' = t2.Distance' + cast(cast(@right as int)+1 as varchar(1)) +'
    t1.Distance' + cast(cast(@left as int)+2 as varchar(1)) + ' = t2.Distance' + cast(cast(@right as int)+2 as varchar(1)) +'
    t1.Rating' + cast(cast(@left as int)+1 as varchar(1)) + ' = t2.Rating' + cast(cast(@right as int)+1 as varchar(1)) +'
    t1.Rating' + cast(cast(@left as int)+2 as varchar(1)) + ' = t2.Rating' + cast(cast(@right as int)+2 as varchar(1)) +'
    ' when '3' then
    ' t1.PortID' + cast(cast(@left as int)+1 as varchar(1)) + ' = t2.PortID' + cast(cast(@right as int)+1 as varchar(1)) +'
    t1.Distance' + cast(cast(@left as int)+1 as varchar(1)) + ' = t2.Distance' + cast(cast(@right as int)+1 as varchar(1)) +'
    t1.Rating' + cast(cast(@left as int)+1 as varchar(1)) + ' = t2.Rating' + cast(cast(@right as int)+1 as varchar(1)) +'
    ' when '3' then
    ' t1.PortID' + @left + ' = null '
    t1.Distance' + @left + ' = null '
    t1.Rating' + @left + ' = null '
    else '' end'
    from [TravelRank] t1
    inner join [TravelRank] t2
    on t1.destination = t2.destination
    where t1.PortID1 = 101'
    print @SQL
    -- exec (@SQL)
    select @left = cast(cast(@left as int) + 1 as varchar(1))
    , @right = cast(cast(@right as int) + 1 as varchar(1))
    end
    Thanks a lot in advance.

    declare @SQL varchar(4000)
    declare @left varchar(1)
    declare @right varchar(1)
    select @left = '1',@right = '2'
    while @left < 4
    begin
    select @SQL = '
    update t1.PortID' + @left + ' = t2.PortID' + @right + '
    t1.Distance' + @left + ' = t2.Distance' + @right + '
    t1.Rating' + @left + ' = t2.Rating' + @right + '
    '+CASE @left
    when '1' then
    ' t1.PortID' + cast(cast(@left as int)+1 as varchar(1)) + ' = t2.PortID' + cast(cast(@right as int)+1 as varchar(1)) +'
    t1.PortID' + cast(cast(@left as int)+2 as varchar(1)) + ' = t2.PortID' + cast(cast(@right as int)+2 as varchar(1)) +'
    t1.PortID' + cast(cast(@left as int)+3 as varchar(1)) + ' = t2.PortID' + cast(cast(@right as int)+3 as varchar(1)) +'
    t1.Distance' + cast(cast(@left as int)+1 as varchar(1)) + ' = t2.Distance' + cast(cast(@right as int)+1 as varchar(1)) +'
    t1.Distance' + cast(cast(@left as int)+2 as varchar(1)) + ' = t2.Distance' + cast(cast(@right as int)+2 as varchar(1)) +'
    t1.Distance' + cast(cast(@left as int)+3 as varchar(1)) + ' = t2.Distance' + cast(cast(@right as int)+3 as varchar(1)) +'
    t1.Rating' + cast(cast(@left as int)+1 as varchar(1)) + ' = t2.Rating' + cast(cast(@right as int)+1 as varchar(1)) +'
    t1.Rating' + cast(cast(@left as int)+2 as varchar(1)) + ' = t2.Rating' + cast(cast(@right as int)+2 as varchar(1)) +'
    t1.Rating' + cast(cast(@left as int)+3 as varchar(1)) + ' = t2.Rating' + cast(cast(@right as int)+3 as varchar(1)) +'
    ' when '2' then
    ' t1.PortID' + cast(cast(@left as int)+1 as varchar(1)) + ' = t2.PortID' + cast(cast(@right as int)+1 as varchar(1)) +'
    t1.PortID' + cast(cast(@left as int)+2 as varchar(1)) + ' = t2.PortID' + cast(cast(@right as int)+2 as varchar(1)) +'
    t1.Distance' + cast(cast(@left as int)+1 as varchar(1)) + ' = t2.Distance' + cast(cast(@right as int)+1 as varchar(1)) +'
    t1.Distance' + cast(cast(@left as int)+2 as varchar(1)) + ' = t2.Distance' + cast(cast(@right as int)+2 as varchar(1)) +'
    t1.Rating' + cast(cast(@left as int)+1 as varchar(1)) + ' = t2.Rating' + cast(cast(@right as int)+1 as varchar(1)) +'
    t1.Rating' + cast(cast(@left as int)+2 as varchar(1)) + ' = t2.Rating' + cast(cast(@right as int)+2 as varchar(1)) +'
    ' when '3' then
    ' t1.PortID' + cast(cast(@left as int)+1 as varchar(1)) + ' = t2.PortID' + cast(cast(@right as int)+1 as varchar(1)) +'
    t1.Distance' + cast(cast(@left as int)+1 as varchar(1)) + ' = t2.Distance' + cast(cast(@right as int)+1 as varchar(1)) +'
    t1.Rating' + cast(cast(@left as int)+1 as varchar(1)) + ' = t2.Rating' + cast(cast(@right as int)+1 as varchar(1)) +'
    ' when '3' then
    ' t1.PortID' + @left + ' = null '+
    ' t1.Distance' + @left + ' = null '+
    ' t1.Rating' + @left + ' = null '
    else '' END + '
    from [TravelRank] t1
    inner join [TravelRank] t2
    on t1.destination = t2.destination
    where t1.PortID1 = 101'
    print @SQL
    -- exec (@SQL)
    select @left = cast(cast(@left as int) + 1 as varchar(1))
    , @right = cast(cast(@right as int) + 1 as varchar(1))
    end
    You were missing a couple of concats. This runs now, but I'm not completely sure if it's the output you were expecting.

  • Instead of Insert, Verify not inserting into identity column.

    I am writing an Instead of Insert trigger. I would like to fire an error when inserting into an 'Identity' column.  Since UPDATE([ColumnName]) always returns TRUE for insert statements, is there an easy/fast way around this? I don't
    want to use: 
    IF(EXISTS(SELECT [i].[AS_ID] FROM [inserted] [i] WHERE [i].[AS_ID] IS NULL))
    here is my pseudo-code...
    CREATE VIEW [org].[Assets]
    WITH SCHEMABINDING
    AS
    SELECT
    [AS].[AS_ID], -- Identity field in base table
    [AS].[Tag],
    [AS].[Name]
    FROM [org].[AS_Assets_Rewind](DEFAULT, DEFAULT, DEFAULT) [AS];
    GO
    CREATE TRIGGER [org].[Assets_Insert]
    ON [org].[Assets]
    INSTEAD OF INSERT
    AS
    BEGIN
    SET NOCOUNT ON;
    SET XACT_ABORT ON;
    -- How does this statment need to be written to throw the error?
    -- UPDATE([AS_ID]) always returns TRUE
    IF(UPDATE([AS_ID]))
    RAISERROR('INSERT into the anchor identity column ''AS_ID'' is not allowed.', 16, 1) WITH NOWAIT;
    -- Is there a faster/better method than this?
    IF(EXISTS(SELECT [i].[AS_ID] FROM [inserted] [i] WHERE [i].[AS_ID] IS NOT NULL))
    RAISERROR('INSERT into the anchor identity column ''AS_ID'' is not allowed.', 16, 1) WITH NOWAIT;
    -- Do Stuff
    END;
    -- Should error for inserting into [AS_ID] field (which is an identity field)
    INSERT INTO [org].[Assets]([AS_ID], [Tag], [Name])
    VALUES(1, 'f451', 'Paper burns'),
    (2, 'k505.928', 'Paper burns in Chemistry');
    -- No error should occur
    INSERT INTO [org].[Assets]([Tag], [Name])
    VALUES('f451', 'Paper burns'),
    ('k505.928', 'Paper burns in Chemistry');

    IDENTITY is in single quotes, maybe i should have made it italics to indicate it is not the name of the column, but a logical reference to a column that has the identity property set.
    the "_Rewind" suffix is to indicate the function "looks back into time".
    Schema design is wrong? How do you recommend creating a SQL Server 2012 database that can
    1) keep a history of data with respect to changing time and transaction time
    2) update data that does not arrive at one time, aka, I may get your name and phone number today, but your e-mail address tomorrow - we need to be able to differentiate when different data was updated.
    3) no physical deletes (due to needing to go back in time)
    FYI: 
    I have examined R. Snodgrass's, L. Rönnbäck's, and T. Johnston's bi-temporal design patterns.
    here is the DDL:
    CREATE SCHEMA [posit];
    GO
    CREATE SCHEMA [org];
    GO
    CREATE TABLE [posit].[PO_Positor]
    [PO_PositorId] INT NOT NULL IDENTITY(1, 1),
    [PO_PositorApp] NVARCHAR(128) NOT NULL CONSTRAINT [DF__PO_Positor_PO_PositorApp] DEFAULT(APP_NAME()),
    CONSTRAINT [CL__PO_Positor_PO_PositorApp] CHECK([PO_PositorApp] <> ''),
    [PO_PositorName] NVARCHAR(128) NOT NULL CONSTRAINT [DF__PO_Positor_PO_PositorName] DEFAULT(SUSER_SNAME()),
    CONSTRAINT [CL__PO_Positor_PO_PositorName] CHECK([PO_PositorName] <> ''),
    [PO_PositorHost] NVARCHAR(128) NOT NULL CONSTRAINT [DF__PO_Positor_PO_PositorHost] DEFAULT(HOST_NAME()),
    CONSTRAINT [CL__PO_Positor_PO_PositorHost] CHECK([PO_PositorHost] <> ''),
    [PO_PositorSID] VARBINARY(85) NOT NULL CONSTRAINT [DF__PO_Positor_PO_PositorSID] DEFAULT(SUSER_SID()),
    [PO_PositorUID] INT NOT NULL CONSTRAINT [DF__PO_Positor_PO_PositorUID] DEFAULT(SUSER_ID()),
    [PO_PositorNote] VARCHAR(348) NULL CONSTRAINT [CL__PO_Positor_PO_PositorNote] CHECK([PO_PositorNote] <> ''),
    [PO_PositorInserted] DATETIMEOFFSET(7) NOT NULL CONSTRAINT [DF__PO_Positor_PO_PositorInserted] DEFAULT(SYSDATETIMEOFFSET()),
    [PO_RowGuid] UNIQUEIDENTIFIER NOT NULL CONSTRAINT [DF__PO_Positor_PO_RowGuid] DEFAULT(NEWSEQUENTIALID()) ROWGUIDCOL,
    CONSTRAINT [UX__PO_Positor_PO_RowGuid] UNIQUE NONCLUSTERED([PO_RowGuid]),
    CONSTRAINT [PK__PO_Positor] PRIMARY KEY ([PO_PositorId] ASC),
    CONSTRAINT [UK__PO_Positor] UNIQUE CLUSTERED ([PO_PositorApp] ASC, [PO_PositorHost] ASC, [PO_PositorUID] ASC)
    GO
    CREATE PROCEDURE [posit].[Insert_Positor]
    @PositorNote NVARCHAR(348) = NULL
    AS
    BEGIN
    DECLARE @ProcedureDesc NVARCHAR(261) = N'[' + OBJECT_SCHEMA_NAME(@@PROCID) + N'].[' + OBJECT_NAME(@@PROCID) + N']';
    SET @PositorNote = COALESCE(@PositorNote, N'Automatically created by: ' + @ProcedureDesc);
    DECLARE @Id TABLE
    [Id] INT NOT NULL
    INSERT INTO [posit].[PO_Positor]([PO_PositorNote])
    OUTPUT [inserted].[PO_PositorId]
    INTO @Id([Id])
    VALUES (@PositorNote);
    RETURN (SELECT TOP 1 [i].[Id] FROM @Id [i]);
    END;
    GO
    CREATE PROCEDURE [posit].[Return_PositorId]
    AS
    BEGIN
    DECLARE @PositorId INT = [posit].[Get_PositorId]();
    IF (@PositorId IS NULL)
    BEGIN
    DECLARE @ProcedureDesc NVARCHAR(261) = N'[' + OBJECT_SCHEMA_NAME(@@PROCID) + N'].[' + OBJECT_NAME(@@PROCID) + N']';
    DECLARE @PositorNote NVARCHAR(348) = N'Automatically created by: ' + @ProcedureDesc;
    EXECUTE @PositorId = [posit].[Insert_Positor] @PositorNote;
    END;
    RETURN @PositorId;
    END;
    GO
    CREATE FUNCTION [posit].[Get_PositorId]
    RETURNS INT
    AS
    BEGIN
    DECLARE @PositorId INT = NULL;
    SELECT TOP 1
    @PositorId = [p].[PO_PositorId]
    FROM
    [posit].[PO_Positor] [p]
    WHERE [p].[PO_PositorApp] = APP_NAME()
    AND [p].[PO_PositorHost] = HOST_NAME()
    AND [p].[PO_PositorUID] = SUSER_ID();
    RETURN @PositorId;
    END;
    GO
    CREATE TABLE [org].[AS_Assets]
    [AS_ID] INT NOT NULL IDENTITY(1, 1),
    [AS_PositedBy] INT NOT NULL CONSTRAINT [DF__AS_Assets_PositedBy] DEFAULT([posit].[Get_PositorId]())
    CONSTRAINT [FK__AS_Assets_PositedBy] REFERENCES [posit].[PO_Positor]([PO_PositorId]),
    [AS_PositedAt] DATETIMEOFFSET(7) NOT NULL CONSTRAINT [DF__AS_Assets_PositedAt] DEFAULT(SYSDATETIMEOFFSET()),
    [AS_RowGuid] UNIQUEIDENTIFIER NOT NULL CONSTRAINT [DF__AS_Assets_RowGuid] DEFAULT(NEWSEQUENTIALID()) ROWGUIDCOL,
    CONSTRAINT [UX__AS_Assets_RowGuid] UNIQUE([AS_RowGuid]),
    CONSTRAINT [PK__AS_Assets] PRIMARY KEY CLUSTERED ([AS_ID] ASC)
    GO
    CREATE TABLE [org].[AS_NME_Assets_Name_Posit]
    [AS_NME_ID] INT NOT NULL IDENTITY(1, 1),
    [AS_NME_AS_ID] INT NOT NULL CONSTRAINT [FK__AS_NME_Assets_Name_Posit_AS_NME_AS_ID] REFERENCES [org].[AS_Assets]([AS_ID]),
    [AS_NME_AssetTag] VARCHAR(12) NOT NULL CONSTRAINT [CL__AS_NME_Assets_Name_Posit_AS_NME_AssetTag] CHECK([AS_NME_AssetTag] <> ''),
    [AS_NME_AssetName] NVARCHAR(24) NOT NULL CONSTRAINT [CL__AS_NME_Assets_Name_Posit_AS_NME_AssetName] CHECK([AS_NME_AssetName] <> ''),
    [AS_NME_AssetDetail] NVARCHAR(48) NOT NULL CONSTRAINT [CL__AS_NME_Assets_Name_Posit_AS_NME_AssetDetail] CHECK([AS_NME_AssetDetail] <> ''),
    [AS_NME_Checksum] AS BINARY_CHECKSUM([AS_NME_AssetTag], [AS_NME_AssetName], [AS_NME_AssetDetail])
    PERSISTED NOT NULL,
    [AS_NME_ChangedAt] DATETIMEOFFSET(7) NOT NULL CONSTRAINT [DF__AS_NME_Assets_Name_Posit_ChangedAt] DEFAULT(SYSDATETIMEOFFSET()),
    [AS_NME_RowGuid] UNIQUEIDENTIFIER NOT NULL CONSTRAINT [DF__AS_NME_Assets_Name_Posit_RowGuid] DEFAULT(NEWSEQUENTIALID()) ROWGUIDCOL,
    CONSTRAINT [UX__AS_NME_Assets_Name_Posit_RowGuid] UNIQUE NONCLUSTERED([AS_NME_RowGuid]),
    CONSTRAINT [PK__AS_NME_Assets_Name_Posit] PRIMARY KEY([AS_NME_ID] ASC),
    CONSTRAINT [UK__AS_NME_Assets_Name_Posit] UNIQUE CLUSTERED([AS_NME_AS_ID] ASC, [AS_NME_ChangedAt] DESC, [AS_NME_Checksum] ASC)
    WITH (FILLFACTOR = 95)
    GO
    CREATE TABLE [org].[AS_NME_Assets_Name_Annex]
    [AS_NME_ID] INT NOT NULL CONSTRAINT [FK__AS_NME_Assets_Name_Annex_AS_NME_ID] REFERENCES [org].[AS_NME_Assets_Name_Posit]([AS_NME_ID]),
    [AS_NME_PositedBy] INT NOT NULL CONSTRAINT [DF__AS_NME_Assets_Name_Annex_PositedBy] DEFAULT([posit].[Get_PositorId]())
    CONSTRAINT [FK__AS_NME_Assets_Name_Annex_PositedBy] REFERENCES [posit].[PO_Positor]([PO_PositorId]),
    [AS_NME_PositedAt] DATETIMEOFFSET(7) NOT NULL CONSTRAINT [DF__AS_NME_Assets_Name_Annex_PositedAt] DEFAULT(SYSDATETIMEOFFSET()),
    [AS_NME_PositReliability] TINYINT NOT NULL CONSTRAINT [DF__AS_NME_Assets_Name_Annex_PositRel] DEFAULT(50),
    [AS_NME_PositReliable] AS CONVERT(BIT, CASE WHEN [AS_NME_PositReliability] > 0 THEN 1 ELSE 0 END)
    PERSISTED NOT NULL,
    [AS_NME_RowGuid] UNIQUEIDENTIFIER NOT NULL CONSTRAINT [DF__AS_NME_Assets_Name_Annex_RowGuid] DEFAULT(NEWSEQUENTIALID()) ROWGUIDCOL,
    CONSTRAINT [UX__AS_NME_Assets_Name_Annex_RowGuid] UNIQUE NONCLUSTERED([AS_NME_RowGuid]),
    CONSTRAINT [PK__AS_NME_Assets_Name_Annex] PRIMARY KEY CLUSTERED([AS_NME_ID] ASC, [AS_NME_PositedAt] DESC, [AS_NME_PositedBy] ASC)
    WITH (FILLFACTOR = 95)
    GO
    CREATE TABLE [org].[AS_GEO_Assets_Location_Posit]
    [AS_GEO_ID] INT NOT NULL IDENTITY(1, 1),
    [AS_GEO_AS_ID] INT NOT NULL CONSTRAINT [FK__AS_GEO_Assets_Location_Posit_AS_GEO_AS_ID] REFERENCES [org].[AS_Assets]([AS_ID]),
    [AS_GEO_Longitude_Deg] DECIMAL(17, 14) NOT NULL CONSTRAINT [CR__AS_GEO_Assets_Location_Posit_Longitude_Deg_L] CHECK([AS_GEO_Longitude_Deg] >= -180.0),
    CONSTRAINT [CR__AS_GEO_Assets_Location_Posit_Longitude_Deg_U] CHECK([AS_GEO_Longitude_Deg] <= 180.0),
    [AS_GEO_Latitude_Deg] DECIMAL(17, 14) NOT NULL CONSTRAINT [CR__AS_GEO_Assets_Location_Posit_Latitude_Deg_L] CHECK([AS_GEO_Latitude_Deg] >= -90.0),
    CONSTRAINT [CR__AS_GEO_Assets_Location_Posit_Latitude_Deg_U] CHECK([AS_GEO_Latitude_Deg] <= 90.0),
    [AS_GEO_Wgs84] AS geography::STPointFromText('POINT(' + CONVERT(VARCHAR(19), [AS_GEO_Longitude_Deg], 0) + ' ' + CONVERT(VARCHAR(19), [AS_GEO_Latitude_Deg], 0) + ')', 4326),
    [AS_GEO_Checksum] AS BINARY_CHECKSUM([AS_GEO_Longitude_Deg], [AS_GEO_Latitude_Deg])
    PERSISTED NOT NULL,
    [AS_GEO_ChangedAt] DATETIMEOFFSET(7) NOT NULL CONSTRAINT [DF__AS_GEO_Assets_Location_Posit_ChangedAt] DEFAULT(SYSDATETIMEOFFSET()),
    [AS_GEO_RowGuid] UNIQUEIDENTIFIER NOT NULL CONSTRAINT [DF__AS_GEO_Assets_Location_Posit_RowGuid] DEFAULT(NEWSEQUENTIALID()) ROWGUIDCOL,
    CONSTRAINT [UX__AS_GEO_Assets_Location_Posit_RowGuid] UNIQUE NONCLUSTERED([AS_GEO_RowGuid]),
    CONSTRAINT [PK__AS_GEO_Assets_Location_Posit] PRIMARY KEY([AS_GEO_ID] ASC),
    CONSTRAINT [UK__AS_GEO_Assets_Location_Posit] UNIQUE CLUSTERED([AS_GEO_AS_ID] ASC, [AS_GEO_ChangedAt] DESC, [AS_GEO_Checksum] ASC)
    WITH (FILLFACTOR = 95)
    GO
    CREATE TABLE [org].[AS_GEO_Assets_Location_Annex]
    [AS_GEO_ID] INT NOT NULL CONSTRAINT [FK__AS_GEO_Assets_Location_Annex_AS_GEO_ID] REFERENCES [org].[AS_GEO_Assets_Location_Posit]([AS_GEO_ID]),
    [AS_GEO_PositedBy] INT NOT NULL CONSTRAINT [DF__AS_GEO_Assets_Location_Annex_PositedBy] DEFAULT([posit].[Get_PositorId]())
    CONSTRAINT [FK__AS_GEO_Assets_Location_Annex_PositedBy] REFERENCES [posit].[PO_Positor]([PO_PositorId]),
    [AS_GEO_PositedAt] DATETIMEOFFSET(7) NOT NULL CONSTRAINT [DF__AS_GEO_Assets_Location_Annex_PositedAt] DEFAULT(SYSDATETIMEOFFSET()),
    [AS_GEO_PositReliability] TINYINT NOT NULL CONSTRAINT [DF__AS_GEO_Assets_Location_Annex_PositRel] DEFAULT(50),
    [AS_GEO_PositReliable] AS CONVERT(BIT, CASE WHEN [AS_GEO_PositReliability] > 0 THEN 1 ELSE 0 END)
    PERSISTED NOT NULL,
    [AS_GEO_RowGuid] UNIQUEIDENTIFIER NOT NULL CONSTRAINT [DF__AS_GEO_Assets_Location_Annex_RowGuid] DEFAULT(NEWSEQUENTIALID()) ROWGUIDCOL,
    CONSTRAINT [UX__AS_GEO_Assets_Location_Annex_RowGuid] UNIQUE NONCLUSTERED([AS_GEO_RowGuid]),
    CONSTRAINT [PK__AS_GEO_Assets_Location_Annex] PRIMARY KEY CLUSTERED([AS_GEO_ID] ASC, [AS_GEO_PositedAt] DESC, [AS_GEO_PositedBy] ASC)
    WITH (FILLFACTOR = 95)
    GO
    CREATE FUNCTION [org].[AS_NME_Assets_Name_Posit_RW]
    @ChangedBefore DATETIMEOFFSET(7) = '9999-12-31'
    RETURNS TABLE
    WITH SCHEMABINDING
    AS
    RETURN
    SELECT
    [p].[AS_NME_ID],
    [p].[AS_NME_AS_ID],
    [p].[AS_NME_AssetTag],
    [p].[AS_NME_AssetName],
    [p].[AS_NME_AssetDetail],
    [p].[AS_NME_Checksum],
    [p].[AS_NME_ChangedAt]
    FROM
    [org].[AS_NME_Assets_Name_Posit] [p]
    WHERE
    [p].[AS_NME_ChangedAt] < @ChangedBefore;
    GO
    CREATE FUNCTION [org].[AS_NME_Assets_Name_Annex_RW]
    @PositedBefore DATETIMEOFFSET(7) = '9999-12-31'
    RETURNS TABLE
    WITH SCHEMABINDING
    AS
    RETURN
    SELECT
    [a].[AS_NME_ID],
    [a].[AS_NME_PositedBy],
    [a].[AS_NME_PositedAt],
    [a].[AS_NME_PositReliability],
    [a].[AS_NME_PositReliable]
    FROM
    [org].[AS_NME_Assets_Name_Annex] [a]
    WHERE[a].[AS_NME_PositedAt] < @PositedBefore;
    GO
    CREATE FUNCTION [org].[AS_NME_Assets_Name_RW]
    @ChangedBefore DATETIMEOFFSET(7) = '9999-12-31',
    @PositedBefore DATETIMEOFFSET(7) = '9999-12-31'
    RETURNS TABLE
    WITH SCHEMABINDING
    AS
    RETURN
    SELECT
    [p].[AS_NME_ID],
    [p].[AS_NME_AS_ID],
    [p].[AS_NME_AssetTag],
    [p].[AS_NME_AssetName],
    [p].[AS_NME_AssetDetail],
    [p].[AS_NME_Checksum],
    [p].[AS_NME_ChangedAt],
    [a].[AS_NME_PositedBy],
    [a].[AS_NME_PositedAt],
    [a].[AS_NME_PositReliability],
    [a].[AS_NME_PositReliable]
    FROM
    [org].[AS_NME_Assets_Name_Posit_RW](@ChangedBefore) [p]
    INNER JOIN
    [org].[AS_NME_Assets_Name_Annex_RW](@PositedBefore) [a]
    ON [a].[AS_NME_ID] = [p].[AS_NME_ID]
    AND [a].[AS_NME_PositedAt] = (
    SELECT TOP 1
    [s].[AS_NME_PositedAt]
    FROM
    [org].[AS_NME_Assets_Name_Annex_RW](@PositedBefore) [s]
    WHERE [s].[AS_NME_ID] = [p].[AS_NME_ID]
    ORDER BY
    [s].[AS_NME_PositedAt] DESC
    GO
    CREATE FUNCTION [org].[AS_GEO_Assets_Location_Posit_RW]
    @ChangedBefore DATETIMEOFFSET(7) = '9999-12-31'
    RETURNS TABLE
    WITH SCHEMABINDING
    AS
    RETURN
    SELECT
    [p].[AS_GEO_ID],
    [p].[AS_GEO_AS_ID],
    [p].[AS_GEO_Longitude_Deg],
    [p].[AS_GEO_Latitude_Deg],
    [p].[AS_GEO_Wgs84],
    [p].[AS_GEO_Checksum],
    [p].[AS_GEO_ChangedAt]
    FROM
    [org].[AS_GEO_Assets_Location_Posit] [p]
    WHERE
    [p].[AS_GEO_ChangedAt] < @ChangedBefore;
    GO
    CREATE FUNCTION [org].[AS_GEO_Assets_Location_Annex_RW]
    @PositedBefore DATETIMEOFFSET(7) = '9999-12-31'
    RETURNS TABLE
    WITH SCHEMABINDING
    AS
    RETURN
    SELECT
    [a].[AS_GEO_ID],
    [a].[AS_GEO_PositedBy],
    [a].[AS_GEO_PositedAt],
    [a].[AS_GEO_PositReliability],
    [a].[AS_GEO_PositReliable]
    FROM
    [org].[AS_GEO_Assets_Location_Annex] [a]
    WHERE
    [a].[AS_GEO_PositedAt] < @PositedBefore;
    GO
    CREATE FUNCTION [org].[AS_GEO_Assets_Location_RW]
    @ChangedBefore DATETIMEOFFSET(7) = '9999-12-31',
    @PositedBefore DATETIMEOFFSET(7) = '9999-12-31'
    RETURNS TABLE
    WITH SCHEMABINDING
    AS
    RETURN
    SELECT
    [p].[AS_GEO_ID],
    [p].[AS_GEO_AS_ID],
    [p].[AS_GEO_Longitude_Deg],
    [p].[AS_GEO_Latitude_Deg],
    [p].[AS_GEO_Wgs84],
    [p].[AS_GEO_Checksum],
    [p].[AS_GEO_ChangedAt],
    [a].[AS_GEO_PositedBy],
    [a].[AS_GEO_PositedAt],
    [a].[AS_GEO_PositReliability],
    [a].[AS_GEO_PositReliable]
    FROM
    [org].[AS_GEO_Assets_Location_Posit_RW](@ChangedBefore) [p]
    INNER JOIN
    [org].[AS_GEO_Assets_Location_Annex_RW](@PositedBefore) [a]
    ON [a].[AS_GEO_ID] = [p].[AS_GEO_ID]
    AND [a].[AS_GEO_PositedAt] = (
    SELECT TOP 1
    [s].[AS_GEO_PositedAt]
    FROM
    [org].[AS_GEO_Assets_Location_Annex_RW](@PositedBefore) [s]
    WHERE [s].[AS_GEO_ID] = [p].[AS_GEO_ID]
    ORDER BY
    [s].[AS_GEO_PositedAt] DESC
    GO
    CREATE FUNCTION [org].[AS_Assets_Rewind]
    @ChangedBefore DATETIMEOFFSET(7) = '9999-12-31',
    @PositedBefore DATETIMEOFFSET(7) = '9999-12-31',
    @Reliable BIT = 1
    RETURNS TABLE
    WITH SCHEMABINDING
    AS
    RETURN
    SELECT
    [AS].[AS_ID],
    [AS].[AS_PositedBy],
    [AS].[AS_PositedAt],
    [NME].[AS_NME_ID],
    [NME].[AS_NME_AS_ID],
    [NME].[AS_NME_AssetTag],
    [NME].[AS_NME_AssetName],
    [NME].[AS_NME_AssetDetail],
    [NME].[AS_NME_Checksum],
    [NME].[AS_NME_ChangedAt],
    [NME].[AS_NME_PositedBy],
    [NME].[AS_NME_PositedAt],
    [NME].[AS_NME_PositReliability],
    [NME].[AS_NME_PositReliable],
    [GEO].[AS_GEO_ID],
    [GEO].[AS_GEO_AS_ID],
    [GEO].[AS_GEO_Longitude_Deg],
    [GEO].[AS_GEO_Latitude_Deg],
    [GEO].[AS_GEO_Wgs84],
    [GEO].[AS_GEO_Checksum],
    [GEO].[AS_GEO_ChangedAt],
    [GEO].[AS_GEO_PositedBy],
    [GEO].[AS_GEO_PositedAt],
    [GEO].[AS_GEO_PositReliability],
    [GEO].[AS_GEO_PositReliable]
    FROM
    [org].[AS_Assets] [AS]
    INNER JOIN
    [org].[AS_NME_Assets_Name_RW](@ChangedBefore, @PositedBefore) [NME]
    ON [NME].[AS_NME_AS_ID] = [AS].[AS_ID]
    AND [NME].[AS_NME_PositReliable] = @Reliable
    AND [NME].[AS_NME_ID] = (
    SELECT TOP 1
    [NME_s].[AS_NME_ID]
    FROM
    [org].[AS_NME_Assets_Name_RW](@ChangedBefore, @PositedBefore) [NME_s]
    WHERE [NME_s].[AS_NME_AS_ID] = [AS].[AS_ID]
    ORDER BY
    [NME_s].[AS_NME_ChangedAt] DESC,
    [NME_s].[AS_NME_PositedAt] DESC
    LEFT OUTER JOIN
    [org].[AS_GEO_Assets_Location_RW](@ChangedBefore, @PositedBefore) [GEO]
    ON [GEO].[AS_GEO_AS_ID] = [AS].[AS_ID]
    AND [GEO].[AS_GEO_PositReliable] = @Reliable
    AND [GEO].[AS_GEO_ID] = (
    SELECT TOP 1
    [GEO_s].[AS_GEO_ID]
    FROM
    [org].[AS_GEO_Assets_Location_RW](@ChangedBefore, @PositedBefore) [GEO_s]
    WHERE [GEO_s].[AS_GEO_AS_ID] = [AS].[AS_ID]
    ORDER BY
    [GEO_s].[AS_GEO_ChangedAt] DESC,
    [GEO_s].[AS_GEO_PositedAt] DESC
    GO
    CREATE VIEW [org].[AS_NME_Assets_Name_SK]
    WITH SCHEMABINDING
    AS
    SELECT
    [p].[AS_NME_ID],
    [p].[AS_NME_AS_ID],
    [p].[AS_NME_AssetTag],
    [p].[AS_NME_AssetName],
    [p].[AS_NME_AssetDetail],
    [p].[AS_NME_Checksum],
    [p].[AS_NME_ChangedAt],
    [a].[AS_NME_PositedBy],
    [a].[AS_NME_PositedAt],
    [a].[AS_NME_PositReliability],
    [a].[AS_NME_PositReliable]
    FROM
    [org].[AS_NME_Assets_Name_Posit] [p]
    INNER JOIN
    [org].[AS_NME_Assets_Name_Annex] [a]
    ON [a].[AS_NME_ID] = [p].[AS_NME_ID];
    GO
    CREATE UNIQUE CLUSTERED INDEX [PK__AS_NME_Assets_Name_SK]
    ON [org].[AS_NME_Assets_Name_SK]
    [AS_NME_AS_ID] ASC,
    [AS_NME_ChangedAt] DESC,
    [AS_NME_PositedAt] DESC,
    [AS_NME_PositedBy] ASC,
    [AS_NME_PositReliable] ASC
    WITH (FILLFACTOR = 95);
    GO
    CREATE VIEW [org].[AS_GEO_Assets_Location_SK]
    WITH SCHEMABINDING
    AS
    SELECT
    [p].[AS_GEO_ID],
    [p].[AS_GEO_AS_ID],
    [p].[AS_GEO_Wgs84],
    [p].[AS_GEO_Checksum],
    [p].[AS_GEO_ChangedAt],
    [a].[AS_GEO_PositedBy],
    [a].[AS_GEO_PositedAt],
    [a].[AS_GEO_PositReliability],
    [a].[AS_GEO_PositReliable]
    FROM
    [org].[AS_GEO_Assets_Location_Posit] [p]
    INNER JOIN
    [org].[AS_GEO_Assets_Location_Annex] [a]
    ON [a].[AS_GEO_ID] = [p].[AS_GEO_ID];
    GO
    CREATE UNIQUE CLUSTERED INDEX [PK__AS_GEO_Assets_Location_SK]
    ON [org].[AS_GEO_Assets_Location_SK]
    [AS_GEO_AS_ID] ASC,
    [AS_GEO_ChangedAt] DESC,
    [AS_GEO_PositedAt] DESC,
    [AS_GEO_PositedBy] ASC,
    [AS_GEO_PositReliable] ASC
    WITH (FILLFACTOR = 95);
    GO
    CREATE VIEW [org].[AS_Assets_Current]
    WITH SCHEMABINDING
    AS
    SELECT
    [AS].[AS_ID],
    [AS].[AS_PositedBy],
    [AS].[AS_PositedAt],
    [AS].[AS_NME_ID],
    [AS].[AS_NME_AS_ID],
    [AS].[AS_NME_AssetTag],
    [AS].[AS_NME_AssetName],
    [AS].[AS_NME_AssetDetail],
    [AS].[AS_NME_Checksum],
    [AS].[AS_NME_ChangedAt],
    [AS].[AS_NME_PositedBy],
    [AS].[AS_NME_PositedAt],
    [AS].[AS_NME_PositReliability],
    [AS].[AS_NME_PositReliable],
    [AS].[AS_GEO_ID],
    [AS].[AS_GEO_AS_ID],
    [AS].[AS_GEO_Longitude_Deg],
    [AS].[AS_GEO_Latitude_Deg],
    [AS].[AS_GEO_Wgs84],
    [AS].[AS_GEO_Checksum],
    [AS].[AS_GEO_ChangedAt],
    [AS].[AS_GEO_PositedBy],
    [AS].[AS_GEO_PositedAt],
    [AS].[AS_GEO_PositReliability],
    [AS].[AS_GEO_PositReliable]
    FROM [org].[AS_Assets_Rewind](DEFAULT, DEFAULT, DEFAULT) [AS];
    GO
    CREATE TRIGGER [org].[AS_Assets_Current_Insert]
    ON [org].[AS_Assets_Current]
    INSTEAD OF INSERT
    AS
    BEGIN
    SET NOCOUNT ON;
    SET XACT_ABORT ON;
    IF(EXISTS(SELECT [i].[AS_ID] FROM [inserted] [i] WHERE [i].[AS_ID] IS NOT NULL))
    RAISERROR('INSERT into the anchor identity column ''AS_ID'' is not allowed.', 16, 1) WITH NOWAIT;
    IF(EXISTS(SELECT [i].[AS_NME_ID] FROM [inserted] [i] WHERE [i].[AS_NME_ID] IS NOT NULL))
    RAISERROR('INSERT into the identity column ''AS_NME_ID'' is not allowed.', 16, 1) WITH NOWAIT;
    IF(EXISTS(SELECT [i].[AS_NME_AS_ID] FROM [inserted] [i] WHERE [i].[AS_NME_AS_ID] IS NOT NULL))
    RAISERROR('INSERT into the foreign key identity column ''AS_NME_AS_ID'' is not allowed.', 16, 1) WITH NOWAIT;
    IF(EXISTS(SELECT [i].[AS_NME_Checksum] FROM [inserted] [i] WHERE [i].[AS_NME_Checksum] IS NOT NULL))
    RAISERROR('INSERT into the computed column ''AS_NME_Checksum'' is not allowed.', 16, 1) WITH NOWAIT;
    IF(EXISTS(SELECT [i].[AS_NME_PositReliable] FROM [inserted] [i] WHERE [i].[AS_NME_PositReliable] IS NOT NULL))
    RAISERROR('INSERT into the computed column ''AS_NME_PositReliable'' is not allowed.', 16, 1) WITH NOWAIT;
    IF(EXISTS(SELECT [i].[AS_GEO_ID] FROM [inserted] [i] WHERE [i].[AS_GEO_ID] IS NOT NULL))
    RAISERROR('INSERT into the identity column ''AS_GEO_ID'' is not allowed.', 16, 1) WITH NOWAIT;
    IF(EXISTS(SELECT [i].[AS_GEO_AS_ID] FROM [inserted] [i] WHERE [i].[AS_GEO_AS_ID] IS NOT NULL))
    RAISERROR('INSERT into the foreign key identity column ''AS_GEO_AS_ID'' is not allowed.', 16, 1) WITH NOWAIT;
    IF(EXISTS(SELECT [i].[AS_GEO_Checksum] FROM [inserted] [i] WHERE [i].[AS_GEO_Checksum] IS NOT NULL))
    RAISERROR('INSERT into the computed column ''AS_GEO_Checksum'' is not allowed.', 16, 1) WITH NOWAIT;
    IF(EXISTS(SELECT [i].[AS_GEO_PositReliable] FROM [inserted] [i] WHERE [i].[AS_GEO_PositReliable] IS NOT NULL))
    RAISERROR('INSERT into the computed column ''AS_GEO_PositReliable'' is not allowed.', 16, 1) WITH NOWAIT;
    IF(EXISTS(SELECT [i].[AS_GEO_Wgs84] FROM [inserted] [i] WHERE [i].[AS_GEO_Wgs84] IS NOT NULL))
    RAISERROR('INSERT into the computed column ''AS_GEO_Wgs84'' is not allowed.', 16, 1) WITH NOWAIT;
    DECLARE @Reliability INT = 50;
    DECLARE @Now DATETIMEOFFSET(7) = SYSDATETIMEOFFSET();
    DECLARE @PositedBy INT;
    EXECUTE @PositedBy = [posit].[Return_PositorId];
    DECLARE @Inserted TABLE
    [AS_PositedBy] INT NOT NULL,
    [AS_PositedAt] DATETIMEOFFSET(7) NOT NULL,
    [AS_NME_AssetTag] VARCHAR(12) NOT NULL CHECK([AS_NME_AssetTag] <> ''),
    [AS_NME_AssetName] NVARCHAR(24) NOT NULL CHECK([AS_NME_AssetName] <> ''),
    [AS_NME_AssetDetail] NVARCHAR(48) NOT NULL CHECK([AS_NME_AssetDetail] <> ''),
    [AS_NME_Checksum] AS BINARY_CHECKSUM([AS_NME_AssetTag], [AS_NME_AssetName], [AS_NME_AssetDetail])
    PERSISTED NOT NULL,
    [AS_NME_ChangedAt] DATETIMEOFFSET(7) NOT NULL,
    [AS_NME_PositedBy] INT NOT NULL,
    [AS_NME_PositedAt] DATETIMEOFFSET(7) NOT NULL,
    [AS_NME_PositReliability] TINYINT NOT NULL,
    [AS_GEO_Longitude_Deg] DECIMAL(17, 14) NULL CHECK([AS_GEO_Longitude_Deg] >= -180.0), CHECK([AS_GEO_Longitude_Deg] <= 180.0),
    [AS_GEO_Latitude_Deg] DECIMAL(17, 14) NULL CHECK([AS_GEO_Latitude_Deg] >= -90.0), CHECK([AS_GEO_Latitude_Deg] <= 90.0),
    [AS_GEO_Checksum] AS BINARY_CHECKSUM([AS_GEO_Longitude_Deg], [AS_GEO_Latitude_Deg])
    PERSISTED NOT NULL,
    [AS_GEO_ChangedAt] DATETIMEOFFSET(7) NOT NULL,
    [AS_GEO_PositedBy] INT NOT NULL,
    [AS_GEO_PositedAt] DATETIMEOFFSET(7) NOT NULL,
    [AS_GEO_PositReliability] TINYINT NOT NULL,
    [ROW_ID] INT NOT NULL,
    PRIMARY KEY([ROW_ID] ASC)
    INSERT INTO @Inserted
    [AS_PositedBy],
    [AS_PositedAt],
    [AS_NME_AssetTag],
    [AS_NME_AssetName],
    [AS_NME_AssetDetail],
    [AS_NME_ChangedAt],
    [AS_NME_PositedBy],
    [AS_NME_PositedAt],
    [AS_NME_PositReliability],
    [AS_GEO_Longitude_Deg],
    [AS_GEO_Latitude_Deg],
    [AS_GEO_ChangedAt],
    [AS_GEO_PositedBy],
    [AS_GEO_PositedAt],
    [AS_GEO_PositReliability],
    [ROW_ID]
    SELECT
    COALESCE([i].[AS_PositedBy], @PositedBy),
    COALESCE([i].[AS_PositedAt], @Now),
    [i].[AS_NME_AssetTag],
    [i].[AS_NME_AssetName],
    [i].[AS_NME_AssetDetail],
    COALESCE([i].[AS_NME_ChangedAt], @Now),
    COALESCE([i].[AS_NME_PositedBy], @PositedBy),
    COALESCE([i].[AS_NME_PositedAt], @Now),
    COALESCE([i].[AS_NME_PositReliability], @Reliability),
    [i].[AS_GEO_Longitude_Deg],
    [i].[AS_GEO_Latitude_Deg],
    COALESCE([i].[AS_GEO_ChangedAt], @Now),
    COALESCE([i].[AS_GEO_PositedBy], @PositedBy),
    COALESCE([i].[AS_GEO_PositedAt], @Now),
    COALESCE([i].[AS_GEO_PositReliability], @Reliability),
    ROW_NUMBER() OVER (PARTITION BY [i].[AS_ID] ORDER BY [i].[AS_ID])
    FROM
    [inserted] [i]
    LEFT OUTER JOIN
    [org].[AS_Assets_Current] [c]
    ON [c].[AS_NME_Checksum] = BINARY_CHECKSUM([i].[AS_NME_AssetTag], [i].[AS_NME_AssetName], [i].[AS_NME_AssetDetail])
    WHERE
    [i].[AS_ID] IS NULL
    AND [i].[AS_NME_ID] IS NULL
    AND [i].[AS_NME_AS_ID] IS NULL
    AND [i].[AS_GEO_ID] IS NULL
    AND [i].[AS_GEO_AS_ID] IS NULL
    AND [c].[AS_NME_AS_ID] IS NULL;
    DECLARE @Anchor TABLE
    [ROW_ID] INT NOT NULL IDENTITY(1, 1),
    [AS_ID] INT NOT NULL,
    PRIMARY KEY([ROW_ID] ASC),
    UNIQUE CLUSTERED([AS_ID] ASC)
    INSERT INTO [org].[AS_Assets]
    [AS_PositedBy],
    [AS_PositedAt]
    OUTPUT
    [inserted].[AS_ID]
    INTO
    @Anchor
    [AS_ID]
    SELECT
    [i].[AS_PositedBy],
    [i].[AS_PositedAt]
    FROM
    @Inserted [i]
    ORDER BY
    [i].[ROW_ID] ASC;
    DECLARE @NME_Posit TABLE
    [AS_NME_ID] INT NOT NULL,
    [AS_NME_Checksum] INT NOT NULL,
    PRIMARY KEY([AS_NME_ID] ASC),
    UNIQUE CLUSTERED([AS_NME_Checksum] ASC)
    INSERT INTO [org].[AS_NME_Assets_Name_Posit]
    [AS_NME_AS_ID],
    [AS_NME_AssetTag],
    [AS_NME_AssetName],
    [AS_NME_AssetDetail],
    [AS_NME_ChangedAt]
    OUTPUT
    [inserted].[AS_NME_ID],
    [inserted].[AS_NME_Checksum]
    INTO
    @NME_Posit
    [AS_NME_ID],
    [AS_NME_Checksum]
    SELECT
    [a].[AS_ID],
    [i].[AS_NME_AssetTag],
    [i].[AS_NME_AssetName],
    [i].[AS_NME_AssetDetail],
    [i].[AS_NME_ChangedAt]
    FROM
    @Inserted [i]
    LEFT OUTER JOIN
    @Anchor [a]
    ON [a].[ROW_ID] = [i].[ROW_ID]
    WHERE [a].[AS_ID] IS NOT NULL;
    INSERT INTO [org].[AS_NME_Assets_Name_Annex]
    [AS_NME_ID],
    [AS_NME_PositedBy],
    [AS_NME_PositedAt],
    [AS_NME_PositReliability]
    SELECT
    [p].[AS_NME_ID],
    [i].[AS_NME_PositedBy],
    [i].[AS_NME_PositedAt],
    [i].[AS_NME_PositReliability]
    FROM
    @Inserted [i]
    INNER JOIN
    @NME_Posit [p]
    ON [p].[AS_NME_Checksum] = [i].[AS_NME_Checksum];
    DECLARE @GEO_Posit TABLE
    [AS_GEO_ID] INT NOT NULL,
    [AS_GEO_Checksum] INT NOT NULL,
    PRIMARY KEY([AS_GEO_ID] ASC),
    UNIQUE CLUSTERED([AS_GEO_Checksum] ASC)
    INSERT INTO [org].[AS_GEO_Assets_Location_Posit]
    [AS_GEO_AS_ID],
    [AS_GEO_Longitude_Deg],
    [AS_GEO_Latitude_Deg],
    [AS_GEO_ChangedAt]
    OUTPUT
    [inserted].[AS_GEO_ID],
    [inserted].[AS_GEO_Checksum]
    INTO
    @GEO_Posit
    [AS_GEO_ID],
    [AS_GEO_Checksum]
    SELECT
    [a].[AS_ID],
    [i].[AS_GEO_Longitude_Deg],
    [i].[AS_GEO_Latitude_Deg],
    [i].[AS_NME_ChangedAt]
    FROM
    @Inserted [i]
    LEFT OUTER JOIN
    @Anchor [a]
    ON [a].[ROW_ID] = [i].[ROW_ID]
    WHERE [a].[AS_ID] IS NOT NULL
    AND [i].[AS_GEO_Longitude_Deg] IS NOT NULL
    AND [i].[AS_GEO_Latitude_Deg] IS NOT NULL;
    INSERT INTO [org].[AS_GEO_Assets_Location_Annex]
    [AS_GEO_ID],
    [AS_GEO_PositedBy],
    [AS_GEO_PositedAt],
    [AS_GEO_PositReliability]
    SELECT
    [p].[AS_GEO_ID],
    [i].[AS_GEO_PositedBy],
    [i].[AS_GEO_PositedAt],
    [i].[AS_GEO_PositReliability]
    FROM
    @Inserted [i]
    INNER JOIN
    @GEO_Posit [p]
    ON [p].[AS_GEO_Checksum] = [i].[AS_GEO_Checksum];
    END;
    GO
    CREATE TRIGGER [org].[AS_Assets_Current_Update]
    ON [org].[AS_Assets_Current]
    INSTEAD OF UPDATE
    AS
    BEGIN
    SET NOCOUNT ON;
    SET XACT_ABORT ON;
    IF(UPDATE([AS_ID]))
    RAISERROR('Cannot update the anchor identity column ''AS_ID''.', 16, 1);
    IF(UPDATE([AS_PositedBy]))
    RAISERROR('Cannot update the anchor column ''AS_PositedBy''.', 16, 1);
    IF(UPDATE([AS_PositedAt]))
    RAISERROR('Cannot update the anchor column ''AS_PositedAt''.', 16, 1);
    IF(UPDATE([AS_NME_ID]))
    RAISERROR('Cannot update the identity column ''AS_NME_ID''.', 16, 1);
    IF(UPDATE([AS_NME_AS_ID]))
    RAISERROR('Cannot update the foreign key identity column ''AS_NME_AS_ID''.', 16, 1);
    IF(UPDATE([AS_NME_Checksum]))
    RAISERROR('Cannot update the computed column ''AS_NME_Checksum''.', 16, 1);
    IF(UPDATE([AS_NME_PositReliable]))
    RAISERROR('Cannot update the computed column ''AS_NME_PositReliable''.', 16, 1);
    IF(UPDATE([AS_GEO_ID]))
    RAISERROR('Cannot update the identity column ''AS_GEO_ID''.', 16, 1);
    IF(UPDATE([AS_GEO_AS_ID]))
    RAISERROR('Cannot update the foreign key identity column ''AS_GEO_AS_ID''.', 16, 1);
    IF(UPDATE([AS_GEO_Checksum]))
    RAISERROR('Cannot update the computed column ''AS_GEO_Checksum''.', 16, 1);
    IF(UPDATE([AS_GEO_PositReliable]))
    RAISERROR('Cannot update the computed column ''AS_GEO_PositReliable''.', 16, 1);
    IF(UPDATE([AS_GEO_Wgs84]))
    RAISERROR('Cannot update the computed column ''AS_GEO_Wgs84''.', 16, 1);
    DECLARE @Now DATETIMEOFFSET(7) = SYSDATETIMEOFFSET();
    DECLARE @PositedBy INT;
    EXECUTE @PositedBy = [posit].[Return_PositorId];
    DECLARE @Inserted TABLE
    [AS_ID] INT NOT NULL,
    [AS_PositedBy] INT NOT NULL,
    [AS_PositedAt] DATETIMEOFFSET(7) NOT NULL,
    [AS_NME_ID] INT NOT NULL,
    [AS_NME_AS_ID] INT NOT NULL,
    [AS_NME_AssetTag] VARCHAR(12) NULL CHECK([AS_NME_AssetTag] <> ''),
    [AS_NME_AssetName] NVARCHAR(24) NULL CHECK([AS_NME_AssetName] <> ''),
    [AS_NME_AssetDetail] NVARCHAR(48) NULL CHECK([AS_NME_AssetDetail] <> ''),
    [AS_NME_Checksum] AS BINARY_CHECKSUM([AS_NME_AssetTag], [AS_NME_AssetName], [AS_NME_AssetDetail])
    PERSISTED NOT NULL,
    [AS_NME_ChangedAt] DATETIMEOFFSET(7) NOT NULL,
    [AS_NME_PositedBy] INT NOT NULL,
    [AS_NME_PositedAt] DATETIMEOFFSET(7) NOT NULL,
    [AS_NME_PositReliability] TINYINT NOT NULL,
    [AS_GEO_ID] INT NULL,
    [AS_GEO_AS_ID] INT NULL,
    [AS_GEO_Longitude_Deg] DECIMAL(17, 14) NULL CHECK([AS_GEO_Longitude_Deg] >= -180.0), CHECK([AS_GEO_Longitude_Deg] <= 180.0),
    [AS_GEO_Latitude_Deg] DECIMAL(17, 14) NULL CHECK([AS_GEO_Latitude_Deg] >= -90.0), CHECK([AS_GEO_Latitude_Deg] <= 90.0),
    [AS_GEO_Checksum] AS BINARY_CHECKSUM([AS_GEO_Longitude_Deg], [AS_GEO_Latitude_Deg])
    PERSISTED NOT NULL,
    [AS_GEO_ChangedAt] DATETIMEOFFSET(7) NULL,
    [AS_GEO_PositedBy] INT NULL,
    [AS_GEO_PositedAt] DATETIMEOFFSET(7) NULL,
    [AS_GEO_PositReliability] TINYINT NULL,
    [ROW_ID] INT NOT NULL,
    PRIMARY KEY([ROW_ID] ASC)
    INSERT INTO @Inserted
    [AS_ID],
    [AS_PositedBy],
    [AS_PositedAt],
    [AS_NME_ID],
    [AS_NME_AS_ID],
    [AS_NME_AssetTag],
    [AS_NME_AssetName],
    [AS_NME_AssetDetail],
    [AS_NME_ChangedAt],
    [AS_NME_PositedBy],
    [AS_NME_PositedAt],
    [AS_NME_PositReliability],
    [AS_GEO_ID],
    [AS_GEO_AS_ID],
    [AS_GEO_Longitude_Deg],
    [AS_GEO_Latitude_Deg],
    [AS_GEO_ChangedAt],
    [AS_GEO_PositedBy],
    [AS_GEO_PositedAt],
    [AS_GEO_PositReliability],
    [ROW_ID]
    SELECT
    [i].[AS_ID],
    CASE WHEN (UPDATE([AS_PositedBy])) THEN [i].[AS_PositedBy] ELSE @PositedBy END,
    CASE WHEN (UPDATE([AS_PositedAt])) THEN [i].[AS_PositedAt] ELSE @Now END,
    [i].[AS_NME_ID],
    [i].[AS_NME_AS_ID],
    [i].[AS_NME_AssetTag],
    [i].[AS_NME_AssetName],
    [i].[AS_NME_AssetDetail],
    CASE WHEN (UPDATE([AS_NME_ChangedAt])) THEN [i].[AS_NME_ChangedAt] ELSE @Now END,
    CASE WHEN (UPDATE([AS_NME_PositedBy])) THEN [i].[AS_NME_PositedBy] ELSE @PositedBy END,
    CASE WHEN (UPDATE([AS_NME_PositedAt])) THEN [i].[AS_NME_PositedAt] ELSE @Now END,
    [i].[AS_NME_PositReliability],
    [i].[AS_GEO_ID],
    [i].[AS_GEO_AS_ID],
    [i].[AS_GEO_Longitude_Deg],
    [i].[AS_GEO_Latitude_Deg],
    CASE WHEN (UPDATE([AS_GEO_ChangedAt])) THEN [i].[AS_GEO_ChangedAt] ELSE @Now END,
    CASE WHEN (UPDATE([AS_GEO_PositedBy])) THEN [i].[AS_GEO_PositedBy] ELSE @PositedBy END,
    CASE WHEN (UPDATE([AS_GEO_PositedAt])) THEN [i].[AS_GEO_PositedAt] ELSE @Now END,
    [i].[AS_GEO_PositReliability],
    ROW_NUMBER() OVER (PARTITION BY [i].[AS_ID] ORDER BY [i].[AS_ID])
    FROM
    [inserted] [i]
    LEFT OUTER JOIN
    [org].[AS_Assets_Current] [c]
    ON [c].[AS_NME_Checksum] = BINARY_CHECKSUM([i].[AS_NME_AssetTag], [i].[AS_NME_AssetName], [i].[AS_NME_AssetDetail])
    LEFT OUTER JOIN
    [org].[AS_Assets_Current] [g]
    ON [g].[AS_GEO_Checksum] = BINARY_CHECKSUM([i].[AS_GEO_Longitude_Deg], [i].[AS_GEO_Latitude_Deg])
    WHERE
    [c].[AS_NME_AS_ID] IS NULL
    OR [g].[AS_NME_AS_ID] IS NULL;
    DECLARE @NME_Posit TABLE
    [AS_NME_ID] INT NOT NULL,
    [AS_NME_Checksum] INT NOT NULL,
    PRIMARY KEY([AS_NME_ID] ASC),
    UNIQUE CLUSTERED([AS_NME_Checksum] ASC)
    INSERT INTO [org].[AS_NME_Assets_Name_Posit]
    [AS_NME_AS_ID],
    [AS_NME_AssetTag],
    [AS_NME_AssetName],
    [AS_NME_AssetDetail],
    [AS_NME_ChangedAt]
    OUTPUT
    [inserted].[AS_NME_ID],
    [inserted].[AS_NME_Checksum]
    INTO
    @NME_Posit
    [AS_NME_ID],
    [AS_NME_Checksum]
    SELECT
    [i].[AS_NME_AS_ID],
    [i].[AS_NME_AssetTag],
    [i].[AS_NME_AssetName],
    [i].[AS_NME_AssetDetail],
    [i].[AS_NME_ChangedAt]
    FROM @Inserted [i];
    INSERT INTO [org].[AS_NME_Assets_Name_Annex]
    [AS_NME_ID],
    [AS_NME_PositedBy],
    [AS_NME_PositedAt],
    [AS_NME_PositReliability]
    SELECT
    [p].[AS_NME_ID],
    CASE WHEN (UPDATE([AS_NME_PositedBy])) THEN [i].[AS_NME_PositedBy] ELSE @PositedBy END,
    CASE WHEN (UPDATE([AS_NME_PositedAt])) THEN [i].[AS_NME_PositedAt] ELSE @Now END,
    [i].[AS_NME_PositReliability]
    FROM @Inserted [i]
    INNER JOIN
    @NME_Posit [p]
    ON [p].[AS_NME_Checksum] = [i].[AS_NME_Checksum];
    INSERT INTO [org].[AS_NME_Assets_Name_Annex]
    [AS_NME_ID],
    [AS_NME_PositedBy],
    [AS_NME_PositedAt],
    [AS_NME_PositReliability]
    SELECT
    [i].[AS_NME_ID],
    CASE WHEN (UPDATE([AS_NME_PositedBy])) THEN [i].[AS_NME_PositedBy] ELSE @PositedBy END,
    CASE WHEN (UPDATE([AS_NME_PositedAt])) THEN [i].[AS_NME_PositedAt] ELSE @Now END,
    [i].[AS_NME_PositReliability]
    FROM [inserted] [i]
    LEFT OUTER JOIN
    @NME_Posit [p]
    ON [p].[AS_NME_Checksum] = BINARY_CHECKSUM([i].[AS_NME_AssetTag])
    WHERE
    [p].[AS_NME_ID] IS NULL;
    DECLARE @GEO_Posit TABLE
    [AS_GEO_ID] INT NOT NULL,
    [AS_GEO_Checksum] INT NOT NULL,
    PRIMARY KEY([AS_GEO_ID] ASC),
    UNIQUE CLUSTERED([AS_GEO_Checksum] ASC)
    INSERT INTO [org].[AS_GEO_Assets_Location_Posit]
    [AS_GEO_AS_ID],
    [AS_GEO_Longitude_Deg],
    [AS_GEO_Latitude_Deg],
    [AS_GEO_ChangedAt]
    OUTPUT
    [inserted].[AS_GEO_ID],
    [inserted].[AS_GEO_Checksum]
    INTO
    @GEO_Posit
    [AS_GEO_ID],
    [AS_GEO_Checksum]
    SELECT
    [i].[AS_GEO_AS_ID],
    [i].[AS_GEO_Longitude_Deg],
    [i].[AS_GEO_Latitude_Deg],
    [i].[AS_GEO_ChangedAt]
    FROM @Inserted [i];
    INSERT INTO [org].[AS_GEO_Assets_Location_Annex]
    [AS_GEO_ID],
    [AS_GEO_PositedBy],
    [AS_GEO_PositedAt],
    [AS_GEO_PositReliability]
    SELECT
    [p].[AS_GEO_ID],
    CASE WHEN (UPDATE([AS_GEO_PositedBy])) THEN [i].[AS_GEO_PositedBy] ELSE @PositedBy END,
    CASE WHEN (UPDATE([AS_GEO_PositedAt])) THEN [i].[AS_GEO_PositedAt] ELSE @Now END,
    [i].[AS_GEO_PositReliability]
    FROM @Inserted [i]
    INNER JOIN
    @GEO_Posit [p]
    ON [p].[AS_GEO_Checksum] = [i].[AS_GEO_Checksum];
    INSERT INTO [org].[AS_GEO_Assets_Location_Annex]
    [AS_GEO_ID],
    [AS_GEO_PositedBy],
    [AS_GEO_PositedAt],
    [AS_GEO_PositReliability]
    SELECT
    [i].[AS_GEO_ID],
    CASE WHEN (UPDATE([AS_GEO_PositedBy])) THEN [i].[AS_GEO_PositedBy] ELSE @PositedBy END,
    CASE WHEN (UPDATE([AS_GEO_PositedAt])) THEN [i].[AS_GEO_PositedAt] ELSE @Now END,
    [i].[AS_GEO_PositReliability]
    FROM [inserted] [i]
    LEFT OUTER JOIN
    @GEO_Posit [p]
    ON [p].[AS_GEO_Checksum] = BINARY_CHECKSUM([i].[AS_GEO_Longitude_Deg], [i].[AS_GEO_Latitude_Deg])
    WHERE
    [p].[AS_GEO_ID] IS NULL;
    END;
    GO
    CREATE TRIGGER [org].[AS_Assets_Current_Delete]
    ON [org].[AS_Assets_Current]
    INSTEAD OF DELETE
    AS
    BEGIN
    SET NOCOUNT ON;
    DECLARE @Now DATETIMEOFFSET(7) = SYSDATETIMEOFFSET();
    DECLARE @PositedBy INT;
    EXECUTE @PositedBy = [posit].[Return_PositorId];
    INSERT INTO [org].[AS_NME_Assets_Name_Annex]
    [AS_NME_ID],
    [AS_NME_PositedBy],
    [AS_NME_PositedAt],
    [AS_NME_PositReliability]
    SELECT
    [d].[AS_NME_ID],
    @PositedBy,
    @Now,
    0
    FROM
    [deleted] [d];
    INSERT INTO [org].[AS_GEO_Assets_Location_Annex]
    [AS_GEO_ID],
    [AS_GEO_PositedBy],
    [AS_GEO_PositedAt],
    [AS_GEO_PositReliability]
    SELECT
    [d].[AS_GEO_ID],
    @PositedBy,
    @Now,
    0
    FROM
    [deleted] [d];
    END;
    GO
    INSERT INTO [org].[AS_Assets_Current]
    [AS].[AS_NME_AssetTag],
    [AS].[AS_NME_AssetName],
    [AS].[AS_NME_AssetDetail],
    [AS].[AS_GEO_Longitude_Deg],
    [AS].[AS_GEO_Latitude_Deg]
    SELECT
    [AS].[AS_NME_AssetTag],
    [AS].[AS_NME_AssetName],
    [AS].[AS_NME_AssetDetail],
    [AS].[AS_GEO_Longitude_Deg],
    [AS].[AS_GEO_Latitude_Deg]
    FROM (VALUES
    ('999', 'Dallas Test 3', 'Dallas Test 3', NULL, NULL, 'Plant', 'AUT', NULL)
    ) [AS]([AS_NME_AssetTag], [AS_NME_AssetName], [AS_NME_AssetDetail], [AS_GEO_Longitude_Deg], [AS_GEO_Latitude_Deg], [CN_ADD_Type_ID], [CN_ADD_Country_ID], [CN_ADD_State]);
    GO

  • How can I insert into a table other than the default table in a form

    Hi,
    I want to insert into a table with some fields value of a form of another table. I have written insert code On successful submission of that form, but after submit it gives the following error
    An unexpected error occurred: ORA-06502: PL/SQL: numeric or value error (WWV-16016)
    My code is like this
    declare
    l_trn_id number;
    l_provider_role varchar2(3);
    l_provider_id varchar2(10);
    begin
    l_trn_id := p_session.get_value_as_number(p_block_name=>'DEFAULT',p_attribute_name=>'A_TRANSACTION_ID');
    l_provider_id := p_session.get_value_as_varchar2(p_block_name=>'DEFAULT',p_attribute_name=>'A_PROVIDER1');
    l_PROVIDER_ROLE := p_session.get_value_as_varchar2(p_block_name=>'DEFAULT',p_attribute_name=>'A_PROVIDER_ROLE1');
    if (l_provider_role is not null) and (l_provider_id is not null) then
    insert into service_provider_trans_records(service_provider_id,transaction_id,role_type_id)
    values(l_provider_id, l_trn_id, l_provider_role);
    commit;
    end if;
    end;
    Where 'PROVIDER1' and 'PROVIDER_ROLE1' are not table fields.
    How can do that or why this error comes ? Any idea?
    Thanks
    Sumita

    Hi,
    When do you get this error? Is it while running or while creating the form.
    Here is a sample code which inserts a non-database column dummy into a table called dummy. This is done in successful procedure.
    declare
    l_dummy varchar2(1000);
    begin
    l_dummy := p_session.get_value_as_varchar2(p_block_name=>'DEFAULT',
    p_attribute_name=>'A_DUMMY');
    insert into sjayaram903_1g.dummy values(l_dummy);commit;
    end;
    Please check in your case if the size of the local variable is enough to hold the values being returned.
    Thanks,
    Sharmila

  • Splitting data in one row and inserting into seperate rows into a new table

    i have a table table1 like the following
    column1     column2          column3
    a     b,cbdm,d     hj
    ba     hello          man
    i have to insert data from this table to table2 in the following format
    Column1          column2          column3
    a          b          hj
    a          cbdm          hj
    a          d          hj
    ba          hello          man
    It is like if data in column2 is seperated by comma i have to insert it into each sperate row
    I have to write a single procedure which will do the above. Please,Please help me
    I am very new to SQL

    I am absolutely agreeing with Billy.
    But I think u will be able to learn a lot if u try to understand the following code
    sql>select * from t1;
         C1 C2 C3
         a  b,cdbm,d  hj 
         ba  hello  man 
         x  aa,d,df  yy 
    sql>select * from t2;
         no rows selected
    sql>insert into t2
          select col1,col2,col3
         from (
         select t1.c1 col1,t1.c2 col4,t1.c3 col3,
         substr(t1.c2,
                  case n when 1 then 1 else instr(t1.c2,',',1,n-1)+1 end,
                       (case instr(t1.c2,',',1,n) when 0 then length(t1.c2)+1 else instr(t1.c2,',',1,n) end) -
                      (case n when 1 then 1 else instr(t1.c2,',',1,n-1)+1 end)) col2,n
           from t1,(select c1,c2,c3,
                              row_number() over(partition by c1,c2,c3 order by c1) n
                      from t1 connect by level <=
                                                   (length(t1.c2)-length(replace(t1.c2,',','')))) t2
           where t1.c1 = t2.c1)
         where not(n != 1 and col4=col2);
    sql>select * from t2;
          C1 C2 C3
          a  b  hj 
          a  cdbm  hj 
          a  d  hj 
          ba  hello  man 
          x  aa  yy 
          x  d  yy 
          x  df  yy                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

  • T-SQL - PL/SQL conversion: insert into ... select problem

    Hi!
    If I have an insert into ... select statement in T-SQL without a 'from' clause:
    bq. insert into sometable (foo, bar) \\ select 5, case when @bar=5 then 4 else 3 end
    its translation is:
    bq. INSERT INTO sometable \\ ( foo, bar ) \\ VALUES ( 5, CASE \\ WHEN 5 = 5 THEN 4 \\ ELSE 3 \\ END col );
    and I got: ORA-00917: "missing comma" for that. I'm not sure if it's a bug in the migration code, or is there a trick so that I can use 'CASE' in an insert into .. values statement somehow?
    Zoli

    You have to remove the column name. I just simplified your test case to check and it's working:
    CREATE TABLE test(
      id NUMBER
    INSERT
      INTO test(id)
      VALUES(CASE WHEN 5=5 THEN 4 ELSE 3 END)
    SELECT *
      FROM test
    ;C.

  • Insert into using a select and dynamic sql

    Hi,
    I've got hopefully easy question. I have a procedure that updates 3 tables with 3 different update statements. The procedure goes through and updates through ranges I pass in. I am hoping to create another table which will pass in those updates as an insert statement and append the data on to the existing data.
    I am thinking of using dynamic sql, but I am sure there is an easy way to do it using PL/SQL as well. I have pasted the procedure below, and what I'm thinking would be a good way to do it. Below I have pasted my procedure and the bottom is the insert statement I want to use. I am faily sure I can do it using dynamic SQL, but I am not familiar with the syntax.
    CREATE OR REPLACE PROCEDURE ACTIVATE_PHONE_CARDS (min_login in VARCHAR2, max_login in VARCHAR2, vperc in VARCHAR2) IS
    BEGIN
    UPDATE service_t SET status = 10100
    WHERE poid_id0 in
    (SELECT poid_id0 FROM service_t
    WHERE poid_type='/service/telephony'
    AND login >= min_login AND login <= max_login);
    DBMS_OUTPUT.put_line( 'Service Status:' || sql%rowcount);
    UPDATE account_t SET status = 10100
    WHERE poid_id0 IN
    (SELECT account_obj_id0 FROM service_t
    WHERE poid_type = '/service/telephony'
    AND login >= min_login AND login <= max_login);
    DBMS_OUTPUT.put_line( 'Account Status:' || sql%rowcount);
    UPDATE account_nameinfo_t SET title=Initcap(vperc)
    WHERE obj_id0 IN
    (SELECT account_obj_id0 FROM service_t
    WHERE poid_type='/service/telephony'
    AND login >=min_login AND login <= max_login);
    DBMS_OUTPUT.put_line('Job Title:' || sql%rowcount);
    INSERT INTO phone_card_activation values which = 'select a.status, s.status, s.login, to_char(d.sysdate,DD-MON-YYYY), ani.title
    from account_t a, service_t s, account_nameinfo_t ani, dual d
    where service_t.login between service_t.min_login and service_t.max_login
    and ani.for_key=a.pri_key
    and s.for_key=a.pri_key;'
    END;
    Thanks for any advice, and have a good weekend.
    Geordie

    Correct my if I am wrong but aren't these equal?
    UPDATE service_t SET status = 10100
    WHERE poid_id0 in
    (SELECT poid_id0 FROM service_t
    WHERE poid_type='/service/telephony'
    AND login >= min_login AND login <= max_login);
    (update all the records where there id is in the sub-query that meet the WHERE Clause)
    AND
    UPDATE service_t SET status = 10100
    WHERE poid_type='/service/telephony'
    AND login >= min_login AND login <= max_login);
    (update all the records that meet the WHERE Clause)
    This should equate to the same record set, in which case the second update would be quicker without the sub-query.

  • Float truncated when inserted into table

    Hi,
    I'm trying to insert float values into a table with a column of type NUMBER(4,2) and the values are getting truncated to integer values. My environment is :
    Database: Oracle8i Enterprise Edition Release 8.1.7.0.0 - Production
    jdbc lib: 9.0.1.4
    I tried it with the jdbc lib version 9.0.1 and it works, but I didn't figure a way to use it, since I'm using it in a 9iAS environment and there's no driver classes12dms.jar for this version. If I try to use the classes12.zip version I get an exception when trying to access the database complaining about some classes missing (These classes missing are in the classes12dms.jar)
    Here's a simple test case:
    import java.sql.*;
    public class TesteFloat {
    public TesteFloat() {
    public static void main(String[] args) {
    Connection con = null;
    PreparedStatement stmt = null;
    try {
    Class.forName("oracle.jdbc.driver.OracleDriver");
    con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:hal9000","kitbordo","kitbordo");
    stmt = con.prepareStatement("insert into teste (teste) values (?)");
    for (int i = 0; i < 10; i++) {
    stmt.setFloat(1,2.32f * i );
    //stmt.addBatch();
    stmt.executeUpdate();
    //stmt.executeBatch();
    con.commit();
    } catch (Exception e) {
    e.printStackTrace();
    } finally {
    if (con != null) {
    try {
    con.close();
    } catch (SQLException e) {
    CREATE TABLE TESTE
    TESTE NUMBER(4,2)
    )

    Hi,
    I'm trying to insert float values into a table with a column of type NUMBER(4,2) and the values are getting truncated to integer values. My environment is :
    Database: Oracle8i Enterprise Edition Release 8.1.7.0.0 - Production
    jdbc lib: 9.0.1.4
    I tried it with the jdbc lib version 9.0.1 and it works, but I didn't figure a way to use it, since I'm using it in a 9iAS environment and there's no driver classes12dms.jar for this version. If I try to use the classes12.zip version I get an exception when trying to access the database complaining about some classes missing (These classes missing are in the classes12dms.jar)
    Here's a simple test case:
    import java.sql.*;
    public class TesteFloat {
    public TesteFloat() {
    public static void main(String[] args) {
    Connection con = null;
    PreparedStatement stmt = null;
    try {
    Class.forName("oracle.jdbc.driver.OracleDriver");
    con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:hal9000","kitbordo","kitbordo");
    stmt = con.prepareStatement("insert into teste (teste) values (?)");
    for (int i = 0; i < 10; i++) {
    stmt.setFloat(1,2.32f * i );
    //stmt.addBatch();
    stmt.executeUpdate();
    //stmt.executeBatch();
    con.commit();
    } catch (Exception e) {
    e.printStackTrace();
    } finally {
    if (con != null) {
    try {
    con.close();
    } catch (SQLException e) {
    CREATE TABLE TESTE
    TESTE NUMBER(4,2)
    )

  • How can I insert into a view cache???

    I have a problem in JClient like that:
    A form that have 3 JTable:
    - Table1 get data from UserTable in DB
    - Table2 get data from GroupTable in DB
    - Table3 get data from GroupTable in DB
    and 2 buttons: btnAdd, btnRemove.
    When I select a row in Table1, Table2 will get all records in GroupTable that row in Table1 belonging to and Table3 will get all records in GroupTable that row in Table1 not belonging to.
    I want that when I select some rows in Table3 and click btnAdd then those rows in Table3 will be insert into Table2 and remove those rows from Table3. And when I select some rows in Table2 and click btnRemove then those rows in Table2 will be insert into Table3 and remove those rows from Table2.
    Can you show me how to show that problem.
    Thanks.

    Hi,
    When do you get this error? Is it while running or while creating the form.
    Here is a sample code which inserts a non-database column dummy into a table called dummy. This is done in successful procedure.
    declare
    l_dummy varchar2(1000);
    begin
    l_dummy := p_session.get_value_as_varchar2(p_block_name=>'DEFAULT',
    p_attribute_name=>'A_DUMMY');
    insert into sjayaram903_1g.dummy values(l_dummy);commit;
    end;
    Please check in your case if the size of the local variable is enough to hold the values being returned.
    Thanks,
    Sharmila

  • Pl/sql block with "insert into" and schema qualified table throws "error"

    Simplified test case:
    Oracle9i EE Release 9.2.0.3.0
    Oracle JDeveloper 9.0.3.2 Build 1145
    create user u1 identified by u1
    default tablespace users
    quota unlimited on users;
    grant connect, resource to u1;
    revoke unlimited tablespace from u1;
    create user u2 identified by u2
    default tablespace users
    quota unlimited on users;
    grant connect, resource to u2;
    revoke unlimited tablespace from u2;
    As user u2:
    create table u2.t
    c1 number
    grant select, update, insert, delete on u2.t to u1;
    As user u1:
    create or replace package test_pkg as
    procedure do_insert (p_in number);
    end;
    create or replace package body test_pkg as
    procedure do_insert (p_in number) is
    begin
    insert into u2.t values (p_in);
    commit;
    end;
    end;
    All of the above works fine using command-line sql*plus, and is clearly a simplified version of the actual code to demonstrate the issue at hand. Using JDeveloper, it complains about 'expected ;' at the 'values' keyword in the insert statement. Removing the schema qualification from the table name allows JDeveloper to not flag the statement as an error, but I do not want to create synonyms (private or public) to point to all the tables in the real packages. Since JDeveloper flags the insert statement as an error, I can not browse the package structure etc, even though it compiles with no problems. What gives?
    Thanks in advance for any suggestions, etc...

    Hi Bryan,
    Thanks for following up on this. I will look for the bug fix to be published.
    - Mark

  • Insert into - commit_form - records disapears

    Hi folks;
    I 'm working with Forms6i and Oracle 9i r2 database.
    I got a problem on a table T_ENV, it's an session environnement table where I store informations about the login_name, id_session and else...
    When a user is log to the application (trigger WHEN_NEW_FORM_INSTANCE) , a new row is created in T_ENV.
    GO_BLOCK ('T_ENV');
    select userenv('sessionid') into :ENV_SESSION.ID_SESSION;
    select sysdate into :env_session.date_session
    :env_session.nom_login := :global.nom_user;
    insert into T_ENV values (:env_session.id_session,:env_session.date_session,etc...);
    commit_form;I have research all the "CLEAR_BLOCK('T_ENV')" and "DELETE FROM T_ENV" commands and comments it.
    So I want to know why, after 10-15 minutes, the new records in the table T_ENV disappears (were deleted) ?
    Is it done by a paramter of the database ?
    How to debug the "commit_form" command ?
    Thanks for any help, regards.

    Yes, I meant "insert into" statement in your when-new-form-instance trigger.
    You have block T_ENV. If this block based on t_env table and you assign values to the items of this block, forms will issue additional insert statement during commit.
    But.., if you can see record from another session - this is not the case. Most likely you are deleting record somewhere else.
    Search forms for "delete_record" or "delete from t_env".
    May be you have some database job, or database trigger that deletes from t_env

Maybe you are looking for