Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7494747
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T17:45:37+00:00 2026-05-29T17:45:37+00:00

I have an application where two column could be populated from a number of

  • 0

I have an application where two column could be populated from a number of different applications; one, the XY location (stgeometry) is determined by the application: two, the XY location is typed by the user into separate x and y columns.

I’ve come up with the following trigger to populate the X and Y column in scenario one, and to populate the shape column in scenario 2. It seems to be looping and not populating either column. Where did I go wrong?

    ALTER trigger [dbo].[update_location_geometry]
    --Trigger to allow non-gis input of XY data in X_Coord and Y_Coord columns, yet still populate the WKB
    --shape column with coordinate using 26917 EPSG so that new location will be available to the gis without
    -- additional user intervention
    on [dbo].[TBL_LOCATIONS]
    instead of insert as
    begin
--this block populates stgeometry.shape col with user-input XY and is not using the application to
-- determine xy
    if exists (select top 1 * from inserted where shape is not null)

    update dbo.tbl_locations
    set SHAPE=Geometry::STPointFromText('POINT(' + CAST(X_Coord AS VARCHAR(20)) + ' ' + 
                        CAST(Y_Coord AS VARCHAR(20)) + ')', 26917)
    WHERE (UPDATE(X_Coord) OR UPDATE(Y_Coord))
                 AND objectId IN (SELECT ins.objectId FROM inserted ins) 


  --this block populates the XY column from stgeometry.shape when the application calculated XY   
    update dbo.tbl_locations
    set X_Coord = SHAPE.STX,Y_Coord = SHAPE.STY 
    WHERE (UPDATE(Shape))
                 AND objectId IN (SELECT ins.objectId FROM inserted ins)
    End

Tried this as well, still looping:

  ALTER trigger [dbo].[update_location_geometry]
    --Trigger to allow non-gis input of XY data in X_Coord and Y_Coord columns, yet still populate the WKB
    --shape column with coordinate using 26917 EPSG so that new location will be available to the gis without
    -- additional user intervention
    on [dbo].[TBL_LOCATIONS]
    instead of insert as -- Aaron changed update to insert here


IF UPDATE(X_Coord) OR UPDATE(Y_Coord)

Begin
update dbo.tbl_locations
set SHAPE=Geometry::STPointFromText('POINT(' + CAST(X_Coord AS VARCHAR(20)) + ' ' + 
CAST(Y_Coord AS VARCHAR(20)) + ')', 26917)

END
IF UPDATE(Shape) 
Begin
update dbo.tbl_locations
set X_Coord = SHAPE.STX,Y_Coord = SHAPE.STY 

End

And this produces syntax error:

instead of update as

IF update (X_Coord) OR UPDATE(Y_Coord) AND objectId IN (SELECT ins.objectId FROM inserted ins)

Begin
update dbo.tbl_locations
set SHAPE=Geometry::STPointFromText('POINT(' + CAST(X_Coord AS VARCHAR(20)) + ' ' + 
CAST(Y_Coord AS VARCHAR(20)) + ')', 26917)

END
IF UPDATE(Shape)AND objectId IN (SELECT ins.objectId FROM inserted ins)
Begin
update dbo.tbl_locations
set X_Coord = SHAPE.STX,Y_Coord = SHAPE.STY 

End


Msg 207, Level 16, State 1, Procedure update_location_geometry, Line 10
Invalid column name 'objectId'.
Msg 207, Level 16, State 1, Procedure update_location_geometry, Line 18
Invalid column name 'objectId'.

Final Solution:

ALTER TRIGGER [dbo].[update_location_geometry] ON [dbo].[TBL_LOCATIONS] 
INSTEAD OF INSERT 
AS BEGIN     
IF EXISTS (SELECT 1 FROM inserted WHERE SHAPE IS NOT NULL)     
BEGIN         
INSERT dbo.TBL_Locations(SHAPE, X_Coord, Y_Coord,objectid, loc_name)             
SELECT SHAPE, X_Coord = SHAPE.STX,Y_Coord = SHAPE.STY,objectid, loc_name            
FROM inserted;     
END     
ELSE     
BEGIN         
INSERT dbo.TBL_Locations(SHAPE, X_Coord, Y_Coord,objectid,loc_name)             
SELECT SHAPE=Geometry::STPointFromText('POINT('
+ CAST(X_Coord AS VARCHAR(20)) + ' ' 
+ CAST(Y_Coord AS VARCHAR(20)) + ')', 26917),
X_Coord, Y_Coord,objectid,loc_name
FROM inserted;
END
END

Example of insert:

INSERT INTO [ATBI].[dbo].[TBL_LOCATIONS]
           ([OBJECTID]
           ,[X_Coord]
           ,[Y_Coord]
           ,Loc_Name,
           Notes)

     VALUES
           (368
           ,324999.997
           ,3970798.079
           ,'blahblah'
           ,'notes of test')

Production table:

