Showing posts with label order. Show all posts
Showing posts with label order. Show all posts

Thursday, March 22, 2012

Creating a System.Management assembly in order for my own Assembly to work?

Hi

I am a bit paranoid about what I just did to my SQL Server 2005 with this CLR experiment.

I created a Class Lib in C# called inLineLib that has a class Queue which represents an object with an ID field.

in another separate namespace called inLineCLRsql, I created a class called test which will hold the function to be accessed from DB, I referenced and created an instances of the Queue class, and retrieve it's ID in a function called PrintMessage.

namespace inlineCLRsql{

public static class test{

public static void PrintMessage(){

inLineLib.Queue q = new inLineLib.Queue();

int i = q.queueId ;

Microsoft.SqlServer.Server.SqlContext.Pipe.Send(i.ToString());

}

}

}

to access this from the db, I attempted to create an assembley referencing inLineCLRsql.dll. This didn't work as it complained about inLineLib assembly not existing in the db. I then attempted to create an assembley for inLineLib but it barfed saying System.Management assembly not created.

so what I did is (and this is where I need to know if I just ruined sql server or not):

1- ALTER DATABASE myDB SET TRUSTWORTHY ON;.

2- CREATE ASSEMBLY SystemManagement

FROM 'C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Management.dll'

WITH PERMISSION_SET = UNSAFE

3- CREATE ASSEMBLY inLineLibMaster

FROM 'D:\inLine\Server\inLineLib\bin\Debug\inLineLib.dll'

WITH PERMISSION_SET = unsafe

4- and finally

CREATE ASSEMBLY inLineLib

FROM 'D:\inLine\Server\CLRSQL\inlineCLRsql\bin\Debug\inlineCLRsql.dll'

WITH PERMISSION_SET = SAFE

Everything works after those steps (which took some trial and error). I can create a sproc like:

CREATE PROC sp_test AS

EXTERNAL NAME inLineLib.[inlineCLRsql.test].PrintMessage

and it returns the Queue ID

Is there anything unadvisable about the steps above?

Thanks for your help

M

System.Management is not supported by CLR integration in SQL Server 2005. What this means is that it has not been tested for reliability and security to work perfectly under a hosted environment. Some (or all) parts of this assembly might work just fine and some (or none) might not work reliably under conditions like memory pressure, stack overflow, stress etc. You need to test your scenario well (for reliablilty and security) and ensure that it works for you. The list of supported .NET framework assemblies is following:

CustomMarshalers

Microsoft.VisualBasic

Microsoft.VisualC

mscorlib

System

System.Configuration

System.Data

System.Data.OracleClient

System.Data.SqlXml

System.Deployment

System.Security

System.Transactions

System.Web.Services

System.Xml

creating a stored procedure-- help

im novice to sqlserver and stored procedures.
Can the php code below be converted to a stored procedure
$y=1;
$query = "Select * From tblNews Order By aOrder";
$result = mysql_query($query,$db_connection);
$NoRows = mysql_num_rows($result);
if ($NoRows != 0 )
{
while ($row = mysql_fetch_array($result))
{
$UpdateQuery = "Update tblNews
Set aOrder= $y
Where ID=".$row["ID"];
mysql_query($UpdateQuery,$db_connection)
;
$y++;
}
}
basically, i want select all from tblNews, order by aOrder
then update aOrder in each record starting at 1 and incrementing by 1
untill all the records have been processed
Can anyone help me please, is it possible
thanks in advance
SteveIf all values in column [aOrder] are diff, then you can try,
update tblNews
set aOrder = (select count(*) from tblNews as a where a.aOrder <=
tblNews.aOrder)
AMB
"ahoy hoy" wrote:

