Problem mit Stored Procedure

Hallo

Meine Stored Procedure hier für SQL-Server 2000 scheint ein endlos-Loop zu machen, jedenfalls meldet mein C#-Programm per Exception einen TimeOut.

Ich probiere darin folgendes:

Es wird solange eine fünfstellige Zufallszahl (im Bereich 10’000 bis 99’000) probiert bis eine „gewürfelt“ wurde, zu der es noch keinen Record gibt.

In C# funktioniert das problemlos:

private int GetID()
{
System.Data.SqlClient.SqlCommand cmdInternal;
string s = „select count(ZrNr) from Zentralregister where ZrNr = @ZrNr“;
cmdInternal = new System.Data.SqlClient.SqlCommand(s, base.Connection);
// Wenn eine Transaktion vorhanden ist, wird diese genutzt
if (base.Transaction != null)
cmdInternal.Transaction = base.Transaction;
cmdInternal.Parameters.Add("@ZrNr", System.Data.SqlDbType.Int, 4, „ZrNr“);
Random random;
random = new Random();
bool unique = false;
int i = 0;
int j;
while (unique != true)
{
i = random.Next(10000, 100000);
cmdInternal.Parameters[0].Value = i;
j = (int)cmdInternal.ExecuteScalar();
if (j == 0)
unique = true;
}
return i;
}

für die Stored Procedure habe ich folgendes probiert:

CREATE PROCEDURE RandomTest
AS
declare @ZrNr int

select @ZrNr = rand(10) * 100000
while exists
(select ZrNr
from Zentralregister
where ZrNr = @ZrNr and MUser is Null and IUser is Null)
select @ZrNr = rand(10) * 100000

return @ZrNr
GO

Danke für Infos :wink:

Hallo,

muss es nicht heissen:

SET @ZrNr = SELECT rand(10)*100000

Gruß

Peter