我在SQL SERVER中创建了一些视图。我有一个最终视图,我将所有其他视图结合在一起。这是我想用来填充我的tblInfoTable的视图。问题是,我有3个查询SOLD,BOUGHT,RETURNED我需要按FROM和TO日期过滤。出于测试目的,我在设置视图时硬编码了这些日期,以确保它们正常工作。因为我是SQL的新手,所以我没有意识到我无法将参数传递给视图。但是现在它已经完成了,我正在编写一个简单的存储过程,我希望传递这两个值。所以,这是怎么回事......
Truncate table tbl_InfoTable <- table where I want to enter data
INSERT INTO tbl_InfoTable
(UserID, CompanyID, CompanyName, Sold, Bought, Returned)
SELECT
'1234',
CompanyID,
CompanyName,
Sold,
Bought,
Returned
FROM vBegRollFwdCompleted <-- This is my main VIEW from which I'd be able to select everything if it wasnt for the fact that I need filtering in SOLD BOUGHT RETURNED.
Where vAdded.transDate between '12/1/2014' and '12/31/2015' <-- here's where the issue arises. I added a field in my vADDED view TransDate; where; >1/1/1995 since I needed to have it there in order to reference it. But when i write this WHERE statement it tells me that "The multi-part identifier "***vAdded.transDate" could not be bound."***
我只想尝试为ADDED值执行此操作。我不知道问题可能是什么?
答案 0 :(得分:1)
问题是vAdded视图不是查询的一部分,即使它构成了vBegRollFwdCompleted的一部分。您可以更改vBegRollFwdCompleted视图以包含transDate列并更改WHERE子句以引用vBegRollFwdCompleted.transDate,或者您可以将vAdded视图左移加入vBegRollFwdCompleted ...但此时您将拥有SQL服务器多次从该视图中选择。