Hi,
has fewer columns than were specified in the column list
I am guessing that the issue is with this part, but until you provide the queries to preproduce the scenario, there is no much sense in discussion:
CROSS APPLY Demographics.nodes('//XM:StoreSurvey') as StoreSurvey(AnnualSales,AnnualRevenue)
Your Demographics.nodes
probably returns only one column but you configured it to a SET of two columns StoreSurvey(AnnualSales,AnnualRevenue)
and this make no sense.
Note! You use tables which we do not have, so we cannot reproduce your scenario.
You must provide queries to create the relevant table(s) and insert sample rows so we will be able to discuss and test
If the above is not clear and did not solve your issue then please provide (1) queries to create the relevant table(s) and insert some sample data. (2) What version of SQL Server you use. (3) according to the sample data, what is your expected result. (4) please describe hop you got your expected result out of the sample data.
By the way, You declare a CTE named XMLNAMESPACES and you never use it
More detailed information
SQL Server queries returns a tabular structure = set of rows that all the rows has the same structure. You cannot have different number of columns in rows in the same tabular structure. or insert more columns that the table has (insert 3 columns to table with 2 columns for example) and so on. This error usually raise when you have incompatibility in the number of rows in the set of columns that you use with the number of column in the entity which you want to insert these.
Here are some more demo of such type of issues, which can help you understand it:
use tempdb
GO
/************************************************* */
CREATE TABLE [dbo].[Customer] (
[CustomerID] INT NOT NULL IDENTITY(1, 1),
[CustomerName] VARCHAR(100),
[State] CHAR(2)
)
GO
CREATE VIEW [dbo].[Customer_NY] (
[CustomerID], [CustomerName], [State]
)
AS
SELECT [CustomerID], [CustomerName]
FROM [dbo].[Customer]
GO
-- Issue: 'Customer_NY' has fewer columns than were specified in the column list.
--> A: You cannot configure the view with 3 column but only use 2 columns in the select
/************************************************* */
CREATE TABLE T (id int)
GO
INSERT T(id) VALUES (1,'s')
GO
-- There are fewer columns in the INSERT statement than values specified in the VALUES clause. The number of values in the VALUES clause must match the number of columns specified in the INSERT statement.
/************************************************* */