Solved

SQL Loop help (temp tables etc)

Posted on 2013-01-17
3
196 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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
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.

867 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

18 Experts available now in Live!

Get 1:1 Help Now