Solved

SQL Loop help (temp tables etc)

Posted on 2013-01-17
3
203 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how the fundamental information of how to create a table.

691 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