вторник, 25 септември 2007 г.

Sql LEFT JOIN "problem". (Does not returns results)

Select x.*, y.one, y.two
From x left join y on x.someid = x.someid

works fine

but

Select x.*, y.one, y.two
From x left join y on x.someid = x.someid
Where y=somevalue

does not work.

The solution is simple. Move where clause in join clause
Select x.*, y.one, y.two
From x left join y on x.someid = x.someid and y=somevalue

четвъртък, 20 септември 2007 г.

MSSQL 2000 Query results in random order

I have a web application displaying jokes. One of the functionalities it supports is showing a random joke.
I need the get the MSSQL server to randomize the jokes for me.

Here is my very simple solution:

SELECT *
FROM joke
ORDER BY NEWID()

Each execution generates different random results.

четвъртък, 13 септември 2007 г.

Define a datatable sub set using a function in MSSQL Server

Lets have a Projects table which contains our projects.
In our queries we want to use sub set of that table data depends on some parameters, UserID for example. If the relation is simple it is easy, just add it in where clause.

We want to use that sub set in like a table

CREATE FUNCTION Func_UserProjects (@UserID int) RETURNS TABLE
as
RETURN ( SELECT Distinct p.*
From Projects p
Join ProjectPositions pp on p.ProjectID = pp.ProjectID
Join UsersOnProjectPositions up on up.ProjectPositionID = pp.ProjectPositionID
Where ((UserID = @UserID )

After definition we can use it in select statements for example

Declare @UserID int
Set @UserID int

Select *
FROM SceneComments sc WITH(NOLOCK)
Inner Join Scenes S WITH(NOLOCK) On S.SceneID = sc.SceneID
Inner Join Episodes E WITH(NOLOCK) On E.EpisodeID = S.EpisodeID
Inner Join Func_UserProjects(@UserID) P on P.ProjectID = E.ProjectID

петък, 3 август 2007 г.

Microsoft SQL SERVER - @@IDENTITY vs SCOPE_IDENTITY() vs IDENT_CURRENT

SELECT @@IDENTITY It returns the last IDENTITY value produced on a connection, regardless of the table that produced the value, and regardless of the scope of the statement that produced the value. @@IDENTITY will return the last identity value entered into a table in your current session. While @@IDENTITY is limited to the current session, it is not limited to the current scope. If you have a trigger on a table that causes an identity to be created in another table, you will get the identity that was created last, even if it was the trigger that created it. SELECT SCOPE_IDENTITY() It returns the last IDENTITY value produced on a connection and by a statement in the same scope, regardless of the table that produced the value. SCOPE_IDENTITY(), like @@IDENTITY, will return the last identity value created in the current session, but it will also limit it to your current scope as well. In other words, it will return the last identity value that you explicitly created, rather than any identity that was created by a trigger or a user defined function. SELECT IDENT_CURRENT(’tablename’) It returns the last IDENTITY value produced in a table, regardless of the connection that created the value, and regardless of the scope of the statement that produced the value. IDENT_CURRENT is not limited by scope and session; it is limited to a specified table. IDENT_CURRENT returns the identity value generated for a specific table in any session and any scope. To avoid the potential problems associated with adding a trigger later on, always use SCOPE_IDENTITY() to return the identity of the recently added row in your T SQL Statement or Stored Procedure.