Click here to Skip to main content
15,920,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello friends
Below is my store procedure code

SQL
create procedure sp_branchinsert(@branchid int,@schoolname varchar(100),@branchname varchar(50),@createddateandtime datetime)
as
begin
declare @schooid int
select School_id from School where School_name=@schoolname;
set @schooid=School_id;
insert into Branch(Branch_id,School_id,Branch_name,Created_date_time)values(@branchid,@schooid,@branchname,@createddateandtime)
end


The error is invalid column school_id in the set line.what is my task is i want to select school_id from school using store procedure local variable @schoolname.and then this Schoo_id value is need to inserted into the new table branch. if my qurey is wrong, how to write query for the above task.
Posted

Try something like this:
SQL
INSERT INTO Branch (schoolId, branchId) VALUES ((SELECT schoolId FROM School WHERE schoolName=@schoolname), @Branchid)
 
Share this answer
 
SQL
select School_id from School where School_name=@schoolname;
set @schooid=School_id;

change this lines with,

SQL
select @schooid=School_id from School where School_name=@schoolname;


Happy Coding!
:)
 
Share this answer
 
v2
try this

You have to set value for @schoolId from the select query. You cant set it separately.

Incorrect:
SQL
select School_id from School where School_name=@schoolname;
set @schooid=School_id;




Correct one:
select @schooid=School_id from School where School_name=@schoolname

SQL
create procedure sp_branchinsert(@branchid int,@schoolname varchar(100),@branchname varchar(50),@createddateandtime datetime)
as
begin
declare @schooid int
select @schooid=School_id from School where School_name=@schoolname
insert into Branch(Branch_id,School_id,Branch_name,Created_date_time)values(@branchid,@schooid,@branchname,@createddateandtime)
end
 
Share this answer
 
Hi
What About This

SQL
create procedure sp_branchinsert(@branchid int,@schoolname varchar(100),@branchname varchar(50),@createddateandtime datetime)
as
begin
declare @schooid int
set @schooid = isnull(( select School_id from School where School_name=@schoolname),0);
insert into Branch(Branch_id,School_id,Branch_name,Created_date_time)values(@branchid,@schooid,@branchname,@createddateandtime)
end



I Hope That Help You
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900