Here is some test code. The results (in this one simple case using SQL 2012) are:
I hope I'm making sense.
IF OBJECT_ID('dbo.test', 'U') IS NOT NULL
DROP TABLE dbo.test;
IF OBJECT_ID('dbo.vtest','V') IS NOT NULL
DROP VIEW dbo.vtest;
IF OBJECT_ID('dbo.vtest_star','V') IS NOT NULL
DROP VIEW dbo.vtest_star;
CREATE TABLE test (
old_id int,
old_name varchar(100),
old_address varchar(100),
old_phone varchar(15)
)
INSERT INTO test(old_id, old_name, old_address, old_phone) VALUES
(1, 'Name 1', 'Address 1', '502-555-1111'),
(2, 'Name 2', 'Address 2', '502-555-2222'),
(3, 'Name 3', 'Address 3', '812-555-3333'),
(4, 'Name 4', 'Address 4', '812-555-4444'),
(5, 'Name 5', 'Address 5', '812-555-5555')
GO
-- create a view with a * in the CTE and explicit fields in the query
CREATE VIEW vtest AS
WITH t AS (SELECT * FROM TEST WHERE old_phone LIKE '812%')
SELECT old_id, old_name, old_address, old_phone FROM t
GO
-- create a view with a * in the CTE and the query
CREATE VIEW vtest_star AS
WITH t AS (SELECT * FROM TEST WHERE old_phone LIKE '812%')
SELECT * FROM t
GO
-- this correctly returns all records where the phone number starts with 812
SELECT * FROM vtest
-- this correctly returns all records where the phone number starts with 812
SELECT * FROM vtest_star
-- re-create the test table with a new field in the second position "new_type"
IF OBJECT_ID('dbo.test', 'U') IS NOT NULL
DROP TABLE dbo.test;
CREATE TABLE test (
old_id int,
new_type varchar(20), -- new column
old_name varchar(100),
old_address varchar(100),
old_phone varchar(15)
)
GO
INSERT INTO test(old_id, new_type, old_name, old_address, old_phone) VALUES
(1, 'Type A', 'Name 1', 'Address 1', '502-555-1111'),
(2, 'Type B', 'Name 2', 'Address 2', '502-555-2222'),
(3, 'Type B', 'Name 3', 'Address 3', '812-555-3333'),
(4, 'Type B', 'Name 4', 'Address 4', '812-555-4444'),
(5, 'Type A', 'Name 5', 'Address 5', '812-555-5555')
GO
-- this correctly returns all records where the phone number starts with 812
SELECT * FROM vtest
-- this returns the correct records, but the data are in the wrong fields
SELECT * FROM vtest_star