How to set variable from a SQL query?

前端 未结 9 1414
被撕碎了的回忆
被撕碎了的回忆 2020-11-28 01:19

I\'m trying to set a variable from a SQL query:

declare @ModelID uniqueidentifer

Select @ModelID = select modelid from models
where areaid = \'South Coast\'         


        
9条回答
  •  鱼传尺愫
    2020-11-28 01:49

    To ASSIGN variables using a SQL select the best practice is as shown below

    ->DECLARE co_id INT ;
    ->DECLARE sname VARCHAR(10) ;
    
    ->SELECT course_id INTO co_id FROM course_details ;
    ->SELECT student_name INTO sname FROM course_details;
    

    IF you have to assign more than one variable in a single line you can use this same SELECT INTO

    ->DECLARE val1 int;
    ->DECLARE val2 int;
    
    ->SELECT student__id,student_name INTO val1,val2 FROM student_details;
    
    --HAPPY CODING-- 
    

提交回复
热议问题