TwelvestoneBack End

SQL Embedded Query


Sign in

  • Waiting for Godot ( 730 k posts )
    Just conversation.
  • Thunder Dome ( 23 k posts )
    Photoshop Tennis and Collabs.
  • Photography ( 5.1 k posts )
    For all you shutterbugs, sh...
  • Flash ( 18 k posts )
    ActionScripting to tweens, ...
  • Front End ( 5.9 k posts )
    general front end design an...
  • Back End ( 9.7 k posts )
    serverside scripting, progr...
  • Projects and Theory ( 12 k posts )
    This forum is for discussio...
  • FAQ ( 269 posts )
    All those nagging questions...
  • Design ( 17 k posts )
    graphics & all aspects of g...
  • Purgatory ( 3.6 k posts )
    12stone Jail, feel free to ...
JimmyTheGent
 
2012-01-15

Hey folks

I am having a problem getting my head around an embedded query, any suggestions on how to achieve the desired results.

I have a temp table in a stored procedure that is populated from another database table. However there is info missing and the temp table is left with a string 'TBC' in one of the fields (GT). So I am trying to write a query that can lookup the correct GT Code using a field in the temp table.

There is a lookup table, that has postcode as a PK and the needed code as a column. SO I tried looking up based on the postcode. The problem is my embedded select brings back all fields where posts codes match. Is there any way to make it match just the 1 row at a time without a loop? Currently I get an error due to too many results returned from the subquery. each row will have a postcode to match and a GT code to find.

Hope I explained that alrigtht

[code]

UPDATE #tmptable SET GT=(SELECT l.gtCode from LookupTable l JOIN #tmptable t ON t.postcode = l.postcode WHERE Postcode=PostCode

[/code]

Stinky
 
2012-01-15

Good to see you got back in k

FYI, we're using markdown instead of BBCode these days. Also, if you want to get fancy with the code, you can embed gists in your posts like so:

https://gist.github.com/1613768

JimmyTheGent
 
2012-01-16

Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

I have changed the postcode column on the lookup table to pcode to try and avoid confusion. removing the WHERE clause in the embedded select didnt work, it still matches too many results.

The lookup table postcode are the first 4 characters, for example EH10, EH11, GL10 etc and I am matching them to LEFT(Postcode, 4) which should return the same, problem is it will match ever row in the table in the embedded select like that. I am thinking it might be easier to do in a loop with VB rather than have the SQL do the work...

Sorry, you must be a member to post to a conversation. Either log in or sign up to get involved.
TwelvestoneBack End

SQL Embedded Query