从字符串转换为uniqueidentifier错误时转换失败

时间:2012-03-13 15:16:47

标签: sql sql-server-2008 stored-procedures

我知道围绕Stack Overflow有很多类似的问题,但没有一个能解决我的问题。

这是我存储过程中的一行:

SET @AdIDNumber = PARSENAME(REPLACE(@AdIDNumber, '-', '.'), 1);

ADIDNumber是传递给此存储过程的参数。我正在尝试将ADIDNumber nvarchar的最后一个组件分隔为'-'。我收到以下错误:

  

从字符串转换为uniqueidentifier时转换失败。

奇怪的是,每次都不会发生这种情况。以下是一些有效的ADIDNumbers

188CR-000O-000199
188CR-000D-000105

和一些导致错误的内容:

188CR-000A-000505
188CR-000S-001459

我做错了什么?请帮忙。

谢谢。

编辑:

这是我的整个存储过程:

ALTER PROCEDURE [dbo].[sp_getOutlookDetails]
-- Add the parameters for the stored procedure here
@CountryName nvarchar(max),
@CommunityName nvarchar(max),
@AdIDNumber nvarchar(max)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

DECLARE @dbName nvarchar(max);
-- remove spaces
SET @CountryName = LOWER(REPLACE(@CountryName, ' ', ''));
SET @CommunityName = LOWER(REPLACE(@CommunityName, ' ', ''));


SET @dbName = @CountryName;
IF @CommunityName IS NOT NULL
    SET @dbName = @dbName+'_'+@CommunityName;

IF DB_ID(@dbname) IS NULL
    BEGIN
    RAISERROR('Database not found',16,1)
    RETURN
    END

DECLARE @tab AS TABLE (AdName nvarchar(max), OwnerJALName nvarchar(max), OwnerEmail nvarchar(max), AdContactEmail nvarchar(max), 
                  AdWebsiteLink nvarchar(max), Ad1stPhone nvarchar(max), Ad2ndPhone nvarchar(max), VideoUrl nvarchar(max), 
                  AdBusinessType nvarchar(max), AdBusinessLocation nvarchar(max), 
                  PublishStatus INTEGER, MediaGalleryEnabled INTEGER, MapTabEnabled nvarchar(max),
                  MapCategory nvarchar(max), VMEnabled INTEGER);

-- format the PostID
SET @AdIDNumber = PARSENAME(REPLACE(@AdIDNumber, '-', '.'), 1); 

-- we fill the table with LocationCoordinates in the MapCategory column twice so we can replace it later
DECLARE @dynsql nvarchar(max);
DECLARE @PostIDInt INTEGER;
SET @PostIDInt = CAST(@AdIDNumber AS INTEGER);
SET @dynsql = N'USE '+ QUOTENAME(@dbname) + N' SELECT dbo.cs_Posts.Subject, dbo.cs_Users.UserName, dbo.cs_Users.Email, dbo.drive_DirectoryPosts.ContactEmail, 
                  dbo.drive_DirectoryPosts.ContactWebsite, dbo.drive_DirectoryPosts.ContactPhone, dbo.drive_DirectoryPosts.ContactPhoneOther, dbo.drive_DirectoryPosts.VideoUrl, 
                  dbo.drive_DirectoryPosts.ContactBusinessType, dbo.drive_DirectoryPosts.ContactBusinessLocation, 
                  dbo.drive_DirectoryPosts.IsActive, dbo.drive_DirectoryPosts.IsMediaGalleryActive, 
                  dbo.drive_DirectoryPosts.LocationCoordinates, dbo.drive_DirectoryPosts.LocationCoordinates, dbo.drive_DirectoryPosts.IsMediaGalleryActive
                  FROM dbo.drive_DirectoryPosts INNER JOIN
                  dbo.cs_Posts ON dbo.drive_DirectoryPosts.PostID = dbo.cs_Posts.PostID INNER JOIN
                  dbo.cs_Users ON dbo.cs_Posts.UserID = dbo.cs_Users.UserID
                  WHERE dbo.cs_Posts.PostID = @PostIDInt';

INSERT INTO @tab EXECUTE sp_executesql @dynsql, N'@PostIDInt INTEGER', @PostIDInt = @PostIDInt;

DECLARE @MapCategory nvarchar(max);
DECLARE @VMEnabled INTEGER;
EXEC [dbo].sp_getMapCategory @dbName, @PostIDInt, @MapCategory OUTPUT, @VMEnabled OUTPUT;

UPDATE @tab SET MapCategory = @MapCategory;
UPDATE @tab SET VMEnabled = @VMEnabled;
SELECT * FROM @tab;

2 个答案:

答案 0 :(得分:0)

也许是这样的:

DECLARE @AdIDNumber VARCHAR(MAX)
SET @AdIDNumber='188CR-000O-000199'
DECLARE @REVERSE VARCHAR(MAX)=REVERSE(@AdIDNumber)
select REVERSE(LEFT(@REVERSE,CHARINDEX('-',@REVERSE)-1))

答案 1 :(得分:-1)

它是由sp_getMapCategory存储过程引起的。