How to insert asc/desc in addParameter for sorting in SQL query
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I have this code
dBCommand.AddParameter("@SORT", "asc");
Below is my query.
SELECT TOP(1) RECEIPTID [RECEIPTID], TRANSTIME [TRANSTIME] FROM RetailTransactionTable
WHERE TRANSDATE = @TRANSDATE
AND RECEIPTID != ''
AND STORE = @STORE
AND TERMINAL = @TERMINAL
ORDER BY TRANSTIME @SORT
How can I place an asc and desc in AddParameter? Because it throws an error I guess because it is not in the right format?
c# sql sql-server
add a comment |
I have this code
dBCommand.AddParameter("@SORT", "asc");
Below is my query.
SELECT TOP(1) RECEIPTID [RECEIPTID], TRANSTIME [TRANSTIME] FROM RetailTransactionTable
WHERE TRANSDATE = @TRANSDATE
AND RECEIPTID != ''
AND STORE = @STORE
AND TERMINAL = @TERMINAL
ORDER BY TRANSTIME @SORT
How can I place an asc and desc in AddParameter? Because it throws an error I guess because it is not in the right format?
c# sql sql-server
You can't do that - modify query instead.
– Reniuz
Nov 16 '18 at 14:41
You would have to use dynamic sql to do this.
– Sean Lange
Nov 16 '18 at 14:42
Any other workaround that is close to that code? I just wanna replace that @SORT. because I wanna order by asc and desc.
– beginnerlaravelvue
Nov 16 '18 at 14:42
Maybe I should make 2 queries that have asc and desc.
– beginnerlaravelvue
Nov 16 '18 at 14:44
1
You could use two queries and an IF statement. But be careful, that can lead some performance issues. sqlinthewild.co.za/index.php/2009/09/15/…
– Sean Lange
Nov 16 '18 at 14:44
add a comment |
I have this code
dBCommand.AddParameter("@SORT", "asc");
Below is my query.
SELECT TOP(1) RECEIPTID [RECEIPTID], TRANSTIME [TRANSTIME] FROM RetailTransactionTable
WHERE TRANSDATE = @TRANSDATE
AND RECEIPTID != ''
AND STORE = @STORE
AND TERMINAL = @TERMINAL
ORDER BY TRANSTIME @SORT
How can I place an asc and desc in AddParameter? Because it throws an error I guess because it is not in the right format?
c# sql sql-server
I have this code
dBCommand.AddParameter("@SORT", "asc");
Below is my query.
SELECT TOP(1) RECEIPTID [RECEIPTID], TRANSTIME [TRANSTIME] FROM RetailTransactionTable
WHERE TRANSDATE = @TRANSDATE
AND RECEIPTID != ''
AND STORE = @STORE
AND TERMINAL = @TERMINAL
ORDER BY TRANSTIME @SORT
How can I place an asc and desc in AddParameter? Because it throws an error I guess because it is not in the right format?
c# sql sql-server
c# sql sql-server
asked Nov 16 '18 at 14:39
beginnerlaravelvuebeginnerlaravelvue
528
528
You can't do that - modify query instead.
– Reniuz
Nov 16 '18 at 14:41
You would have to use dynamic sql to do this.
– Sean Lange
Nov 16 '18 at 14:42
Any other workaround that is close to that code? I just wanna replace that @SORT. because I wanna order by asc and desc.
– beginnerlaravelvue
Nov 16 '18 at 14:42
Maybe I should make 2 queries that have asc and desc.
– beginnerlaravelvue
Nov 16 '18 at 14:44
1
You could use two queries and an IF statement. But be careful, that can lead some performance issues. sqlinthewild.co.za/index.php/2009/09/15/…
– Sean Lange
Nov 16 '18 at 14:44
add a comment |
You can't do that - modify query instead.
– Reniuz
Nov 16 '18 at 14:41
You would have to use dynamic sql to do this.
– Sean Lange
Nov 16 '18 at 14:42
Any other workaround that is close to that code? I just wanna replace that @SORT. because I wanna order by asc and desc.
– beginnerlaravelvue
Nov 16 '18 at 14:42
Maybe I should make 2 queries that have asc and desc.
– beginnerlaravelvue
Nov 16 '18 at 14:44
1
You could use two queries and an IF statement. But be careful, that can lead some performance issues. sqlinthewild.co.za/index.php/2009/09/15/…
– Sean Lange
Nov 16 '18 at 14:44
You can't do that - modify query instead.
– Reniuz
Nov 16 '18 at 14:41
You can't do that - modify query instead.
– Reniuz
Nov 16 '18 at 14:41
You would have to use dynamic sql to do this.
– Sean Lange
Nov 16 '18 at 14:42
You would have to use dynamic sql to do this.
– Sean Lange
Nov 16 '18 at 14:42
Any other workaround that is close to that code? I just wanna replace that @SORT. because I wanna order by asc and desc.
– beginnerlaravelvue
Nov 16 '18 at 14:42
Any other workaround that is close to that code? I just wanna replace that @SORT. because I wanna order by asc and desc.
– beginnerlaravelvue
Nov 16 '18 at 14:42
Maybe I should make 2 queries that have asc and desc.
– beginnerlaravelvue
Nov 16 '18 at 14:44
Maybe I should make 2 queries that have asc and desc.
– beginnerlaravelvue
Nov 16 '18 at 14:44
1
1
You could use two queries and an IF statement. But be careful, that can lead some performance issues. sqlinthewild.co.za/index.php/2009/09/15/…
– Sean Lange
Nov 16 '18 at 14:44
You could use two queries and an IF statement. But be careful, that can lead some performance issues. sqlinthewild.co.za/index.php/2009/09/15/…
– Sean Lange
Nov 16 '18 at 14:44
add a comment |
4 Answers
4
active
oldest
votes
Within the query, you have an if statement which orders the results based on the @sort parameter value.
something like:
if @sort = 'ascending'
select ... order by transtime asc
else
select ... order by transtime desc
it doesn't look like a stored proc to me... but: the exact same approach could be used with/without stored proc
– Marc Gravell♦
Nov 16 '18 at 14:46
@MarcGravell yes that makes sense, thanks.
– user1666620
Nov 16 '18 at 14:49
add a comment |
The sort direction is part of the query itself and cannot be parameterized. You would need to either construct the query to embed ASC
or DESC
into the SQL (essentially string.Format
or concatenation, but just of the ASC
/DESC
part - not of the input values), or have 2 different completed queries that you issue.
Alright. Thanks!
– beginnerlaravelvue
Nov 16 '18 at 14:46
add a comment |
It is unfortunately not possible. You can set if-else structure to handle this issue like:
if(orderParameter == "asc")
{
SELECT TOP(1) RECEIPTID [RECEIPTID], TRANSTIME [TRANSTIME] FROM RetailTransactionTable
WHERE TRANSDATE = @TRANSDATE
AND RECEIPTID != ''
AND STORE = @STORE
AND TERMINAL = @TERMINAL
ORDER BY TRANSTIME ASC
}
else
{
SELECT TOP(1) RECEIPTID [RECEIPTID], TRANSTIME [TRANSTIME] FROM RetailTransactionTable
WHERE TRANSDATE = @TRANSDATE
AND RECEIPTID != ''
AND STORE = @STORE
AND TERMINAL = @TERMINAL
ORDER BY TRANSTIME DESC
}
Or you can use String.Format(..) to set your query like
string orderParameter = "asc";
string sql = String.Format("SELECT TOP 1 RECEIPTID [RECEIPTID], TRANSTIME [TRANSTIME]
FROM RetailTransactionTable
WHERE TRANSDATE = @TRANSDATE
AND RECEIPTID != ''
AND STORE = @STORE
AND TERMINAL = @TERMINAL
ORDER BY TRANSTIME {0}",orderParameter);
add a comment |
If by chance TRANSTIME
has a datatype of datetime
you can convert to a float and then apply a factor.
HOWEVER, I suspect a conditional approach would be more performant.
SELECT TOP(1) RECEIPTID [RECEIPTID], TRANSTIME [TRANSTIME] FROM RetailTransactionTable
WHERE TRANSDATE = @TRANSDATE
AND RECEIPTID != ''
AND STORE = @STORE
AND TERMINAL = @TERMINAL
ORDER BY convert(float,TRANSTIME) * IIF(@Sort='asc',1,-1)
add a comment |
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
});
}
});
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%2f53339959%2fhow-to-insert-asc-desc-in-addparameter-for-sorting-in-sql-query%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
Within the query, you have an if statement which orders the results based on the @sort parameter value.
something like:
if @sort = 'ascending'
select ... order by transtime asc
else
select ... order by transtime desc
it doesn't look like a stored proc to me... but: the exact same approach could be used with/without stored proc
– Marc Gravell♦
Nov 16 '18 at 14:46
@MarcGravell yes that makes sense, thanks.
– user1666620
Nov 16 '18 at 14:49
add a comment |
Within the query, you have an if statement which orders the results based on the @sort parameter value.
something like:
if @sort = 'ascending'
select ... order by transtime asc
else
select ... order by transtime desc
it doesn't look like a stored proc to me... but: the exact same approach could be used with/without stored proc
– Marc Gravell♦
Nov 16 '18 at 14:46
@MarcGravell yes that makes sense, thanks.
– user1666620
Nov 16 '18 at 14:49
add a comment |
Within the query, you have an if statement which orders the results based on the @sort parameter value.
something like:
if @sort = 'ascending'
select ... order by transtime asc
else
select ... order by transtime desc
Within the query, you have an if statement which orders the results based on the @sort parameter value.
something like:
if @sort = 'ascending'
select ... order by transtime asc
else
select ... order by transtime desc
edited Nov 16 '18 at 14:46
answered Nov 16 '18 at 14:45
user1666620user1666620
4,3501224
4,3501224
it doesn't look like a stored proc to me... but: the exact same approach could be used with/without stored proc
– Marc Gravell♦
Nov 16 '18 at 14:46
@MarcGravell yes that makes sense, thanks.
– user1666620
Nov 16 '18 at 14:49
add a comment |
it doesn't look like a stored proc to me... but: the exact same approach could be used with/without stored proc
– Marc Gravell♦
Nov 16 '18 at 14:46
@MarcGravell yes that makes sense, thanks.
– user1666620
Nov 16 '18 at 14:49
it doesn't look like a stored proc to me... but: the exact same approach could be used with/without stored proc
– Marc Gravell♦
Nov 16 '18 at 14:46
it doesn't look like a stored proc to me... but: the exact same approach could be used with/without stored proc
– Marc Gravell♦
Nov 16 '18 at 14:46
@MarcGravell yes that makes sense, thanks.
– user1666620
Nov 16 '18 at 14:49
@MarcGravell yes that makes sense, thanks.
– user1666620
Nov 16 '18 at 14:49
add a comment |
The sort direction is part of the query itself and cannot be parameterized. You would need to either construct the query to embed ASC
or DESC
into the SQL (essentially string.Format
or concatenation, but just of the ASC
/DESC
part - not of the input values), or have 2 different completed queries that you issue.
Alright. Thanks!
– beginnerlaravelvue
Nov 16 '18 at 14:46
add a comment |
The sort direction is part of the query itself and cannot be parameterized. You would need to either construct the query to embed ASC
or DESC
into the SQL (essentially string.Format
or concatenation, but just of the ASC
/DESC
part - not of the input values), or have 2 different completed queries that you issue.
Alright. Thanks!
– beginnerlaravelvue
Nov 16 '18 at 14:46
add a comment |
The sort direction is part of the query itself and cannot be parameterized. You would need to either construct the query to embed ASC
or DESC
into the SQL (essentially string.Format
or concatenation, but just of the ASC
/DESC
part - not of the input values), or have 2 different completed queries that you issue.
The sort direction is part of the query itself and cannot be parameterized. You would need to either construct the query to embed ASC
or DESC
into the SQL (essentially string.Format
or concatenation, but just of the ASC
/DESC
part - not of the input values), or have 2 different completed queries that you issue.
answered Nov 16 '18 at 14:44
Marc Gravell♦Marc Gravell
794k19821622567
794k19821622567
Alright. Thanks!
– beginnerlaravelvue
Nov 16 '18 at 14:46
add a comment |
Alright. Thanks!
– beginnerlaravelvue
Nov 16 '18 at 14:46
Alright. Thanks!
– beginnerlaravelvue
Nov 16 '18 at 14:46
Alright. Thanks!
– beginnerlaravelvue
Nov 16 '18 at 14:46
add a comment |
It is unfortunately not possible. You can set if-else structure to handle this issue like:
if(orderParameter == "asc")
{
SELECT TOP(1) RECEIPTID [RECEIPTID], TRANSTIME [TRANSTIME] FROM RetailTransactionTable
WHERE TRANSDATE = @TRANSDATE
AND RECEIPTID != ''
AND STORE = @STORE
AND TERMINAL = @TERMINAL
ORDER BY TRANSTIME ASC
}
else
{
SELECT TOP(1) RECEIPTID [RECEIPTID], TRANSTIME [TRANSTIME] FROM RetailTransactionTable
WHERE TRANSDATE = @TRANSDATE
AND RECEIPTID != ''
AND STORE = @STORE
AND TERMINAL = @TERMINAL
ORDER BY TRANSTIME DESC
}
Or you can use String.Format(..) to set your query like
string orderParameter = "asc";
string sql = String.Format("SELECT TOP 1 RECEIPTID [RECEIPTID], TRANSTIME [TRANSTIME]
FROM RetailTransactionTable
WHERE TRANSDATE = @TRANSDATE
AND RECEIPTID != ''
AND STORE = @STORE
AND TERMINAL = @TERMINAL
ORDER BY TRANSTIME {0}",orderParameter);
add a comment |
It is unfortunately not possible. You can set if-else structure to handle this issue like:
if(orderParameter == "asc")
{
SELECT TOP(1) RECEIPTID [RECEIPTID], TRANSTIME [TRANSTIME] FROM RetailTransactionTable
WHERE TRANSDATE = @TRANSDATE
AND RECEIPTID != ''
AND STORE = @STORE
AND TERMINAL = @TERMINAL
ORDER BY TRANSTIME ASC
}
else
{
SELECT TOP(1) RECEIPTID [RECEIPTID], TRANSTIME [TRANSTIME] FROM RetailTransactionTable
WHERE TRANSDATE = @TRANSDATE
AND RECEIPTID != ''
AND STORE = @STORE
AND TERMINAL = @TERMINAL
ORDER BY TRANSTIME DESC
}
Or you can use String.Format(..) to set your query like
string orderParameter = "asc";
string sql = String.Format("SELECT TOP 1 RECEIPTID [RECEIPTID], TRANSTIME [TRANSTIME]
FROM RetailTransactionTable
WHERE TRANSDATE = @TRANSDATE
AND RECEIPTID != ''
AND STORE = @STORE
AND TERMINAL = @TERMINAL
ORDER BY TRANSTIME {0}",orderParameter);
add a comment |
It is unfortunately not possible. You can set if-else structure to handle this issue like:
if(orderParameter == "asc")
{
SELECT TOP(1) RECEIPTID [RECEIPTID], TRANSTIME [TRANSTIME] FROM RetailTransactionTable
WHERE TRANSDATE = @TRANSDATE
AND RECEIPTID != ''
AND STORE = @STORE
AND TERMINAL = @TERMINAL
ORDER BY TRANSTIME ASC
}
else
{
SELECT TOP(1) RECEIPTID [RECEIPTID], TRANSTIME [TRANSTIME] FROM RetailTransactionTable
WHERE TRANSDATE = @TRANSDATE
AND RECEIPTID != ''
AND STORE = @STORE
AND TERMINAL = @TERMINAL
ORDER BY TRANSTIME DESC
}
Or you can use String.Format(..) to set your query like
string orderParameter = "asc";
string sql = String.Format("SELECT TOP 1 RECEIPTID [RECEIPTID], TRANSTIME [TRANSTIME]
FROM RetailTransactionTable
WHERE TRANSDATE = @TRANSDATE
AND RECEIPTID != ''
AND STORE = @STORE
AND TERMINAL = @TERMINAL
ORDER BY TRANSTIME {0}",orderParameter);
It is unfortunately not possible. You can set if-else structure to handle this issue like:
if(orderParameter == "asc")
{
SELECT TOP(1) RECEIPTID [RECEIPTID], TRANSTIME [TRANSTIME] FROM RetailTransactionTable
WHERE TRANSDATE = @TRANSDATE
AND RECEIPTID != ''
AND STORE = @STORE
AND TERMINAL = @TERMINAL
ORDER BY TRANSTIME ASC
}
else
{
SELECT TOP(1) RECEIPTID [RECEIPTID], TRANSTIME [TRANSTIME] FROM RetailTransactionTable
WHERE TRANSDATE = @TRANSDATE
AND RECEIPTID != ''
AND STORE = @STORE
AND TERMINAL = @TERMINAL
ORDER BY TRANSTIME DESC
}
Or you can use String.Format(..) to set your query like
string orderParameter = "asc";
string sql = String.Format("SELECT TOP 1 RECEIPTID [RECEIPTID], TRANSTIME [TRANSTIME]
FROM RetailTransactionTable
WHERE TRANSDATE = @TRANSDATE
AND RECEIPTID != ''
AND STORE = @STORE
AND TERMINAL = @TERMINAL
ORDER BY TRANSTIME {0}",orderParameter);
answered Nov 16 '18 at 14:48
Eray BalkanliEray Balkanli
4,57852347
4,57852347
add a comment |
add a comment |
If by chance TRANSTIME
has a datatype of datetime
you can convert to a float and then apply a factor.
HOWEVER, I suspect a conditional approach would be more performant.
SELECT TOP(1) RECEIPTID [RECEIPTID], TRANSTIME [TRANSTIME] FROM RetailTransactionTable
WHERE TRANSDATE = @TRANSDATE
AND RECEIPTID != ''
AND STORE = @STORE
AND TERMINAL = @TERMINAL
ORDER BY convert(float,TRANSTIME) * IIF(@Sort='asc',1,-1)
add a comment |
If by chance TRANSTIME
has a datatype of datetime
you can convert to a float and then apply a factor.
HOWEVER, I suspect a conditional approach would be more performant.
SELECT TOP(1) RECEIPTID [RECEIPTID], TRANSTIME [TRANSTIME] FROM RetailTransactionTable
WHERE TRANSDATE = @TRANSDATE
AND RECEIPTID != ''
AND STORE = @STORE
AND TERMINAL = @TERMINAL
ORDER BY convert(float,TRANSTIME) * IIF(@Sort='asc',1,-1)
add a comment |
If by chance TRANSTIME
has a datatype of datetime
you can convert to a float and then apply a factor.
HOWEVER, I suspect a conditional approach would be more performant.
SELECT TOP(1) RECEIPTID [RECEIPTID], TRANSTIME [TRANSTIME] FROM RetailTransactionTable
WHERE TRANSDATE = @TRANSDATE
AND RECEIPTID != ''
AND STORE = @STORE
AND TERMINAL = @TERMINAL
ORDER BY convert(float,TRANSTIME) * IIF(@Sort='asc',1,-1)
If by chance TRANSTIME
has a datatype of datetime
you can convert to a float and then apply a factor.
HOWEVER, I suspect a conditional approach would be more performant.
SELECT TOP(1) RECEIPTID [RECEIPTID], TRANSTIME [TRANSTIME] FROM RetailTransactionTable
WHERE TRANSDATE = @TRANSDATE
AND RECEIPTID != ''
AND STORE = @STORE
AND TERMINAL = @TERMINAL
ORDER BY convert(float,TRANSTIME) * IIF(@Sort='asc',1,-1)
answered Nov 16 '18 at 14:55
John CappellettiJohn Cappelletti
47.4k62647
47.4k62647
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%2f53339959%2fhow-to-insert-asc-desc-in-addparameter-for-sorting-in-sql-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
You can't do that - modify query instead.
– Reniuz
Nov 16 '18 at 14:41
You would have to use dynamic sql to do this.
– Sean Lange
Nov 16 '18 at 14:42
Any other workaround that is close to that code? I just wanna replace that @SORT. because I wanna order by asc and desc.
– beginnerlaravelvue
Nov 16 '18 at 14:42
Maybe I should make 2 queries that have asc and desc.
– beginnerlaravelvue
Nov 16 '18 at 14:44
1
You could use two queries and an IF statement. But be careful, that can lead some performance issues. sqlinthewild.co.za/index.php/2009/09/15/…
– Sean Lange
Nov 16 '18 at 14:44