October 2, 2007 at 11:49 pm
Hello Everyone,
I am facing a problem when I create a temp table and then insert records into that and later with in that stored procedure I add a new column and try to insert and update records in the same table and then it causes problem which states the newly added column doesn't exists while just after adding the new column if I use sp_help system stored procedure to get details if the newly added column exists of not then it shows the new column.
Here is the T-SQL of that stored procedure and help me out:
CREATE PROC test_proc1
AS
BEGIN
CREATE TABLE test_table1 (col1 int not null, col2 varchar(50) null)
INSER INTO test_table1 VALUES (101, 'Temp Data1')
INSER INTO test_table1 VALUES (102, 'Temp Data2')
ALTER TABLE test_table1 ADD col3 varchar(50) null
exec sp_help 'test_table1'
INSER INTO test_table1 VALUES (103, 'Temp Data3', 'New Col Data1')
DROP TABLE test_table1
END
Thanks
Vishal
October 3, 2007 at 1:56 am
The SQL Server does not know which columns to insert!!!
CREATE PROC test_proc1
AS
BEGIN
CREATE TABLE test_table1 (col1 int not null, col2 varchar(50) null)
INSERT INTO test_table1 VALUES (101, 'Temp Data1')
INSERT INTO test_table1 VALUES (102, 'Temp Data2')
ALTER TABLE test_table1 ADD col3 varchar(50) null
exec sp_help 'test_table1'
INSERT INTO test_table1 (col1, col3) VALUES (103, 'Temp Data3')
DROP TABLE test_table1
END
GO
N 56°04'39.16"
E 12°55'05.25"
October 3, 2007 at 3:51 am
Sorry Peter, my mistake. I edited the last insert statement. Please try to execute the same and then you get to know what's the prob.
Anyway...thanks 🙂
October 3, 2007 at 4:45 am
It's called SCOPE.
After ALTERING a table, an insert can't take place directly.
However, there is a workaround
CREATE PROCtest_proc1
AS
BEGIN
CREATE TABLE test_table1 (col1 int not null, col2 varchar(50) null)
INSERT INTO test_table1 (col1, col2) VALUES (101, 'Temp Data1')
INSERT INTO test_table1 (col1, col2) VALUES (102, 'Temp Data2')
ALTER TABLE test_table1 ADD col3 varchar(50) null
exec('INSERT INTO test_table1 VALUES (103, ''Temp Data3'', ''New Col Data1'')')
select * from test_table1
DROP TABLE test_table1
END
GO
exec test_proc1
drop PROC test_proc1
N 56°04'39.16"
E 12°55'05.25"
October 3, 2007 at 5:14 am
Thanks Peter,
I was doing the same thing but I was thinking if I did something or I may be lacking somewhere.
Thanks.
October 3, 2007 at 7:22 am
Vishal,
Just a thought... Is this a permanent nonreporting you're modifiying? The reason I ask is if you're actually doing this to some "real" data (other than a reporting table), you may actually be violating all sorts of good practices including things like 3rd normal form... and those violations can make your life very difficult in the future. For example... adding month columns to something like a CostDetail table will make it very difficult to work on all costs from all months and years...
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply