Click here to Skip to main content
15,887,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have a couple of tables in my database.
I want to copy/move column Msk709 from table RD11 to table RD21.
I tried the following:
C#
"INSERT INTO RD21 (Msk709) SELECT RD11.Msk709 FROM RD11"

I get the following error:
Invalid column name 'Msk709'

Next try, I tried to make the column:
C#
"ALTER TABLE RD21 ADD Msk709 NVARCHAR(5) ";

and after that:
SQL
"INSERT INTO RD21 (Msk709) SELECT RD11.Msk709 FROM RD11"

I get the error:
Cannot insert the value NULL into column 'AnställningsNummer', table 'KompetensMatris.dbo.RD21'; column does not allow nulls. INSERT fails.
The statement has been terminated.


In the RD21 table I have the following columns:
AnställningsNummer, Msk100, Msk201, Msk710


Am I missing something?
Can someone point me to the right direction?

Vidor
Posted

I think you just need to change your new column to allow nulls (as there are obviously some nulls in the source table)

ALTER TABLE RD21 ADD Msk709 NVARCHAR(5) NULL
 
Share this answer
 
Comments
[no name] 14-May-13 3:36am    
I get the same error:
Cannot insert the value NULL into column 'AnställningsNummer', table 'KompetensMatris.dbo.RD21'; column does not allow nulls. INSERT fails.
The statement has been terminated.
I managed to solve the problem:

First
string alterSQL = "ALTER TABLE <totable> ADD <column> NVARCHAR(5) ";

Now comes the update
string qryMove ="UPDATE <totable> SET <column> = (SELECT <column> FROM <fromtable> WHERE <fromtable.keycolumn> = <totable.keycolumn>)";

After update I can remove the old column från the table:
string strDelColumn = "ALTER TABLE <fromtable> DROP COLUMN <column>;

Execute the SQL queries
Functions Execute = new Functions();
Execute.MySqlCommand(alterSQL, connCopy);
Execute.MySqlCommand(qryMove, connCopy);
Execute.MySqlCommand(strDelColumn, connCopy);


That's all.

Vidor
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900