-4

我不断收到此错误

关键字 from 附近的语法不正确

当我尝试运行此查询时。我对 SQL 相当陌生,所以我可能错过了一些东西。我正在使用 SQL Server 2008

基本上我想要做的是让列lastreturndate具有 value = Checkoutdate + Loanperiod。由于loanperiod在不同的表中,我创建了一个包含Checkoutdate, lastreturndate, loanperiod.

我正在努力实现的示例

Checkoutdate    loanperiod      lastreturndate

  2012-1-01          3           2012-4-01

SQL 代码:

DECLARE @lastreturndate DATETIME
SET @lastreturndate = dateadd(month,loanperiod,CheckOutDate)
INSERT INTO Loan(lastreturndate) VALUES (@lastreturndate) 
FROM dbo.returndateview

dbo.returndateview= 是我创建的视图

图片链接 http://imageshack.us/a/img69/3048/68810818.png到ERD

注意item_details中的category应该叫做loan period

链接到我的观点http://img194.imageshack.us/img194/8200/viewsm.png

4

2 回答 2

1

代替

INSERT INTO Loan(lastreturndate) VALUES (@lastreturndate) 
FROM dbo.returndateview

尝试

insert into Loan(lastreturndate)
select ___ from dbo.returndateview where ___ = @lastreturndate

(您需要使用视图中的列填写_)。

..或者它可能很简单:

DECLARE @lastreturndate DATETIME
SELECT @lastreturndate = dateadd(month,loanperiod,CheckOutDate) FROM dbo.returndateview
INSERT INTO Loan(lastreturndate) VALUES (@lastreturndate) 

根据您上次的编辑,我认为这就是您想要的:

update loan set
    lastReturndate = dateadd(month, detail.loanperiod, CheckOutDate)
from loan
join item on loan.barcode = item.barcode
join item_detals detail on item.isbn = detail.isbn

如果您需要您的观点,我相信您需要将其更改为:

create view returnDateView as
select
   detail.loanperiod,
   loan.CheckOutDate,
   loan.LastReturndate
from loan
join item on loan.barcode = item.barcode
join item_detals detail on item.isbn = detail.isbn
于 2013-02-24T11:30:35.567 回答
1

我想你正在尝试做一个UPDATE. 假设您的视图和 Loan 表是相关的loanid,您可以尝试

Update L set L.lastreturndate = dateadd(month,v.loanperiod,v.CheckOutDate)
From Loat L join dbo.returndateview v
            on L.loanId = v.loanId --You should have this relation

或者,如果您可以将视图重新设计为

create view dbo.returndateview
as
  select Checkoutdate,loanperiod,
       dateadd(month,loanperiod,CheckOutDate) lastreturndate
  from YourTable
于 2013-02-24T11:36:36.720 回答