我有以下查询,现在奇怪的是如果我在我的开发和pre-prod服务器上运行此查询它运行正常。如果我在生产中运行它就会失败。
我已经想通了,如果我只运行Select语句它很高兴但是一旦我尝试插入表变量就会抱怨它。
DECLARE @RESULTS TABLE
(
[Parent] VARCHAR(255)
,[client] VARCHAR(255)
,[ComponentName] VARCHAR(255)
,[DealName] VARCHAR(255)
,[Purchase Date] DATETIME
,[Start Date] DATETIME
,[End Date] DATETIME
,[Value] INT
,[Currency] VARCHAR(255)
,[Brand] VARCHAR(255)
,[Business Unit] VARCHAR(255)
,[Region] VARCHAR(255)
,[DealID] INT
)
INSERT INTO @RESULTS
SELECT DISTINCT
ClientName 'Parent'
,F.ClientID 'client'
,ComponentName
,A.DealName
,CONVERT(SMALLDATETIME , ISNULL(PurchaseDate , '1900-01-01')) 'Purchase Date'
,CONVERT(SMALLDATETIME , ISNULL(StartDate , '1900-01-01')) 'Start Date'
,CONVERT(SMALLDATETIME , ISNULL(EndDate , '1900-01-01')) 'End Date'
,DealValue 'Value'
,D.Currency 'Currency'
,ShortBrand 'Brand'
,G.BU 'Business Unit'
,C.DMRegion 'Region'
,DealID
FROM
LTCDB_admin_tbl_Deals A
INNER JOIN dbo_DM_Brand B
ON A.BrandID = B.ID
INNER JOIN LTCDB_admin_tbl_DM_Region C
ON A.Region = C.ID
INNER JOIN LTCDB_admin_tbl_Currency D
ON A.Currency = D.ID
INNER JOIN LTCDB_admin_tbl_Deal_Clients E
ON A.DealID = E.Deal_ID
INNER JOIN LTCDB_admin_tbl_Clients F
ON E.Client_ID = F.ClientID
INNER JOIN LTCDB_admin_tbl_DM_BU G
ON G.ID = A.BU
INNER JOIN LTCDB_admin_tbl_Deal_Components H
ON A.DealID = H.Deal_ID
INNER JOIN LTCDB_admin_tbl_Components I
ON I.ComponentID = H.Component_ID
WHERE
EndDate != '1899-12-30T00:00:00.000'
AND StartDate < EndDate
AND B.ID IN ( 1 , 2 , 5 , 6 , 7 , 8 , 10 , 12 )
AND C.SalesRegionID IN ( 1 , 3 , 4 , 11 , 16 )
AND A.BU IN ( 1 , 2 , 3 , 4 , 5 , 6 , 8 , 9 , 11 , 12 , 15 , 16 , 19 , 20 , 22 , 23 , 24 , 26 , 28 , 30 )
AND ClientID = 16128
SELECT ... FROM @Results
我收到以下错误
Msg 8180, Level 16, State 1, Line 1
Statement(s) could not be prepared.
Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "Tbl1021.ComponentName" could not be bound.
Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "Tbl1011.Currency" could not be bound.
Msg 207, Level 16, State 1, Line 1
Invalid column name 'Col2454'.
Msg 207, Level 16, State 1, Line 1
Invalid column name 'Col2461'.
Msg 207, Level 16, State 1, Line 1
Invalid column name 'Col2491'.
Msg 207, Level 16, State 1, Line 1
Invalid column name 'Col2490'.
Msg 207, Level 16, State 1, Line 1
Invalid column name 'Col2482'.
Msg 207, Level 16, State 1, Line 1
Invalid column name 'Col2478'.
Msg 207, Level 16, State 1, Line 1
Invalid column name 'Col2477'.
Msg 207, Level 16, State 1, Line 1
Invalid column name 'Col2475'.
编辑 - 编辑 - 编辑 - 编辑 - 编辑 - 编辑 通过消除过程,我发现跟随并想知道是否有人可以对此有所了解。
INNER JOIN LTCDB_admin_tbl_Deal_Components H ON A.DealID = H.Deal_ID
答案 0 :(得分:3)
这些观点中有任何一个吗?我似乎记得在更改视图定义和不运行sp_refreshview之后得到类似的奇怪错误。我无法在任何地方看到“Tbl1021”文本,所以我假设这可能是在View定义中?
如果是这样,这里有一个脚本来刷新所有视图How do I create a stored procedure that calls sp_refreshview for each view in the database?
答案 1 :(得分:1)
好的我还是不知道是什么导致了这个或者正确的答案是什么,但这是我最终做的事情来解决它。
所以我对表中的数据进行了备份,删除了违规表,并使用所有键,索引等重新创建了它,现在恢复了订单。
以前失败的所有查询现在都能完美运行。很奇怪
答案 2 :(得分:0)
尤金 如果不在我的开发系统上运行代码,我要解决的第一件事就是代码中别名的使用不一致 您应该尽可能在良好的编码实践中识别对象{owner} I.E通过在这个例子中使用前缀每个对象与相关的别名A,B,C等..
I.E例如ClientName'Parent'使此A.ClientName为'Parent' 如果此列在表中别名为A
SELECT DISTINCT
ClientName 'Parent'
,F.ClientID 'client'
,ComponentName
,A.DealName
,CONVERT(SMALLDATETIME , ISNULL(PurchaseDate , '1900-01-01')) 'Purchase Date'
,CONVERT(SMALLDATETIME , ISNULL(StartDate , '1900-01-01')) 'Start Date'
,CONVERT(SMALLDATETIME , ISNULL(EndDate , '1900-01-01')) 'End Date'
,DealValue 'Value'
,D.Currency 'Currency'
,ShortBrand 'Brand'
,G.BU 'Business Unit'
,C.DMRegion 'Region'
,DealID
FROM
LTCDB_admin_tbl_Deals A