Coldfusion find if column has value












1















I have a table which has a column LanguageID. It has seven values: 1, 2, 3, 4, 5, 6, 22



Now I have a function that gets the languageID of the current user.



I want to check if that languageID exists in my table, if it exists return that value, if not then return default 2. If 2 doesn't exist in that table then return 1.



This is my query:



<cfquery name="NameName" datasource="mydatabase">
SELECT DISTINCT SomeID,
SomeName,
LanguageID
FROM myDatabase WITH(NOLOCK)
WHERE BlacklistReasonID = <cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.BlacklistReasonID#" />
<cfif isDefined(arguments.LanguageID)> //I want to check if the arguments.LanguageID exists in the table
AND LanguageID = <cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.LanguageID#" />
<cfelseif NOT isDefined(arguments.LanguageID) > //ako nema value
AND LanguageID = 2 //default, put languageID to 2
<cfelse>
AND LanguageID = 1 //if languageID 2 doesn't exist in that table, then put 1
</cfif>
</cfquery>


So real life example. The funtion activates, the LanguageID is 3. That's ok, the first cfif check to see if the table has a value of 3, it has, ok then it puts the languageID to 3 and ends.



Second scenario. The funtion activates, the LanguageID is 44. The first cfif checks to see if the table has a value of 44, it doesn't have that value, so the second cfif activates and puts the LanguageID to 2.










