Showing posts with label situation. Show all posts
Showing posts with label situation. Show all posts

Wednesday, March 28, 2012

How can I read the last commited record values while an other transaction is updating

The situation is like this:
I have a record in a table that I want to read and I want the last committed
version of it.
The problem is that another transaction is updating the record at the same
time in a verry long transaction. While this transaction is running I can
not read the record because it is locked. How can I read the current
commited values from this record without blocking?
I do not want to use READ UNCOMMITED isolation level because if the long
running transaction is rolled back than I would have read a uncommited
value.
This situation happens in a dotnet application using 2 SQLConnection classes
(and connections to the DB) to read and update the same record.
Thanks,
Sandor"Sandor Heese" <nospam@.hotmail.com> wrote in message
news:Ot2Iyf2HFHA.2156@.TK2MSFTNGP09.phx.gbl...
> The situation is like this:
> I have a record in a table that I want to read and I want the last
> committed version of it.
> The problem is that another transaction is updating the record at the same
> time in a verry long transaction. While this transaction is running I can
> not read the record because it is locked. How can I read the current
> commited values from this record without blocking?
> I do not want to use READ UNCOMMITED isolation level because if the long
> running transaction is rolled back than I would have read a uncommited
> value.
> This situation happens in a dotnet application using 2 SQLConnection
> classes (and connections to the DB) to read and update the same record.
>
First, why are you using 2 SqlConnections?
Second, no there is no way to read the last commited version of a locked
row. That is "snapshot isolation" and it's a new feature of SQL Server
2005. With SQL Server 2000 you must choose between reading the row after
it's commited, or read the dirty row.
David|||Thanks David for your comment.
I came to the same conclusion you did, but I did not want to beleave that it
was not possible :-)
I am using 2 connections because I can't do 2 transaction at the same time
over 1 SQLConnection.
In SQL Server 2005 this will be possible but not at the moment. (It is
always in the next version :-( )
Sandor
"David Browne" <davidbaxterbrowne no potted meat@.hotmail.com> wrote in
message news:uUvhOo2HFHA.2564@.tk2msftngp13.phx.gbl...
> "Sandor Heese" <nospam@.hotmail.com> wrote in message
> news:Ot2Iyf2HFHA.2156@.TK2MSFTNGP09.phx.gbl...
> First, why are you using 2 SqlConnections?
> Second, no there is no way to read the last commited version of a locked
> row. That is "snapshot isolation" and it's a new feature of SQL Server
> 2005. With SQL Server 2000 you must choose between reading the row after
> it's commited, or read the dirty row.
> David
>

Monday, March 19, 2012

How can I make a "template" of my database?

I have a situation where my database might be deleted every so oftern and I
would like to find a way to "import" sort of a "blank template" of the
original database. Is there a way to do this?
I realize that I could have my program code just create another database,
but that's kind of a hastle, so I was looking for an easier way.
res0gyio@.verizon.netIn Enterprise Manager, right click on the database, all tasks - Generate
script - then select the appropiate options you would like (I addtion to
selecting all your tables on the first tab, I suggest Indexs & defaults on
the last tab)
Mo
"michaaal" <res0gyio@.verizon.net> wrote in message
news:uBiT7mwCEHA.1600@.tk2msftngp13.phx.gbl...
> I have a situation where my database might be deleted every so oftern and
I
> would like to find a way to "import" sort of a "blank template" of the
> original database. Is there a way to do this?
> I realize that I could have my program code just create another database,
> but that's kind of a hastle, so I was looking for an easier way.
> res0gyio@.verizon.net
>|||Almost forgot to mention, you can run the script in Query Analyzer to
re-structure your databases (it'll also wipe it out first if you selected
the "drop table" option while creating the script)
Mo
"michaaal" <res0gyio@.verizon.net> wrote in message
news:uBiT7mwCEHA.1600@.tk2msftngp13.phx.gbl...
> I have a situation where my database might be deleted every so oftern and
I
> would like to find a way to "import" sort of a "blank template" of the
> original database. Is there a way to do this?
> I realize that I could have my program code just create another database,
> but that's kind of a hastle, so I was looking for an easier way.
> res0gyio@.verizon.net
>|||A lots of tools and options to generate DDL scripts:
http://www.karaszi.com/sqlserver/in...rate_script.asp
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
"michaaal" <res0gyio@.verizon.net> wrote in message
news:uBiT7mwCEHA.1600@.tk2msftngp13.phx.gbl...
> I have a situation where my database might be deleted every so oftern and
I
> would like to find a way to "import" sort of a "blank template" of the
> original database. Is there a way to do this?
> I realize that I could have my program code just create another database,
> but that's kind of a hastle, so I was looking for an easier way.
> res0gyio@.verizon.net
>

Sunday, February 19, 2012

How can I get a count of all records in each table in my DB?

Hoping you can help me here... I have a situation where I need to provide
a
script that can be run through query analyzer to return the number of record
s
in each table in my DB.
I would like it to be dynamic, as the number of tables in the DB changes.
Pretty simple query (Select count(*) from tblXXX), but I can't seem to find
where the actual table names are stored within the system tables so that I
can run through all of them.
I know that I could use Enterprise Manager and view the DB as a TaskPad to
see the number of records in each table, but the person who will be doing
this only has permissions to view the DB through QA.
Any thoughts on a script that will do this?
Thanks in advance...
gTry,
use northwind
go
-- undocumented sp
exec sp_msforeachtable 'select ''?'', count(*) from ?'
go
-- using sysindexes
dbcc updateusage (0)
select object_name([id]), rowcnt
from sysindexes
where indid in (0, 1) and objectproperty([id], 'IsUserTable') = 1
order by 1
go
-- using a cursor and dyanmic sql
create table #t (
table_name sysname,
rowcnt int,
)
declare @.sql nvarchar(4000)
declare @.ts sysname
declare @.tn sysname
declare my_cursor cursor
local
fast_forward
for
select
table_schema,
table_name
from
information_schema.tables
where
table_type = 'BASE TABLE'
open my_cursor
while 1 = 1
begin
fetch next from my_cursor into @.ts, @.tn
if @.@.error != 0 or @.@.fetch_status != 0 break
set @.sql = N'select ''' + @.tn + ''' as table_name, count(*) as rowcnt from
[' + @.ts + '].[' + @.tn + ']'
insert into #t
exec sp_executesql @.sql
end
close my_cursor
deallocate my_cursor
select * from #t order by table_name
drop table #t
go
AMB
"Greg Toronto" wrote:

> Hoping you can help me here... I have a situation where I need to provid
e a
> script that can be run through query analyzer to return the number of reco
rds
> in each table in my DB.
> I would like it to be dynamic, as the number of tables in the DB changes.
> Pretty simple query (Select count(*) from tblXXX), but I can't seem to fin
d
> where the actual table names are stored within the system tables so that I
> can run through all of them.
> I know that I could use Enterprise Manager and view the DB as a TaskPad to
> see the number of records in each table, but the person who will be doing
> this only has permissions to view the DB through QA.
> Any thoughts on a script that will do this?
> Thanks in advance...
> g|||In SQL Server 2000:
DBCC UPDATEUSAGE(0);
select o.name, i.rowcnt
from sysobjects o
inner join sysindexes i
on o.id = i.id
WHERE i.indid IN (0,1)
AND OBJECTPROPERTY(o.id, 'IsMsShipped') = 0
ORDER BY o.name
In SQL Server 2005, it's a little more complex, accounting for table
partitions:
SELECT
t.name,
[RowCount] = SUM
(
CASE
WHEN (p.index_id < 2) AND (a.type = 1) THEN p.rows
ELSE 0
END
)
FROM
sys.tables t
INNER JOIN sys.partitions p
ON t.object_id = p.object_id
INNER JOIN sys.allocation_units a
ON p.partition_id = a.container_id
GROUP BY
t.name;
"Greg Toronto" <GregToronto@.discussions.microsoft.com> wrote in message
news:1AEB569D-DA79-4B7B-9892-932B37D4823E@.microsoft.com...
> Hoping you can help me here... I have a situation where I need to
> provide a
> script that can be run through query analyzer to return the number of
> records
> in each table in my DB.
> I would like it to be dynamic, as the number of tables in the DB changes.
> Pretty simple query (Select count(*) from tblXXX), but I can't seem to
> find
> where the actual table names are stored within the system tables so that I
> can run through all of them.
> I know that I could use Enterprise Manager and view the DB as a TaskPad to
> see the number of records in each table, but the person who will be doing
> this only has permissions to view the DB through QA.
> Any thoughts on a script that will do this?
> Thanks in advance...
> g