There appears to be something up with Microsoft's database engines, and I can't easily find anything on Google about it so thought I'd stick a post together and see if anyone else has found the same problem.
I have so far tried this against SQL Server 2000, Access 2003 and Access 2007, and all give the same result.
The idea: Add a literal string field to a resultset, based on the setting of a boolean flag in the data....
The setup:
CREATE TABLE [TABLE1] (
[Description] [nvarchar] (255) NULL ,
[Flag] [bit] NOT NULL
)
CREATE VIEW Q_FLAGSET AS
SELECT Description, 'Extra Info to display, ONLY when flag set' AS Result
FROM TABLE1
WHERE (Flag <> 0)
CREATE VIEW Q_FLAG_RESULT AS
SELECT TABLE1.*, Q_FLAGSET.Result
FROM TABLE1 LEFT OUTER JOIN Q_FLAGSET ON TABLE1.Description = Q_FLAGSET.Description
INSERT INTO TABLE1 ([Description],[Flag]) VALUES ('Flag Is Set',1)
INSERT INTO TABLE1 ([Description],[Flag]) VALUES ('Flag Is Not Set',0)
The Expected result from Q_FLAG_RESULT:
[Description], [Flag], [Result]
'Flag Is Set', 1, 'Extra Info to display, ONLY when flag set'
'Flag Is Not Set', 0, <NULL>
The Actual result from Q_FLAG_RESULT:
[Description], [Flag], [Result]
'Flag Is Set', 1, 'Extra Info to display, ONLY when flag set'
'Flag Is Not Set', 0, 'Extra Info to display, ONLY when flag set'
If you also include Q_FLAGSET.Description AS FlagSetDescription as part of the returned fields of Q_FLAG_RESULT, that field returns <NULL> as expected, which leads me to the workaround I have ended up using...
I added in a third query which returns the string liternal, so that Q_FLAGSET appears (to the query engines) to be returning data from a field, i.e.
CREATE Q_RETURN_LITERAL AS
SELECT DISTINCT TABLE1.Flag, 'Extra Info to display, ONLY when flag set' AS Result
FROM TABLE1
WHERE (Flag<>0)
CREATE Q_FLAGSET AS
SELECT TABLE1.Description, Q_RETURN_LITERAL.Result
FROM TABLE1 INNER JOIN Q_RETURN_LITERAL ON TABLE1.Flag = Q_RETURN_LITERAL.Flag
WHERE (TABLE1.Flag<>0)
Soooo.... am I crazy to think the first setup should have worked? And is this "feature" of SQL Server and Access documented anywhere?