share|improve this question

























  • The way your condition is, it will never go in cfelse block. Either arguments.LanguageID exists or it does not. So either it goes to 1st part or 2nd.

    – CFML_Developer
    Nov 23 '18 at 12:07











  • @CFML_Developer I know that. That's why I'm stuck here :(

    – IkePr
    Nov 23 '18 at 13:03











  • If LanguageID has a default value of 2, under what circumstances would it be 1?

    – Dan Bracuk
    Nov 23 '18 at 13:44











  • Do you actually mean to check if the table contains a row with LanguageID = 2 and fall back to a row with LanguageID = 1 in case it doesn't?

    – Alex
    Nov 23 '18 at 19:54













  • How many rows are you expecting out of this query?

    – James A Mohler
    Nov 24 '18 at 3:05
















1















I have a table which has a column LanguageID. It has seven values: 1, 2, 3, 4, 5, 6, 22



Now I have a function that gets the languageID of the current user.



I want to check if that languageID exists in my table, if it exists return that value, if not then return default 2. If 2 doesn't exist in that table then return 1.



This is my query:



<cfquery name="NameName" datasource="mydatabase">
SELECT DISTINCT SomeID,
SomeName,
LanguageID
FROM myDatabase WITH(NOLOCK)
WHERE BlacklistReasonID = <cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.BlacklistReasonID#" />
<cfif isDefined(arguments.LanguageID)> //I want to check if the arguments.LanguageID exists in the table
AND LanguageID = <cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.LanguageID#" />
<cfelseif NOT isDefined(arguments.LanguageID) > //ako nema value
AND LanguageID = 2 //default, put languageID to 2
<cfelse>
AND LanguageID = 1 //if languageID 2 doesn't exist in that table, then put 1
</cfif>
</cfquery>


So real life example. The funtion activates, the LanguageID is 3. That's ok, the first cfif check to see if the table has a value of 3, it has, ok then it puts the languageID to 3 and ends.



Second scenario. The funtion activates, the LanguageID is 44. The first cfif checks to see if the table has a value of 44, it doesn't have that value, so the second cfif activates and puts the LanguageID to 2.










share|improve this question

























  • The way your condition is, it will never go in cfelse block. Either arguments.LanguageID exists or it does not. So either it goes to 1st part or 2nd.

    – CFML_Developer
    Nov 23 '18 at 12:07











  • @CFML_Developer I know that. That's why I'm stuck here :(

    – IkePr
    Nov 23 '18 at 13:03











  • If LanguageID has a default value of 2, under what circumstances would it be 1?

    – Dan Bracuk
    Nov 23 '18 at 13:44











  • Do you actually mean to check if the table contains a row with LanguageID = 2 and fall back to a row with LanguageID = 1 in case it doesn't?

    – Alex
    Nov 23 '18 at 19:54













  • How many rows are you expecting out of this query?

    – James A Mohler
    Nov 24 '18 at 3:05














1












1








1








I have a table which has a column LanguageID. It has seven values: 1, 2, 3, 4, 5, 6, 22



Now I have a function that gets the languageID of the current user.



I want to check if that languageID exists in my table, if it exists return that value, if not then return default 2. If 2 doesn't exist in that table then return 1.



This is my query:



<cfquery name="NameName" datasource="mydatabase">
SELECT DISTINCT SomeID,
SomeName,
LanguageID
FROM myDatabase WITH(NOLOCK)
WHERE BlacklistReasonID = <cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.BlacklistReasonID#" />
<cfif isDefined(arguments.LanguageID)> //I want to check if the arguments.LanguageID exists in the table
AND LanguageID = <cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.LanguageID#" />
<cfelseif NOT isDefined(arguments.LanguageID) > //ako nema value
AND LanguageID = 2 //default, put languageID to 2
<cfelse>
AND LanguageID = 1 //if languageID 2 doesn't exist in that table, then put 1
</cfif>
</cfquery>


So real life example. The funtion activates, the LanguageID is 3. That's ok, the first cfif check to see if the table has a value of 3, it has, ok then it puts the languageID to 3 and ends.



Second scenario. The funtion activates, the LanguageID is 44. The first cfif checks to see if the table has a value of 44, it doesn't have that value, so the second cfif activates and puts the LanguageID to 2.










share|improve this question
















I have a table which has a column LanguageID. It has seven values: 1, 2, 3, 4, 5, 6, 22



Now I have a function that gets the languageID of the current user.



I want to check if that languageID exists in my table, if it exists return that value, if not then return default 2. If 2 doesn't exist in that table then return 1.



This is my query:



<cfquery name="NameName" datasource="mydatabase">
SELECT DISTINCT SomeID,
SomeName,
LanguageID
FROM myDatabase WITH(NOLOCK)
WHERE BlacklistReasonID = <cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.BlacklistReasonID#" />
<cfif isDefined(arguments.LanguageID)> //I want to check if the arguments.LanguageID exists in the table
AND LanguageID = <cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.LanguageID#" />
<cfelseif NOT isDefined(arguments.LanguageID) > //ako nema value
AND LanguageID = 2 //default, put languageID to 2
<cfelse>
AND LanguageID = 1 //if languageID 2 doesn't exist in that table, then put 1
</cfif>
</cfquery>


So real life example. The funtion activates, the LanguageID is 3. That's ok, the first cfif check to see if the table has a value of 3, it has, ok then it puts the languageID to 3 and ends.



Second scenario. The funtion activates, the LanguageID is 44. The first cfif checks to see if the table has a value of 44, it doesn't have that value, so the second cfif activates and puts the LanguageID to 2.







sql coldfusion






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 24 '18 at 3:04









James A Mohler

7,106123353




7,106123353










asked Nov 23 '18 at 11:44









IkePrIkePr

186215




186215













  • The way your condition is, it will never go in cfelse block. Either arguments.LanguageID exists or it does not. So either it goes to 1st part or 2nd.

    – CFML_Developer
    Nov 23 '18 at 12:07











  • @CFML_Developer I know that. That's why I'm stuck here :(

    – IkePr
    Nov 23 '18 at 13:03











  • If LanguageID has a default value of 2, under what circumstances would it be 1?

    – Dan Bracuk
    Nov 23 '18 at 13:44











  • Do you actually mean to check if the table contains a row with LanguageID = 2 and fall back to a row with LanguageID = 1 in case it doesn't?

    – Alex
    Nov 23 '18 at 19:54













  • How many rows are you expecting out of this query?

    – James A Mohler
    Nov 24 '18 at 3:05



















  • The way your condition is, it will never go in cfelse block. Either arguments.LanguageID exists or it does not. So either it goes to 1st part or 2nd.

    – CFML_Developer
    Nov 23 '18 at 12:07











  • @CFML_Developer I know that. That's why I'm stuck here :(

    – IkePr
    Nov 23 '18 at 13:03











  • If LanguageID has a default value of 2, under what circumstances would it be 1?

    – Dan Bracuk
    Nov 23 '18 at 13:44











  • Do you actually mean to check if the table contains a row with LanguageID = 2 and fall back to a row with LanguageID = 1 in case it doesn't?

    – Alex
    Nov 23 '18 at 19:54













  • How many rows are you expecting out of this query?

    – James A Mohler
    Nov 24 '18 at 3:05

















The way your condition is, it will never go in cfelse block. Either arguments.LanguageID exists or it does not. So either it goes to 1st part or 2nd.

– CFML_Developer
Nov 23 '18 at 12:07





The way your condition is, it will never go in cfelse block. Either arguments.LanguageID exists or it does not. So either it goes to 1st part or 2nd.

– CFML_Developer
Nov 23 '18 at 12:07













@CFML_Developer I know that. That's why I'm stuck here :(

– IkePr
Nov 23 '18 at 13:03





@CFML_Developer I know that. That's why I'm stuck here :(

– IkePr
Nov 23 '18 at 13:03













If LanguageID has a default value of 2, under what circumstances would it be 1?

– Dan Bracuk
Nov 23 '18 at 13:44





If LanguageID has a default value of 2, under what circumstances would it be 1?

– Dan Bracuk
Nov 23 '18 at 13:44













Do you actually mean to check if the table contains a row with LanguageID = 2 and fall back to a row with LanguageID = 1 in case it doesn't?

– Alex
Nov 23 '18 at 19:54







Do you actually mean to check if the table contains a row with LanguageID = 2 and fall back to a row with LanguageID = 1 in case it doesn't?

– Alex
Nov 23 '18 at 19:54















How many rows are you expecting out of this query?

– James A Mohler
Nov 24 '18 at 3:05





How many rows are you expecting out of this query?

– James A Mohler
Nov 24 '18 at 3:05












1 Answer
1






active

oldest

votes


















3














This can be done in the SQL itself. That would likely be much more performant than having ColdFusion try to do query processing.



I made a couple of assumptions, based on your original question. First, that this query should only return 1 result (TOP 1). And second, that your default languages are 2 for Default1 and 1 for Default2. This also includes that the value you are searching for will be greater than 1 or 2.



SELECT TOP 1 SomeID, SomeName, LanguageID
FROM (
SELECT SomeID, SomeName, LanguageID
FROM t1
WHERE BlacklistReasonID = <cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.BlacklistReasonID#">
AND LanguageID = <cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.Language#">

UNION ALL

SELECT SomeID, SomeName, LanguageID
FROM t1
WHERE BlacklistReasonID = <cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.BlacklistReasonID#">
AND LanguageID IN ( <cfqueryparam value="1,2" cfsqltype="CF_SQL_INTEGER" list="yes"> ) /* DEFAULT IDs */
) s1
ORDER BY id DESC
;


This works because a query will return no results if the filtering condition is not matched, but when joined to a query that does have results (your defaults) those will appear in the whole query. Then I order the results and take the top 1.



The basic gist of the query is at: https://dbfiddle.uk/?rdbms=sqlserver_2012&fiddle=6f729939d3305e49032eab56d88aa877



I also included several demonstrations of how this works if your LanguageID is not found, and one possible way of using default IDs that aren't 1 or 2.





And to keep it all in one place:




CREATE TABLE t1 ( id int, lang varchar(10) ) ;
INSERT INTO t1 (id, lang)
VALUES (1,'Default 2'),(2,'Default 1'),(3,'Klingon')
,(50, 'Common'),(20, 'Nadsat'),(55, 'Furbish')
;
/* If you will only have one language per id, and default IDs will always be 2 and 1 */
/* Match */
SELECT TOP 1 id, lang
FROM (
SELECT id, lang
FROM t1
WHERE id = 3

UNION ALL

SELECT id, lang
FROM t1
WHERE id IN (1,2) /* DEFAULT IDs */
) s1
ORDER BY id DESC
;



id | lang
-: | :------
3 | Klingon


/* No match. */
SELECT TOP 1 id, lang
FROM (
SELECT id, lang
FROM t1
WHERE id = 99999

UNION ALL

SELECT id, lang
FROM t1
WHERE id IN (1,2) /* DEFAULT IDs */
) s1
ORDER BY id DESC
;



id | lang
-: | :--------
2 | Default 1






/* No Default1 Language */
CREATE TABLE t2 ( id int, lang varchar(10) ) ;
INSERT INTO t2 (id, lang)
VALUES (1,'Default 2'),(3,'Klingon')
,(50, 'Common'),(20, 'Nadsat'),(55, 'Furbish')
;

SELECT TOP 1 id, lang
FROM (
SELECT id, lang
FROM t2
WHERE id = 9999

UNION ALL

SELECT id, lang
FROM t2
WHERE id IN (1,2) /* DEFAULT IDs */
) s1
ORDER BY id DESC
;



id | lang
-: | :--------
1 | Default 2






/* No Default Language */
CREATE TABLE t3 ( id int, lang varchar(10) ) ;
INSERT INTO t3 (id, lang)
VALUES (3,'Klingon')
,(50, 'Common'),(20, 'Nadsat'),(55, 'Furbish')
;

/* No match. No Default. */
SELECT TOP 1 id, lang
FROM (
SELECT id, lang
FROM t3
WHERE id = 9999

UNION ALL

SELECT id, lang
FROM t3
WHERE id IN (1,2) /* DEFAULT IDs */
) s1
ORDER BY id DESC
;



id | lang
-: | :---






/* Match, but no default */
SELECT TOP 1 id, lang
FROM (
SELECT id, lang
FROM t3
WHERE id = 3

UNION ALL

SELECT id, lang
FROM t3
WHERE id IN (1,2) /* DEFAULT IDs */
) s1
ORDER BY id DESC
;



id | lang
-: | :------
3 | Klingon






/* Default 1 and 2 are not ID 2 and 1. */
CREATE TABLE t4 ( id int, lang varchar(10) ) ;
INSERT INTO t4 (id, lang)
VALUES (40,'Default 2'),(42,'Default 1'),(3,'Klingon')
,(50, 'Common'),(20, 'Nadsat'),(55, 'Furbish')
;

/* No match. Pick Default. */
SELECT TOP 1 s1.id, s1.lang
FROM (
SELECT id, lang
FROM t4
WHERE id = 9999

UNION ALL

SELECT s2.id, s2.lang
FROM (
SELECT TOP 1 id, lang
, CASE
WHEN ID = 42 THEN 2
WHEN ID = 40 THEN 1
ELSE 0
END AS sortOrder
FROM t4
WHERE id IN (40,42) /* NEW DEFAULT IDs */
ORDER BY sortOrder DESC
) s2
) s1
ORDER BY s1.id DESC
;



id | lang
-: | :--------
42 | Default 1



db<>fiddle here






share|improve this answer
























  • I also removed NOLOCK from the query. See my comment above. blogs.sentryone.com/aaronbertrand/bad-habits-nolock-everywhere

    – Shawn
    Nov 25 '18 at 22:35











  • Qapla'! Exactly what I was going to suggest ;-)

    – Ageax
    Nov 28 '18 at 23:38






  • 1





    @Ageax I've already warned you. Being in my head can be a scary place.

    – Shawn
    Nov 29 '18 at 0:08











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53446096%2fcoldfusion-find-if-column-has-value%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














This can be done in the SQL itself. That would likely be much more performant than having ColdFusion try to do query processing.



I made a couple of assumptions, based on your original question. First, that this query should only return 1 result (TOP 1). And second, that your default languages are 2 for Default1 and 1 for Default2. This also includes that the value you are searching for will be greater than 1 or 2.



SELECT TOP 1 SomeID, SomeName, LanguageID
FROM (
SELECT SomeID, SomeName, LanguageID
FROM t1
WHERE BlacklistReasonID = <cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.BlacklistReasonID#">
AND LanguageID = <cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.Language#">

UNION ALL

SELECT SomeID, SomeName, LanguageID
FROM t1
WHERE BlacklistReasonID = <cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.BlacklistReasonID#">
AND LanguageID IN ( <cfqueryparam value="1,2" cfsqltype="CF_SQL_INTEGER" list="yes"> ) /* DEFAULT IDs */
) s1
ORDER BY id DESC
;


This works because a query will return no results if the filtering condition is not matched, but when joined to a query that does have results (your defaults) those will appear in the whole query. Then I order the results and take the top 1.



The basic gist of the query is at: https://dbfiddle.uk/?rdbms=sqlserver_2012&fiddle=6f729939d3305e49032eab56d88aa877



I also included several demonstrations of how this works if your LanguageID is not found, and one possible way of using default IDs that aren't 1 or 2.





And to keep it all in one place:




CREATE TABLE t1 ( id int, lang varchar(10) ) ;
INSERT INTO t1 (id, lang)
VALUES (1,'Default 2'),(2,'Default 1'),(3,'Klingon')
,(50, 'Common'),(20, 'Nadsat'),(55, 'Furbish')
;
/* If you will only have one language per id, and default IDs will always be 2 and 1 */
/* Match */
SELECT TOP 1 id, lang
FROM (
SELECT id, lang
FROM t1
WHERE id = 3

UNION ALL

SELECT id, lang
FROM t1
WHERE id IN (1,2) /* DEFAULT IDs */
) s1
ORDER BY id DESC
;



id | lang
-: | :------
3 | Klingon


/* No match. */
SELECT TOP 1 id, lang
FROM (
SELECT id, lang
FROM t1
WHERE id = 99999

UNION ALL

SELECT id, lang
FROM t1
WHERE id IN (1,2) /* DEFAULT IDs */
) s1
ORDER BY id DESC
;



id | lang
-: | :--------
2 | Default 1






/* No Default1 Language */
CREATE TABLE t2 ( id int, lang varchar(10) ) ;
INSERT INTO t2 (id, lang)
VALUES (1,'Default 2'),(3,'Klingon')
,(50, 'Common'),(20, 'Nadsat'),(55, 'Furbish')
;

SELECT TOP 1 id, lang
FROM (
SELECT id, lang
FROM t2
WHERE id = 9999

UNION ALL

SELECT id, lang
FROM t2
WHERE id IN (1,2) /* DEFAULT IDs */
) s1
ORDER BY id DESC
;



id | lang
-: | :--------
1 | Default 2






/* No Default Language */
CREATE TABLE t3 ( id int, lang varchar(10) ) ;
INSERT INTO t3 (id, lang)
VALUES (3,'Klingon')
,(50, 'Common'),(20, 'Nadsat'),(55, 'Furbish')
;

/* No match. No Default. */
SELECT TOP 1 id, lang
FROM (
SELECT id, lang
FROM t3
WHERE id = 9999

UNION ALL

SELECT id, lang
FROM t3
WHERE id IN (1,2) /* DEFAULT IDs */
) s1
ORDER BY id DESC
;



id | lang
-: | :---






/* Match, but no default */
SELECT TOP 1 id, lang
FROM (
SELECT id, lang
FROM t3
WHERE id = 3

UNION ALL

SELECT id, lang
FROM t3
WHERE id IN (1,2) /* DEFAULT IDs */
) s1
ORDER BY id DESC
;



id | lang
-: | :------
3 | Klingon






/* Default 1 and 2 are not ID 2 and 1. */
CREATE TABLE t4 ( id int, lang varchar(10) ) ;
INSERT INTO t4 (id, lang)
VALUES (40,'Default 2'),(42,'Default 1'),(3,'Klingon')
,(50, 'Common'),(20, 'Nadsat'),(55, 'Furbish')
;

/* No match. Pick Default. */
SELECT TOP 1 s1.id, s1.lang
FROM (
SELECT id, lang
FROM t4
WHERE id = 9999

UNION ALL

SELECT s2.id, s2.lang
FROM (
SELECT TOP 1 id, lang
, CASE
WHEN ID = 42 THEN 2
WHEN ID = 40 THEN 1
ELSE 0
END AS sortOrder
FROM t4
WHERE id IN (40,42) /* NEW DEFAULT IDs */
ORDER BY sortOrder DESC
) s2
) s1
ORDER BY s1.id DESC
;



id | lang
-: | :--------
42 | Default 1



db<>fiddle here






share|improve this answer
























  • I also removed NOLOCK from the query. See my comment above. blogs.sentryone.com/aaronbertrand/bad-habits-nolock-everywhere

    – Shawn
    Nov 25 '18 at 22:35











  • Qapla'! Exactly what I was going to suggest ;-)

    – Ageax
    Nov 28 '18 at 23:38






  • 1





    @Ageax I've already warned you. Being in my head can be a scary place.

    – Shawn
    Nov 29 '18 at 0:08
















3














This can be done in the SQL itself. That would likely be much more performant than having ColdFusion try to do query processing.



I made a couple of assumptions, based on your original question. First, that this query should only return 1 result (TOP 1). And second, that your default languages are 2 for Default1 and 1 for Default2. This also includes that the value you are searching for will be greater than 1 or 2.



SELECT TOP 1 SomeID, SomeName, LanguageID
FROM (
SELECT SomeID, SomeName, LanguageID
FROM t1
WHERE BlacklistReasonID = <cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.BlacklistReasonID#">
AND LanguageID = <cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.Language#">

UNION ALL

SELECT SomeID, SomeName, LanguageID
FROM t1
WHERE BlacklistReasonID = <cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.BlacklistReasonID#">
AND LanguageID IN ( <cfqueryparam value="1,2" cfsqltype="CF_SQL_INTEGER" list="yes"> ) /* DEFAULT IDs */
) s1
ORDER BY id DESC
;


This works because a query will return no results if the filtering condition is not matched, but when joined to a query that does have results (your defaults) those will appear in the whole query. Then I order the results and take the top 1.



The basic gist of the query is at: https://dbfiddle.uk/?rdbms=sqlserver_2012&fiddle=6f729939d3305e49032eab56d88aa877



I also included several demonstrations of how this works if your LanguageID is not found, and one possible way of using default IDs that aren't 1 or 2.





And to keep it all in one place:




CREATE TABLE t1 ( id int, lang varchar(10) ) ;
INSERT INTO t1 (id, lang)
VALUES (1,'Default 2'),(2,'Default 1'),(3,'Klingon')
,(50, 'Common'),(20, 'Nadsat'),(55, 'Furbish')
;
/* If you will only have one language per id, and default IDs will always be 2 and 1 */
/* Match */
SELECT TOP 1 id, lang
FROM (
SELECT id, lang
FROM t1
WHERE id = 3

UNION ALL

SELECT id, lang
FROM t1
WHERE id IN (1,2) /* DEFAULT IDs */
) s1
ORDER BY id DESC
;



id | lang
-: | :------
3 | Klingon


/* No match. */
SELECT TOP 1 id, lang
FROM (
SELECT id, lang
FROM t1
WHERE id = 99999

UNION ALL

SELECT id, lang
FROM t1
WHERE id IN (1,2) /* DEFAULT IDs */
) s1
ORDER BY id DESC
;



id | lang
-: | :--------
2 | Default 1






/* No Default1 Language */
CREATE TABLE t2 ( id int, lang varchar(10) ) ;
INSERT INTO t2 (id, lang)
VALUES (1,'Default 2'),(3,'Klingon')
,(50, 'Common'),(20, 'Nadsat'),(55, 'Furbish')
;

SELECT TOP 1 id, lang
FROM (
SELECT id, lang
FROM t2
WHERE id = 9999

UNION ALL

SELECT id, lang
FROM t2
WHERE id IN (1,2) /* DEFAULT IDs */
) s1
ORDER BY id DESC
;



id | lang
-: | :--------
1 | Default 2






/* No Default Language */
CREATE TABLE t3 ( id int, lang varchar(10) ) ;
INSERT INTO t3 (id, lang)
VALUES (3,'Klingon')
,(50, 'Common'),(20, 'Nadsat'),(55, 'Furbish')
;

/* No match. No Default. */
SELECT TOP 1 id, lang
FROM (
SELECT id, lang
FROM t3
WHERE id = 9999

UNION ALL

SELECT id, lang
FROM t3
WHERE id IN (1,2) /* DEFAULT IDs */
) s1
ORDER BY id DESC
;



id | lang
-: | :---






/* Match, but no default */
SELECT TOP 1 id, lang
FROM (
SELECT id, lang
FROM t3
WHERE id = 3

UNION ALL

SELECT id, lang
FROM t3
WHERE id IN (1,2) /* DEFAULT IDs */
) s1
ORDER BY id DESC
;



id | lang
-: | :------
3 | Klingon






/* Default 1 and 2 are not ID 2 and 1. */
CREATE TABLE t4 ( id int, lang varchar(10) ) ;
INSERT INTO t4 (id, lang)
VALUES (40,'Default 2'),(42,'Default 1'),(3,'Klingon')
,(50, 'Common'),(20, 'Nadsat'),(55, 'Furbish')
;

/* No match. Pick Default. */
SELECT TOP 1 s1.id, s1.lang
FROM (
SELECT id, lang
FROM t4
WHERE id = 9999

UNION ALL

SELECT s2.id, s2.lang
FROM (
SELECT TOP 1 id, lang
, CASE
WHEN ID = 42 THEN 2
WHEN ID = 40 THEN 1
ELSE 0
END AS sortOrder
FROM t4
WHERE id IN (40,42) /* NEW DEFAULT IDs */
ORDER BY sortOrder DESC
) s2
) s1
ORDER BY s1.id DESC
;



id | lang
-: | :--------
42 | Default 1



db<>fiddle here






share|improve this answer
























  • I also removed NOLOCK from the query. See my comment above. blogs.sentryone.com/aaronbertrand/bad-habits-nolock-everywhere

    – Shawn
    Nov 25 '18 at 22:35











  • Qapla'! Exactly what I was going to suggest ;-)

    – Ageax
    Nov 28 '18 at 23:38






  • 1





    @Ageax I've already warned you. Being in my head can be a scary place.

    – Shawn
    Nov 29 '18 at 0:08














3












3








3







This can be done in the SQL itself. That would likely be much more performant than having ColdFusion try to do query processing.



I made a couple of assumptions, based on your original question. First, that this query should only return 1 result (TOP 1). And second, that your default languages are 2 for Default1 and 1 for Default2. This also includes that the value you are searching for will be greater than 1 or 2.



SELECT TOP 1 SomeID, SomeName, LanguageID
FROM (
SELECT SomeID, SomeName, LanguageID
FROM t1
WHERE BlacklistReasonID = <cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.BlacklistReasonID#">
AND LanguageID = <cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.Language#">

UNION ALL

SELECT SomeID, SomeName, LanguageID
FROM t1
WHERE BlacklistReasonID = <cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.BlacklistReasonID#">
AND LanguageID IN ( <cfqueryparam value="1,2" cfsqltype="CF_SQL_INTEGER" list="yes"> ) /* DEFAULT IDs */
) s1
ORDER BY id DESC
;


This works because a query will return no results if the filtering condition is not matched, but when joined to a query that does have results (your defaults) those will appear in the whole query. Then I order the results and take the top 1.



The basic gist of the query is at: https://dbfiddle.uk/?rdbms=sqlserver_2012&fiddle=6f729939d3305e49032eab56d88aa877



I also included several demonstrations of how this works if your LanguageID is not found, and one possible way of using default IDs that aren't 1 or 2.





And to keep it all in one place:




CREATE TABLE t1 ( id int, lang varchar(10) ) ;
INSERT INTO t1 (id, lang)
VALUES (1,'Default 2'),(2,'Default 1'),(3,'Klingon')
,(50, 'Common'),(20, 'Nadsat'),(55, 'Furbish')
;
/* If you will only have one language per id, and default IDs will always be 2 and 1 */
/* Match */
SELECT TOP 1 id, lang
FROM (
SELECT id, lang
FROM t1
WHERE id = 3

UNION ALL

SELECT id, lang
FROM t1
WHERE id IN (1,2) /* DEFAULT IDs */
) s1
ORDER BY id DESC
;



id | lang
-: | :------
3 | Klingon


/* No match. */
SELECT TOP 1 id, lang
FROM (
SELECT id, lang
FROM t1
WHERE id = 99999

UNION ALL

SELECT id, lang
FROM t1
WHERE id IN (1,2) /* DEFAULT IDs */
) s1
ORDER BY id DESC
;



id | lang
-: | :--------
2 | Default 1






/* No Default1 Language */
CREATE TABLE t2 ( id int, lang varchar(10) ) ;
INSERT INTO t2 (id, lang)
VALUES (1,'Default 2'),(3,'Klingon')
,(50, 'Common'),(20, 'Nadsat'),(55, 'Furbish')
;

SELECT TOP 1 id, lang
FROM (
SELECT id, lang
FROM t2
WHERE id = 9999

UNION ALL

SELECT id, lang
FROM t2
WHERE id IN (1,2) /* DEFAULT IDs */
) s1
ORDER BY id DESC
;



id | lang
-: | :--------
1 | Default 2






/* No Default Language */
CREATE TABLE t3 ( id int, lang varchar(10) ) ;
INSERT INTO t3 (id, lang)
VALUES (3,'Klingon')
,(50, 'Common'),(20, 'Nadsat'),(55, 'Furbish')
;

/* No match. No Default. */
SELECT TOP 1 id, lang
FROM (
SELECT id, lang
FROM t3
WHERE id = 9999

UNION ALL

SELECT id, lang
FROM t3
WHERE id IN (1,2) /* DEFAULT IDs */
) s1
ORDER BY id DESC
;



id | lang
-: | :---






/* Match, but no default */
SELECT TOP 1 id, lang
FROM (
SELECT id, lang
FROM t3
WHERE id = 3

UNION ALL

SELECT id, lang
FROM t3
WHERE id IN (1,2) /* DEFAULT IDs */
) s1
ORDER BY id DESC
;



id | lang
-: | :------
3 | Klingon






/* Default 1 and 2 are not ID 2 and 1. */
CREATE TABLE t4 ( id int, lang varchar(10) ) ;
INSERT INTO t4 (id, lang)
VALUES (40,'Default 2'),(42,'Default 1'),(3,'Klingon')
,(50, 'Common'),(20, 'Nadsat'),(55, 'Furbish')
;

/* No match. Pick Default. */
SELECT TOP 1 s1.id, s1.lang
FROM (
SELECT id, lang
FROM t4
WHERE id = 9999

UNION ALL

SELECT s2.id, s2.lang
FROM (
SELECT TOP 1 id, lang
, CASE
WHEN ID = 42 THEN 2
WHEN ID = 40 THEN 1
ELSE 0
END AS sortOrder
FROM t4
WHERE id IN (40,42) /* NEW DEFAULT IDs */
ORDER BY sortOrder DESC
) s2
) s1
ORDER BY s1.id DESC
;



id | lang
-: | :--------
42 | Default 1



db<>fiddle here






share|improve this answer













This can be done in the SQL itself. That would likely be much more performant than having ColdFusion try to do query processing.



I made a couple of assumptions, based on your original question. First, that this query should only return 1 result (TOP 1). And second, that your default languages are 2 for Default1 and 1 for Default2. This also includes that the value you are searching for will be greater than 1 or 2.



SELECT TOP 1 SomeID, SomeName, LanguageID
FROM (
SELECT SomeID, SomeName, LanguageID
FROM t1
WHERE BlacklistReasonID = <cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.BlacklistReasonID#">
AND LanguageID = <cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.Language#">

UNION ALL

SELECT SomeID, SomeName, LanguageID
FROM t1
WHERE BlacklistReasonID = <cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.BlacklistReasonID#">
AND LanguageID IN ( <cfqueryparam value="1,2" cfsqltype="CF_SQL_INTEGER" list="yes"> ) /* DEFAULT IDs */
) s1
ORDER BY id DESC
;


This works because a query will return no results if the filtering condition is not matched, but when joined to a query that does have results (your defaults) those will appear in the whole query. Then I order the results and take the top 1.



The basic gist of the query is at: https://dbfiddle.uk/?rdbms=sqlserver_2012&fiddle=6f729939d3305e49032eab56d88aa877



I also included several demonstrations of how this works if your LanguageID is not found, and one possible way of using default IDs that aren't 1 or 2.





And to keep it all in one place:




CREATE TABLE t1 ( id int, lang varchar(10) ) ;
INSERT INTO t1 (id, lang)
VALUES (1,'Default 2'),(2,'Default 1'),(3,'Klingon')
,(50, 'Common'),(20, 'Nadsat'),(55, 'Furbish')
;
/* If you will only have one language per id, and default IDs will always be 2 and 1 */
/* Match */
SELECT TOP 1 id, lang
FROM (
SELECT id, lang
FROM t1
WHERE id = 3

UNION ALL

SELECT id, lang
FROM t1
WHERE id IN (1,2) /* DEFAULT IDs */
) s1
ORDER BY id DESC
;



id | lang
-: | :------
3 | Klingon


/* No match. */
SELECT TOP 1 id, lang
FROM (
SELECT id, lang
FROM t1
WHERE id = 99999

UNION ALL

SELECT id, lang
FROM t1
WHERE id IN (1,2) /* DEFAULT IDs */
) s1
ORDER BY id DESC
;



id | lang
-: | :--------
2 | Default 1






/* No Default1 Language */
CREATE TABLE t2 ( id int, lang varchar(10) ) ;
INSERT INTO t2 (id, lang)
VALUES (1,'Default 2'),(3,'Klingon')
,(50, 'Common'),(20, 'Nadsat'),(55, 'Furbish')
;

SELECT TOP 1 id, lang
FROM (
SELECT id, lang
FROM t2
WHERE id = 9999

UNION ALL

SELECT id, lang
FROM t2
WHERE id IN (1,2) /* DEFAULT IDs */
) s1
ORDER BY id DESC
;



id | lang
-: | :--------
1 | Default 2






/* No Default Language */
CREATE TABLE t3 ( id int, lang varchar(10) ) ;
INSERT INTO t3 (id, lang)
VALUES (3,'Klingon')
,(50, 'Common'),(20, 'Nadsat'),(55, 'Furbish')
;

/* No match. No Default. */
SELECT TOP 1 id, lang
FROM (
SELECT id, lang
FROM t3
WHERE id = 9999

UNION ALL

SELECT id, lang
FROM t3
WHERE id IN (1,2) /* DEFAULT IDs */
) s1
ORDER BY id DESC
;



id | lang
-: | :---






/* Match, but no default */
SELECT TOP 1 id, lang
FROM (
SELECT id, lang
FROM t3
WHERE id = 3

UNION ALL

SELECT id, lang
FROM t3
WHERE id IN (1,2) /* DEFAULT IDs */
) s1
ORDER BY id DESC
;



id | lang
-: | :------
3 | Klingon






/* Default 1 and 2 are not ID 2 and 1. */
CREATE TABLE t4 ( id int, lang varchar(10) ) ;
INSERT INTO t4 (id, lang)
VALUES (40,'Default 2'),(42,'Default 1'),(3,'Klingon')
,(50, 'Common'),(20, 'Nadsat'),(55, 'Furbish')
;

/* No match. Pick Default. */
SELECT TOP 1 s1.id, s1.lang
FROM (
SELECT id, lang
FROM t4
WHERE id = 9999

UNION ALL

SELECT s2.id, s2.lang
FROM (
SELECT TOP 1 id, lang
, CASE
WHEN ID = 42 THEN 2
WHEN ID = 40 THEN 1
ELSE 0
END AS sortOrder
FROM t4
WHERE id IN (40,42) /* NEW DEFAULT IDs */
ORDER BY sortOrder DESC
) s2
) s1
ORDER BY s1.id DESC
;



id | lang
-: | :--------
42 | Default 1



db<>fiddle here







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 25 '18 at 22:32









ShawnShawn

3,51711324




3,51711324













  • I also removed NOLOCK from the query. See my comment above. blogs.sentryone.com/aaronbertrand/bad-habits-nolock-everywhere

    – Shawn
    Nov 25 '18 at 22:35











  • Qapla'! Exactly what I was going to suggest ;-)

    – Ageax
    Nov 28 '18 at 23:38






  • 1





    @Ageax I've already warned you. Being in my head can be a scary place.

    – Shawn
    Nov 29 '18 at 0:08



















  • I also removed NOLOCK from the query. See my comment above. blogs.sentryone.com/aaronbertrand/bad-habits-nolock-everywhere

    – Shawn
    Nov 25 '18 at 22:35











  • Qapla'! Exactly what I was going to suggest ;-)

    – Ageax
    Nov 28 '18 at 23:38






  • 1





    @Ageax I've already warned you. Being in my head can be a scary place.

    – Shawn
    Nov 29 '18 at 0:08

















I also removed NOLOCK from the query. See my comment above. blogs.sentryone.com/aaronbertrand/bad-habits-nolock-everywhere

– Shawn
Nov 25 '18 at 22:35





I also removed NOLOCK from the query. See my comment above. blogs.sentryone.com/aaronbertrand/bad-habits-nolock-everywhere

– Shawn
Nov 25 '18 at 22:35













Qapla'! Exactly what I was going to suggest ;-)

– Ageax
Nov 28 '18 at 23:38





Qapla'! Exactly what I was going to suggest ;-)

– Ageax
Nov 28 '18 at 23:38




1




1





@Ageax I've already warned you. Being in my head can be a scary place.

– Shawn
Nov 29 '18 at 0:08





@Ageax I've already warned you. Being in my head can be a scary place.

– Shawn
Nov 29 '18 at 0:08


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53446096%2fcoldfusion-find-if-column-has-value%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

TypeError: fit_transform() missing 1 required positional argument: 'X'