Solved

use ALTER TABLE on a Table Variable

Posted on 2003-11-07
4
781 Views
Last Modified: 2012-05-04
Hey folks,

Im stumped.  I have a table variable that I need to alter dynamically:

DELCARE @tDate smalldatetime
DECLARE @BeginDate smalldatetime
DECLARE @EndDate smalldatetime

SET @BeginDate = '1/1/03'
SET @EndDate = '12/31/03'
SET @tDate = @Begindate

DECLARE @Scores TABLE (StudentName varchar(100))

WHILE (@tDate < @EndDate)  BEGIN
  SET @tVar = dbo.MonthName(Month(@tDate),1) + ' ' +  convert(varchar,year(@tDate))
  ALTER TABLE @Scores ADD @tVar VARCHAR(20) NULL
  SET @tDate = DATEADD(month,1,@tDate)
END

If the answer is you cant use ALTER TABLE for Table Variables, then how can I change my table variable after its been declared

I also tried DECLARE @Scores TABLE (@fieldNames) and that didn't work at all.

HELP!!
0
Comment
Question by:DialM4Monkey
4 Comments
 
LVL 15

Accepted Solution

by:
namasi_navaretnam earned 250 total points
ID: 9703225
If you need to dynamicaly add column you will need to use traditional temp tables.

drop table #test

create table #test
(
col1 int
)

alter table #test
add col2 int

select * from #test
0
 
LVL 22

Expert Comment

by:CJ_S
ID: 9704079
Just like you cannot place constraints on a table variable.

create table x(id smalldatetime constraint t_id default getdate())
and
declare @t table (id smalldatetime constraint t_id default getdate())


Use temporary tables instead like namasi pointed out. Just don't forget to remove them once you're finished.
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now