Solved

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

Posted on 2014-10-26
6
414 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
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.

 
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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

770 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