Solved

SQL Loop help (temp tables etc)

Posted on 2013-01-17
3
192 Views
Last Modified: 2013-01-18
I have a main table and a small lookup table.

Im trying to get the ids from my streets table to my benchmarks table.

So in streets I have 2 columns.

Id
Streetname

In Benchmarks I have a streetid field that I want to contain the value of the Id column in streets table.  I need to use Streets.Streetname and Benchmarks.Street1 as the where.

So in psuedo code:  
update dbo.benchmarks set dbo.benchmkarks.street1id = dbo.streets.id where dbo.benchmarks.street1id = dbo.streets.streetname.

Obviously this will take a loop to read in all the rows in streets and then do the processing unfortunately ive never grasped fully how to get this right in sql.

Thanks in advance and please view the attachment that shows my columns.

my db schema
0
Comment
Question by:rochestermn
3 Comments
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 38789749
update dbo.benchmarks
set dbo.benchmkarks.street1id = dbo.streets.id
FROM dbo.streets
where dbo.benchmarks.street1id = dbo.streets.streetname.
0
 
LVL 39

Accepted Solution

by:
appari earned 500 total points
ID: 38790074
try this

update A
set street1id = B.id
FROM dbo.benchmarks  A join  dbo.streets B
on A.street1 = B.streetname
0
 

Author Closing Comment

by:rochestermn
ID: 38793314
Worked perfectly!  Guess I didnt need a temp table after all.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

747 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

12 Experts available now in Live!

Get 1:1 Help Now