2010-07-14 75 views
1

我一直在敲打我的头撞墙了几个小时试图弄清楚这一点,SQL是给我下面的错误SQL服务器:转换从字符串转换为uniqueidentifier

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不太好。我希望这里有人能够发现我的问题。

回答

8

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

+0

D'OH!我完全打算把@ForProfessionalID放在那里,谢谢 – Gallen 2010-07-14 00:51:39

+0

我希望我能不止一次地对此赞不绝口。没有什么比得到小鲍比桌子抓住大脑的时刻! – Moose 2010-07-14 00:55:20

+0

呵呵,谢谢大家! – LittleBobbyTables 2010-07-14 00:59:41

2

的问题是:

where ForProfessionalID = @ProfessionalName 

我想你想

where ForProfessionalID = @ProfessionalID 
相关问题