July 29, 2009 at 12:59 pm
I created the following SP but when I execute it in query analyzer I receive an error saying the Edit field is an invalid column name. I know it runs thru the point of adding the Edit field because I can see it in the table but when the update statement is run it can't find the new field in the table to make the updates. Can someone give me some guidance on what I am doing wrong? Thanks!
create procedure ImportandAltersp AS
select *
into Table2
from Table1
where date > '07/01/2009'
alter table Table2
add Edit char (3)
update table2
set Edit = 'Yes'
go
July 29, 2009 at 5:03 pm
you need to either built your table with all it's columns, or use the EXEC(@sqlstatement) to do the alter an iupdate.
the database engine expects either a GO statement, so it knows the ALTER command is completed, so that it is commited to the metatadata.
the best solution build the whole thing in one pass:
select Table1.*,'Yes' As [Edit]
into Table2
from Table1
where date > '07/01/2009'
Lowell
July 30, 2009 at 4:14 am
You should not create/alter a table thru procedure. What happens when you run the procedure twice?
Failing to plan is Planning to fail
July 30, 2009 at 5:08 am
i assumed this was pseudocode and he was using a temp table in the procedure;if it's a real table, Madhivanan is right , you'll have concurrency issues.
Lowell
July 31, 2009 at 10:56 am
Here's some code I did for a data-driven conversion in SQL 2005. It should work for SQL 2000, but you won't need the section for the varchar(max).
First the stored proc:
create proc up_add_field_to_NCEM43_Data
@fld_name nvarchar(30), @fld_size int, @fld_type varchar(30)
as
begin
declare @sqlbuff nvarchar(255)
if (substring(@fld_name,1,1) = '$')
select @fld_name = substring(@fld_name,2,datalength(@fld_name)-1)
if (@fld_size > 8000)
begin
set @sqlbuff = 'Alter table NCEM43_Data '
+ 'add ['
+ @fld_name
+ '] '
+ @fld_type
+ '(max) NULL'
end
else
begin
set @sqlbuff = 'Alter table NCEM43_Data '
+ 'add ['
+ @fld_name
+ '] '
+ @fld_type
+ '('
+ convert(varchar(4),@fld_size)
+ ') NULL'
end
print @sqlbuff
exec (@sqlbuff)
if (@@error != 0)
return -1
else
return 0
end
go
Then some calling code:
declare @error_countint
DECLARE @retstatint
declare @fld_namenvarchar(30)
execute @retstat = up_add_field_to_NCEM43_Data @fld_name, 255, 'nvarchar'
if (@retstat != 0)
begin
set @error_count = 1 + @error_count
end
Of course you still will have the challenge of updating a column. My solution was to also put the update statement into a stored procedure much like the first one.
Good luck!
Beth Richards
Sybase, Oracle and MSSQL DBA
July 31, 2009 at 9:36 pm
@sqluser...
Ummm... what is this for? I mean, what is the business reason behind adding columns to a new table? The reason why I'm asking is because if we knew the real reason behind why you are trying to do this, we just might be able to come up with a better solution than the one you're trying to solve.
--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