Subject | Re: [firebird-support] Is this possible in SQL? |
---|---|
Author | Pavel Menshchikov |
Post date | 2005-02-21T18:40:29Z |
Hello Joe,
JM> I have two tables in a master-detail relationship. For example:
JM> Customers
JM> ===============
JM> Custid
JM> Field1
JM> Field2
JM> Field3
JM> Custnotes
JM> Sales
JM> ===============
JM> Saleid
JM> Custid
JM> FieldA
JM> What I want to do is take all of the Sales for each customer and dump the
JM> contents of ALL of the FieldA values into the single Custnotes field. So,
JM> if a customer has 4 Sales records, then that customer's Custnotes field
JM> would have 4 FieldA values contatenated together.
JM> Is there a way to do this in SQL?
You may try it via a stored procedure - something like the following:
-----
SET TERM ^ ;
CREATE PROCEDURE UpdateCustomer (
iCustID INTEGER)
AS
DECLARE VARIABLE toPush VARCHAR(1000); /* choose the max length yourself */
DECLARE VARIABLE TMP VARCHAR(10); /* must be the same type as Sales.FieldA */
begin
toPush='';
for select FieldA from Sales
where Custid = :iCustID /* probably order by... */ into :tmp do
toPush = toPush || tmp || '
'; /* cancatenate using CRLF; you may need to cast tmp to varchar */
update Customers set Custnotes = :toPush
where Custid = :iCustID;
end^
SET TERM ; ^
-----
To update customer notes perform
-----
execute procedure UpdateCustomer(<required customer id>);
-----
HTH
--
Best regards,
Pavel Menshchikov
http://www.ls-software.com
JM> I have two tables in a master-detail relationship. For example:
JM> Customers
JM> ===============
JM> Custid
JM> Field1
JM> Field2
JM> Field3
JM> Custnotes
JM> Sales
JM> ===============
JM> Saleid
JM> Custid
JM> FieldA
JM> What I want to do is take all of the Sales for each customer and dump the
JM> contents of ALL of the FieldA values into the single Custnotes field. So,
JM> if a customer has 4 Sales records, then that customer's Custnotes field
JM> would have 4 FieldA values contatenated together.
JM> Is there a way to do this in SQL?
You may try it via a stored procedure - something like the following:
-----
SET TERM ^ ;
CREATE PROCEDURE UpdateCustomer (
iCustID INTEGER)
AS
DECLARE VARIABLE toPush VARCHAR(1000); /* choose the max length yourself */
DECLARE VARIABLE TMP VARCHAR(10); /* must be the same type as Sales.FieldA */
begin
toPush='';
for select FieldA from Sales
where Custid = :iCustID /* probably order by... */ into :tmp do
toPush = toPush || tmp || '
'; /* cancatenate using CRLF; you may need to cast tmp to varchar */
update Customers set Custnotes = :toPush
where Custid = :iCustID;
end^
SET TERM ; ^
-----
To update customer notes perform
-----
execute procedure UpdateCustomer(<required customer id>);
-----
HTH
--
Best regards,
Pavel Menshchikov
http://www.ls-software.com