Showing posts with label ive. Show all posts
Showing posts with label ive. Show all posts

Monday, March 12, 2012

Primary Key

Hello all,
I'm taking over a project from another developer and i've run into a bit of a problem.

This developer had a bad habit of not using primary keys when designing various databases used by his programs. So now i've got approx 1000 tables all of which do not have primary keys assigned.

Does anyone know of a tsql script that i can run that will loop through each table and add a primary key field?

Thanks in advance?

Richard M.

It sounds temping, but you need to understand several things.

What is a primary key on a table, and what is an index. You might need a complex key (say 3 fields), but you add an "autonumber" field to propgate as a foreighn key. Then what about indexing other fields, because without it, a database can become bogged down as data grows.

You are a brave man to take this one on. I would suggest requesting time to rationalise what is there, especially with so many tables. The risks of failure in your project is increased if you do not review what is there.

It is however, possible to add indexes. Look at the system tables, from which you can get the names of all user tables - you can do this in VB or C# ny calling a stored proc to get the tables names. Then simply execute some SQL on a connection to run the same script to add an ID column - there are plenty of examples of adding fields in SQL in the SQL Server help.

Good luck

|||

The good news is you can ALTER your tables manually in Enterprise Manager and generate the script to create the new table in a new version of your database. The bad news is the tool is very expensive, the cheap one is $400 called AdeptSQL but you can test drive Embarcadero for 14 days. Try the link below for details. 1000 tables makes it worth looking into tools so you don't make expensive mistakes. Hope this helps.

http://www.msdner.com/forum/thread78800.html

http://www.embarcadero.com/products/dbartisan/index.html

Friday, March 9, 2012

Primary Database Backup in Log Shipping Scenario

I've setup a Log Shipping scenario for one of our databases. This works fine.
Also, every night a full database backup of the primary database is made to a
disk location. Normally, the transaction log would be truncated after a full
database backup. This behavior is unwanted in a Log Shipping scenario since
the content of the transaction log needs to be applied to the secondary
database. We've seen that since the Log Shipping scenario is in place, the
log is no longer truncated after the full database backup of the primary
database. How does the primary database "know" not to truncate the
transaction log? In other words, I'd like to know how the full database
backup mechanism works in a Log Shipping environment.
Are you saying you with issue a backup log with truncate_only statement
after your backup has finished?
ie
backup log fulltext with truncate_only
If so, this will break your log shipping chain and should not be done.
The way a log shipping works is a backup is done. Embedded in the backup is
the LSN (Log Sequence Number) for the transaction log. The next log backup
you do contains log enteries starting at the lsn for this last backup. So
you can restore the log to the database backup where the lsn matches the lsn
embedded in the backup.
If you then deploy log shipping and do a backup the backup entry is in the
log but it is ignored, so you can continue to apply subsequent log dumps
without breaking the chain.
Does this answer your question?
Hilary Cotter
Looking for a SQL Server replication book?
http://www.nwsu.com/0974973602.html
Looking for a FAQ on Indexing Services/SQL FTS
http://www.indexserverfaq.com
"Wilbert" <Wilbert@.discussions.microsoft.com> wrote in message
news:542507D8-50F4-4CEC-BEE5-36DF1EBBA607@.microsoft.com...
> I've setup a Log Shipping scenario for one of our databases. This works
> fine.
> Also, every night a full database backup of the primary database is made
> to a
> disk location. Normally, the transaction log would be truncated after a
> full
> database backup. This behavior is unwanted in a Log Shipping scenario
> since
> the content of the transaction log needs to be applied to the secondary
> database. We've seen that since the Log Shipping scenario is in place, the
> log is no longer truncated after the full database backup of the primary
> database. How does the primary database "know" not to truncate the
> transaction log? In other words, I'd like to know how the full database
> backup mechanism works in a Log Shipping environment.
|||Hello Hilary,
I'll clarify my question: I'd like to know how SQL Server "knows" whether a
Log Shipping scenario is in place, and I'd like an explanation to the
subsequent difference in behavior when performing a Full Database backup
(truncate the log in "normal" operation and not truncating the log in a Log
Shipping scenario).
Best regards, Wilbert
|||SQL Server does not know if a log is being shipped or not. Log shipping fits
into the existing point in time recovery scheme that sql server uses to
recover databases.
If you truncate a log it will remove all committed entries in that log.
Logged open transaction and other non-committed commands will remain in the
log since the last checkpoint. There is no log truncation after a back up in
full recovery model, not in bulk logged. The problem with bulk logged is
that minimally logged operations are minimally logged and will destroy your
log shipping chain.
Hilary Cotter
Looking for a SQL Server replication book?
http://www.nwsu.com/0974973602.html
Looking for a FAQ on Indexing Services/SQL FTS
http://www.indexserverfaq.com
"Wilbert" <Wilbert@.discussions.microsoft.com> wrote in message
news:00B5F549-71CE-4089-A81A-A537B6BA212F@.microsoft.com...
> Hello Hilary,
> I'll clarify my question: I'd like to know how SQL Server "knows" whether
> a
> Log Shipping scenario is in place, and I'd like an explanation to the
> subsequent difference in behavior when performing a Full Database backup
> (truncate the log in "normal" operation and not truncating the log in a
> Log
> Shipping scenario).
> Best regards, Wilbert
|||Hello Hilary,
Isn't it so that after a full database backup the committed entries in the
log are removed?
That's the origin of my question: in "normal" operation these entries are
removed from the log, while in a log shipping scenario this doesn't seem to
be the case. This led me to believe that SQL Server somehow "know" that log
shipping is taking place and that the entries in the log should be retained
until the next log backup.
Best regards, Wilbert
|||No, the log is insert and read only. What happens is that it is a record of
all of your database activity. Before anything hits disk it is written to
your log. When you issue a checkpoint the checkpoint is written to the log
and all dirty pages (including uncommitted pages are written to disk).
Should a failure occur the log is consulted to find the last checkpoint and
uncommitted transactions which occurred after the last checkpoint are
removed from disk and committed transactions which committed after the last
checkpoint are written to disk.
When you do a db backup nothing happens to the log other than the backup
happened. When you do a log backup, the internal vlf's in the log buffer I
believe have their status changed to 0 if there are no active transactions
in them. But the log itself does not have anything happen to it.
The above applies to full recovery model.
Hilary Cotter
Looking for a SQL Server replication book?
http://www.nwsu.com/0974973602.html
Looking for a FAQ on Indexing Services/SQL FTS
http://www.indexserverfaq.com
"Wilbert" <Wilbert@.discussions.microsoft.com> wrote in message
news:668C79A5-01E2-4336-94D9-EB76B8D23866@.microsoft.com...
> Hello Hilary,
> Isn't it so that after a full database backup the committed entries in the
> log are removed?
> That's the origin of my question: in "normal" operation these entries are
> removed from the log, while in a log shipping scenario this doesn't seem
> to
> be the case. This led me to believe that SQL Server somehow "know" that
> log
> shipping is taking place and that the entries in the log should be
> retained
> until the next log backup.
> Best regards, Wilbert

