在下面的情况下,Combine视图中的最终选择失败,有什么想法为什么?为什么sql select语句失败?
子集表没有对应于MasterCodes中不会转换为整数值的行。
CREATE TABLE MasterCodes (
ID INT
, Code VARCHAR(10))
GO
CREATE TABLE Subset (
ID INT)
GO
CREATE VIEW Combine AS
SELECT S.ID
, M.Code
, CAST(M.Code AS INT) IntCode
FROM Subset S
INNER JOIN MasterCodes M ON M.ID = S.ID
GO
INSERT MasterCodes (ID, Code) VALUES (1, '1')
INSERT MasterCodes (ID, Code) VALUES (2, '2')
INSERT MasterCodes (ID, Code) VALUES (3, 'three')
INSERT MasterCodes (ID, Code) VALUES (4, '4')
INSERT Subset (ID) VALUES (1)
INSERT Subset (ID) VALUES (2)
INSERT Subset (ID) VALUES (4)
SELECT * FROM Combine -- 3 rows returned
SELECT * FROM Combine WHERE Code = '2' -- 1 row returned
SELECT * FROM Combine WHERE Code = '3' -- 0 rows returned
SELECT * FROM Combine WHERE IntCode = 2 -- fails, error msg is
Msg 245, Level 16, State 1, Line 15
Conversion failed when converting the varchar value 'three' to data type int.
环境是Sql2k5标准(64位)ON WIN2K3服务器R2
有点格式化会让你更多的回应。 – pugmarx 2009-06-26 21:54:43