1

I am executing a stored procedure with below set of sql statements in the following order but getting this weird error : "Column name or number of supplied values does not match table definition."

I am having a stage table "staging_Table" which is getting loaded with incoming data files and doesn't have given columns "id","Name","dob","age_diffrence";

staging_Table_1 has same columns as staging_Table including four extra columns "id","Name","dob","age_diffrence" which is being calculated using CTE.

My stored procedure has given set of statements and are defined in following order:

--Dropping below four extra columns from staging_Table_1 so that it can be loaded from initial staging_Table.

Alter Table staging_Table_1
Drop Column "id","Name","dob","age_diffrence";

--now both the insert and select table has same set of columns

insert into staging_Table_1 select * from staging_Table;

--creating a backup and truncate the staging_Table_1

select * into staging_Table_2 from staging_Table_1;
Truncate Table staging_Table_1;

--Adding extra columns to staging_Table_1

Alter Table staging_Table_1
Add [id] [varchar] (20),[Name] [varchar] (100),[DOB] [Date],[Age_diffrence] [int];

with reporting_group_id as
(
select a.*,b.acc_num as id
from staging_Table_2 a left join (select distinct id,acct_num from sometable) as b
on a.id=b.id
)
,reporting_group_name as 
(
select a.*,b.group_name as Name
from reporting_group_id a left join from sometable b
on a.id=b.id
)
,reporting_group_age as 
(
select a.*,b.dob as dob, datediff(year,b.dob,b.start_date) -
   ( case when dateadd(year, datediff(year,b.dob,b.start_date),b.b.dob)
     > b.start_date then 1 else 0 end
    ) as age_diffrence

from reporting_group_name a left join(select dob,start_date from some table)b
on a.id=b.id
)

Insert into staging_table_1
select * from reporting_group_age;
end
)

I am not sure what's happening here though both the insert and select has same column structures and are executing fine as individually.

Update-: I tried different approach as listing of too many columns on select and insert was making stored procedure code lengthy. since it has been marked as duplicate so cannot answer it. Updating the question with solution works in my case. I break my code in two stored procedures and replaced CTE with temp tables.

Sub Stored procedure code:

use somedb
GO
Create procedure [dbo].[sp_group_1]
as
begin

Alter Table staging_Table_1
Drop Column "id","Name","dob","age_diffrence";

insert into staging_Table_1 select * from staging_Table;
select * into staging_Table_2 from staging_Table_1;

Truncate Table staging_Table_1;

Alter Table staging_Table_1
Add [id] [varchar] (20),[Name] [varchar] (100),[DOB] [Date],[Age_diffrence] [int];

end

Main Stored procedure code:

use somedb;
GO

create procedure [dbo].[sp_group_2]
as 
begin

Exec [dbo].[sp_group_1];

select * into #reporting_group_id from
(
select a.*,b.acc_num as id
from staging_Table_2 a left join (select distinct id,acct_num from sometable) as b
on a.id=b.id
) as M
select * into #reporting_group_name  from
(
select a.*,b.group_name as Name
from #reporting_group_id a left join from sometable b
on a.id=b.id
) as M

select * into #reporting_group_age from
(
select a.*,b.dob as dob, datediff(year,b.dob,b.start_date) -
   ( case when dateadd(year, datediff(year,b.dob,b.start_date),b.b.dob)
     > b.start_date then 1 else 0 end
    ) as age_diffrence

from #reporting_group_name a left join(select dob,start_date from sometable)b
on a.id=b.id
)
)as M

Insert into staging_table_1
select * from #reporting_group_age;
end
vikrant rana
  • 4,509
  • 6
  • 32
  • 72
  • 4
    List the columns for the `inset` and the error will go away. – Gordon Linoff Jul 01 '19 at 10:36
  • 1
    Why is this not a duplicate of say [Column name or number of supplied values does not match table definition](https://stackoverflow.com/questions/1152932/column-name-or-number-of-supplied-values-does-not-match-table-definition) or [SQL Server error: Column name or number of supplied values does not match table definition](https://stackoverflow.com/questions/27720625/sql-server-error-column-name-or-number-of-supplied-values-does-not-match-table), when it clearly appears to be? – Thom A Jul 01 '19 at 10:39
  • Thanks. In the last statement or do I need to list at every select ? Insert into staging_table_1 select * from reporting_group_age; – vikrant rana Jul 01 '19 at 10:39
  • 3
    You need to list the columns in the `INSERT` clause, and the `SELECT` (instead of using `*`). – Thom A Jul 01 '19 at 10:40
  • I tried different approach since the list of columns was too large and making stored procedure code lengthy. The new approach is working fine without listing the column names for insert and select.. not able to post as an answer – vikrant rana Jul 01 '19 at 12:14
  • @Larnu . Thanks. Yeah Listing of columns can solve the issue but this solution working fine in my case. – vikrant rana Jul 01 '19 at 12:32
  • 1
    In general it's not advisable to use * in a select statement. There are some case where it's useful, I use it a lot for general queries in SSMS, but inside a stored procedure I'd always advise expanding the list out. If you feel it is making your code too long, the It's always possible to split a single procedure into several sub procedures. Although in this instance you'd need to declare the temp tables at top level and `Insert into` at sub rather than `select into`. – Matthew Baker Jul 01 '19 at 13:02
  • @Matthew Baker. Thanks. for ease of doing I used it. was running short of time to test the logic. :-( I will change the code to list all the columns later. – vikrant rana Jul 01 '19 at 13:09
  • 1
    In a compiled SP, certainly not using `*` is not the right choice. If the underlying table is changed, the `*` won't reflect that unless the object using `*` is recompiled as well. – Thom A Jul 01 '19 at 13:20
  • insert into staging_Table_1 select * from staging_Table and select * into staging_Table_2 from staging_Table_1;. I finally changed my code to list all the columns for above statements.. as suggested. It's working find now.. Thanks to all ☺️ – vikrant rana Jul 02 '19 at 19:12

0 Answers0