Assistance with SQL Server SELECT query
I've just started to learn SQL and would be very thankful if you can help with SELECT query. There are input variables: @p_IPN
and @p_SecondName
, the table has SECONDNAME
, FIRSTNAME
, MIDDLENAME
and IPN
columns.
I need to perform search in my table by the specified input characters with the following conditions:
if none of variables is specified, the query should return the entire table;
if
@p_IPN
OR@p_SecondName
is specified, the query should perform search byIPN LIKE @p_IPN OR SECONDNAME LIKE @p_SecondName
;if both variables are specified, the query should return rows with input characters of
@p_IPN
AND@p_SecondName
(IPN LIKE @p_IPN AND SECONDNAME LIKE @p_SecondName
)
I used the following query:
IF (@p_IPN IS NULL AND @p_SecondName IS NULL)
SELECT
CAST(IPN AS NVARCHAR(MAX)) AS IPN, FIRSTNAME, SECONDNAME, MIDDLENAME
FROM
dbo.BE_BlackList
ELSE IF (@p_IPN IS NULL)
SELECT
CAST(IPN AS NVARCHAR(MAX)) AS IPN, FIRSTNAME, SECONDNAME, MIDDLENAME
FROM
dbo.BE_BlackList
WHERE
SECONDNAME LIKE @p_SecondName
ELSE IF (@p_SecondName IS NULL)
SELECT
CAST(IPN AS NVARCHAR(MAX)) AS IPN, FIRSTNAME, SECONDNAME, MIDDLENAME
FROM
dbo.BE_BlackList
WHERE
IPN LIKE @p_IPN
ELSE
SELECT
CAST(IPN AS NVARCHAR(MAX)) AS IPN, FIRSTNAME, SECONDNAME, MIDDLENAME
FROM
dbo.BE_BlackList
WHERE
IPN LIKE @p_IPN AND SECONDNAME LIKE @p_SecondName
It works well, still, I need to have the same result using SELECT query.
I tried this:
SELECT CASE WHEN @p_IPN IS NULL AND @p_Secondname IS NULL THEN
(SELECT cast (IPN AS nvarchar(max)) AS IPN, FIRSTNAME, MIDDLENAME, SECONDNAME FROM dbo.BE_BlackList)
WHEN @p_SecondName IS NULL THEN
(SELECT cast(IPN AS nvarchar(max)) AS IPN, FIRSTNAME, MIDDLENAME, SECONDNAME FROM dbo.BE_BlackList
WHERE IPN LIKE @p_IPN)
WHEN @p_IPN IS NULL THEN
(SELECT cast(IPN AS nvarchar(max)) AS IPN, FIRSTNAME, MIDDLENAME, SECONDNAME FROM dbo.BE_BlackList
WHERE SECONDNAME LIKE @p_Secondname)
ELSE
(SELECT cast (IPN AS nvarchar(max)) AS IPN, FIRSTNAME, MIDDLENAME, SECONDNAME FROM dbo.BE_BlackList
WHERE SECONDNAME LIKE @p_Secondname AND IPN LIKE @p_IPN)
END
FROM dbo.BE_BlackList
I get this error:
Msg 116, Level 16, State 1, Line 10
Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.
I am not sure how to use CASE WHEN EXISTS
here (if possible at all). Could you help, please?
sql sql-server
add a comment |
I've just started to learn SQL and would be very thankful if you can help with SELECT query. There are input variables: @p_IPN
and @p_SecondName
, the table has SECONDNAME
, FIRSTNAME
, MIDDLENAME
and IPN
columns.
I need to perform search in my table by the specified input characters with the following conditions:
if none of variables is specified, the query should return the entire table;
if
@p_IPN
OR@p_SecondName
is specified, the query should perform search byIPN LIKE @p_IPN OR SECONDNAME LIKE @p_SecondName
;if both variables are specified, the query should return rows with input characters of
@p_IPN
AND@p_SecondName
(IPN LIKE @p_IPN AND SECONDNAME LIKE @p_SecondName
)
I used the following query:
IF (@p_IPN IS NULL AND @p_SecondName IS NULL)
SELECT
CAST(IPN AS NVARCHAR(MAX)) AS IPN, FIRSTNAME, SECONDNAME, MIDDLENAME
FROM
dbo.BE_BlackList
ELSE IF (@p_IPN IS NULL)
SELECT
CAST(IPN AS NVARCHAR(MAX)) AS IPN, FIRSTNAME, SECONDNAME, MIDDLENAME
FROM
dbo.BE_BlackList
WHERE
SECONDNAME LIKE @p_SecondName
ELSE IF (@p_SecondName IS NULL)
SELECT
CAST(IPN AS NVARCHAR(MAX)) AS IPN, FIRSTNAME, SECONDNAME, MIDDLENAME
FROM
dbo.BE_BlackList
WHERE
IPN LIKE @p_IPN
ELSE
SELECT
CAST(IPN AS NVARCHAR(MAX)) AS IPN, FIRSTNAME, SECONDNAME, MIDDLENAME
FROM
dbo.BE_BlackList
WHERE
IPN LIKE @p_IPN AND SECONDNAME LIKE @p_SecondName
It works well, still, I need to have the same result using SELECT query.
I tried this:
SELECT CASE WHEN @p_IPN IS NULL AND @p_Secondname IS NULL THEN
(SELECT cast (IPN AS nvarchar(max)) AS IPN, FIRSTNAME, MIDDLENAME, SECONDNAME FROM dbo.BE_BlackList)
WHEN @p_SecondName IS NULL THEN
(SELECT cast(IPN AS nvarchar(max)) AS IPN, FIRSTNAME, MIDDLENAME, SECONDNAME FROM dbo.BE_BlackList
WHERE IPN LIKE @p_IPN)
WHEN @p_IPN IS NULL THEN
(SELECT cast(IPN AS nvarchar(max)) AS IPN, FIRSTNAME, MIDDLENAME, SECONDNAME FROM dbo.BE_BlackList
WHERE SECONDNAME LIKE @p_Secondname)
ELSE
(SELECT cast (IPN AS nvarchar(max)) AS IPN, FIRSTNAME, MIDDLENAME, SECONDNAME FROM dbo.BE_BlackList
WHERE SECONDNAME LIKE @p_Secondname AND IPN LIKE @p_IPN)
END
FROM dbo.BE_BlackList
I get this error:
Msg 116, Level 16, State 1, Line 10
Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.
I am not sure how to use CASE WHEN EXISTS
here (if possible at all). Could you help, please?
sql sql-server
Sounds like a Catch-all query. I suggest having a read up on these: Catch-all Queries, followed by Revisiting Catch-all Queries.
– Larnu
Jan 3 at 15:50
Hi Lina, You are thinking of SQL like other 3 GL Languages (e.g. Java, C#, etc.). It is a 4 GL language and is used differently.
– LeWoody
Jan 3 at 15:54
Thank @Larnu , the article is helpful!
– Lina Senchenko
Jan 3 at 16:43
add a comment |
I've just started to learn SQL and would be very thankful if you can help with SELECT query. There are input variables: @p_IPN
and @p_SecondName
, the table has SECONDNAME
, FIRSTNAME
, MIDDLENAME
and IPN
columns.
I need to perform search in my table by the specified input characters with the following conditions:
if none of variables is specified, the query should return the entire table;
if
@p_IPN
OR@p_SecondName
is specified, the query should perform search byIPN LIKE @p_IPN OR SECONDNAME LIKE @p_SecondName
;if both variables are specified, the query should return rows with input characters of
@p_IPN
AND@p_SecondName
(IPN LIKE @p_IPN AND SECONDNAME LIKE @p_SecondName
)
I used the following query:
IF (@p_IPN IS NULL AND @p_SecondName IS NULL)
SELECT
CAST(IPN AS NVARCHAR(MAX)) AS IPN, FIRSTNAME, SECONDNAME, MIDDLENAME
FROM
dbo.BE_BlackList
ELSE IF (@p_IPN IS NULL)
SELECT
CAST(IPN AS NVARCHAR(MAX)) AS IPN, FIRSTNAME, SECONDNAME, MIDDLENAME
FROM
dbo.BE_BlackList
WHERE
SECONDNAME LIKE @p_SecondName
ELSE IF (@p_SecondName IS NULL)
SELECT
CAST(IPN AS NVARCHAR(MAX)) AS IPN, FIRSTNAME, SECONDNAME, MIDDLENAME
FROM
dbo.BE_BlackList
WHERE
IPN LIKE @p_IPN
ELSE
SELECT
CAST(IPN AS NVARCHAR(MAX)) AS IPN, FIRSTNAME, SECONDNAME, MIDDLENAME
FROM
dbo.BE_BlackList
WHERE
IPN LIKE @p_IPN AND SECONDNAME LIKE @p_SecondName
It works well, still, I need to have the same result using SELECT query.
I tried this:
SELECT CASE WHEN @p_IPN IS NULL AND @p_Secondname IS NULL THEN
(SELECT cast (IPN AS nvarchar(max)) AS IPN, FIRSTNAME, MIDDLENAME, SECONDNAME FROM dbo.BE_BlackList)
WHEN @p_SecondName IS NULL THEN
(SELECT cast(IPN AS nvarchar(max)) AS IPN, FIRSTNAME, MIDDLENAME, SECONDNAME FROM dbo.BE_BlackList
WHERE IPN LIKE @p_IPN)
WHEN @p_IPN IS NULL THEN
(SELECT cast(IPN AS nvarchar(max)) AS IPN, FIRSTNAME, MIDDLENAME, SECONDNAME FROM dbo.BE_BlackList
WHERE SECONDNAME LIKE @p_Secondname)
ELSE
(SELECT cast (IPN AS nvarchar(max)) AS IPN, FIRSTNAME, MIDDLENAME, SECONDNAME FROM dbo.BE_BlackList
WHERE SECONDNAME LIKE @p_Secondname AND IPN LIKE @p_IPN)
END
FROM dbo.BE_BlackList
I get this error:
Msg 116, Level 16, State 1, Line 10
Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.
I am not sure how to use CASE WHEN EXISTS
here (if possible at all). Could you help, please?
sql sql-server
I've just started to learn SQL and would be very thankful if you can help with SELECT query. There are input variables: @p_IPN
and @p_SecondName
, the table has SECONDNAME
, FIRSTNAME
, MIDDLENAME
and IPN
columns.
I need to perform search in my table by the specified input characters with the following conditions:
if none of variables is specified, the query should return the entire table;
if
@p_IPN
OR@p_SecondName
is specified, the query should perform search byIPN LIKE @p_IPN OR SECONDNAME LIKE @p_SecondName
;if both variables are specified, the query should return rows with input characters of
@p_IPN
AND@p_SecondName
(IPN LIKE @p_IPN AND SECONDNAME LIKE @p_SecondName
)
I used the following query:
IF (@p_IPN IS NULL AND @p_SecondName IS NULL)
SELECT
CAST(IPN AS NVARCHAR(MAX)) AS IPN, FIRSTNAME, SECONDNAME, MIDDLENAME
FROM
dbo.BE_BlackList
ELSE IF (@p_IPN IS NULL)
SELECT
CAST(IPN AS NVARCHAR(MAX)) AS IPN, FIRSTNAME, SECONDNAME, MIDDLENAME
FROM
dbo.BE_BlackList
WHERE
SECONDNAME LIKE @p_SecondName
ELSE IF (@p_SecondName IS NULL)
SELECT
CAST(IPN AS NVARCHAR(MAX)) AS IPN, FIRSTNAME, SECONDNAME, MIDDLENAME
FROM
dbo.BE_BlackList
WHERE
IPN LIKE @p_IPN
ELSE
SELECT
CAST(IPN AS NVARCHAR(MAX)) AS IPN, FIRSTNAME, SECONDNAME, MIDDLENAME
FROM
dbo.BE_BlackList
WHERE
IPN LIKE @p_IPN AND SECONDNAME LIKE @p_SecondName
It works well, still, I need to have the same result using SELECT query.
I tried this:
SELECT CASE WHEN @p_IPN IS NULL AND @p_Secondname IS NULL THEN
(SELECT cast (IPN AS nvarchar(max)) AS IPN, FIRSTNAME, MIDDLENAME, SECONDNAME FROM dbo.BE_BlackList)
WHEN @p_SecondName IS NULL THEN
(SELECT cast(IPN AS nvarchar(max)) AS IPN, FIRSTNAME, MIDDLENAME, SECONDNAME FROM dbo.BE_BlackList
WHERE IPN LIKE @p_IPN)
WHEN @p_IPN IS NULL THEN
(SELECT cast(IPN AS nvarchar(max)) AS IPN, FIRSTNAME, MIDDLENAME, SECONDNAME FROM dbo.BE_BlackList
WHERE SECONDNAME LIKE @p_Secondname)
ELSE
(SELECT cast (IPN AS nvarchar(max)) AS IPN, FIRSTNAME, MIDDLENAME, SECONDNAME FROM dbo.BE_BlackList
WHERE SECONDNAME LIKE @p_Secondname AND IPN LIKE @p_IPN)
END
FROM dbo.BE_BlackList
I get this error:
Msg 116, Level 16, State 1, Line 10
Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.
I am not sure how to use CASE WHEN EXISTS
here (if possible at all). Could you help, please?
sql sql-server
sql sql-server
edited Jan 3 at 15:58
marc_s
583k13011241270
583k13011241270
asked Jan 3 at 15:48
Lina SenchenkoLina Senchenko
334
334
Sounds like a Catch-all query. I suggest having a read up on these: Catch-all Queries, followed by Revisiting Catch-all Queries.
– Larnu
Jan 3 at 15:50
Hi Lina, You are thinking of SQL like other 3 GL Languages (e.g. Java, C#, etc.). It is a 4 GL language and is used differently.
– LeWoody
Jan 3 at 15:54
Thank @Larnu , the article is helpful!
– Lina Senchenko
Jan 3 at 16:43
add a comment |
Sounds like a Catch-all query. I suggest having a read up on these: Catch-all Queries, followed by Revisiting Catch-all Queries.
– Larnu
Jan 3 at 15:50
Hi Lina, You are thinking of SQL like other 3 GL Languages (e.g. Java, C#, etc.). It is a 4 GL language and is used differently.
– LeWoody
Jan 3 at 15:54
Thank @Larnu , the article is helpful!
– Lina Senchenko
Jan 3 at 16:43
Sounds like a Catch-all query. I suggest having a read up on these: Catch-all Queries, followed by Revisiting Catch-all Queries.
– Larnu
Jan 3 at 15:50
Sounds like a Catch-all query. I suggest having a read up on these: Catch-all Queries, followed by Revisiting Catch-all Queries.
– Larnu
Jan 3 at 15:50
Hi Lina, You are thinking of SQL like other 3 GL Languages (e.g. Java, C#, etc.). It is a 4 GL language and is used differently.
– LeWoody
Jan 3 at 15:54
Hi Lina, You are thinking of SQL like other 3 GL Languages (e.g. Java, C#, etc.). It is a 4 GL language and is used differently.
– LeWoody
Jan 3 at 15:54
Thank @Larnu , the article is helpful!
– Lina Senchenko
Jan 3 at 16:43
Thank @Larnu , the article is helpful!
– Lina Senchenko
Jan 3 at 16:43
add a comment |
2 Answers
2
active
oldest
votes
Instead of using IF ELSE, you could use WHERE for your condition filter
SELECT
CAST(IPN AS NVARCHAR(MAX)) AS IPN, FIRSTNAME, SECONDNAME, MIDDLENAME
FROM
dbo.BE_BlackList
WHERE
(@p_IPN IS NULL OR IPN LIKE @p_IPN)
AND
(@p_SecondName IS NULL OR SECONDNAME LIKE @p_SecondName)
add a comment |
The issue is with your CASE WHEN
syntax. Think of CASE WHEN
as a column. In other words, SELECT CASE WHEN a = b, THEN c FROM dbo.BE_BlackList
You are saying SELECT CASE WHEN a = b then SELECT c from dbo.BE_Blacklist
- Notice there are two select statements here.
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54025583%2fassistance-with-sql-server-select-query%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Instead of using IF ELSE, you could use WHERE for your condition filter
SELECT
CAST(IPN AS NVARCHAR(MAX)) AS IPN, FIRSTNAME, SECONDNAME, MIDDLENAME
FROM
dbo.BE_BlackList
WHERE
(@p_IPN IS NULL OR IPN LIKE @p_IPN)
AND
(@p_SecondName IS NULL OR SECONDNAME LIKE @p_SecondName)
add a comment |
Instead of using IF ELSE, you could use WHERE for your condition filter
SELECT
CAST(IPN AS NVARCHAR(MAX)) AS IPN, FIRSTNAME, SECONDNAME, MIDDLENAME
FROM
dbo.BE_BlackList
WHERE
(@p_IPN IS NULL OR IPN LIKE @p_IPN)
AND
(@p_SecondName IS NULL OR SECONDNAME LIKE @p_SecondName)
add a comment |
Instead of using IF ELSE, you could use WHERE for your condition filter
SELECT
CAST(IPN AS NVARCHAR(MAX)) AS IPN, FIRSTNAME, SECONDNAME, MIDDLENAME
FROM
dbo.BE_BlackList
WHERE
(@p_IPN IS NULL OR IPN LIKE @p_IPN)
AND
(@p_SecondName IS NULL OR SECONDNAME LIKE @p_SecondName)
Instead of using IF ELSE, you could use WHERE for your condition filter
SELECT
CAST(IPN AS NVARCHAR(MAX)) AS IPN, FIRSTNAME, SECONDNAME, MIDDLENAME
FROM
dbo.BE_BlackList
WHERE
(@p_IPN IS NULL OR IPN LIKE @p_IPN)
AND
(@p_SecondName IS NULL OR SECONDNAME LIKE @p_SecondName)
answered Jan 3 at 16:12
EricZEricZ
5,3772228
5,3772228
add a comment |
add a comment |
The issue is with your CASE WHEN
syntax. Think of CASE WHEN
as a column. In other words, SELECT CASE WHEN a = b, THEN c FROM dbo.BE_BlackList
You are saying SELECT CASE WHEN a = b then SELECT c from dbo.BE_Blacklist
- Notice there are two select statements here.
add a comment |
The issue is with your CASE WHEN
syntax. Think of CASE WHEN
as a column. In other words, SELECT CASE WHEN a = b, THEN c FROM dbo.BE_BlackList
You are saying SELECT CASE WHEN a = b then SELECT c from dbo.BE_Blacklist
- Notice there are two select statements here.
add a comment |
The issue is with your CASE WHEN
syntax. Think of CASE WHEN
as a column. In other words, SELECT CASE WHEN a = b, THEN c FROM dbo.BE_BlackList
You are saying SELECT CASE WHEN a = b then SELECT c from dbo.BE_Blacklist
- Notice there are two select statements here.
The issue is with your CASE WHEN
syntax. Think of CASE WHEN
as a column. In other words, SELECT CASE WHEN a = b, THEN c FROM dbo.BE_BlackList
You are saying SELECT CASE WHEN a = b then SELECT c from dbo.BE_Blacklist
- Notice there are two select statements here.
answered Jan 3 at 15:59
aGuyaGuy
6191633
6191633
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54025583%2fassistance-with-sql-server-select-query%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Sounds like a Catch-all query. I suggest having a read up on these: Catch-all Queries, followed by Revisiting Catch-all Queries.
– Larnu
Jan 3 at 15:50
Hi Lina, You are thinking of SQL like other 3 GL Languages (e.g. Java, C#, etc.). It is a 4 GL language and is used differently.
– LeWoody
Jan 3 at 15:54
Thank @Larnu , the article is helpful!
– Lina Senchenko
Jan 3 at 16:43