SSIS - using variables in Execute SQL Task

  • I have built the following query in SSMS, when I add it to an Execute SQL Task in SSIS. I get this error - "[Execute SQL Task] Error: Executing the query "SELECT @columnz = COALESCE(@columnz + ',[' + times..." failed with the following error:

    "Must declare the scalar variable "@columnz".". Possible failure reasons: Problems with the query,

    "ResultSet" property not set correctly, parameters not set correctly, or connection not established correctly."

    Query:

    use design

    drop table tmpNCPCNCDownstreamMaxUtilization3wks

    select node, max(utilization) as max_Utilization, DATE

    into tmpNCPCNCDownstreamMaxUtilization3wks

    from stage_ncpcncdownstream_temporal

    WHERE Date BETWEEN DATEADD(day, -20, GETDATE()) AND GETDATE()

    group by node, date

    order by node

    go

    alter table tmpNCPCNCDownstreamMaxUtilization3wks add Timestamp varchar(50)

    go

    update tmpNCPCNCDownstreamMaxUtilization3wks

    set Timestamp = 'WeekOf_' + cast(left(Date, 11) as varchar)

    go

    --drop table tmpNCPCNC_DownstreamNodeUtilizationMaxPivot

    --go

    declare @columnz varchar (8000)

    go

    SELECT @columnz = COALESCE(@columnz + ',[' + timestamp + ']','[' + timestamp+ ']')

    FROM tmpNCPCNCDownstreamMaxUtilization3wks group by timestamp order by timestamp

    go

    --pivot contents in @columns

    declare @query varchar(8000)

    SET @query ='select * into tmpNCPCNC_DownstreamNodeUtilizationMaxPivot from

    (select node, timestamp, Max_Utilization from tmpNCPCNCDownstreamMaxUtilization3wks)a

    pivot

    (

    sum(max_Utilization) for timestamp in('+ @columnz +')

    )as p'

    execute (@query)

    --empty contents from NCPCNCDownstreamMaxUtilization3wks

    update tmpNCPCNCDownstreamMaxUtilization3wks

    set timestamp =''

    truncate table tmpNCPCNC_MaxDownstreamNodeUtilzationChart3Week

    --load to table to be joined to Node feature class

    insert into tmpNCPCNC_MaxDownstreamNodeUtilzationChart3Week

    (Node, TwoWeeksAgo, PreviousWeek, CurrentWeek)

    select * from tmpNCPCNC_DownstreamNodeUtilizationMaxPivot

    --calculate average utilization for all values in each record

    update tmpNCPCNC_MaxDownstreamNodeUtilzationChart3Week

    set average = (coalesce(twoweeksago, 0) + coalesce(previousweek, 0) + coalesce(currentweek, 0))/3

    from tmpNCPCNC_MaxDownstreamNodeUtilzationChart3Week

    Any idea why?

  • You're abusing the GOs. GOs are batch separator, so variables won't be available in the next batch.

    The following part is useless.

    declare @columnz varchar (8000)

    go

    Remove all the GOs from your code and it might work correctly.

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply