消息512 ... SQL Server代码

时间:2016-07-12 19:06:03

标签: sql sql-server

我的代码有问题:

drop type Pesel
go 

drop assembly pesel 
go 

create assembly pesel 
authorization dbo from 'C:\Users\Logan\Desktop\Projekt\Projekt.dll' with permission_set = safe 
go 

create type dbo.Pesel 
external name Pesel.Pesel 
go 

declare @a Pesel 

select * from dbo.dane 

set @a = (select numer_pesel as pesel from dbo.dane) 
select @a.ValidatePesel() 
select @a.BirthDate() 
select @a.GetGender()

我收到了一个错误:

  

Msg 512,Level 16,State 1,Line 15
  子查询返回的值超过1。当子查询遵循=,!=,<,< =,>,> =或子查询用作表达式时,不允许这样做。

当我尝试这样的例子:

set @a = cast('93071611479' as pesel)

一切都很好,但它仍然只有1个答案:P

编辑:

我设法至少部分地解决了我的问题,但现在我想将结果放到表中

drop type Pesel
go
drop assembly pesel
go
create assembly pesel
authorization dbo
from 'C:\Users\Logan\Desktop\Projekt\Projekt.dll'
with permission_set = safe
go
create type dbo.Pesel
external name Pesel.Pesel
go
declare @a Pesel
select * from dbo.dane
declare @b int
set @b = 0
while @b < (select max(P_Id) from dane)
begin
set @a = (select numer_pesel as pesel from dane where P_Id = 1+@b) 
set @b += 1
if (select numer_pesel from dane) = null
break
select @a.ValidatePesel()
select @a.BirthDate() 
select @a.GetGender()
end

我试图创建表并将上面的选项放入列中但不知何故它不起作用。任何提示家伙:)?

2 个答案:

答案 0 :(得分:0)

阐述戈登的评论:

set @a = (select numer_pesel as pesel from dbo.dane) 

在上面的语句中,select numer_pesel as pesel from dbo.dane返回多个值。这就像是说:

set @a = 123 456 654

相反,您可以选择TOP 1只是为了让您的代码运行,然后在SELECT语句中弄清楚您需要做什么来获得您想要的实际值。

set @a = (select top 1 numer_pesel as pesel from dbo.dane)

您必须使用某种条件或过滤器限制子选择(select numer_pesel as pesel from dbo.dane)

答案 1 :(得分:0)

错误发生在以下行:

set @a = (select numer_pesel as pesel from dbo.dane) 

运行以下查询,看它是否返回多行。

select numer_pesel as pesel from dbo.dane

如果是这样,请在where语句中添加一个条件,使查询返回唯一的numer_pesel