简体   繁体   English

通过用户ID获取全名

[英]Get the full name by userID

Here is the thing: I have a Table named users with this columns(firstName,LastName,UserID,....) 事情是这样的:我有一个名为users的表,其中包含此列(firstName,LastName,UserID,....)

on my website in the URL there is an UserID now on a specific page I have a textbox the question is 在我的网站的URL中,现在在特定页面上有一个UserID,我有一个文本框,问题是

how can I get the Full name by the UserID I mean userID 7 = Jason Dow into that textbox 如何通过UserID获得全名,我的意思是userID 7 = Jason Dow进入该文本框

how can I do that in the stored prucedure? 如何在存储的过程中做到这一点?

heres what I did: 这是我所做的:

CREATE FUNCTION [dbo].[fnGetFullNameFromUserID]
(   
    @inUserID_INT int
)
RETURNS @Results TABLE
    (
        FirstName varchar(50),
        LastName varchar(50)
    )

AS
BEGIN

INSERT INTO @Results (FirstName,LastName)
    -- Add the SELECT statement with parameter references here
    SELECT FirstName,LastName
    FROM Users
    WHERE UserID = @inUserID_INT


        RETURN
END
SELECT FirstName + ' '  + LastName
FROM Users
WHERE UserID = @inUserID_INT

You can concatenate fields using the + sign. 您可以使用+号连接字段。

Apologies for the formatting but for some reason I am having difficulty doing this from my phone 抱歉,格式化,但由于某些原因,我无法通过手机执行此操作

What you did is not a stored procedure, it is a function. 您所做的不是存储过程,而是一个函数。 If you want to create a stored procedure, use something similar to this : 如果要创建存储过程,请使用类似于以下内容的方法:

CREATE PROCEDURE sp_GetFullNameFromID(
@userID int
)
AS
BEGIN
    SELECT FirstName, LastName
    FROM Users
    WHERE UserId = @userId
END

You could also concatenate the first name and last name into one column like proposed in another answer : 您还可以将名字和姓氏连接到一个列中,就像在另一个答案中建议的那样:

CREATE PROCEDURE sp_GetFullNameFromID(
@userID int
)
AS
BEGIN
    SELECT (FirstName + ' ' + LastName) AS FullName
    FROM Users
    WHERE UserId = @userId
END

Let's just hope I didn't mess up the syntax. 只是希望我不会弄乱语法。

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

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