SQL Server Subquery returned more than 1 value with SQL 2008 but successfully with SQL 2000

qyswt5oh  于 12个月前  发布在  其他
关注(0)|答案(2)|浏览(83)

I have excuted store procedure in both SQL Server 2000 and SQL Server 2008 like this:

DECLARE @mobile varchar(20)
BEGIN
SET @mobile = (select Mobile from NodesMobile where nodeId = 1) //@mobile will return '123;456'
INSERT INTO dbo.SMS([sSub], [sContent]) 
SELECT *,'sms_content' from dbo.splitstring(@mobile)
END

in this, SELECT *,'sms_content' from dbo.splitstring(@mobile) will return:

Mobile  Content
123     sms_content
456     sms_content

In SQL server 2000, it is successfully insert 2 rows to dbo.SMS:

Id   sSub     sContent
1    123      sms_content 
2    456      sms_content

But it is show error in SQL Server 2008:

Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression

I dont know why???

EDIT: dbo.splitstring is a function for split string by ';'

CREATE FUNCTION [dbo].[splitstring] ( @stringToSplit nvarchar(255) )
RETURNS
 @returnList TABLE ([Name] [nvarchar] (500))
AS
BEGIN

 DECLARE @name NVARCHAR(255)
 DECLARE @pos INT

 WHILE CHARINDEX(';', @stringToSplit) > 0
 BEGIN
  SELECT @pos  = CHARINDEX(';', @stringToSplit)  
  SELECT @name = SUBSTRING(@stringToSplit, 1, @pos-1)

  INSERT INTO @returnList 
  SELECT @name

  SELECT @stringToSplit = SUBSTRING(@stringToSplit, @pos+1, LEN(@stringToSplit)-@pos)
 END

 INSERT INTO @returnList
 SELECT @stringToSplit

 RETURN
END
iecba09b

iecba09b1#

Your date might be different, try limit your subquery results.

DECLARE @mobile varchar(20)
BEGIN
SET @mobile = (select top 1 Mobile from NodesMobile)
INSERT INTO dbo.SMS([sSub], [sContent]) 
SELECT [Name],'sms_content' from dbo.splitstring(@mobile)
END
tct7dpnv

tct7dpnv2#

The problem is not in string_split, but rather in the mobile part: SET @mobile = (select Mobile from NodesMobile where nodeId = 1)

This can never succeed if you have more than one match from NodesMobile table.

This is how it looks on SQL Server 2000:

select *, 1 as nodeid into nodesmobile
from
(
    select  123, N'sms_content'
    union all select    456, N'sms_content'
) t (Mobile,Content)

SELECT @@version
go
DECLARE @mobile varchar(20)

SET @mobile = (select Mobile from NodesMobile where nodeId = 1)
select @mobile

Outputs:

Microsoft SQL Server  2000 - 8.00.818 (Intel X86) 
    May 31 2003 16:08:15 
    Copyright (c) 1988-2003 Microsoft Corporation
    Workgroup Edition on Windows NT 5.2 (Build 3790: Service Pack 2)

State: 21000, error code: 512, Line: 4, ProcName:
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

I'm guessing something else is going on

相关问题