Wednesday, March 7, 2012

Previous Value Calculation driving me nuts

Hey all,
I've posted my question in the OLAP sql group, but so far no one has
responded, so im hoping you guys can help me out...
My problem is that i am trying to create a value added calculation in a
named query. The calculation i am trying looks like this in algebra
form: (current cost-previous cost)-(current profits-previous profits)
Now the previous cost or previous profits data is based on dates found
in another table (called:tbo.Dates) if that makes any difference
Costs & Profits in table called tbo.Values
If more info is needed, please let me know.
Thanks & Cheers,
Can you provide the DDL (Create Table.. etc) and some sample Data and
what the expected results are?
Barry
daveoram24@.hotmail.com wrote:
> Hey all,
> I've posted my question in the OLAP sql group, but so far no one has
> responded, so im hoping you guys can help me out...
> My problem is that i am trying to create a value added calculation in a
> named query. The calculation i am trying looks like this in algebra
> form: (current cost-previous cost)-(current profits-previous profits)
> Now the previous cost or previous profits data is based on dates found
> in another table (called:tbo.Dates) if that makes any difference
> Costs & Profits in table called tbo.Values
> If more info is needed, please let me know.
> Thanks & Cheers,

Monday, February 20, 2012

Preventing deadlocks

I've noticed a couple of very nasty deadlocks on our new server lately, and
I'd like some advice on how to "ignore" them. What's happening is that
someone's machine and/or Access dies into the VB debugger in the middle of a
SELECT, and the process locks. This pretty much stops all access to the
server, and ends backups as well!
I don't see any way to prevent these problems on the user machine, so how to
I ignore them on the server? Is there some timeout I've forgot to set when we
set up the machine?
Also, when I attempt to kill the process in Manager it seems to either not
work, or take a very long time. Is this normal?
If it takes a long time to kill the process, it's probably doing a rollback.
"Maury Markowitz" wrote:

