Række med værdi ID=3 Skal vel sorteres efter række med ID=2 + evt. under kategorier. Ellers burde rækken med ID=1 vel også sorteres efter alle rækker med underkategorier.
Men hvis ovenstående output er OK kan nedenstående erklæring af en Stored Procedure anvendes:
-Procedure Start---------------------------- CREATE PROCEDURE procGetWeirdOrder AS
SELECT ID, SubID, Vaerdi FROM tblEks126051 ORDER BY ISNULL(NULLIF(SubID, 0), ID), subID -Procedure Slut-----------------------------
Min problematik består i at sortere en tabel. Værdien i kolonnen SubID skal sorteres ud fra ID\'et, så ID\'et kommer først og dernæst dets børn, dvs SubID\'erne. F.eks følgende tabel(Ikke sorteret):
Terry>> Ok I\'ll try and give you the best explanation I can,
ID SubID ----------- 1 0 // (ID 1, SubID 0) is first because SubID has no parents. 3 1 // (ID 3, SubID 1) comes next because it has ID 1 as a parent. The value of the SubID indicate parent relationship. 4 1 // (ID 4, SubID 1) has ID 1 as a parent 6 4 // (ID 6, SubID 4) has ID 4 as a parent 7 4 // (ID 7, SubID 4) has ID 4 as a parent 8 7 // (ID 8, SubID 7) has ID 7 as a parent 9 7 // (ID 9, SubID 7) has ID 7 as a parent 5 1 // (ID 5, SubID 1) has ID 1 as a parent 2 0 // (ID 2, SubID 0) has no parent
No! Consider the table structure as a men on a webpage. For example you have the following menu:
- Menu1 - SubMenu1 To Menu1 - SubMenu2 To Menu1 - SubMenu3 To Menu1 - SubSubMenu1 To SubMenu3 - SubSubMenu2 To SubMenu3 - SubSubSubMenu1 To SubSubMenu2 - SubSubSubMenu2 To SubSubMenu2 - Submenu4 To 1 - Menu2 - Menu3
entracore> I havent given up yet (almost) but I think I will need some time to think. Will get back when I have an answer or when I give up (never have yet but there is always a first time)
entracore>I can see EXACTLY what you want. Your menu diagram helped the bricks fall in place.
Problem is how to do it in SQL! Which version of SQL are you using? I think it will be necessary to make a function and its not so easy in 7.0. Things are easier in 2000 but I dont have that installed!!!
I have actually solved this in Access by making a temp table. Where are you using this, in ASP or VB. Perhaps some of the work could be done there?
Ok! what do you intend to use it for, your menu? How many records do you expect in the tabel. If it was done in VBScript and not many records then you shouldnt have any performance problems.
As I have said, I dont think it can be done with a single SQL statement. As far as I can see there has to be a recursive call to a function to traverse into each (menu) adding each element on the way. This could be done in a Function in SQL Server (if it allows recursive calls) As you may well know a recursive call is a function which calls its self. It could also be done in your ASP (VBCript).
I except up 40 or 50 records in my table. I would prefer a Function in SQL. But is that necessary? Couldn\'t it be done using a Cursor or a Loop in my Stored Procedure?
entracore> Do you have Access on your PC and if so do you know how to use it?
I can send you a little \"demo\" so you can maybe see why it snecessaryy to have a recursive call. You can use the debugger and step through the program to see exactly what is happening.
-Procedure Start------------------------------------------------------------ CREATE PROCEDURE procEks126051Build ( @parentID INT, @temptablename VARCHAR(64) ) AS SET NOCOUNT ON
DECLARE @ID INT DECLARE @subID INT DECLARE @vaerdi NVARCHAR(25)
-Procedure Start------------------------------------------------------------ CREATE PROCEDURE procEks126051Sub ( @parentID INT, @subCursor CURSOR VARYING OUTPUT ) AS SET NOCOUNT ON
--!! HER SKAL DU ÆNDRE TABELNAVN !!-- SET @subCursor = CURSOR FOR SELECT ID, subID, vaerdi FROM tblEks126051 WHERE( @parentID <> 0 AND tblEks126051.subID = @parentID ) OR ( @parentID = 0 AND tblEks126051.subID = 0) ORDER BY ID
Hvis din tabel har den struktur og de variabel typer du nævnte i det oprindelige spørgsmål, kan du bruge koden direkte hvis du blot ændrer tabelnavnet (jeg har angivet med en kommentar hvor).
Ellers kan du selv se om du kan finde de korrekte variable der skal skiftes, ellers kan du lige skrive med den rigtige tabeldefinition.
LKP> I was usre I had made a comment earlier this evening, but it isnt to be seen. Anyway, it saves me having to install SQL. How do you call this from say VB or even ASP?
Eftersom den main proceduren ikke returnerer nogen værdier (ud over selvfølgelig en cursor(recordset)), kan du uden videre kalde den med execute kommandoen for en connection.
Var det det du mente?
Entracore>> Også et stort tak for de mange point herfra
LKP>Ingen problem, jeg kan godt skrive på dansk (efter 18 år) men det er MEGET nemmere på engelsk :o)
Ja, det var det jeg mente! Igen tak!
Synes godt om
Ny brugerNybegynder
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.