【问题标题】:SQL Server: Conversion failed when converting from a character string to uniqueidentifierSQL Server:从字符串转换为唯一标识符时转换失败
【发布时间】:2010-07-14 00:45:54
【问题描述】:

在过去的一个小时里,我一直在用头撞墙试图弄清楚这一点,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 还不太熟悉。我希望这里的人能够发现我的问题。

【问题讨论】:

    标签: sql sql-server stored-procedures type-conversion


    【解决方案1】:

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

    【讨论】:

    • 天啊!我完全打算把@ForProfessionalID 放在那里,谢谢
    • 我希望我能多次对此表示赞同。没有什么比被 Little Bobby Tables 抓住的脑洞大开更棒的了!
    【解决方案2】:

    问题是:

    where ForProfessionalID = @ProfessionalName
    

    我想你想要

    where ForProfessionalID = @ProfessionalID
    

    【讨论】:

      猜你喜欢
      • 2015-04-21
      • 2011-12-15
      • 2013-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多