debugsqlModerate
Cannot INSERT Into Newly Created Column
Viewed 0 times
cannotinsertcolumncreatedintonewly
Problem
I have a simple test table like this:
Within a transaction, I try to add a column and then insert into the newly created column:
The Problem is an error message when I run the above code:
Why is this causing an error? If I add the column in a different batch, outside the transaction, it'll work. My problem is that I want to add the column within the transaction. Why the error?
CREATE TABLE MyTable (x INT);Within a transaction, I try to add a column and then insert into the newly created column:
BEGIN TRANSACTION;
PRINT 'Adding column, ''SupplementalDividends'', to MyTable table.';
ALTER TABLE MyTable
ADD SupplementalDividends DECIMAL(18,6);
PRINT 'Column added successfully....';
PRINT 'Ready to INSERT into MyTable ...';
INSERT INTO MyTable (x, SupplementalDividends)
VALUES (1, 3.2);
PRINT '**** CHANGES COMPLETE -- COMMITTING.';
COMMIT TRANSACTION;The Problem is an error message when I run the above code:
Invalid column name 'SupplementalDividends'.Why is this causing an error? If I add the column in a different batch, outside the transaction, it'll work. My problem is that I want to add the column within the transaction. Why the error?
Solution
It's a binding issue. The code is bound to the metadata of the table at compile time and it is not late bound.
Try using EXEC and dynamic SQL to overcome this limitation:
Another option is using a stored procedure to insert the data: late binding applies to stored procedure, but not to ad-hoc queries. Again, you would have to use dynamic SQL to create the procedure, but it could make it easier for you to pass parameters:
A temporary stored procedure would work as well:
Try using EXEC and dynamic SQL to overcome this limitation:
BEGIN TRANSACTION;
PRINT 'Adding column, ''SupplementalDividends'', to MyTable table.';
ALTER TABLE MyTable
ADD SupplementalDividends DECIMAL(18,6);
PRINT 'Column added successfully....';
PRINT 'Ready to INSERT into MyTable ...';
EXEC('
INSERT INTO MyTable (x, SupplementalDividends)
VALUES (1, 3.2);
')
PRINT '**** CHANGES COMPLETE -- COMMITTING.';
COMMIT TRANSACTION;Another option is using a stored procedure to insert the data: late binding applies to stored procedure, but not to ad-hoc queries. Again, you would have to use dynamic SQL to create the procedure, but it could make it easier for you to pass parameters:
BEGIN TRANSACTION;
PRINT 'Adding column, ''SupplementalDividends'', to MyTable table.';
ALTER TABLE MyTable
ADD SupplementalDividends DECIMAL(18,6);
PRINT 'Column added successfully....';
PRINT 'Ready to INSERT into MyTable ...';
EXEC('
CREATE PROCEDURE insData @p1 int, @p2 DECIMAL(18,6)
AS
BEGIN
INSERT INTO MyTable (x, SupplementalDividends)
VALUES (@p1, @p2);
END')
EXEC InsData 1, 3.2;
PRINT '**** CHANGES COMPLETE -- COMMITTING.';
COMMIT TRANSACTION;A temporary stored procedure would work as well:
BEGIN TRANSACTION;
PRINT 'Adding column, ''SupplementalDividends'', to MyTable table.';
ALTER TABLE MyTable
ADD SupplementalDividends DECIMAL(18,6);
PRINT 'Column added successfully....';
PRINT 'Ready to INSERT into MyTable ...';
EXEC('
CREATE PROCEDURE #insData @p1 int, @p2 DECIMAL(18,6)
AS
BEGIN
INSERT INTO MyTable (x, SupplementalDividends)
VALUES (@p1, @p2);
END')
EXEC #InsData 1, 3.2;
PRINT '**** CHANGES COMPLETE -- COMMITTING.';
COMMIT TRANSACTION;Code Snippets
BEGIN TRANSACTION;
PRINT 'Adding column, ''SupplementalDividends'', to MyTable table.';
ALTER TABLE MyTable
ADD SupplementalDividends DECIMAL(18,6);
PRINT 'Column added successfully....';
PRINT 'Ready to INSERT into MyTable ...';
EXEC('
INSERT INTO MyTable (x, SupplementalDividends)
VALUES (1, 3.2);
')
PRINT '**** CHANGES COMPLETE -- COMMITTING.';
COMMIT TRANSACTION;BEGIN TRANSACTION;
PRINT 'Adding column, ''SupplementalDividends'', to MyTable table.';
ALTER TABLE MyTable
ADD SupplementalDividends DECIMAL(18,6);
PRINT 'Column added successfully....';
PRINT 'Ready to INSERT into MyTable ...';
EXEC('
CREATE PROCEDURE insData @p1 int, @p2 DECIMAL(18,6)
AS
BEGIN
INSERT INTO MyTable (x, SupplementalDividends)
VALUES (@p1, @p2);
END')
EXEC InsData 1, 3.2;
PRINT '**** CHANGES COMPLETE -- COMMITTING.';
COMMIT TRANSACTION;BEGIN TRANSACTION;
PRINT 'Adding column, ''SupplementalDividends'', to MyTable table.';
ALTER TABLE MyTable
ADD SupplementalDividends DECIMAL(18,6);
PRINT 'Column added successfully....';
PRINT 'Ready to INSERT into MyTable ...';
EXEC('
CREATE PROCEDURE #insData @p1 int, @p2 DECIMAL(18,6)
AS
BEGIN
INSERT INTO MyTable (x, SupplementalDividends)
VALUES (@p1, @p2);
END')
EXEC #InsData 1, 3.2;
PRINT '**** CHANGES COMPLETE -- COMMITTING.';
COMMIT TRANSACTION;Context
StackExchange Database Administrators Q#162906, answer score: 10
Revisions (0)
No revisions yet.