Solved

UPDATE from a SELECT

Posted on 2006-11-21
1
269 Views
Last Modified: 2008-02-07
Hi,

I want to create T-SQL stored procedure that will do the following task :

I have 2 tables. A and B. A and B have 4 fields in common. The fields are named F1, F2, F3 and F4. B is the reference table that contain an ID (say K1 as the field name) and the 4 fields (Fx). A is the data table that contains both the foreign key K1 and also the fields (Fx). So basically, I can do a select on table A and get by using relatinal data the related value of F1, F2, F3 and F4 using the foreign key of A.K1 and primary key of B.K1. But B is so big (millions records), I want to remove the relational for specific queries that has to be very fast and a so select on one table only (A). So periodically, I want to execute a stored procedure that will take the data from B and put them in A using both a SELECT (to get the related data) and an UPDATE (to update the table A).

Look this pseudo code, I want this in TSQL

ARECORDSTOBEUPDATED = SELECT ALL RECORDS FROM A THAT DO NOT CONTAIN F1 VALUE (this means we did not normalized it yet)

( FOREACH MAINDATAROW IN ARECORDSTOBEUPDATED DO:

  REFERENCEROW =  SELECT TOP 1 FROM B WHERE B.K1 = MAINDATAROW .K1

 UPDATE MAINDATAROW WITH REFERENCEROW DATA (F1, F2, F3, F4)
)

This is quite simple, but I really don't know how to achieve this.

Thanks a lot in advance.
0
Comment
Question by:pmengal
1 Comment
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 250 total points
Comment Utility
update a
  SET F1 = B.F1, F2 = B.F2, F3 = B.F3, F4 = B.F4
from A
join B
  on A.K1 = B.K1
 and A.F1 IS NULL
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now