Sql Svr 2000. Can I add a column to a table that is the contents of a
variable?
create table doc_exa (id INT)
go
declare @emp_cd varchar(10)
declare testCursor CURSOR for
select top 8 emp_cd from emp where termdate is null and home_store_cd='08'
open testCursor
fetch next from testCursor into @emp_cd
while @@fetch_status=0
begin
alter table doc_exa add @emp_cd
end
expected result
doc_exa
id INT
slsp1 varchar(10)
slsp4 varchar(10)
slsp88 varchar(10)
slsp89 varchar(10)
slsp234 varchar(10)
slsp651 varchar(10)
slsp794 varchar(10)
slsp846 varchar(10)
Seems like a strange requirement to me. However, that ALTER could be done as dynamic SQL. Put the
ALTER TABLE in a variable then to EXEC(@sql).
--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://sqlblog.com/blogs/tibor_karaszi
"Peter" <P
@discussions.microsoft.com> wrote in message
news:3CF0D56F-C8A7-4BB3-9A24-B89309AB8BDB@microsoft.com...
> Sql Svr 2000. Can I add a column to a table that is the contents of a
> variable?
> create table doc_exa (id INT)
> go
> declare @emp_cd varchar(10)
> declare testCursor CURSOR for
> select top 8 emp_cd from emp where termdate is null and home_store_cd='08'
> open testCursor
> fetch next from testCursor into @emp_cd
> while @@fetch_status=0
> begin
> alter table doc_exa add @emp_cd
> end
> expected result
> doc_exa
> id INT
> slsp1 varchar(10)
> slsp4 varchar(10)
> slsp88 varchar(10)
> slsp89 varchar(10)
> slsp234 varchar(10)
> slsp651 varchar(10)
> slsp794 varchar(10)
> slsp846 varchar(10)