子查询使用 SQL 2008 返回超过 1 个值,但使用 SQL 2000 成功

问题描述 投票:0回答:2

我在 SQL Server 2000 和 SQL Server 2008 中都执行了存储过程,如下所示:

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

在此,SELECT *,'sms_content' from dbo.splitstring(@mobile) 将返回:

Mobile  Content
123     sms_content
456     sms_content

在SQL Server 2000中,成功插入2行到dbo.SMS:

Id   sSub     sContent
1    123      sms_content 
2    456      sms_content

但是在 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

不知道为什么???

编辑:dbo.splitstring 是一个用 ';' 分割字符串的函数

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
sql-server stored-procedures sql-server-2008-r2 sql-server-2000
2个回答
0
投票

您的日期可能会有所不同,请尝试

limit
您的
subquery
结果。

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

0
投票

问题不在于 string_split,而在于移动部分:

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

如果 NodesMobile 表中有多个匹配项,这永远不会成功。

这就是它在 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

输出:

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.

我猜还有其他事情发生

© www.soinside.com 2019 - 2024. All rights reserved.