CREATE TABLE [dbo].[TBL_LOCATIONS](
    [OBJECTID] [int] NOT NULL,
    [FCategory] [nvarchar](16) NULL,
    [MapMethod] [nvarchar](4) NULL,
    [HError] [nvarchar](50) NULL,
    [MapSource] [nvarchar](255) NULL,
    [SourceDate] [datetime2](7) NULL,
    [EditDate] [datetime2](7) NULL,
    [Notes] [nvarchar](255) NULL,
    [Site_ID] [uniqueidentifier] NULL,
    [Meta_MID] [nvarchar](50) NULL,
    [X_Coord] [numeric](38, 8) NULL,
    [Y_Coord] [numeric](38, 8) NULL,
    [Coord_Units] [nvarchar](50) NULL,
    [Coord_System] [nvarchar](50) NULL,
    [UTM_Zone] [nvarchar](50) NULL,
    [Accuracy_Notes] [nvarchar](max) NULL,
    [Unit_Code] [nvarchar](12) NULL,
    [Loc_Name] [nvarchar](100) NULL,
    [Loc_Type] [nvarchar](25) NULL,
    [Updated_Date] [nvarchar](50) NULL,
    [Loc_Notes] [nvarchar](max) NULL,
    [Datum] [nvarchar](5) NULL,
    [Watershed] [nvarchar](50) NULL,
    [StreamName] [nvarchar](50) NULL,
    [NHDReachCode] [nvarchar](14) NULL,
    [TOPO_NAME] [nvarchar](50) NULL,
    [Trail] [nvarchar](100) NULL,
    [Road] [nvarchar](50) NULL,
    [Elevation] [numeric](38, 8) NULL,
    [LAT] [numeric](38, 8) NULL,
    [LON] [numeric](38, 8) NULL,
    [Year_] [nvarchar](4) NULL,
    [County] [nvarchar](30) NULL,
    [State] [nvarchar](30) NULL,
    [IsExtant] [nvarchar](3) NULL,
    [IsSenstive] [nvarchar](3) NULL,
    [Eco_Notes] [nvarchar](50) NULL,
    [EcoGroup] [nvarchar](50) NULL,
    [ELCode] [smallint] NULL,
    [Validation] [nvarchar](50) NULL,
    [LocationDescription] [nvarchar](max) NULL,
    [LocationDirections] [nvarchar](max) NULL,
    [VerbatimLocation] [nvarchar](255) NULL,
    [PlaceName] [nvarchar](75) NULL,
    [SHAPE] [geometry] NULL,
    [Location_ID] [uniqueidentifier] NOT NULL,
 CONSTRAINT [PK_TBL_LOCATIONS] PRIMARY KEY CLUSTERED 
(
    [OBJECTID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

ALTER TABLE [dbo].[TBL_LOCATIONS]  WITH CHECK ADD  CONSTRAINT [g2_ck] CHECK  (([SHAPE].[STSrid]=(26917)))
GO

ALTER TABLE [dbo].[TBL_LOCATIONS] CHECK CONSTRAINT [g2_ck]
GO

ALTER TABLE [dbo].[TBL_LOCATIONS] ADD  CONSTRAINT [DF__TBL_LOCAT__Globa__12C8C788]  DEFAULT (newsequentialid()) FOR [Location_ID]
GO
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T17:45:39+00:00Added an answer on May 29, 2026 at 5:45 pm

    In an insert trigger, does checking UPDATE make sense? Did you mean to make it an INSTEAD OF UPDATE trigger instead of INSTEAD OF INSERT?

    Also, assuming it should be an UPDATE trigger, I would usually check this way as opposed to the WHERE clause:

    IF UPDATE(X_Coord) OR UPDATE(Y_Coord)
    BEGIN
        -- first update without first where clause
    END
    
    IF UPDATE(Shape)
    BEGIN
        -- second update without first where clause
    END
    

    Ok so I was stuck on the fact that you were trying to use an instead of insert trigger for an update. The problem is that an instead of trigger requires you to still perform the insert operation yourself, because the trigger is running instead of the insert. Try this (note that if there are other columns being inserted along with SHAPE or X_Coord/Y_Coord, you’ll need to pull those from inserted as well):

    CREATE TRIGGER [dbo].[update_location_geometry]
    ON [dbo].[TBL_LOCATIONS]
    INSTEAD OF INSERT
    AS
    BEGIN
        INSERT dbo.TBL_Locations(ObjectID, SHAPE, X_Coord, Y_Coord)
            SELECT ObjectID, SHAPE, X_Coord = SHAPE.STX, Y_Coord = SHAPE.STY 
            FROM inserted WHERE SHAPE IS NOT NULL;
    
        INSERT dbo.TBL_Locations(ObjectID, SHAPE, X_Coord, Y_Coord)
            SELECT ObjectID, SHAPE=Geometry::STPointFromText('POINT(' 
              + CAST(X_Coord AS VARCHAR(20)) + ' ' 
              + CAST(Y_Coord AS VARCHAR(20)) + ')', 26917), 
              X_Coord, Y_Coord 
            FROM inserted WHERE SHAPE IS NULL;
    END
    GO
    

    I tried this with the following INSERT statements and they populated the table just fine:

    INSERT dbo.TBL_Locations(ObjectID, X_Coord, Y_Coord) 
        SELECT 1, 20, 30;
    
    INSERT dbo.TBL_Locations(ObjectID, Shape) 
        SELECT 2, 0x25690000010C00000000000034400000000000003E30;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application in which I'm currently using a two column table in
We have two tables in our application that both have a ShowOrder column. We
I have an application that has two threads. The first one (the main thread)
I have application in VB.net that have two different form (Form1 and Form2). Now
The issue at hand is this. We have a web application with two different
I have two windows application, one is a windows service which create EventWaitHandle and
I have two application that need to talk to each other. App1 needs to
I have a web application and two class files, First class is MyClass.class which
I have a web application containing two web services, let's say PublicHello.asmx and RestrictedHello.asmx
Is tight looping in a program bad? I have an application that has two

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.