Quantcast
Channel: VBForums - Database Development
Viewing all articles
Browse latest Browse all 2918

[RESOLVED] MS SQL stored procedure with ORDER BY parameter

$
0
0
I want users to have the option to ORDER BY data before creating the report.

ORDER BY is defined with 3 values (database column names); this call goes with the stored procedure.

Code:

ALTER PROCEDURE [dbo].[vozila_report_all_servisi]
@MySelect varchar(8),
@MyOrderBy varchar(20)

AS

SELECT reg,dat_unosa,zaduzio,marka,model,godiste,gorivo,zapremina,kilometri,trenutni_kilometri,ukupno_kilometri,vrsta_vozila,vrsta_servisa,servis,detaljno,ukupno_placeno
FROM vozila_servisi
WHERE @MySelect=RIGHT(MG, 4)
ORDER BY
CASE
WHEN @MyOrderBy ='Reg Oznaka' THEN reg
WHEN @MyOrderBy='Marka' THEN marka
ELSE dat_unosa
 
END

For some reason, I get errors and don't understand why they show. The same error appears if the stored procedure is triggered via the app, or if I execute the stored procedure using MSSMS. Pictures should provide more about this problem.

So basically, I want the SELECT statement to be ORDERED by how the user has chosen before the report is created.

Name:  errorapp.png
Views: 42
Size:  18.0 KBName:  errormssms.jpg
Views: 42
Size:  18.8 KB

MG varchar(8), marka varchar(200), dat_unosa date. It bothers me that the query wants to convert the string to date/time, which is unnecessary. It only needs to use that column and order by its values.
Attached Images
  

Viewing all articles
Browse latest Browse all 2918

Latest Images

Trending Articles



Latest Images