简体   繁体   English

ALTER 表中存储的列名 function

[英]ALTER table column name in stored function

I'm working with the following function:我正在使用以下 function:

ALTER FUNCTION TestFunc (@year INT)
RETURNS @RetTable TABLE(
    Forename VARCHAR(32),
    Lastname VARCHAR(32),
    Course1 INT,
    Course2 INT,
    Course3 INT
)

In the body of the function I'm declaring various variables such as:在 function 的正文中,我声明了各种变量,例如:

DECLARE @CourseOne VARCHAR(12);
DECLARE @CourseTwo VARCHAR(12);
DECLARE @CouseThree VARCHAR(12);

I then want to use these variables in order to apply another column name as following:然后我想使用这些变量来应用另一个列名,如下所示:

INSERT INTO @RetTable SELECT Forename, Lastname, C1 AS @CourseOne, C2 AS @CourseTwo, C3 AS @CourseThree FROM Teachers

The problem in this case is that the column name will still be " Course1, Course2, Course3 " even though I tried to "rename" the columns in the INSERT这种情况下的问题是,即使我尝试“重命名” INSERT中的列,列名仍将是“ Course1,Course2,Course3

Is there any way to make this work or is there any other way I could tackle this problem?有什么方法可以完成这项工作,还是有其他方法可以解决这个问题?

You cannot rename the columns in the table.您不能重命名表中的列。 Why do you want to rename columns in the middle of a function?为什么要重命名 function 中间的列?

You could rename them when you call the function您可以在调用 function 时重命名它们

SELECT Forename, Lastname, Course1 AS CourseOne, Course2 AS CourseTwo,Course3 AS Course3
FROM TestFunc(@year)

OR you could change your function declaration to have the tables named CourseOne, etc. at the start或者您可以更改您的 function 声明以在开始时将表命名为 CourseOne 等

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM