Solved

Set variable error

Posted on 2012-12-28
6
260 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 69

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 250 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 69

Expert Comment

by:Scott Pletcher
ID: 38727737
Isn't that exactly the same SELECT I posted??
0
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 

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 250 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

Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

Question has a verified solution.

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

Suggested Solutions

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 …
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how the fundamental information of how to create a table.

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