Click here to Skip to main content
15,894,343 members
Home / Discussions / Database
   

Database

 
AnswerRe: Stroing Word Document in the Database Pin
turbochimp19-Sep-05 19:42
turbochimp19-Sep-05 19:42 
AnswerRe: Stroing Word Document in the Database Pin
enjoycrack19-Sep-05 19:43
enjoycrack19-Sep-05 19:43 
GeneralRe: Stroing Word Document in the Database Pin
Ahsan Askare19-Sep-05 21:06
Ahsan Askare19-Sep-05 21:06 
AnswerRe: Stroing Word Document in the Database Pin
Michael P Butler20-Sep-05 2:34
Michael P Butler20-Sep-05 2:34 
QuestionNewbish Problem Pin
tatchung19-Sep-05 18:30
tatchung19-Sep-05 18:30 
AnswerRe: Newbish Problem Pin
Edbert P19-Sep-05 19:34
Edbert P19-Sep-05 19:34 
GeneralRe: Newbish Problem Pin
tatchung19-Sep-05 20:08
tatchung19-Sep-05 20:08 
GeneralRe: Newbish Problem Pin
Edbert P19-Sep-05 20:39
Edbert P19-Sep-05 20:39 
Well, I'll try to explain as much as I can.
Basically there are 2 functions there (GetModule and Split).
The Split functions is used to convert from text (e.g. '1,2,3,4') into a table with 2 columns (Id, Value).
So, splitting the value of '1,2,3,4' will result in:
Id |  Value
 1     1
 2     2
 3     3
 4     4


This is because SQL statement CAN accept e.g. SELECT * FROM [TABLE] WHERE ID IN '1,2,3,4' but it CAN'T ACCEPT SELECT @ FROM [TABLE] WHERE ID IN @ListOfIDs.
Notice that the only difference is @ListOfIDs which contains the value '1,2,3,4' as varchar/string.
Therefore, @ListOfIDs need to be split into several rows using the Split UDF before it can be used in an SQL WHERE condition.
You don't need to dwell too deep in Split function right now, but if you need further explanation let me know.

The second function, GetModule, is used to get the Id and Module of the parent row (the first ID you specified), and then tries to concatenate the children ids into the result.
CREATE FUNCTION dbo.GetModule
{
       @ListID nvarchar(2000)
}

The code above basically says we want to create a User Defined Function called GetModule belonging to dbo user. It accepts a parameter of nvarchar (or unicode string) with length of 2000.
DECLARE @ChildIDs varchar(100);

The above declares a variable called @ChildIDs which can have a varchar up to 100 in size.
--Concatenate all ChildIDs of all IDs in @ListID. Basically COALESCE is used to replace NULL with '' (empty string)
SELECT @ChildIDs = COALESCE (@ChildIDs + ', ', '') + ChildIDs          
FROM tblModule WHERE ID IN (SELECT value FROM dbo.Split(@ListID)))

The above tries to select all the ChildIDs columns from the tblModule and combines them, so for example if your ID (in the @ListID variable) is 91, then it will result in '911,912,913,915'.
But if your @ListID contains '90, 91' it will return the ChildIDs as a combined string which is '901' + '911,912,913,915' ending up in '901,911,912,913,915'.
COALESCE is basically a function that checks if @ChildIDs is null then return '' instead of returning null. You can replace it with IsNull function.

After we get all the child ids of our rows, we then check whether there are child ids or not.
-- Selects the Parent's ID and ModuleID, then UNION with ChildIDs' result if there are ChildIDs
IF @ChildIDs = null

  SELECT ID, ModuleID                                     --This is to return only parent result if there are no ChildIDs
  FROM tblModule 
  WHERE ID IN (SELECT value FROM dbo.Split(@ListID)

The function above says if there are no child ids, then just return the parents' ID and ModuleID (e.g. if the parent ID is 91, it will return '91', null).
ELSE

  SELECT ID, ModuleID
  FROM tblModule 
  WHERE ID IN (SELECT value FROM dbo.Split(@ListID)

  UNION                                                    --This is the recursive code to union with result of ChildIDs

  SELECT ID, ModuleID
  FROM dbo.GetModule(@ChildIDs)

In the code above we check if there are child ids, then we should combine (UNION) both the parent result and the children results. The children results will be taken by calling the GetModule and passing the children's ids of the parent's id.

AFAIK, IF and ELSE also exists in MySQL as they are standard SQL syntax. Coalesce might not, but you can replace it with IsNull or a similar function that checks if a value is null then return something else.

That's my explanation so far.
Try each part of the code one by one to have a better understanding of them (e.g. the split function, the function to return child ids).
I hope that helps.


Ed
GeneralRe: Newbish Problem Pin
tatchung19-Sep-05 21:09
tatchung19-Sep-05 21:09 
GeneralRe: Newbish Problem Pin
Edbert P20-Sep-05 13:44
Edbert P20-Sep-05 13:44 
QuestionUpdate Command Failing Pin
japel19-Sep-05 0:42
japel19-Sep-05 0:42 
AnswerRe: Update Command Failing Pin
enjoycrack19-Sep-05 0:54
enjoycrack19-Sep-05 0:54 
AnswerRe: Update Command Failing Pin
Luis Alonso Ramos19-Sep-05 8:19
Luis Alonso Ramos19-Sep-05 8:19 
GeneralRe: Update Command Failing Pin
japel19-Sep-05 10:36
japel19-Sep-05 10:36 
AnswerRe: Update Command Failing Pin
Luis Alonso Ramos20-Sep-05 19:49
Luis Alonso Ramos20-Sep-05 19:49 
Questionusing @@IDENTITY (or nething like this)for char datatype(sql server) Pin
utsav_verma18-Sep-05 23:53
utsav_verma18-Sep-05 23:53 
AnswerADDON:using @@IDENTITY (or nething like this)for char datatype(sql server) Pin
utsav_verma19-Sep-05 0:04
utsav_verma19-Sep-05 0:04 
AnswerRe: using @@IDENTITY (or nething like this)for char datatype(sql server) Pin
utsav_verma19-Sep-05 0:22
utsav_verma19-Sep-05 0:22 
GeneralRe: using @@IDENTITY (or nething like this)for char datatype(sql server) Pin
enjoycrack19-Sep-05 0:51
enjoycrack19-Sep-05 0:51 
QuestionMultiple Choice Pin
Leslie Sanford18-Sep-05 19:25
Leslie Sanford18-Sep-05 19:25 
AnswerRe: Multiple Choice Pin
Colin Angus Mackay18-Sep-05 20:26
Colin Angus Mackay18-Sep-05 20:26 
GeneralRe: Multiple Choice Pin
Leslie Sanford19-Sep-05 6:21
Leslie Sanford19-Sep-05 6:21 
AnswerRe: Multiple Choice Pin
Edbert P18-Sep-05 20:31
Edbert P18-Sep-05 20:31 
QuestionSQL union Pin
phokojoe18-Sep-05 5:31
phokojoe18-Sep-05 5:31 
AnswerRe: SQL union Pin
Colin Angus Mackay18-Sep-05 12:00
Colin Angus Mackay18-Sep-05 12:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.