> im novice to sqlserver and stored procedures.
> Can the php code below be converted to a stored procedure
> $y=1;
> $query = "Select * From tblNews Order By aOrder";
> $result = mysql_query($query,$db_connection);
> $NoRows = mysql_num_rows($result);
> if ($NoRows != 0 )
> {
> while ($row = mysql_fetch_array($result))
> {
> $UpdateQuery = "Update tblNews
> Set aOrder= $y
> Where ID=".$row["ID"];
> mysql_query($UpdateQuery,$db_connectio
n);
> $y++;
> }
> }
>
> basically, i want select all from tblNews, order by aOrder
> then update aOrder in each record starting at 1 and incrementing by 1
> untill all the records have been processed
> Can anyone help me please, is it possible
> thanks in advance
> Steve
>|||
Steve,
You could use something along the lines of this... (Un-Tested)
Create Proc TestProcedure
As Begin
Declare @.ID Integer
Declare @.NewOrder Integer
Set @.NewOrder = 1
Declare OrderCursor Cursor For
Select ID From tblNews
Order By aOrder
Open OrderCursor
Fetch Next From OrderCursor Into @.ID
While @.@.Fetch_Status = 0
Begin
Update tblNews
Set aOrder = @.NewOrder
Where ID = @.ID
Set @.NewOrder = @.NewOrder + 1
Fetch Next From OrderCursor Into @.ID
End
Close OrderCursor
Deallocate OrderCursor
End
Go
Although if it is a huge amount of Data and performance is an issue
then I would probably not use a Cursor.
Hope this helps
Barry|||Barry
thank you so much!
i wouldve been trying to figure that out for days, it is exactly what i
needed.
Just needed to use the correct field names and rename Interger to Int,
proc to procedure!
Now i can finish my job
Its only for a small amount of data, 10-20 records
Awesome
Steve :)
Barry wrote:

> Steve,
>
> You could use something along the lines of this... (Un-Tested)
>
> Create Proc TestProcedure
> As Begin
>
> Declare @.ID Integer
> Declare @.NewOrder Integer
> Set @.NewOrder = 1
>
> Declare OrderCursor Cursor For
> Select ID From tblNews
> Order By aOrder
>
> Open OrderCursor
> Fetch Next From OrderCursor Into @.ID
>
> While @.@.Fetch_Status = 0
> Begin
>
> Update tblNews
> Set aOrder = @.NewOrder
> Where ID = @.ID
> Set @.NewOrder = @.NewOrder + 1
> Fetch Next From OrderCursor Into @.ID
> End
> Close OrderCursor
> Deallocate OrderCursor
>
> End
> Go
>
> Although if it is a huge amount of Data and performance is an issue
> then I would probably not use a Cursor.
> Hope this helps
> Barry
>

Friday, February 17, 2012

Created table in SQL SERVER EXPRESS but don't see it

I installed SQL SERVER 2005 Developer Edition over SQL SERVER EXPRESS in order to get the Management Studio.
I created a new Database called TRADING.
I then started a NEW QUERY and created a table and inserted 2 rows.
I then selected from the table and got 0 rows .
I then went to Trading database on the left side and clicked tables and did not see the table I created.
What am I doing wrong. SHould I remove SQL SERVER EXPRESS and then install SQL SERVER 2005 Developer Edition.

look in the master database. you might have created it there.

|||

As Karl suggested, you likely created the table in a database other than your new TRADING database.

In order to use a database, you have to be in the context of that database. You do this by using the USE statement. For example:

CREATE DATABASE Trading

GO

USE Trading

CREATE TABLE MyTable (C1 int, C2 varchar(50))

GO

INSERT INTO MyTable VALUES (1, 'Test')

See the topic 'USE (Transact-SQL)' in Books Online for more information.

You can also see the current database context when you're in the SQL editor by looking at the database listed in the Standard toolbar. Selecting another database from the dropdown list in the toolbar is the same as running a USE <databasename> statement.

Regards,

Gail

create views in the order of their dependency

In SQL 2K...

i need to write a script that writes a script that creates all views in the order that they are nested and I am feeling lazy. Does anyone have anything handy?

For example if View_BOB says...

CREATE VIEW VIEW_BOB
AS
SELECT COL_1 FROM VIEW_JOHN

I need the script to generate a script that creates View_JOHN before View_BOB.You can use Enterprise Manager to generate the script for you and it will take care of dependency of the objects.

SQL Server Helper
http://www.sql-server-helper.com

Tuesday, February 14, 2012

Create view

