Update existing table with query results SQL Server 2012

elseflyer

I am attempting to add the result of my query into the column of an existing table. Thus far the query below finds the nearest electric substation within 30 kms of an electric line.

Select el1.id, 
   el1.geog4269.STAsText(), 
   (Select TOP 1 es.name from Test.dbo.electric_substations as es
    with (index(sidx_es))
    WHERE el1.geog4269.STDistance(es.geog) < 30000
    order by el1.geog4269.STDistance(es.geog)
    )As NearestElectricSubstation
    from Test.dbo.electric_lines AS el1;

Now what I want to do is update a table called NNElines which has the following schema:

CREATE TABLE NNElines
(  
id INT NOT NULL PRIMARY KEY,
Location geography NOT NULL,
Nearest_Esub varchar(50) NOT NULL
);

I want to update id with el1.id from the results, location with el1.geog4269.STAsText(), and Nearest_Esub with NearestElectricSubstation. I am trying a Update query but not getting anything. Any help is appreciated. Thank you

 Update Test.dbo.NNElines
SET id = el1.id,
 Location = el1.geog4269.STAsText()
From(
Select 
fnc.el1.id, 
fnc. el1.geog4269.STAsText()
From Test.dbo.electric_lines AS el1
   CROSS APPLY 
   (Select TOP 1 es.name from Test.dbo.electric_substations as es
    with (index(sidx_es))
    WHERE el1.geog4269.STDistance(es.geog) < 30000
    order by el1.geog4269.STDistance(es.geog)
    ) fnc
    --As NearestElectricSubstation
    --from Test.dbo.electric_lines AS el1;
    );
SQLChao

Try this. You can use JOINs when updating tables. So I joined your query to the NNElines table on ID and updates the NNElines table with the corresponding values from your query.

UPDATE NNElines
SET a.location = b.geog4269.STAsText(),
  a.Nearest_Esub = b.NearestElectricSubstation
FROM NNElines a
JOIN
  (Select el1.id, 
    el1.geog4269.STAsText(), 
    (Select TOP 1 es.name from Test.dbo.electric_substations as es with (index(sidx_es))
       WHERE el1.geog4269.STDistance(es.geog) < 30000
       order by el1.geog4269.STDistance(es.geog)
    )As NearestElectricSubstation
  from Test.dbo.electric_lines AS el1) b 
ON a.id = b.id

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Update existing table with query results SQL Server 2012

From Dev

SQL Server 2012 - Two Table query

From Dev

update existing column with results of select query using sql

From Dev

SQL Server 2012 Update table rate based on rate in table 2

From Dev

SQL Server 2012 Query

From Dev

SQL (Server): Copy query result in existing table with structure of query result

From Dev

SQL Server 2012 Results Grid

From Dev

Hibernate with MS SQL Server 2012 issue with table locks and parallel query

From Dev

sql query - update fields in existing with record values in the same table

From Dev

How to create a new table from existing table with both keys and data in SQL Server 2012 (Management Studio)?

From Dev

Optimizing SQL Server 2012 Query

From Dev

SQL Server 2012 Query Confusion

From Dev

SQL Server 2012 LIMITing query

From Dev

Optimizing SQL Server 2012 Query

From Dev

SQL Server 2012 Query Confusion

From Dev

SQL Server 2012 query / Job

From Dev

Update table from query results

From Dev

Sql Server 2012 (T-SQL) String Comparison (for equality) gives asterisk in query results

From Dev

Index for table in SQL Server 2012

From Dev

Display Table Name in Query Results; SQL Server 2008

From Dev

Update table sql query

From Dev

Source data type "200" not found error when exporting query results to excel Microsoft SQL Server 2012

From Dev

SQL Server 2012 : full-text query has results for compound word but not for compounds (separate terms)

From Dev

SQL Server 2012: Select results not equal to string

From Dev

Update query updates table and not query results

From Dev

SQL Server Update Query says table already exits

From Dev

Update query with a third table involved in SQL Server 2016

From Dev

SQL Query tuning - MS SQL Server -2012

From Dev

SQL query results into table in Python

Related Related

  1. 1

    Update existing table with query results SQL Server 2012

  2. 2

    SQL Server 2012 - Two Table query

  3. 3

    update existing column with results of select query using sql

  4. 4

    SQL Server 2012 Update table rate based on rate in table 2

  5. 5

    SQL Server 2012 Query

  6. 6

    SQL (Server): Copy query result in existing table with structure of query result

  7. 7

    SQL Server 2012 Results Grid

  8. 8

    Hibernate with MS SQL Server 2012 issue with table locks and parallel query

  9. 9

    sql query - update fields in existing with record values in the same table

  10. 10

    How to create a new table from existing table with both keys and data in SQL Server 2012 (Management Studio)?

  11. 11

    Optimizing SQL Server 2012 Query

  12. 12

    SQL Server 2012 Query Confusion

  13. 13

    SQL Server 2012 LIMITing query

  14. 14

    Optimizing SQL Server 2012 Query

  15. 15

    SQL Server 2012 Query Confusion

  16. 16

    SQL Server 2012 query / Job

  17. 17

    Update table from query results

  18. 18

    Sql Server 2012 (T-SQL) String Comparison (for equality) gives asterisk in query results

  19. 19

    Index for table in SQL Server 2012

  20. 20

    Display Table Name in Query Results; SQL Server 2008

  21. 21

    Update table sql query

  22. 22

    Source data type "200" not found error when exporting query results to excel Microsoft SQL Server 2012

  23. 23

    SQL Server 2012 : full-text query has results for compound word but not for compounds (separate terms)

  24. 24

    SQL Server 2012: Select results not equal to string

  25. 25

    Update query updates table and not query results

  26. 26

    SQL Server Update Query says table already exits

  27. 27

    Update query with a third table involved in SQL Server 2016

  28. 28

    SQL Query tuning - MS SQL Server -2012

  29. 29

    SQL query results into table in Python

HotTag

Archive