Solved

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

Posted on 2014-10-26
6
379 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
  • 3
  • 3
6 Comments
 
LVL 15

Expert Comment

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

Set @message = @AddedOrderCount
0
 
LVL 33

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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 33

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 33

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

929 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now