Solved

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

Posted on 2014-10-26
6
449 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 500 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
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 
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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

730 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