我想通過一個視圖來測驗對表“專案”的訪問,該視圖將實作代碼當前為單租戶的多租戶操作。
所以我將 Items 重命名為 _Items 并著手創建一個名為 Items 的視圖。如果我做一個簡單的 SELECT * FROM _Items ,它就可以作業,而且代碼也不明智。然后我嘗試將 items 表與 users 表連接起來,這樣我就可以通過視圖測驗多租戶訪問,但是我遇到了一個我不明白的錯誤。
這是查詢:
CREATE VIEW [dbo].[Items] AS
SELECT * FROM _Items I INNER JOIN Users U
ON I.TenantId = U.TenantId
WHERE U.UserName = SUSER_SNAME()
SELECT * FROM 中的星號被標記并懸停在它上面會產生:SQL71508 :: The model already has an element that has the same name dbo.Items.Id.
在 VS2017 中單擊更新會為每一列產生相同的錯誤。
SQL71508 :: The model already has an element that has the same name dbo.Items.Id.
SQL71508 :: The model already has an element that has the same name dbo.Items.Id.
SQL71508 :: The model already has an element that has the same name dbo.Items.Active.
SQL71508 :: The model already has an element that has the same name dbo.Items.TenantId.
SQL71508 :: The model already has an element that has the same name dbo.Items.Active.
SQL71508 :: The model already has an element that has the same name dbo.Items.TenantId.
我究竟做錯了什么!
TIA
uj5u.com熱心網友回復:
如果您只想讓視圖包含 _Items 中的列,按用戶過濾,請嘗試
CREATE OR ALTER VIEW [dbo].[Items] AS
SELECT I.* FROM _Items I INNER JOIN Users U
ON I.TenantId = U.TenantId
WHERE U.UserName = SUSER_SNAME()
您的 SQL 將從用戶和 _Items 中獲取所有列,并且您肯定在它們之間至少有一個具有相同名稱的列,即 TennantId,即您要加入的列。
uj5u.com熱心網友回復:
VIEW您嘗試創建的似乎已經存在。使用CREATE OR ALTER VIEW而不是CREATE VIEW覆寫它。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/431242.html
上一篇:C-無法釋放分配的結構
下一篇:如何用SQL中的兩個表替換值?
