Avatar billede brynil Nybegynder
24. marts 2002 - 15:00 Der er 8 kommentarer og
1 løsning

Query med variable - hvad er der galt?

Kan nogen hjælpe med denne query. Der er ingen reaktion overhovedet på værdien af @Sort, uanset om der indtastes en værdi eller ikke.

SELECT sag.Emne, sag.Dato
FROM sag
WHERE ((sag.Emne) Like "*"+[@Searching]+"*")
ORDER BY [@Sort];

Avatar billede terry Ekspert
24. marts 2002 - 16:03 #1
ORDER By [SomeField]

is the name of the field @Sort? if so you should try to NOt use special characters su cha s the danish ÆæÅ娸 and spaces they just give problems!
Avatar billede terry Ekspert
24. marts 2002 - 16:04 #2
The ORDER BY is one of the fields in the table for example

SELECT sag.Emne, sag.Dato
FROM sag ORDER BY sag.Dato;
Avatar billede brynil Nybegynder
24. marts 2002 - 16:12 #3
Im not sure of what you mean. My table uses danish letters and its no problem so far.
The pronlem is that i can't assign a value to @Sort in my query, just as I do - without problems - with @Searching. If I ask it to sort/order by Dato, there's no problem. It's the variable aspect that don't work.
Avatar billede terry Ekspert
24. marts 2002 - 16:18 #4
I am NOTsyaing that you shouldnt I am saying that it is BEST that you do not!

@Sort IS THIS A FIELD in your table? it MUST BE! It can NOT be a value? (variable)

SELECT * FROM YourTable ORDER BY AField
Avatar billede terry Ekspert
24. marts 2002 - 16:26 #5
When you write [Searching] Then Access Can not find a field with this name so it expects a paramater this is OK in the LIKE clause.
This IS NOT allowed in the ORDER BY clause! It HAS to be one of the fields in your table an dit CAN NOT be entered through a paramater.
If you want to change the sort field then you have to dynamically create your query!
Avatar billede brynil Nybegynder
24. marts 2002 - 16:30 #6
No, sort is a variable. When I run my query in Access, I'm asked to first input a value for @Searching and secondly one for @Sort. It's just that Access dont grab the value of @Sort which I write.
I'm going to use this query from an ASPX-page. And to avoid writing a query for each possible sort-order, I wanted to assign a value through a variable.
Now, if you say it's not possible, maybe I should avoid a query and instead create and execute a sql-string from within ASPX.
Avatar billede terry Ekspert
24. marts 2002 - 16:31 #7
If you make a query using your example

SELECT sag.Emne, sag.Dato
FROM sag
WHERE ((sag.Emne) Like "*"+[@Searching]+"*")
ORDER BY [@Sort];

In query design view select the check Show box in the column where you can see [@sort] and then run the query. You will now see that whet ever you entered is shown in ALL records and that is why the sort has no effect !!!!

So the sort MUST BE A FIELD and NOT a parameter!
Avatar billede brynil Nybegynder
24. marts 2002 - 16:34 #8
I get it. I wasn't aware the ORDER function was different.
Thank you Terry.
Avatar billede terry Ekspert
24. marts 2002 - 17:17 #9
thnak you too :o)
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Dyk ned i databasernes verden på et af vores praksisnære Access-kurser

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester