SQL Server:从字符串转换为uniqueidentifier时转换失败

Gal*_*len 1 sql sql-server stored-procedures type-conversion

在过去的一小时里,我一直在撞墙,试图解决这个问题,sql给了我以下错误

Msg 8169, Level 16, State 2, Procedure GetAppointmentsByProfessionalName, Line 6
Conversion failed when converting from a character string to uniqueidentifier.
Run Code Online (Sandbox Code Playgroud)

执行此存储过程时

    -- =============================================
    -- Create date: <July 2010>
    -- Description: <Gets a list of appointments for a professionals username>
    -- =============================================
    Drop procedure GetAppointmentsByProfessionalName
    go
    Create procedure GetAppointmentsByProfessionalName(@ProfessionalName varchar(256))
    as
     declare @ProfessionalID uniqueidentifier
     set @ProfessionalID = (select UserId from aspnet_Users where UserName = @ProfessionalName)

     select a.AppointmentID as 'Appointment ID', 
       c.Name as 'Client Name', 
       p.Name as 'Professional Name', 
       a.ProposedDate as 'Date', 
       CONVERT(CHAR(8), a.ProposedTime, 114)as  'Time', 
       a.ClientDescription as 'Client Notes', 
       a.Confirmed as 'Confirmed Appointment'
     from Appointment a join Client c on a.ForClientID = c.ClientID
          join dbo.Professional p on a.ForProfessionalID = p.ProfessionalID
     where ForProfessionalID = @ProfessionalName
    go
Run Code Online (Sandbox Code Playgroud)

我使用默认的asp.net成员资格表,以及以下定义

create table Professional
(
 ProfessionalID uniqueidentifier not null constraint pk_ProID Primary Key references aspnet_Users(UserId),
 Name varchar(256),
 Email varchar(256),
 Phone varchar(256),
 DisplayPictureUrl varchar(256),
 ProfileSubHeader varchar(1000),
 ProfileContent varchar(1000),
 ServicesSubHeader varchar(1000),
 ServicesContent varchar(1000)
)

go

create table Client
(
 ClientID int identity not null constraint pk_ClientID Primary Key clustered,
 Name varchar(256),
 Email varchar(256),
 Phone varchar(256)
)

go

create table AppointmentType
(
 TypeID int identity not null constraint pk_AppointmentTypeID Primary Key clustered,
 Name varchar(256),
 Description varchar(256),
 DisplayPictureUrl varchar(256)
)

go

create table Appointment
(
 AppointmentID int identity not null constraint pk_AppointmentID Primary Key clustered,
 ForClientID int null constraint fk_ForClientID references Client(ClientID),
 ForProfessionalID uniqueidentifier not null constraint fk_ForProfessionalID references aspnet_users(UserID),
 ProposedTime datetime not null,
 ProposedDate datetime not null,
 TypeID int not null constraint fk_TypeID references AppointmentType(TypeID),
 ClientDescription varchar(256) null,
 Confirmed bit
)

GO
Run Code Online (Sandbox Code Playgroud)

它可能是一个语法问题,我对SQL还不是很好.我希望有人能够发现我的问题.

Lit*_*les 8

您的存储过程接受a varchar(256),但您的WHERE语句正在加入ForProfessionalID,这是一个唯一标识符.