Here’s my script:
INSERT INTO
AP09
SELECT
cds AS CDSCODE
FROM
P_Schools
WHERE
active = 1;
There are ten columns in the AP09 table. All but the primary key (CDSCODE) are nullable. I just want to insert CDSCODEs from another table, and have the rest default to null. Instead I get an error:
SQL Server Database Error: Insert Error: Column name or number of supplied values does not match table definition.
If I supply the right number of nulls, the insert works. But the Microsoft documentation says (for INSERT command):
The Database Engine automatically provides a value for the column if the column:
--Has an IDENTITY property. The next incremental identity value is used.
--Has a default. The default value for the column is used.
--Has a timestamp data type. The current timestamp value is used.
--Is nullable. A null value is used.
--Is a computed column. The calculated value is used.
Anyone know why this does not seem to be working as advertised? This is costing me a lot of time, as I have to do this for a number of tables! (It’s SQL Server 2005 BTW)
Since the table
AP09andCDSCODEdont have the same number of columns … you need to specify the cols to insert. Try something like this –