繁体   English   中英

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

[英]SQL Server: Conversion failed when converting from a character string to uniqueidentifier

在过去的一个小时里,我一直在想办法解决这个问题,sql给了我以下错误

Msg 8169, Level 16, State 2, Procedure GetAppointmentsByProfessionalName, Line 6
Conversion failed when converting from a character string to uniqueidentifier.

当执行此存储过程时

    -- =============================================
    -- 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

我正在使用默认的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

这可能是语法问题,我对SQL还不太了解。 我希望这里的人能够发现我的问题。

您的存储过程接受varchar(256) ,但是您的WHERE语句正在连接到ForProfessionalIDForProfessionalID是唯一的标识符。

问题是:

where ForProfessionalID = @ProfessionalName

我想你要

where ForProfessionalID = @ProfessionalID

暂无
暂无

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

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