Cakkie
November 13th, 2002, 11:45 AM
I have a question concerning SQL.
I'm using SQL Server 2000, and my db structure looks something like this.
tblFields
------
FieldID (Int, autoincrement)
FieldName (varchar(20)
tblFieldData
------------
FieldID (int, fk on tblFields)
DocumentID (int, fk on tblDocuments (not shown here))
FieldData (varchar(255))
I'm using this to add extra fields to my document management system on a flexible way. Each template can be assigned a different set of extra fields.
the problem is that I would like to get the content of the extra fields in one statement.
I came up with a user defined function, that takes the DocID as parameter, and returns a varchar(1000). The problem is that it should return a concatenation of all datafields for that DocID.
Say the data in the tables look like this
tblfields
---------
11, 'customer'
12, 'week'
13, 'year'
tblfielddata
------------
11, 76, 'ALFKI'
12, 77, '48'
12, 77, '2002'
I would like the function to return something like:
"week: 48, year: 2002",
so that my sp that is calling this can return data in a form like
[code]
76, "John","Order","Customer: ALFKI"
77, "Cakkie","Timesheet","week: 48, year 2002"
' rather than as if I were to use a JOIN
76, "John","Order","Customer: ALFKI"
77, "Cakkie","Timesheet","week: 48"
77, "Cakkie","Timesheet","year 2002"
Currently, I'm using a single select statement in the function, giving me the first field it finds only (eg: "week: 48").
I know I can use cursors, but I'd rather not, but if that is the only option, I'll just have to live with that, don't I?
Also note that it is important to return only 1 row per document, since I cannot make any changes to the way the client handles the returned data.
Any suggestions are welcome.
I'm using SQL Server 2000, and my db structure looks something like this.
tblFields
------
FieldID (Int, autoincrement)
FieldName (varchar(20)
tblFieldData
------------
FieldID (int, fk on tblFields)
DocumentID (int, fk on tblDocuments (not shown here))
FieldData (varchar(255))
I'm using this to add extra fields to my document management system on a flexible way. Each template can be assigned a different set of extra fields.
the problem is that I would like to get the content of the extra fields in one statement.
I came up with a user defined function, that takes the DocID as parameter, and returns a varchar(1000). The problem is that it should return a concatenation of all datafields for that DocID.
Say the data in the tables look like this
tblfields
---------
11, 'customer'
12, 'week'
13, 'year'
tblfielddata
------------
11, 76, 'ALFKI'
12, 77, '48'
12, 77, '2002'
I would like the function to return something like:
"week: 48, year: 2002",
so that my sp that is calling this can return data in a form like
[code]
76, "John","Order","Customer: ALFKI"
77, "Cakkie","Timesheet","week: 48, year 2002"
' rather than as if I were to use a JOIN
76, "John","Order","Customer: ALFKI"
77, "Cakkie","Timesheet","week: 48"
77, "Cakkie","Timesheet","year 2002"
Currently, I'm using a single select statement in the function, giving me the first field it finds only (eg: "week: 48").
I know I can use cursors, but I'd rather not, but if that is the only option, I'll just have to live with that, don't I?
Also note that it is important to return only 1 row per document, since I cannot make any changes to the way the client handles the returned data.
Any suggestions are welcome.