Must declare the scalar variable - Error

Shweta

Well-known Member
Joined
Jun 5, 2011
Messages
514
Hi All,

I have two tables "student" & "student_add" in SQL. I have created a procedure to update the address of a student. While executing the procedure I am getting an error : Must declare the scalar variable "@s_name".

Table: Student


Student_NameRoll_No
Deepti40
Priya30
Shivya50
Shweta10
Yuvraj20

<tbody>
</tbody>

Table: student_add

Roll_NoStu_Address
10Noida
20Gurgaon
30Ghaziabad
40Delhi
50Faridabad

<tbody>
</tbody>

Procedure:
Code:
Create proc Update_address
@Student_Name nvarchar(20),@Student_address nvarchar(20)
as
begin
update student_add
set Stu_Address = @Student_address
where Roll_No = (select Student_add.roll_no
                from student_add
                inner join student
                on student.roll_no = student_add.roll_no
                where student.student_name = @Student_Name)
end            

----Executing  the procedure



declare @s_name nvarchar(20) = 'Deepti'
declare @s_address nvarchar(20)='Gwalior'


execute update_address @s_name,@s_address ----prompting an error

[COLOR=#ff0000]Msg 137, Level 15, State 2, Line 2[/COLOR]
[COLOR=#ff0000]Must declare the scalar variable "@s_name".[/COLOR]

Instead of declaring the variables, if I pass the values(name and address) directly to the procedure like below, it works fine.

Code:
execute update_address 'Deepti','Gwalior'

Kindly suggest.

Thanks in advance!

Regards,
Shweta Jain
 
Last edited:

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
try putting a semi colon at the end of these two lines

declare @s_name nvarchar(20) = 'Deepti' ;
declare @s_address nvarchar(20)='Gwalior' ;
 
Upvote 0
For what it's worth, works fine for me.

declare @s_name nvarchar(20) = 'Deepti'
declare @s_address nvarchar(20)='Gwalior'


execute update_address @s_name,@s_address ----prompting an error
 
Upvote 0
Thank you both!

Sorry for the delay in response.

I tried putting semicolon at the end but still it's not working. Please suggest.
 
Upvote 0
How are you actually running this? the code you posted can't be right (are you selecting some of that text to execute it?) The code says the error is on line two but which is line 2 when you actually run it?
 
Upvote 0

Forum statistics

Threads
1,215,013
Messages
6,122,694
Members
449,092
Latest member
snoom82

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top