SQL Server How to add a function into a view using select *

z9zf31ra  于 2023-03-07  发布在  其他
关注(0)|答案(1)|浏览(132)

I created a function called f_concat , and I created a view called v_students . Now I have to call the function, and with the view, execute it with a SELECT statement. How do I do that?

PS: I do have to have a function and a view and then use a select statement to combine them.

Here is the code that I've been trying

DROP FUNCTION IF EXISTS f_concat
GO

CREATE FUNCTION f_concat
    (@mike as varchar(10), 
     @fudge as varchar(10)) 
RETURNS varchar(30) AS
BEGIN
    RETURN CONCAT('mike', 'fudge', '-')
END;
GO

CREATE VIEW v_students 
AS
    SELECT 
        s.student_id, 
        s.student_firstname + ' ' + s.student_lastname AS student_name, 
        s.student_gpa, m.major_name
    FROM 
        students s
    JOIN
        majors m ON s.student_major_id = m.major_id
GO

EXEC f_concat @student_name= f_concat

SELECT * FROM v_students
a64a0gku

a64a0gku1#

in your example you would call

SELECT student_id, f_concat(student_name,student_name),student_gpa,major_name FROM v_students

Of yourse your view should not concatenate the names, this would do the function, of course you can use the function directly in the view, if you want that the view returns the complete name

相关问题