简体   繁体   中英

Adding a Where Clause on The Join statement

I have tried adding a where clause in my join statement, at the ON, However i am receiving syntax errors, i am not sure where to put this as i need it to pull the data from a table called systemlookup

     DECLARE @OptionalModules TABLE (moduleid INT, name VarChar(200))
 INSERT INTO @OptionalModules
 SELECT CAST (LookupReference AS INT)
 FROM dbo.systemlookup
 left join @xml.nodes('//Modules/*') as organisation(license) on 
 organisation.license.value('local-name(.)', 'varchar(50)') =
 case LookupReference
 when '1' then 'a'
 when '2' then 'b'
 when '6' then 'c'
 when '8' then 'd'
 when '9' then 'e'
 when '10' then 'f'
 when '11' then 'g'
 when '12' then 'h'
 when '13' then 'i'
 when '14' then 'j'
 when '15' then 'k'
 when '16' then 'l'
 when '17' then 'm'
 when '18' then 'n'
 when '20' then 'o'
 when '21' then 'p'
 when '22' then 'q'
 when '23' then 'r'
 when '24' then 's'
 when '25' then 't'
 when '26' then 'u'
 when '27' then 'v'
 when '28' then 'w'
 when '29' then 'x'
 when '31' then 'y'
 when '32' then 'z'
 when '33' then 'aa'
 when '16016' then 'bb'
end

Add an AND after the ON clause, syntax is like this:

SELECT A
FROM Table A
INNER JOIN Table B ON A.ID = B.ID AND A.ID2 = B.ID2

Thanks for the answers already, i have however found the correct way of doing it, i was missing also another piece of SQL from the bottom of the code

             DECLARE @OptionalModules TABLE (moduleid INT, name VarChar(200))
     INSERT INTO @OptionalModules
     SELECT CAST (LookupReference AS INT)
     FROM dbo.systemlookup
     left join @xml.nodes('//Modules/*') as organisation(license) on 
     organisation.license.value('local-name(.)', 'varchar(50)') =
     case LookupReference
     when '1' then 'a'
     when '2' then 'b'
     when '6' then 'c'
     when '8' then 'd'
     when '9' then 'e'
     when '10' then 'f'
     when '11' then 'g'
     when '12' then 'h'
     when '13' then 'i'
     when '14' then 'j'
     when '15' then 'k'
     when '16' then 'l'
     when '17' then 'm'
     when '18' then 'n'
     when '20' then 'o'
     when '21' then 'p'
     when '22' then 'q'
     when '23' then 'r'
     when '24' then 's'
     when '25' then 't'
     when '26' then 'u'
     when '27' then 'v'
     when '28' then 'w'
     when '29' then 'x'
     when '31' then 'y'
     when '32' then 'z'
     when '33' then 'aa'
     when '16016' then 'bb'
    end
where s.LookupTypeId = 1 and cast(case license.value('.', 'varchar(3)') when 'Yes' then 1 when 'No' then 0 else 1 end as bit) = 1

I have put the where before the cast at the bottom and now returning 30 rows instead of its original 2340.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM