Como incluir um array em uma tabela Sql Server
05/12/2007
0
Uso o Delphi 7 e Sql Server (msde) com acesso ADO.
Gostaria de criar uma tabela com um campo de codigo de clientes e doze campos que contivessem o consumo mensal de, por exemplo, consumo de gas.
Tal qual as contas de luz, gas teria que, no 12º mes. ir substituindo o valor do 1º mes.
Como fazer isto? Pensei em uma tabela com codigo de cliente e um array de 12 campos, entretanto não sei como inserir um array em uma tabela.
Agradeço ajuda.
Rounilo
Posts
14/01/2008
Psergio.p
15/01/2008
Djjunior
O melhor seria uma tabela com 3 campos (desc, data e valor) e no seu select você fazer algo do tipo
select top :Meses from
(select desc, data, valor from custos order by desc, data desc) contas
ai dentro do seu seu programa delphi você ajusta esses dados em uma memoryTable
16/01/2008
Aroldo Zanela
Pode ser ainda mais simples que a forma a seguir, entretanto, para garantir a integridade dos dados, eu usaria uma tabela para os meses com uma constraint para a quantidade de linhas e a tabela de consumo seria uma associativa entre cliente e meses.
Veja o exemplo que modelei no PowerDesigner:
/*==============================================================*/ /* DBMS name: Microsoft SQL Server 2000 */ /* Created on: 16/1/2008 16:31:15 */ /*==============================================================*/ alter table CONSUMO drop constraint FK_CONSUMO_RELATIONS_CLIENTES go alter table CONSUMO drop constraint FK_CONSUMO_RELATIONS_MESES go if exists (select 1 from sysindexes where id = object_id(´CLIENTES´) and name = ´CLIENTES_PK´ and indid > 0 and indid < 255) drop index CLIENTES.CLIENTES_PK go if exists (select 1 from sysindexes where id = object_id(´CONSUMO´) and name = ´CONSUMO_PK´ and indid > 0 and indid < 255) drop index CONSUMO.CONSUMO_PK go if exists (select 1 from sysindexes where id = object_id(´MESES´) and name = ´MESES_PK´ and indid > 0 and indid < 255) drop index MESES.MESES_PK go if exists (select 1 from sysobjects where id = object_id(´CLIENTES´) and type = ´U´) drop table CLIENTES go if exists (select 1 from sysobjects where id = object_id(´CONSUMO´) and type = ´U´) drop table CONSUMO go if exists (select 1 from sysobjects where id = object_id(´MESES´) and type = ´U´) drop table MESES go /*==============================================================*/ /* Table: CLIENTES */ /*==============================================================*/ create table CLIENTES ( CLIE_ID int not null, CLIE_NOME varchar(60) not null ) go /*==============================================================*/ /* Index: CLIENTES_PK */ /*==============================================================*/ create unique index CLIENTES_PK on CLIENTES ( CLIE_ID ASC ) go /*==============================================================*/ /* Table: CONSUMO */ /*==============================================================*/ create table CONSUMO ( CONS_ID int not null, CLIE_ID int null, MES_ID int null, CONS_QTDE int not null ) go /*==============================================================*/ /* Index: CONSUMO_PK */ /*==============================================================*/ create unique index CONSUMO_PK on CONSUMO ( CONS_ID ASC ) go /*==============================================================*/ /* Table: MESES */ /*==============================================================*/ create table MESES ( MES_ID int not null constraint CKC_MES_ID_MESES check (MES_ID between 1 and 12), MES_NOME varchar(10) not null ) go /*==============================================================*/ /* Index: MESES_PK */ /*==============================================================*/ create unique index MESES_PK on MESES ( MES_ID ASC ) go alter table CONSUMO add constraint FK_CONSUMO_RELATIONS_CLIENTES foreign key (CLIE_ID) references CLIENTES (CLIE_ID) go alter table CONSUMO add constraint FK_CONSUMO_RELATIONS_MESES foreign key (MES_ID) references MESES (MES_ID) go
Clique aqui para fazer login e interagir na Comunidade :)