Getting error “Only one expression can be specified in the select list…”











up vote
0
down vote

favorite












I am trying to add a column to a view with the following code:



SELECT ';' + CONTEXT as DriverNotes,

(STUFF((SELECT CustomerID FROM Notes E2 WHERE E2.CustomerID IN (Notes.CustomerID)
FOR XML PATH(''), TYPE, ROOT).value('root[1]','nvarchar(5)'),1,0,'')) as CustomerID FROM NOTES


On it's own it works just fine. When I run it within a View however, I get the following error:



"Only one expression can be specified in the select list when the subquery is not introduced with EXISTS."



I realize that the code here is trying to call two columns and that is what is giving me the error, but I only want one, and that would be CONTEXT. I need this to correlate with Notes.CustomerID but without the column appearing in the query.



I am still quite new to this, so any help would be greatly appreciated.










share|improve this question
























  • What do you mean when you "run it in a view"? What is the definition of your View?
    – Larnu
    yesterday










  • Sorry I am probably using the wrong terminology, but I have dropped and created a view with the intention of adding the code I posted above. When I go to execute the query, I get the error mentioned above.
    – Moodsy
    yesterday

















up vote
0
down vote

favorite












I am trying to add a column to a view with the following code:



SELECT ';' + CONTEXT as DriverNotes,

(STUFF((SELECT CustomerID FROM Notes E2 WHERE E2.CustomerID IN (Notes.CustomerID)
FOR XML PATH(''), TYPE, ROOT).value('root[1]','nvarchar(5)'),1,0,'')) as CustomerID FROM NOTES


On it's own it works just fine. When I run it within a View however, I get the following error:



"Only one expression can be specified in the select list when the subquery is not introduced with EXISTS."



I realize that the code here is trying to call two columns and that is what is giving me the error, but I only want one, and that would be CONTEXT. I need this to correlate with Notes.CustomerID but without the column appearing in the query.



I am still quite new to this, so any help would be greatly appreciated.










share|improve this question
























  • What do you mean when you "run it in a view"? What is the definition of your View?
    – Larnu
    yesterday










  • Sorry I am probably using the wrong terminology, but I have dropped and created a view with the intention of adding the code I posted above. When I go to execute the query, I get the error mentioned above.
    – Moodsy
    yesterday















up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am trying to add a column to a view with the following code:



SELECT ';' + CONTEXT as DriverNotes,

(STUFF((SELECT CustomerID FROM Notes E2 WHERE E2.CustomerID IN (Notes.CustomerID)
FOR XML PATH(''), TYPE, ROOT).value('root[1]','nvarchar(5)'),1,0,'')) as CustomerID FROM NOTES


On it's own it works just fine. When I run it within a View however, I get the following error:



"Only one expression can be specified in the select list when the subquery is not introduced with EXISTS."



I realize that the code here is trying to call two columns and that is what is giving me the error, but I only want one, and that would be CONTEXT. I need this to correlate with Notes.CustomerID but without the column appearing in the query.



I am still quite new to this, so any help would be greatly appreciated.










share|improve this question















I am trying to add a column to a view with the following code:



SELECT ';' + CONTEXT as DriverNotes,

(STUFF((SELECT CustomerID FROM Notes E2 WHERE E2.CustomerID IN (Notes.CustomerID)
FOR XML PATH(''), TYPE, ROOT).value('root[1]','nvarchar(5)'),1,0,'')) as CustomerID FROM NOTES


On it's own it works just fine. When I run it within a View however, I get the following error:



"Only one expression can be specified in the select list when the subquery is not introduced with EXISTS."



I realize that the code here is trying to call two columns and that is what is giving me the error, but I only want one, and that would be CONTEXT. I need this to correlate with Notes.CustomerID but without the column appearing in the query.



I am still quite new to this, so any help would be greatly appreciated.







sql sql-server select






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited yesterday

























asked yesterday









Moodsy

11




11












  • What do you mean when you "run it in a view"? What is the definition of your View?
    – Larnu
    yesterday










  • Sorry I am probably using the wrong terminology, but I have dropped and created a view with the intention of adding the code I posted above. When I go to execute the query, I get the error mentioned above.
    – Moodsy
    yesterday




















  • What do you mean when you "run it in a view"? What is the definition of your View?
    – Larnu
    yesterday










  • Sorry I am probably using the wrong terminology, but I have dropped and created a view with the intention of adding the code I posted above. When I go to execute the query, I get the error mentioned above.
    – Moodsy
    yesterday


















What do you mean when you "run it in a view"? What is the definition of your View?
– Larnu
yesterday




What do you mean when you "run it in a view"? What is the definition of your View?
– Larnu
yesterday












Sorry I am probably using the wrong terminology, but I have dropped and created a view with the intention of adding the code I posted above. When I go to execute the query, I get the error mentioned above.
– Moodsy
yesterday






Sorry I am probably using the wrong terminology, but I have dropped and created a view with the intention of adding the code I posted above. When I go to execute the query, I get the error mentioned above.
– Moodsy
yesterday














1 Answer
1






active

oldest

votes

















