0

For my requirement, I am creating one temporary table like below:

CREATE TABLE #ResourceMonthlyTimeReport
(
    RowId INT IDENTITY(1,1),
    ResourceID UNIQUEIDENTIFIER,
    TaskId UNIQUEIDENTIFIER,
    ProjectId UNIQUEIDENTIFIER,
    -- Some column names containing date should come here.
)

In the above table, date columns should display like below:

[01 Mar, 2013] NVARCHAR(10),
[02 Mar, 2013] NVARCHAR(10),
[03 Mar, 2013] NVARCHAR(10),

... and the number of columns depends on the parameter values @FromDate and @ToDate

I have written a function dbo.F_ST_DaysColumns(@FromDate, @ToDate) that generates the following output as string:

[01 Mar, 2013] NVARCHAR(10),
[02 Mar, 2013] NVARCHAR(10),
[03 Mar, 2013] NVARCHAR(10)

Now, I am not understanding how to append this result to the above temporary table #ResourceMonthlyTimeReport.

I have done something like this:

DECLARE @DateColumns AS VARCHAR(MAX)
SET @DateColumns = dbo.F_ST_DaysColumns(@FromDate, @ToDate)

ALTER TABLE #ResourceMonthlyTimeReport ADD @DateColumns

But, showing error like below:

Msg 102, Level 15, State 1, Procedure ST_Proc_Rpt_MonthlyTimeReportSummary, Line 94
Incorrect syntax near '@DateColumns'

2 Answers 2

1

Oh... it is too simple, but I couldn't think like this before I posted this question in forum. Anyway, somebody else can use it:

EXEC('ALTER TABLE #ResourceMonthlyTimeReport ADD ' + @DateColumns)
Sign up to request clarification or add additional context in comments.

Comments

0

Table Variable is like a temporary table created in the memory which can be used within the scope where it is being created. Once it is created it is not possible to alter the structure of the Table Variable by using ALTER TABLE statement. It is better to add another column in the declaration and use it whenever you want. Note that Table Variables can have columns which accepts NULL values also.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.