Solved

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

Posted on 2014-10-26
6
367 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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Help with SQL Query 23 39
Convert int to military time 8 20
Test a query 23 19
MSSQL: Wrong result in a query (Nulls) 6 21
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
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…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

743 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

11 Experts available now in Live!

Get 1:1 Help Now