Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Set variable error

Posted on 2012-12-28
6
Medium Priority
?
264 Views
Last Modified: 2012-12-28
I have the following in a stored procedure

(Trying to capture value in a variable so can read it from a vb.net app)


DECLARE @TableTierCreditsMonth1 as Decimal(18,2)
DECLARE @TableTierCreditsMonth2 as Decimal(18,2)
DECLARE @TableTierCreditsMonth3 as Decimal(18,2)


;WITH TempTableTierCreditsMonth1 (Playerid,TableTheoMonth1, TablewinMonth1, TableTierCreditsMonth1)
AS

(SELECT PlayerID
            ,sum(Table_TheoWin) AS TableTheoMonth1
            ,sum(Table_Win) AS TablewinMonth1
            ,case when sum(Table_TheoWin) >= sum(Table_Win) * @TableTierMultiplier then sum(Table_TheoWin) else sum(Table_Win) * @TableTierMultiplier end TableTierCreditsMonth1
            
      FROM CopyPlayerManagement..PlayerDay
      WHERE AccountingDate BETWEEN @BeginDateMonth1
                  AND @endDateMonth1
            AND Table_Games > 0
            and playerid = @PlayerID
      GROUP BY PlayerID)
      
            
      set @TableTierCreditsMonth1 = (Select TableTierCreditsMonth1 from TempTableTierCreditsMonth1)

I get an error saying Incorrect syntax near the keyword 'SET'

Not sure why
0
Comment
Question by:johnnyg123
[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
  • 2
  • 2
  • 2
6 Comments
 
LVL 70

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 1000 total points
ID: 38727530
You have to have a SELECT statement after a CTE:

SELECT @TableTierCreditsMonth1 = TableTierCreditsMonth1 from TempTableTierCreditsMonth1
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 38727642
I agree with above comment, the SET alone won't work, as it "forgot" the CTE already.

this will work better:
DECLARE @TableTierCreditsMonth1 as Decimal(18,2)
DECLARE @TableTierCreditsMonth2 as Decimal(18,2)
DECLARE @TableTierCreditsMonth3 as Decimal(18,2)


;WITH TempTableTierCreditsMonth1 (Playerid,TableTheoMonth1, TablewinMonth1, TableTierCreditsMonth1)
AS

(SELECT PlayerID
            ,sum(Table_TheoWin) AS TableTheoMonth1
            ,sum(Table_Win) AS TablewinMonth1
            ,case when sum(Table_TheoWin) >= sum(Table_Win) * @TableTierMultiplier then sum(Table_TheoWin) else sum(Table_Win) * @TableTierMultiplier end TableTierCreditsMonth1
            
      FROM CopyPlayerManagement..PlayerDay
      WHERE AccountingDate BETWEEN @BeginDateMonth1
                  AND @endDateMonth1
            AND Table_Games > 0
            and playerid = @PlayerID
      GROUP BY PlayerID)
                 
SELECT @TableTierCreditsMonth1 = TableTierCreditsMonth1 
from TempTableTierCreditsMonth1 

Open in new window

0
 
LVL 70

Expert Comment

by:Scott Pletcher
ID: 38727737
Isn't that exactly the same SELECT I posted??
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:johnnyg123
ID: 38727773
works fine if I use

SELECT @TableTierCreditsMonth1 = TableTierCreditsMonth1 from TempTableTierCreditsMonth1

But I wanted to capture another variable as well so I added

another select and it tells me the second one is invalid

can I only do 1?
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 1000 total points
ID: 38727956
it must be in the same select:
DECLARE @TableTierCreditsMonth1 as Decimal(18,2)
DECLARE @TableTierCreditsMonth2 as Decimal(18,2)
DECLARE @TableTierCreditsMonth3 as Decimal(18,2)


;WITH TempTableTierCreditsMonth1 (Playerid,TableTheoMonth1, TablewinMonth1, TableTierCreditsMonth1)
AS

(SELECT PlayerID
            ,sum(Table_TheoWin) AS TableTheoMonth1
            ,sum(Table_Win) AS TablewinMonth1
            ,case when sum(Table_TheoWin) >= sum(Table_Win) * @TableTierMultiplier then sum(Table_TheoWin) else sum(Table_Win) * @TableTierMultiplier end TableTierCreditsMonth1
            
      FROM CopyPlayerManagement..PlayerDay
      WHERE AccountingDate BETWEEN @BeginDateMonth1
                  AND @endDateMonth1
            AND Table_Games > 0
            and playerid = @PlayerID
      GROUP BY PlayerID)
                 
SELECT @TableTierCreditsMonth1 = TableTierCreditsMonth1 
 ,  @TableTierCreditsMonth2 = TableTierCreditsMonth2 
from TempTableTierCreditsMonth1 

Open in new window


again: the "CTE" is only part of the select, it's not "defined" to be reused by as many statements later as "needed".

Scott: I posted the full code just trying to make clear that one cannot reuse the CTE for several statements... the CTE expression is part of the statement ...
0
 

Author Closing Comment

by:johnnyg123
ID: 38728146
Thanks Guys!
0

Featured Post

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

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

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

722 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