> I've noticed a couple of very nasty deadlocks on our new server lately, and
> I'd like some advice on how to "ignore" them. What's happening is that
> someone's machine and/or Access dies into the VB debugger in the middle of a
> SELECT, and the process locks. This pretty much stops all access to the
> server, and ends backups as well!
> I don't see any way to prevent these problems on the user machine, so how to
> I ignore them on the server? Is there some timeout I've forgot to set when we
> set up the machine?
> Also, when I attempt to kill the process in Manager it seems to either not
> work, or take a very long time. Is this normal?
|||Maury Markowitz wrote:
> I've noticed a couple of very nasty deadlocks on our new server
> lately, and I'd like some advice on how to "ignore" them. What's
> happening is that someone's machine and/or Access dies into the VB
> debugger in the middle of a SELECT, and the process locks. This
> pretty much stops all access to the server, and ends backups as well!
> I don't see any way to prevent these problems on the user machine, so
> how to I ignore them on the server? Is there some timeout I've forgot
> to set when we set up the machine?
> Also, when I attempt to kill the process in Manager it seems to
> either not work, or take a very long time. Is this normal?
A deadlock does not kill a connection. It only raises an error. The
server doesn't really do anything other than select the deadlock victim
and raise the error. The rest is up to the application. If you have an
application that is encountering a deadlock and locking up, then the
connection is likely remaining connected to the server. However, the
transaction is automatically rolled back by SQL Server, so it's not
clear what is causing your issue.
Are you sure you are having deadlock problems and not blocking issues?
I'm starting to think from your post that you may have confused the
terms. A deadlock is when two or more spids are requesting access to
objects that are locked by the other spids. In effect, they would all
block each other forever. So, SQL Server terminates one of the
transactions (the deadlock victim). In a blocking situation, one spid
requests a lock on a resource that is locked by another spid. Unless you
specify a lock timeout in your code, the first spid will wait forever
for the resource.
So which situation is yours?
David Gugick
Imceda Software
www.imceda.com
|||"David Gugick" wrote:
> A deadlock does not kill a connection. It only raises an error.
That's the problem. The deadlock is occuring because one application died
out on the network and is holding its query open. Then when someone else
issues the same query, it gets stuck. At least I think that's what's
happening.

> connection is likely remaining connected to the server. However, the
> transaction is automatically rolled back by SQL Server, so it's not
> clear what is causing your issue.
Hmmm.

> Are you sure you are having deadlock problems and not blocking issues?
You're right, I'm having blocking issues.

> specify a lock timeout in your code, the first spid will wait forever
> for the resource.
This sounds like the solution. Is this something that can be set globally in
Access? We set up the system to have 60 second timeouts, but this error
appears to occur when the Access app in question has "crashed" into the VB
Debugger.
Maury
|||Maury Markowitz wrote:
> "David Gugick" wrote:
> That's the problem. The deadlock is occuring because one application
> died out on the network and is holding its query open. Then when
> someone else issues the same query, it gets stuck. At least I think
> that's what's happening.
>
That's a blocking issue, not a deadlocking issue as you now know. SQL
Server will eventually release those locks, but it can take some time
until it clears up the connection. Why is the application dying in the
first place?
There is a lock timeout and query timeout that you should be able to set
from your Access code. The lock timeout determines how long a process
will wait on a locked resource before the query is automatically
cancelled. I believe you'll still need to issue a rollback if that
happens.
I don't know enough about Access to help more than that.
David Gugick
Imceda Software
www.imceda.com
|||"David Gugick" wrote:
> That's a blocking issue, not a deadlocking issue as you now know. SQL
> Server will eventually release those locks, but it can take some time
> until it clears up the connection. Why is the application dying in the
> first place?
For different reasons each time, typically due to edge cases that

> There is a lock timeout and query timeout that you should be able to set
> from your Access code.
Ok, I'm going to poke about and see if I can find this. The locks in
question are lasting overnight though, so I don't think it's quite that
simple.
Is there some setting on the server side I can set for this? None of our
queries take more than 30 seconds (my self-imposed limit) so setting this to
5 minutes or something would be entirely reasonable.
|||Maury Markowitz wrote:
> "David Gugick" wrote:
> For different reasons each time, typically due to edge cases that
>
> Ok, I'm going to poke about and see if I can find this. The locks in
> question are lasting overnight though, so I don't think it's quite
> that simple.
> Is there some setting on the server side I can set for this? None of
> our queries take more than 30 seconds (my self-imposed limit) so
> setting this to 5 minutes or something would be entirely reasonable.
If the locks are remaining overnight, it could be because the
application is not throwing an exception and dying, but is locked up
with its connection active on the server. Is this a possibility? I think
I meant setting the lock timeout for the clients who are being locked
out of the system, not the long running process. For that, a query
timeout could help depending on the situation. At least if you use a
lock timeout when this process seems to lock other users out, they 'll
get a nice message saying that they should call support because the
system is having issues and then you can quickly address the problem. It
doesn't prevent the problem, which is either SQL based or application
based.
David Gugick
Imceda Software
www.imceda.com