?
Solved

Must declare the scalar variable "@message".  T-SQL

Posted on 2014-10-26
6
Medium Priority
?
497 Views
Last Modified: 2014-10-26
Question: How can I fix this error?

Thanks.
ALTER PROCEDURE [dbo].[spAddCustomer]
     @FirstName varchar(30)
    , @LastName varchar(30)
    , @Address varchar(50)
    , @City varchar(30)
    , @State varchar(2)
    , @ZipCode varchar(10)
    , @Phone varchar(12)
    , @Email varchar(100)

	, @SoftwareID int = 0
	, @Quantity int = 0
	, @message VARCHAR(100) OUTPUT
As
BEGIN

SET NOCOUNT ON;

Declare @CustID int
Declare @OrderID int
Declare @AddedOrderCount int
Declare @AddedCustomerCount int
Declare @msg varchar(100)

Insert Into tblCustomers(FirstName, LastName, [Address], City, [State], ZipCode, Phone, Email)
                Values(@FirstName, @LastName, @Address, @City, @State, @ZipCode, @Phone, @Email)

Set @CustID = SCOPE_IDENTITY()
Set @AddedCustomerCount = @@ROWCOUNT

if @SoftwareID > 0
Begin
   Insert Into tblOrders (CustomerID, OrderDate)
				       Values(@CustID,GETDATE())
  Set @OrderID = SCOPE_IDENTITY()

  Insert Into tblOrderDetails(OrderID, SoftwareID, Quantity)
			         Values(@OrderID, @SoftwareID, @Quantity) 
End

Set @AddedOrderCount = @@ROWCOUNT
Set @message = @AddedOrderCount

if @@Error>0 
   Begin
     Set @msg ='Customers added: ' + CAST(@AddedCustomerCount AS NVARCHAR(10)) +
              '; Orders added: ' + CAST(@AddedOrderCount AS NVARCHAR(10)) +
	          '; SQL Server error: ' + CAST(@@Error AS NVARCHAR(10))
   End
Else
   Begin
      Set @msg ='Customers added: ' + CAST(@AddedCustomerCount AS NVARCHAR(10)) +
              '; Orders added: ' + CAST(@AddedOrderCount AS NVARCHAR(10));
   End

Return @msg

END 

/*
Declare @msg varchar(100)
Exec @msg = spAddCustomer a,a,a,a,a,a,a,a,1,2, @message OUTPUT
Select @msg from tblCustomer
*/

Open in new window

0
Comment
Question by:Mike Eghtebas
[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
  • 3
  • 3
6 Comments
 
LVL 15

Expert Comment

by:Haris Djulic
ID: 40405719
Just remove this line:

Set @message = @AddedOrderCount
0
 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 40405722
Hi Haris,

I removed it. After running Alter routine, I executed the Exec @msg =...

I still get:

Msg 137, Level 15, State 2, Line 2
Must declare the scalar variable "@message".
0
 
LVL 15

Accepted Solution

by:
Haris Djulic earned 2000 total points
ID: 40405726
You are using this to execute procedure is it?

Declare @msg varchar(100)
Exec spAddCustomer a,a,a,a,a,a,a,a,1,2, @message OUTPUT
Select @msg from tblCustomer

then use it like this :

Declare @msg varchar(100)
Exec @msg = spAddCustomer a,a,a,a,a,a,a,a,1,2, @msg OUTPUT
Select @msg
0
Industry Leaders: 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!

 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 40405730
I will try it now. Meanwhile I found that the error occurs at:

Return @msg

because @msg is varchar but return expects it to be integer.

brb

Back testing the following:

Return 5 --@msg

END 

/*
Declare @msg varchar(100)
Declare @message varchar(100)
Exec @msg = spAddCustomer a,a,a,a,a,a,a,a,1,2, @message OUTPUT
Select @msg 
*/

Open in new window


This works fine. I think I shouldn't get @msg involved with Return.
0
 
LVL 15

Expert Comment

by:Haris Djulic
ID: 40405738
Return parameter is declared in the beginning of the procedure so it should be @message VARCHAR(100) OUTPUT

Did you test my code?
0
 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 40405742
I used part of the correction you made. Applying also some of my findings, the following works
ALTER PROCEDURE [dbo].[spAddCustomer]
     @FirstName varchar(30)
    , @LastName varchar(30)
    , @Address varchar(50)
    , @City varchar(30)
    , @State varchar(2)
    , @ZipCode varchar(10)
    , @Phone varchar(12)
    , @Email varchar(100)

	, @SoftwareID int = 0
	, @Quantity int = 0
	, @message VARCHAR(100) OUTPUT
As
BEGIN

SET NOCOUNT ON;

Declare @CustID int
Declare @OrderID int
Declare @AddedOrderCount int = 0
Declare @AddedCustomerCount int = 0
Declare @msg varchar(100)

Insert Into tblCustomers(FirstName, LastName, [Address], City, [State], ZipCode, Phone, Email)
                Values(@FirstName, @LastName, @Address, @City, @State, @ZipCode, @Phone, @Email)

Set @CustID = SCOPE_IDENTITY()
Set @AddedCustomerCount = @@ROWCOUNT

if @SoftwareID > 0
Begin
   Insert Into tblOrders (CustomerID, OrderDate)
				       Values(@CustID,GETDATE())
  Set @OrderID = SCOPE_IDENTITY()

  Insert Into tblOrderDetails(OrderID, SoftwareID, Quantity)
			         Values(@OrderID, @SoftwareID, @Quantity) 
End

Set @AddedOrderCount = @@ROWCOUNT

if @@Error>0 
   Begin
     Set @msg ='Customers added: ' + CAST(@AddedCustomerCount AS NVARCHAR(10)) +
              '; Orders added: ' + CAST(@AddedOrderCount AS NVARCHAR(10)) +
	          '; SQL Server error: ' + CAST(@@Error AS NVARCHAR(10))
   End
Else
   Begin
      Set @msg ='Customers added: ' + CAST(@AddedCustomerCount AS NVARCHAR(10)) +
              '; Orders added: ' + CAST(@AddedOrderCount AS NVARCHAR(10));
   End

Set @message = @msg

Return @@Error

END 

/*
Declare @msg varchar(100)
Declare @message varchar(100)
Exec @msg = spAddCustomer a,a,a,a,a,a,a,a,1,2, @message OUTPUT
Select @msg 
Select @message 
*/

Open in new window

0

Featured Post

Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.

Question has a verified solution.

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

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Suggested Courses

801 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