Solved

use ALTER TABLE on a Table Variable

Posted on 2003-11-07
4
801 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

733 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