How can I create a view from a SELECT statement that consists of ORDER BY
clause.
I tried the following but got error from Query Analyser:
Create View AnyView as
(Select * from Table1 Order By ...)
I cannot have an Order By ?Use something like
Create View AnyView as
(Select Top 100 Percent * from Table1 Order By ...)
--
HTH,
Vinod Kumar
MCSE, DBA, MCAD
http://www.extremeexperts.com
"Alan" <alanpltse@.yahoo.com.au> wrote in message
news:ujD9hJodDHA.748@.TK2MSFTNGP10.phx.gbl...
> How can I create a view from a SELECT statement that consists of ORDER BY
> clause.
> I tried the following but got error from Query Analyser:
> Create View AnyView as
> (Select * from Table1 Order By ...)
> I cannot have an Order By ?
>|||Thanks.
> Use something like
> Create View AnyView as
> (Select Top 100 Percent * from Table1 Order By ...)
> --
> HTH,
> Vinod Kumar
> MCSE, DBA, MCAD
> http://www.extremeexperts.com
>
> "Alan" <alanpltse@.yahoo.com.au> wrote in message
> news:ujD9hJodDHA.748@.TK2MSFTNGP10.phx.gbl...
> > How can I create a view from a SELECT statement that consists of ORDER
BY
> > clause.
> > I tried the following but got error from Query Analyser:
> >
> > Create View AnyView as
> > (Select * from Table1 Order By ...)
> >
> > I cannot have an Order By ?
> >
> >
>|||Why do you want to do that? A table is not ordered. A view is supposed to look like a table. You are
breaking this very important aspect of the relational model here. Why not quite simply specify the
ORDER BY when you work against the view?
The reason why you can have ORDER BY if you have TOP is when you have a query that is really
dependent on TOP to give correct information, and not to expose a "sorted table".
--
Tibor Karaszi, SQL Server MVP
Archive at: http://groups.google.com/groups?oi=djq&as ugroup=microsoft.public.sqlserver
"Alan" <alanpltse@.yahoo.com.au> wrote in message news:ujD9hJodDHA.748@.TK2MSFTNGP10.phx.gbl...
> How can I create a view from a SELECT statement that consists of ORDER BY
> clause.
> I tried the following but got error from Query Analyser:
> Create View AnyView as
> (Select * from Table1 Order By ...)
> I cannot have an Order By ?
>|||The reason I am going to create a view from a table order by one of the
fields is I am doing a course at institute, this is one of the exercise
questions.
The question is :
Create a view from a table called Customer, include the first name, last
name (alphabetical order), phone and fax. The primary key of the Customer
table is an auto increment field.
So the table Customer is not sorted by last name but the insertion order.
"Tibor Karaszi" <tibor.please_reply_to_public_forum.karaszi@.cornerstone.se>
wrote in message news:uyNUSupdDHA.4020@.tk2msftngp13.phx.gbl...
> Why do you want to do that? A table is not ordered. A view is supposed to
look like a table. You are
> breaking this very important aspect of the relational model here. Why not
quite simply specify the
> ORDER BY when you work against the view?
> The reason why you can have ORDER BY if you have TOP is when you have a
query that is really
> dependent on TOP to give correct information, and not to expose a "sorted
table".
> --
> Tibor Karaszi, SQL Server MVP
> Archive at: http://groups.google.com/groups?oi=djq&as
ugroup=microsoft.public.sqlserver|||> So the table Customer is not sorted by last name but the insertion order.
If this course if about relational databases, then above is incorrect. A table is *not* ordered.
This is one of the most basic parts of the relational theory, and if your lecturer didn't manage to
get that across, I fear you have some problems.
> Create a view from a table called Customer, include the first name, last
> name (alphabetical order), phone and fax. The primary key of the Customer
> table is an auto increment field.
Again, a view is supposed to behave like a table (it is, in the relational lingua, a "named table"),
so it should behave just like a table. The various product's ways to "sort a view" are all
non-relational. See my earlier post for more info.
--
Tibor Karaszi, SQL Server MVP
Archive at: http://groups.google.com/groups?oi=djq&as ugroup=microsoft.public.sqlserver
"Alan" <alanpltse@.yahoo.com.au> wrote in message news:ey4Lm4ydDHA.3596@.tk2msftngp13.phx.gbl...
> The reason I am going to create a view from a table order by one of the
> fields is I am doing a course at institute, this is one of the exercise
> questions.
> The question is :
> Create a view from a table called Customer, include the first name, last
> name (alphabetical order), phone and fax. The primary key of the Customer
> table is an auto increment field.
> So the table Customer is not sorted by last name but the insertion order.
> "Tibor Karaszi" <tibor.please_reply_to_public_forum.karaszi@.cornerstone.se>
> wrote in message news:uyNUSupdDHA.4020@.tk2msftngp13.phx.gbl...
> > Why do you want to do that? A table is not ordered. A view is supposed to
> look like a table. You are
> > breaking this very important aspect of the relational model here. Why not
> quite simply specify the
> > ORDER BY when you work against the view?
> >
> > The reason why you can have ORDER BY if you have TOP is when you have a
> query that is really
> > dependent on TOP to give correct information, and not to expose a "sorted
> table".
> >
> > --
> > Tibor Karaszi, SQL Server MVP
> > Archive at: http://groups.google.com/groups?oi=djq&as
> ugroup=microsoft.public.sqlserver
>|||What the Customer table's primary key is INT IDENTITY, so the records are
not sorted by the name, this is what the table is.
"Tibor Karaszi" <tibor.please_reply_to_public_forum.karaszi@.cornerstone.se>
wrote in message news:#g3izE2dDHA.3792@.tk2msftngp13.phx.gbl...
> > So the table Customer is not sorted by last name but the insertion
order.
> If this course if about relational databases, then above is incorrect. A
table is *not* ordered.
> This is one of the most basic parts of the relational theory, and if your
lecturer didn't manage to
> get that across, I fear you have some problems.
>
> > Create a view from a table called Customer, include the first name, last
> > name (alphabetical order), phone and fax. The primary key of the
Customer
> > table is an auto increment field.
> Again, a view is supposed to behave like a table (it is, in the relational
lingua, a "named table"),
> so it should behave just like a table. The various product's ways to "sort
a view" are all
> non-relational. See my earlier post for more info.|||In addition, we cannot make the LastName as PRIMARY KEY so I think the
auto-increment is needed.
"Alan" <alanpltse@.yahoo.com.au> wrote in message
news:#H8FjXBeDHA.2320@.TK2MSFTNGP12.phx.gbl...
> What the Customer table's primary key is INT IDENTITY, so the records are
> not sorted by the name, this is what the table is.
> "Tibor Karaszi"
<tibor.please_reply_to_public_forum.karaszi@.cornerstone.se>
> wrote in message news:#g3izE2dDHA.3792@.tk2msftngp13.phx.gbl...
> > > So the table Customer is not sorted by last name but the insertion
> order.
> >
> > If this course if about relational databases, then above is incorrect. A
> table is *not* ordered.
> > This is one of the most basic parts of the relational theory, and if
your
> lecturer didn't manage to
> > get that across, I fear you have some problems.
> >
> >
> > > Create a view from a table called Customer, include the first name,
last
> > > name (alphabetical order), phone and fax. The primary key of the
> Customer
> > > table is an auto increment field.
> >
> > Again, a view is supposed to behave like a table (it is, in the
relational
> lingua, a "named table"),
> > so it should behave just like a table. The various product's ways to
"sort
> a view" are all
> > non-relational. See my earlier post for more info.
>|||I'm sorry, but I don't understand what these two posts has to do with "ordering" of the table...
--
Tibor Karaszi, SQL Server MVP
Archive at: http://groups.google.com/groups?oi=djq&as ugroup=microsoft.public.sqlserver
"Alan" <alanpltse@.yahoo.com.au> wrote in message news:uslgy3LeDHA.1820@.TK2MSFTNGP10.phx.gbl...
> In addition, we cannot make the LastName as PRIMARY KEY so I think the
> auto-increment is needed.
> "Alan" <alanpltse@.yahoo.com.au> wrote in message
> news:#H8FjXBeDHA.2320@.TK2MSFTNGP12.phx.gbl...
> > What the Customer table's primary key is INT IDENTITY, so the records are
> > not sorted by the name, this is what the table is.
> >
> > "Tibor Karaszi"
> <tibor.please_reply_to_public_forum.karaszi@.cornerstone.se>
> > wrote in message news:#g3izE2dDHA.3792@.tk2msftngp13.phx.gbl...
> > > > So the table Customer is not sorted by last name but the insertion
> > order.
> > >
> > > If this course if about relational databases, then above is incorrect. A
> > table is *not* ordered.
> > > This is one of the most basic parts of the relational theory, and if
> your
> > lecturer didn't manage to
> > > get that across, I fear you have some problems.
> > >
> > >
> > > > Create a view from a table called Customer, include the first name,
> last
> > > > name (alphabetical order), phone and fax. The primary key of the
> > Customer
> > > > table is an auto increment field.
> > >
> > > Again, a view is supposed to behave like a table (it is, in the
> relational
> > lingua, a "named table"),
> > > so it should behave just like a table. The various product's ways to
> "sort
> > a view" are all
> > > non-relational. See my earlier post for more info.
> >
> >
>