Declaring and Setting SQL Variables

I am trying to write a SQL statement that uses a very simple variable.
I keep getting an error that says:
"The multipart identifier could not de bound"

Here is an example of my statement:

USE TSQL2012
GO
DECLARE @Total Int;
SET @Total = (Sales.OrderDetail.unitprice * Sales.OrderDetail.qty)
SELECT @Total AS 'Total Price'
FROM Sales.OrderDetail

What am I missing
mnoisetteAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Scott PletcherConnect With a Mentor Senior DBACommented:
DECLARE @Total Int;

SELECT @Total = SUM(od.unitprice * od.qty)
FROM Sales.OrderDetail od

SELECT @Total AS 'Total Price'
0
 
Jim HornConnect With a Mentor Microsoft SQL Server Developer, Architect, and AuthorCommented:
>SET @Total = (Sales.OrderDetail.unitprice * Sales.OrderDetail.qty)
Is your intent to run a select query, and assign the single value to @total?

If so, it needs to be something like this...
SELECT @Total = unitprice * qty
FROM Sales.OrderDetail

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.