up vote
0
down vote













Check this query. I think this is what you want :



SELECT Notes.CustomerId, 
STUFF(
(SELECT ';' + CONTEXT FROM Notes E2
WHERE E2.CustomerId = Notes.CustomerId
FOR XML PATH ('')), 1, 1, ''
) DriverNotes
FROM Notes /*Probably it should be Customer table */
GROUP BY Notes.CustomerId





share|improve this answer





















  • This works similar to the code I posted. It does not work when creating a View though. Thank you for the reply.
    – Moodsy
    yesterday










  • okay. could you add your expected output to your post .
    – Zeki Gumus
    yesterday










  • I did, I also added the View I am trying to edit. The expected output would be to just add a single column "Context" to [dbo].[VCustomerJoinLocations].
    – Moodsy
    yesterday











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',
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%2f53417130%2fgetting-error-only-one-expression-can-be-specified-in-the-select-list%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








up vote
0
down vote













Check this query. I think this is what you want :



SELECT Notes.CustomerId, 
STUFF(
(SELECT ';' + CONTEXT FROM Notes E2
WHERE E2.CustomerId = Notes.CustomerId
FOR XML PATH ('')), 1, 1, ''
) DriverNotes
FROM Notes /*Probably it should be Customer table */
GROUP BY Notes.CustomerId





share|improve this answer





















  • This works similar to the code I posted. It does not work when creating a View though. Thank you for the reply.
    – Moodsy
    yesterday










  • okay. could you add your expected output to your post .
    – Zeki Gumus
    yesterday










  • I did, I also added the View I am trying to edit. The expected output would be to just add a single column "Context" to [dbo].[VCustomerJoinLocations].
    – Moodsy
    yesterday















up vote
0
down vote













Check this query. I think this is what you want :



SELECT Notes.CustomerId, 
STUFF(
(SELECT ';' + CONTEXT FROM Notes E2
WHERE E2.CustomerId = Notes.CustomerId
FOR XML PATH ('')), 1, 1, ''
) DriverNotes
FROM Notes /*Probably it should be Customer table */
GROUP BY Notes.CustomerId





share|improve this answer





















  • This works similar to the code I posted. It does not work when creating a View though. Thank you for the reply.
    – Moodsy
    yesterday










  • okay. could you add your expected output to your post .
    – Zeki Gumus
    yesterday










  • I did, I also added the View I am trying to edit. The expected output would be to just add a single column "Context" to [dbo].[VCustomerJoinLocations].
    – Moodsy
    yesterday













up vote
0
down vote










up vote
0
down vote









Check this query. I think this is what you want :



SELECT Notes.CustomerId, 
STUFF(
(SELECT ';' + CONTEXT FROM Notes E2
WHERE E2.CustomerId = Notes.CustomerId
FOR XML PATH ('')), 1, 1, ''
) DriverNotes
FROM Notes /*Probably it should be Customer table */
GROUP BY Notes.CustomerId





share|improve this answer












Check this query. I think this is what you want :



SELECT Notes.CustomerId, 
STUFF(
(SELECT ';' + CONTEXT FROM Notes E2
WHERE E2.CustomerId = Notes.CustomerId
FOR XML PATH ('')), 1, 1, ''
) DriverNotes
FROM Notes /*Probably it should be Customer table */
GROUP BY Notes.CustomerId






share|improve this answer












share|improve this answer



share|improve this answer










answered yesterday









Zeki Gumus

2816




2816












  • This works similar to the code I posted. It does not work when creating a View though. Thank you for the reply.
    – Moodsy
    yesterday










  • okay. could you add your expected output to your post .
    – Zeki Gumus
    yesterday










  • I did, I also added the View I am trying to edit. The expected output would be to just add a single column "Context" to [dbo].[VCustomerJoinLocations].
    – Moodsy
    yesterday


















  • This works similar to the code I posted. It does not work when creating a View though. Thank you for the reply.
    – Moodsy
    yesterday










  • okay. could you add your expected output to your post .
    – Zeki Gumus
    yesterday










  • I did, I also added the View I am trying to edit. The expected output would be to just add a single column "Context" to [dbo].[VCustomerJoinLocations].
    – Moodsy
    yesterday
















This works similar to the code I posted. It does not work when creating a View though. Thank you for the reply.
– Moodsy
yesterday




This works similar to the code I posted. It does not work when creating a View though. Thank you for the reply.
– Moodsy
yesterday












okay. could you add your expected output to your post .
– Zeki Gumus
yesterday




okay. could you add your expected output to your post .
– Zeki Gumus
yesterday












I did, I also added the View I am trying to edit. The expected output would be to just add a single column "Context" to [dbo].[VCustomerJoinLocations].
– Moodsy
yesterday




I did, I also added the View I am trying to edit. The expected output would be to just add a single column "Context" to [dbo].[VCustomerJoinLocations].
– Moodsy
yesterday


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53417130%2fgetting-error-only-one-expression-can-be-specified-in-the-select-list%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

Trompette piccolo

Slow SSRS Report in dynamic grouping and multiple parameters

Simon Yates (cyclisme)