2010-07-14 80 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 
相關問題