2016-07-12 71 views
0

我有問題,我的代碼:消息512 ... 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
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

當我嘗試它,例如像這樣:

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 = [email protected]) 
set @b += 1 
if (select numer_pesel from dane) = null 
break 
select @a.ValidatePesel() 
select @a.BirthDate() 
select @a.GetGender() 
end 

我試圖創建表,並把上述選擇成列,但不知何故,它不起作用。任何提示球員:)?

+8

(1)我刪除了MySQL的標籤,因爲代碼顯然是T-SQL。 (2)錯誤信息非常明顯:'dane'有不止一行。有什麼你不明白的嗎? –

回答

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)用某種條件或過濾器。

+0

然後,沒有明確的'ORDER BY'的'TOP 1' * *只是返回1個任意的行,沒有特定的順序 - 通常不是你想要的...... –

+0

@marc_s我同意,這就是爲什麼我說它只是讓代碼運行,然後他可以找出如何限制它來獲得正確的值。絕對不會是OP想要的開箱即用的價值。 – scsimon

0

錯誤發生在下面一行:

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

運行下面的查詢,看看它是否返回比一行多。

select numer_pesel as pesel from dbo.dane 

如果是這樣,添加具有WHERE語句進行查詢返回唯一numer_pesel條件