Mité
Mité

Reputation: 1118

Assign value to variable in SELECT statement

I have this SQL Server query:

DECLARE @HoursUsed float

SELECT COLUMN_A 
,(SELECT (@HoursUsed = (SELECT dbo.fn_GetProjectHoursUsed(Project.fg_projectId))) 
,@HoursUsed*100
,@HoursUsed*200
FROM Project, OTHER_TABLES)

I need the result from the function to be added to the variable because I will need it in several places later in the query.

The DB function is working fine by itself. If I remove the variable from the SELECT it works fine.

If I try to run the query like this it throws errors like:

Incorrect syntax near =, ), as.

The entire query uses this function several times and I have performance issues with it. So that's why I want to put the value in a variable and call the function only once.

I appreciate your help

Edit: Here is the complete query.

DECLARE @HoursUsed float

SELECT  DISTINCT
    Account.Name as AccountName
    ,Project.FG_SalesRepIdName as Rep
    ,Project.fg_Name as ProjectName
    ,Project.fg_projectId

,(SELECT dbo.fn_GetProjectPriceRate(Project.fg_projectId)) as ProjectPriceRate

,(
SELECT SalesOrder.totalAmount FROM fg_Project, SalesOrder
        WHERE fg_Project.fg_orderId = SalesOrder.salesOrderId 
            AND fg_Project.fg_Name = Project.fg_Name
            AND fg_Project.fg_projectId = Project.fg_projectId
)/dbo.fn_GetProjectPriceRate(Project.fg_projectId) as TotalHours  
    --The total hours are found by division of the total price of the project     with the price rate
    ,(
    SELECT SalesOrder.totalAmount FROM fg_Project, SalesOrder
            WHERE fg_Project.fg_orderId = SalesOrder.salesOrderId 
                AND fg_Project.fg_projectId = Project.fg_projectId
    ) AS TotalAmount

    ,(SELECT @HoursUsed = dbo.fn_GetProjectHoursUsed(Project.fg_projectId)
        SELECT @HoursUsed
    ) as HoursUsed

    ,(
    (SELECT dbo.fn_GetProjectHoursUsed(Project.fg_projectId))
    /
        NULLIF(((SELECT SalesOrder.totalAmount FROM fg_Project, SalesOrder
            WHERE fg_Project.fg_orderId = SalesOrder.salesOrderId 
                AND fg_Project.fg_Name = Project.fg_Name
                AND fg_Project.fg_projectId = Project.fg_projectId
               )
        /
              NULLIF(dbo.fn_GetProjectPriceRate(Project.fg_projectId),0)
             ),0)
    )*100 as PercentHoursUsed
    ,(SELECT dbo.fn_GetProjectHoursBilled(Project.fg_projectId)) as HoursBilled
    ,(SELECT dbo.fn_GetProjectHoursBilled(Project.fg_projectId))
     *
     (SELECT dbo.fn_GetProjectPriceRate(Project.fg_projectId)) as AmountBilled



FROM Account, fg_Project as Project, fg_ProjectElement, Product

WHERE       Account.accountId = Project.fg_regardingId
    AND Project.fg_ProjectId = fg_ProjectElement.fg_projectElemenstId
    AND fg_ProjectElement.fg_ProductId = Product.ProductId
    AND Product.ProductNumber = 'WEB DEV'

Upvotes: 2

Views: 9536

Answers (6)

Andriy M
Andriy M

Reputation: 77657

You can use a subselect like this:

SELECT
  *,
  HoursUsed * 100 AS Hours100
  HoursUsed * 200 AS Hours200
FROM (
  SELECT
    HoursUsed = dbo.fn_GetProjectHoursUsed(Project.fg_projectId),
    other columns
  FROM Project, other tables
  …
) s

No variable is required.

Upvotes: 2

t-clausen.dk
t-clausen.dk

Reputation: 44316

Try this, it will only use the function once for every line and you can refer to the function result

SELECT COLUMN_A, a.HoursUsed, a.HoursUsed*100, a.HoursUsed*200 
FROM Project p CROSS APPLY 
(SELECT  dbo.fn_GetProjectHoursUsed(P.fg_projectId) HoursUsed) a 

Upvotes: 1

Moe Sisko
Moe Sisko

Reputation: 12005

I'm partially guessing your schema, but maybe using a temp table would help ? e.g. something like :

DECLARE @HoursUsedTable TABLE (ProjectId int primary key, HoursUsed float)
INSERT @HoursUsedTable (ProjectId, HoursUsed) 
SELECT fg_projectId, dbo.fn_GetProjectHoursUsed(p.fg_projectId) from Project p


SELECT COLUMN_A, 
hu.HoursUsed,
hu.HoursUsed * 100,
hu.HoursUsed * 200
FROM Project p JOIN @HoursUsedTable hu ON hu.ProjectId = p.fg_projectId
, OTHER_TABLES

Note also that using scalar valued UDFs in some contexts can lead to peformance issues (I'm guessing your UDF returns a scalar ?) - better to use an inline table valued UDF if possible.

Upvotes: 2

RMorrisey
RMorrisey

Reputation: 7739

When you set a variable within the SELECT clause, the variable does not need its own SELECT keyword. The correct form should be:

DECLARE @HoursUsed float

SELECT COLUMN_A,@HoursUsed = dbo.fn_GetProjectHoursUsed(Project.fg_projectId) 
,@HoursUsed*100
FROM SOME_TABLES

If fn_GetProjectHoursUsed() has a table return value, this query isn't sufficiently complex to set @HoursUsed, and you'll need to give us a little more detail on what you're trying to do.

Edit: This turns out to be incorrect; can't combine variable assignment with normal select. Instead, use:

SELECT COLUMN_A,dbo.fn_GetProjectHoursUsed(Project.fg_projectId)*100
    FROM SOME_TABLES

Upvotes: 0

davecoulter
davecoulter

Reputation: 1826

This worked for me:

    DECLARE @HoursUsed float

    SELECT @HoursUsed = (SELECT dbo.fn_GetProjectHoursUsed(Project.fg_projectId))

Try removing the outer parans.

Upvotes: 0

Chandu
Chandu

Reputation: 82893

Try this:

SELECT @HoursUsed = dbo.fn_GetProjectHoursUsed(Project.fg_projectId)
...and rest of your query

Upvotes: 0

Related Questions