Showing posts with label int. Show all posts
Showing posts with label int. Show all posts

Wednesday, March 21, 2012

Is it possible to create thread & start from CLR Stored Proc

My simple CLR Stored procedure is as below:

[Microsoft.SqlServer.Server.SqlProcedure]
public static int MyParallelStoredProc(string name1, string name2)
{
Thread t = null;
Worker wth = null;
int parallel = 2;
Object[] obj = new object [parallel];
SqlPipe p;
p = SqlContext.Pipe;

for (int i = 0; i < parallel; i++)
{
if (i == 0)
wth = new Worker(name1);
else
wth = new Worker(name2);
t = new Thread(new System.Threading.ThreadStart(wth.WorkerProc));
t.Name = "Thread -" + i.ToString() + ":";
t.Start();
p.Send(t.Name + ":Started");
obj[ i] = t;
}
for (int i = 0; i < parallel; i++)
{
t = (System.Threading.Thread)obj[ i];
t.Join();
p.Send(t.Name + ":Finished");
}
return 0;
}

The worker class implementing Thread Proc:

public class Worker
{
private string Name;

public Worker(string name)
{
SqlPipe p;
p = SqlContext.Pipe;
Name = name;
p.Send("In Constructor:" + Name);
}

public void WorkerProc()
{
SqlPipe p;
p = SqlContext.Pipe;
for (int i = 0; i < 10; i++)
p.Send(i.ToString()+":"+Name);
}
}

The assembly is registered with UNSAFE permission set.

CREATE ASSEMBLY
ThreadTest
FROM
'C:\\ThreadTest\bin\Debug\ThreadTest.dll'
WITH
permission_set = unsafe;
GO

CREATE PROC ParallelStoredProc
@.Name1 NVARCHAR(1024),
@.Name2 NVARCHAR(1024)
AS
EXTERNAL NAME ThreadTest.[MyTest.ThreadTest].MyParallelStoredProc

When I invoke the the stored procedure from T-SQL script as below,

EXEC ParallelStoredProc @.Name1, @.Name2

the thread class constructor gets called; but the 'WorkerProc' does not execute ?

Whether an UNSAFE assembly is allowed to spawn threads

inside SQL Server ?

Your code works correctly to start and run threads under unsafe. The reason you think it doesn't work is because the SqlContext connection is not available on new threads, so you can't use it to Pipe.Send information back.

If you try/catch for exceptions in your WorkerProc, you should see an error like the following:

"The requested operation requires a Sql Server execution thread. The current thread was started by user code or other non-Sql Server engine code."

Steven

|||

Thanks steve. Your input was very useful.

If I use SqlConnection in WorkerProc, the thread gets aborted

and goes into "Stopped" state.

It means the main CLR Stored proc can only execute the T-SQL commands ?

WorkerProc's are restricted to computations.

Friday, March 9, 2012

Is it necessary to order again?

I have a function that returns a table:

CREATE FUNCTION dbo.Example(@.Param int)
RETURNS @.Tbl TABLE (
Field1 int,
Field2 int) AS
BEGIN
INSERT @.Tbl (Field1,Field2)
SELECT FieldA,FieldB FROM DataTable
WHERE FieldC = @.Param
ORDER BY FieldA
RETURN
END

The statement that populates the table orders the data. In order
to ensure the results are ordered that way, should the call to the
function include an ordering? I.e., is this sufficient

SELECT * FROM dbo.Example(17)

or is this necessary? --

SELECT * FROM dbo.Example(17) ORDER BY Field1

Thanks!"Jim Geissman" <jim_geissman@.countrywide.com> wrote in message
news:b84bf9dc.0408051457.6ae418c0@.posting.google.c om...
> I have a function that returns a table:
> CREATE FUNCTION dbo.Example(@.Param int)
> RETURNS @.Tbl TABLE (
> Field1 int,
> Field2 int) AS
> BEGIN
> INSERT @.Tbl (Field1,Field2)
> SELECT FieldA,FieldB FROM DataTable
> WHERE FieldC = @.Param
> ORDER BY FieldA
> RETURN
> END
> The statement that populates the table orders the data.

No, it doesn't. Tables are sets of data. They have no order.

Now, your statement may put the data into the table in order... but there's
no guarantee that SQL Server will store it in that order.

> In order
> to ensure the results are ordered that way, should the call to the
> function include an ordering? I.e., is this sufficient
> SELECT * FROM dbo.Example(17)
No

> or is this necessary? --
> SELECT * FROM dbo.Example(17) ORDER BY Field1

Yes.

> Thanks!

Wednesday, March 7, 2012

Is it a bug in SQL CE?

Hi!

I use SQL CE with VS.NET. I find the following bug 2th.

The table has an "ID int IDENTITY(0,1) PRIMARY KEY,". That is my row identity.

I add rows to the table, then I realized that the ID order not in the general order (from 0 to ........)

For example: 6,7,8,0,1,2,3,4,5.

Of course row 6,7 and 8 was added the very last.

The content of each row is not mixed, only the ID order.

Is it a very confused, because we develop mobile invoice programs for PDAs.

What I did wrong?

Thank you!

Does that happen in the Query Analyzer on the PDA?

Maybe it does not order the rows by the primary key column by default (?). I don't know if that would be a bug, although it sounds more convenient if it did order on any key columns.

|||I moved this thread to the SQL Mobile's team forusm|||

Neither SQL Server nor SQL Mobile guarrenty you about the physical order of the rows and you are not expected to concluded something from running multiple queries. It can always change. If you want the rows to be ordered on a column, you should ideally use ORDER BY. Your query result (with out ordering) always depends on the cursor position.

Thanks,

Laxmi Narsimha Rao ORUGANTI, MSFT, SQL Mobile, Microsoft Corporation