2

Sorry for the long title.

I have a statement which needs to grab all the columns from one row from BinConfig:

SELECT * 
FROM BinConfig WITH(NOLOCK) 
WHERE IssuerKey = @IssuerKey

But I also need to grab a single column from one row from CardRangeGroup also based on that IssuerKey column.

What I've tried:

SELECT 
    BinConfig.*, CardRangeGroup.Name 
FROM 
    BinConfig 
JOIN 
    CardRangeGroup WITH(NOLOCK) 
WHERE 
    @IssuerKey = BinConfig.IssuerKey 
    AND @IssuerKey = CardRangeGroup.IssuerKey

Which gives me a syntax error near WHERE. I've tried to find resources online, but everywhere I look I can't find anything explaining how to select rows based on a passed in variable. Any help?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Toby Caulk
  • 266
  • 1
  • 6
  • 21
  • 1
    You're missing the `ON` portion of the `JOIN` statement, SQL needs to know how the tables connect! – alybaba726 Jul 02 '15 at 16:01
  • 1
    if this is MySQL then WITH(NOLOCK) is not supported. See this for equivalent http://itecsoftware.com/with-nolock-table-hint-equivalent-for-mysql – PaulF Jul 02 '15 at 16:03

2 Answers2

5

You need to specify how the tables should be joined. Try this:

SELECT BinConfig.*, CardRangeGroup.Name 
FROM BinConfig 
JOIN CardRangeGroup ON BinConfig.IssuerKey = CardRangeGroup.IssuerKey
WHERE @IssuerKey = CardRangeGroup.IssuerKey

The with(nolock) might not be needed (or a good idea) so I removed it.

jpw
  • 44,361
  • 6
  • 66
  • 86
  • 1
    I don't know how I forgot that, I'm fairly new to SQL and just learning about Join statements. Thank you! – Toby Caulk Jul 02 '15 at 16:04
  • Also, I tagged the question wrong. This is actually SQL-Server, is the `with(nolock)` function still supported? – Toby Caulk Jul 02 '15 at 16:05
  • @TobyCaulk Yes, but it might be bad to use though, see http://stackoverflow.com/questions/686724/sql-server-when-should-you-use-with-nolock and http://blogs.sqlsentry.com/aaronbertrand/bad-habits-nolock-everywhere/ – jpw Jul 02 '15 at 16:07
  • I've heard how bad it can be, but I was told to use it so I still have to. Thank you for warning me though! – Toby Caulk Jul 02 '15 at 16:09
1

try this , you don't need to use where

 SELECT BinConfig.*, CardRangeGroup.Name FROM BinConfig JOIN   
 CardRangeGroup
 ON CardRangeGroup.IssuerKey = BinConfig.IssuerKey AND @IssuerKey = CardRangeGroup.IssuerKey
Karthik
  • 4,950
  • 6
  • 35
  • 65