Solved

use ALTER TABLE on a Table Variable

Posted on 2003-11-07
4
761 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
MS SQL json query/where clause nested array items 2 41
SQL Server 2012 Row Selection 2 30
Caste datetime 2 22
Stored procedure 23 23
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…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

760 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

19 Experts available now in Live!

Get 1:1 Help Now