来自两个具有FK关系的表的SQL视图



我使用SQL Server 2008 R2,我有两个表,如:

create table Client
(
    Id int identity(1,1) Primary Key,
    Name varchar(30) not null,
    LastName varchar(30) not null,
    Tel int not null unique,
    Email varchar(30),
    Uwagi varchar(35),
    Problem bit ,
    Wizyty int
);

create table Wizyta
(
    Id int identity(1,1) Primary Key,
    Data date not null,
    IdClient int not null,
    Opis varchar(30),
    DataZapisu date default(getdate()),
    constraint fk_perWizyta 
       foreign key (IdClient) references Client(Id) on delete cascade
);

我创建了一个视图:

create view Tranzakcje as  
(
    Select 
        Data, (Name+ ' ' + LastName) As Client,  
        Opis, DataZapisu  
    from 
        Wizyta w  
    inner join 
        Klient k on k.Id = w.IdKlient
)

它对我的查询有效,但当我从手机应用程序执行视图时,我会出现错误:

SQLException无效列名Id。

如何解决或如何创建列为Data Client (Name + LastName) Opis, DataZapisu的视图。

您的视图实际上并不包含Id列。如果你需要它,你必须选择你想使用的Id
正如rhholt所评论的:对于Client/Klient表,您似乎在K和C之间切换(字段IdClient/IdKlient也是如此)。。。

最新更新