Hi, I'm having trouble with some SQL. I have a document table which links to a document version table. I'd like to write a query to get the latest enabled version of every document - a resultset with one row per document. Is this possible with resorting to a cursor?
CREATE TABLE tblDoc (
DocId int
)
GO
CREATE TABLE tblDocVersion (
DocVersionId int,
DocId int,
DateCreated datetime,
IsEnabled bit
)
GO
INSERT INTO tblDoc (DocId) VALUES (1)
INSERT INTO tblDoc (DocId) VALUES (2)
INSERT INTO tblDocVersion (DocVersionId, DocId, DateCreated, IsEnabled) VALUES (1, 1, '8/8/2006' , 1)
INSERT INTO tblDocVersion (DocVersionId, DocId, DateCreated, IsEnabled) VALUES (1, 1, '9/9/2006' , 1)
INSERT INTO tblDocVersion (DocVersionId, DocId, DateCreated, IsEnabled) VALUES (1, 1, '10/10/2006' , 0)
INSERT INTO tblDocVersion (DocVersionId, DocId, DateCreated, IsEnabled) VALUES (1, 2, '8/8/2006' , 1)
INSERT INTO tblDocVersion (DocVersionId, DocId, DateCreated, IsEnabled) VALUES (1, 2, '11/11/2006' , 1)
Desired output:
DocId DocVersionId DateCreated
============================
1 2 9/9/2006
2 5 11/11/2006
Thanks!
MrsMUse a subquery:
select tblDocVersion.DocId,
tblDoVersion.DocVersionID,
tblDocVersion.DateCreated
from tblDocVersion
inner join --LastVersions
(select DocId,
max(DateCreated) as DateCreated
from tblDocVersion
group by DocId) LastVersion
on tblDocVersion.DocId = LastVersion.DocID
and tblDocVersion.DateCreated = LastVersion.DateCreated
By the way...why do you prefix your table name with "tbl"? Are you afraid you are going to forget what it is? Did your husbandBOB call and remind you to pick up some petfoodALPO for your dogFIDO today?|||ah nice, joining on a sub query - never thought of that, thanks a lot.
the tbl thing is part of the standards at my company. doesn't do anyone any harm!
thanks again|||the tbl thing is part of the standards at my company. doesn't do anyone any harm!that's highly doubtful
not the "part of the standards at my company" part
the other part
:)|||Did your husbandBOB call and remind you to pick up some petfoodALPO for your dogFIDO today?
hah! :)
i always hated hungarian. intellisense seems to be slowly killing it off in the world of compiled code thankfully. from the bible of umaintainable code (http://mindprod.com/jgloss/unmain.html):
a_crszkvc30LastNameCol:
"It took a team of maintenance engineers nearly 3 days to figure out that this whopper variable name described a const, reference, function argument that was holding information from a database column of type Varchar[30] named "LastName" which was part of the table's primary key"|||the tbl thing is part of the standards at my company. doesn't do anyone any harm!Why don't you suggest a standard that all desktop PCs be painted schoolbus yellow, all laptops be candy-striped red, and all printers have blue polka-dots? Also harmless, and just as sensible.|||I knew one team at MS that had a convention of putting _sp at the end of every sproc.
Presumably this was because they knew there was some downside to putting sp_ at the front.
I'm sure it really helped them distingush procs from tables, which, btw, all had _tbl at the end. ;)
Showing posts with label enabled. Show all posts
Showing posts with label enabled. Show all posts
Wednesday, March 28, 2012
Friday, March 9, 2012
Large value types out of row - objectproperties
Hi,
I know that i can set "large value types out of row" table option using
"sp_tableoption" but i could not find a way to know if it is enabled or
disabled.
sp_tableoption returns nothing so i thought i could use
objectproperties but there doesn't seem to be any property name
documented for "large value types out of row" in msdn.
So, how can i check if it is enabled or not?
Thanks.What version of SQL Server? Object property in 2000 has the TableTextInRowLi
mit attribute you can
ask for.
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
Blog: http://solidqualitylearning.com/blogs/tibor/
<antimon@.gmail.com> wrote in message news:1141611920.307183.105940@.v46g2000cwv.googlegroups
.com...
> Hi,
> I know that i can set "large value types out of row" table option using
> "sp_tableoption" but i could not find a way to know if it is enabled or
> disabled.
> sp_tableoption returns nothing so i thought i could use
> objectproperties but there doesn't seem to be any property name
> documented for "large value types out of row" in msdn.
> So, how can i check if it is enabled or not?
> Thanks.
>|||It is sql server 2005. TableTextInRowLimit works fine but i want to
learn if the option that forces large value types (char(MAX),
varchar(MAX)..) to be stored in seperated pages even if it is a small
piece of data is turned on or not.|||> It is sql server 2005.
Please include version number upfront, makes the whole process quicker... :-
)
Seems like 2005 doesn't expose text in row for the new datatypes. The sys.ta
bles catalog view does,
however...
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
Blog: http://solidqualitylearning.com/blogs/tibor/
<antimon@.gmail.com> wrote in message news:1141656316.186672.224600@.j52g2000cwj.googlegroups
.com...
> It is sql server 2005. TableTextInRowLimit works fine but i want to
> learn if the option that forces large value types (char(MAX),
> varchar(MAX)..) to be stored in seperated pages even if it is a small
> piece of data is turned on or not.
>|||I thought i had it in my first post :) sorry about that.
And thanks for the solution, sys.tables works great.
I know that i can set "large value types out of row" table option using
"sp_tableoption" but i could not find a way to know if it is enabled or
disabled.
sp_tableoption returns nothing so i thought i could use
objectproperties but there doesn't seem to be any property name
documented for "large value types out of row" in msdn.
So, how can i check if it is enabled or not?
Thanks.What version of SQL Server? Object property in 2000 has the TableTextInRowLi
mit attribute you can
ask for.
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
Blog: http://solidqualitylearning.com/blogs/tibor/
<antimon@.gmail.com> wrote in message news:1141611920.307183.105940@.v46g2000cwv.googlegroups
.com...
> Hi,
> I know that i can set "large value types out of row" table option using
> "sp_tableoption" but i could not find a way to know if it is enabled or
> disabled.
> sp_tableoption returns nothing so i thought i could use
> objectproperties but there doesn't seem to be any property name
> documented for "large value types out of row" in msdn.
> So, how can i check if it is enabled or not?
> Thanks.
>|||It is sql server 2005. TableTextInRowLimit works fine but i want to
learn if the option that forces large value types (char(MAX),
varchar(MAX)..) to be stored in seperated pages even if it is a small
piece of data is turned on or not.|||> It is sql server 2005.
Please include version number upfront, makes the whole process quicker... :-
)
Seems like 2005 doesn't expose text in row for the new datatypes. The sys.ta
bles catalog view does,
however...
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
Blog: http://solidqualitylearning.com/blogs/tibor/
<antimon@.gmail.com> wrote in message news:1141656316.186672.224600@.j52g2000cwj.googlegroups
.com...
> It is sql server 2005. TableTextInRowLimit works fine but i want to
> learn if the option that forces large value types (char(MAX),
> varchar(MAX)..) to be stored in seperated pages even if it is a small
> piece of data is turned on or not.
>|||I thought i had it in my first post :) sorry about that.
And thanks for the solution, sys.tables works great.
Subscribe to:
Posts (Atom)