Showing posts with label tables. Show all posts
Showing posts with label tables. Show all posts

Monday, March 26, 2012

how to reliably delete (or drop) tables

Hi folks,
I need to create three tables on the fly that change once a w (they are
frozen crosstabs) and I've decided to create them as report tables for sheer
speed.
I obviously want to create these tables in the database and not in temp and
I have complete permissions when running any code on the server
how can I reliably test for a table and either drop it or clear it only to
recreate it instantly.
I ask because I was getting plenty of errors when I did this the other day
'invalid object name etc..' 'table already exists'
should I use Object_id or the msys tables...
a little code snippet of how you do it would be very helpful
also, is it impossible to put GO between BEGIN and END or even in the body
of a sproc?
many thanks in andvance
CharlesAHere are 2 ways
if exists (SELECT * FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE ='BASE TABLE'
AND TABLE_NAME = ?)
if exists (select * from dbo.sysobjects where id =
object_id(N'[dbo].[TableName]') and OBJECTPROPERTY(id, N'IsUserTable')
= 1)
http://sqlservercode.blogspot.com/|||DECLARE @.o INT;
SET @.o = OBJECT_ID('dbo.tablename');
IF @.o IS NOT NULL AND OBJECTPROPERTY(@.o, 'IsUserTable') = 1
BEGIN
PRINT 'dropping dbo.tablename';
DROP TABLE dbo.tablename;
END;
CREATE TABLE dbo.tablename
(
..
);
"CharlesA" <CharlesA@.discussions.microsoft.com> wrote in message
news:46529FC1-68AA-4425-A6F8-A84B79A5BFC5@.microsoft.com...
> Hi folks,
> I need to create three tables on the fly that change once a w (they are
> frozen crosstabs) and I've decided to create them as report tables for
> sheer
> speed.
> I obviously want to create these tables in the database and not in temp
> and
> I have complete permissions when running any code on the server
> how can I reliably test for a table and either drop it or clear it only to
> recreate it instantly.
> I ask because I was getting plenty of errors when I did this the other day
> 'invalid object name etc..' 'table already exists'
> should I use Object_id or the msys tables...
> a little code snippet of how you do it would be very helpful
>
> also, is it impossible to put GO between BEGIN and END or even in the body
> of a sproc?
> many thanks in andvance
> CharlesA|||Judging from your post you may have several issues but here goes:

> how can I reliably test for a table and either drop it or clear it only to
> recreate it instantly.
> I ask because I was getting plenty of errors when I did this the other day
> 'invalid object name etc..' 'table already exists'
Create a regular base table. A permanent one.

> should I use Object_id or the msys tables...
if object_id('<owner>.<table>') is not null
begin
print 'exists'
end
else
begin
print 'does not exist'
end

> also, is it impossible to put GO between BEGIN and END or even in the body
> of a sproc?
It's possible ;) but it would be syntactically incorrect.
Why would you think you need to create object dynamically? Please elaborate.
ML
http://milambda.blogspot.com/|||for ur first part let assume that the table name is --[mytable] ur
code should be...
if exists (select * from dbo.sysobjects where id =
object_id(N'[dbo].[mytable]') and OBJECTPROPERTY(id, N'IsTable') = 1)
drop table [dbo.mytable]
else
print 'Table not present'
u can remove else part to not print message.....
i think it will help..
ya i think it is not possible to put GO between BEGIN and END...|||hi ML,
I'm answering your question, 'why do you need to create the tables
dynamically'
because
it's a crosstab and it's a moving window on months of the year (12 at a
time) so the columns change once a month and I wish to automate the process
of creating them. It's also a read-only reporting table and not a
transactional or updatable table.
so my plan is to create the table dynamically and then to use dynamic SQL to
populate it, that bit is working already, I had incomprehensible (to me)
errors when I tried to delete and re-create these types of reporting tables
recently
Regards
CharlesA|||:-)
CharlesA|||If it's a 12 month window, then why not just do:
CREATE TABLE dbo.ReportingTable
(
DataPoint VARCHAR(32),
m1 INT,
m2 INT,
m3 INT,
m4 INT,
m5 INT,
m6 INT,
m7 INT,
m8 INT,
m9 INT,
m10 INT,
m11 INT,
m12 INT
)
GO
-- in other words, there is no reason to store '2005-01-01'
-- as a column name. This is data, not metadata.
CREATE TABLE dbo.ReportingStartMonth
(
dt SMALLDATETIME
)
GO
Now, have your starting month stored in the latter table, and the reporting
app can figure out column headers based on that (e.g. month1 =
DATEADD(MONTH, ReportingStartMonth.dt, 0), month2 = DATEADD(MONTH,
ReportingStartMonth.dt, 1), etc).
Or, better yet, store your data in a regular table, however this will
require more changes to the app:
CREATE PROCEDURE dbo.BetterReportingTable
(
Month SMALLDATETIME,
DataPoint1 INT,
DataPoint2 INT
-- , ... etc etc
)
Now you can delete data based on the time range, so you have 12 rows instead
of 12 columns, and also only process one month at a time (because it is
unlikely that data from 11 months ago really needs to be re-generated every
month). The query that retrieves this data can easily pivot it if need be,
but in my experience, most reports are going to show the data in that format
anyway, or can easily work with that format when developing the actual
display. Remember that the table structure does not have to imitate the
physical display for the end user...
Both of these solutions do not require dropping and re-creating the table;
the first is a mere truncate, the latter only requires deletes *if you need
them*... the latter also allows you to keep around years of history if you
need to, and re-create reports at will. The main reportng query can still
be rigged to only get the last 12 months...
A
"CharlesA" <CharlesA@.discussions.microsoft.com> wrote in message
news:FF70F337-5342-4182-AE7A-54E6B7A67B7D@.microsoft.com...
> hi ML,
> I'm answering your question, 'why do you need to create the tables
> dynamically'
> because
> it's a crosstab and it's a moving window on months of the year (12 at a
> time) so the columns change once a month and I wish to automate the
> process
> of creating them. It's also a read-only reporting table and not a
> transactional or updatable table.
> so my plan is to create the table dynamically and then to use dynamic SQL
> to
> populate it, that bit is working already, I had incomprehensible (to me)
> errors when I tried to delete and re-create these types of reporting
> tables
> recently
> Regards
> CharlesA
>|||I for one wouldn't mind seeing some DDL. It sounds like a job for an indexed
view. But I could be wrong.
ML
http://milambda.blogspot.com/|||Thanks Aaron,
your suggestions sound quite sane and well worth following up.
I suppose the purpose behind my hack, was the very underperformant Access
crosstab that was working with a smallish dataset (brought back via a
passthrough query), and I wanted to fix a problem under great deadline
pressure.
your suggestion that the table should not be dropped and recreated is of
course the one to follow
thanks again
regards
Charles Asql

How to release Table unused space

Hi all
I am having some disk space issue, and I notice that I have some tables with
a lot of GB of unused space.
Is it a way to claim and release that unused space?
To give you a better picture, I have just one table with below information
Rows = 131977895
Reserved =146270344 KB
Index_Size = 2767616 KB
Data = 70372568 KB
Unused = 73130160 KB <-- here is my concernd almost 70 GB unused !!!
This table is reindexed every sunday (yesterday was the last reindex) and it
does not look to have fragmentation issues, when I ran a DBCC Showcontig I
get these results:
DBCC SHOWCONTIG scanning 'Sales' table...
Table: 'Sales' (1228791685); index ID: 1, database ID: 16
TABLE level scan performed.
- Pages Scanned........................: 8796978
- Extents Scanned.......................: 1101956
- Extent Switches.......................: 1101956
- Avg. Pages per Extent..................: 8.0
- Scan Density [Best Count:Actual Count]......: 99.79% [1099623:1101957]
- Logical Scan Fragmentation ..............: 0.04%
- Extent Scan Fragmentation ...............: 9.80%
- Avg. Bytes Free per Page................: 647.0
- Avg. Page Density (full)................: 92.01%
DBCC execution completed. If DBCC printed error messages, contact your
system administrator.
Could someone give me an idea on how to release the 70 GB of unused space?
Thanks a lot
JuanDo you have Text or image columns? Did you drop any recently?
--
Andrew J. Kelly SQL MVP
"Juan" <Juan@.discussions.microsoft.com> wrote in message
news:A6F92E41-49D3-4D64-9123-63FE8740E18E@.microsoft.com...
> Hi all
> I am having some disk space issue, and I notice that I have some tables
> with
> a lot of GB of unused space.
> Is it a way to claim and release that unused space?
> To give you a better picture, I have just one table with below information
> Rows = 131977895
> Reserved =146270344 KB
> Index_Size = 2767616 KB
> Data = 70372568 KB
> Unused = 73130160 KB <-- here is my concernd almost 70 GB unused !!!
>
> This table is reindexed every sunday (yesterday was the last reindex) and
> it
> does not look to have fragmentation issues, when I ran a DBCC Showcontig I
> get these results:
> DBCC SHOWCONTIG scanning 'Sales' table...
> Table: 'Sales' (1228791685); index ID: 1, database ID: 16
> TABLE level scan performed.
> - Pages Scanned........................: 8796978
> - Extents Scanned.......................: 1101956
> - Extent Switches.......................: 1101956
> - Avg. Pages per Extent..................: 8.0
> - Scan Density [Best Count:Actual Count]......: 99.79% [1099623:1101957]
> - Logical Scan Fragmentation ..............: 0.04%
> - Extent Scan Fragmentation ...............: 9.80%
> - Avg. Bytes Free per Page................: 647.0
> - Avg. Page Density (full)................: 92.01%
> DBCC execution completed. If DBCC printed error messages, contact your
> system administrator.
>
> Could someone give me an idea on how to release the 70 GB of unused space?
> Thanks a lot
> Juan
>|||Juan,
Your numbers don't match.
The first data you mention is 132 million rows, and 50% unused space.
But then the DBCC SHOWCONTIG output reports 1,228 millions rows and 10%
unused space.
Gert-Jan
Juan wrote:
> Hi all
> I am having some disk space issue, and I notice that I have some tables with
> a lot of GB of unused space.
> Is it a way to claim and release that unused space?
> To give you a better picture, I have just one table with below information
> Rows = 131977895
> Reserved =146270344 KB
> Index_Size = 2767616 KB
> Data = 70372568 KB
> Unused = 73130160 KB <-- here is my concernd almost 70 GB unused !!!
> This table is reindexed every sunday (yesterday was the last reindex) and it
> does not look to have fragmentation issues, when I ran a DBCC Showcontig I
> get these results:
> DBCC SHOWCONTIG scanning 'Sales' table...
> Table: 'Sales' (1228791685); index ID: 1, database ID: 16
> TABLE level scan performed.
> - Pages Scanned........................: 8796978
> - Extents Scanned.......................: 1101956
> - Extent Switches.......................: 1101956
> - Avg. Pages per Extent..................: 8.0
> - Scan Density [Best Count:Actual Count]......: 99.79% [1099623:1101957]
> - Logical Scan Fragmentation ..............: 0.04%
> - Extent Scan Fragmentation ...............: 9.80%
> - Avg. Bytes Free per Page................: 647.0
> - Avg. Page Density (full)................: 92.01%
> DBCC execution completed. If DBCC printed error messages, contact your
> system administrator.
> Could someone give me an idea on how to release the 70 GB of unused space?
> Thanks a lot
> Juan|||Did you get your numbers below from sp_spaceused? You cannot rely on this
for accurate information. DBCC updateusage will help.
Note that from showcontig output your pages are mostly full --> not much
unused space.
--
TheSQLGuru
President
Indicium Resources, Inc.
"Juan" <Juan@.discussions.microsoft.com> wrote in message
news:A6F92E41-49D3-4D64-9123-63FE8740E18E@.microsoft.com...
> Hi all
> I am having some disk space issue, and I notice that I have some tables
> with
> a lot of GB of unused space.
> Is it a way to claim and release that unused space?
> To give you a better picture, I have just one table with below information
> Rows = 131977895
> Reserved =146270344 KB
> Index_Size = 2767616 KB
> Data = 70372568 KB
> Unused = 73130160 KB <-- here is my concernd almost 70 GB unused !!!
>
> This table is reindexed every sunday (yesterday was the last reindex) and
> it
> does not look to have fragmentation issues, when I ran a DBCC Showcontig I
> get these results:
> DBCC SHOWCONTIG scanning 'Sales' table...
> Table: 'Sales' (1228791685); index ID: 1, database ID: 16
> TABLE level scan performed.
> - Pages Scanned........................: 8796978
> - Extents Scanned.......................: 1101956
> - Extent Switches.......................: 1101956
> - Avg. Pages per Extent..................: 8.0
> - Scan Density [Best Count:Actual Count]......: 99.79% [1099623:1101957]
> - Logical Scan Fragmentation ..............: 0.04%
> - Extent Scan Fragmentation ...............: 9.80%
> - Avg. Bytes Free per Page................: 647.0
> - Avg. Page Density (full)................: 92.01%
> DBCC execution completed. If DBCC printed error messages, contact your
> system administrator.
>
> Could someone give me an idea on how to release the 70 GB of unused space?
> Thanks a lot
> Juan
>|||Ey, you are right, I ran a DBCC updateusage and it fix the numbers, I am
getting now just 300 KB of unused space, this makes sense now.
Thanks a lot, I'll keep on mind to run a DBCC UpdateUsage next time I check
the sizes of my tables.
Juan
"TheSQLGuru" wrote:
> Did you get your numbers below from sp_spaceused? You cannot rely on this
> for accurate information. DBCC updateusage will help.
> Note that from showcontig output your pages are mostly full --> not much
> unused space.
> --
> TheSQLGuru
> President
> Indicium Resources, Inc.
> "Juan" <Juan@.discussions.microsoft.com> wrote in message
> news:A6F92E41-49D3-4D64-9123-63FE8740E18E@.microsoft.com...
> > Hi all
> >
> > I am having some disk space issue, and I notice that I have some tables
> > with
> > a lot of GB of unused space.
> >
> > Is it a way to claim and release that unused space?
> >
> > To give you a better picture, I have just one table with below information
> > Rows = 131977895
> > Reserved =146270344 KB
> > Index_Size = 2767616 KB
> > Data = 70372568 KB
> > Unused = 73130160 KB <-- here is my concernd almost 70 GB unused !!!
> >
> >
> > This table is reindexed every sunday (yesterday was the last reindex) and
> > it
> > does not look to have fragmentation issues, when I ran a DBCC Showcontig I
> > get these results:
> > DBCC SHOWCONTIG scanning 'Sales' table...
> > Table: 'Sales' (1228791685); index ID: 1, database ID: 16
> > TABLE level scan performed.
> > - Pages Scanned........................: 8796978
> > - Extents Scanned.......................: 1101956
> > - Extent Switches.......................: 1101956
> > - Avg. Pages per Extent..................: 8.0
> > - Scan Density [Best Count:Actual Count]......: 99.79% [1099623:1101957]
> > - Logical Scan Fragmentation ..............: 0.04%
> > - Extent Scan Fragmentation ...............: 9.80%
> > - Avg. Bytes Free per Page................: 647.0
> > - Avg. Page Density (full)................: 92.01%
> > DBCC execution completed. If DBCC printed error messages, contact your
> > system administrator.
> >
> >
> > Could someone give me an idea on how to release the 70 GB of unused space?
> >
> > Thanks a lot
> >
> > Juan
> >
>
>

How to release Table unused space

Hi all
I am having some disk space issue, and I notice that I have some tables with
a lot of GB of unused space.
Is it a way to claim and release that unused space?
To give you a better picture, I have just one table with below information
Rows = 131977895
Reserved =146270344 KB
Index_Size = 2767616 KB
Data = 70372568 KB
Unused = 73130160 KB <-- here is my concernd almost 70 GB unused !!!
This table is reindexed every sunday (yesterday was the last reindex) and it
does not look to have fragmentation issues, when I ran a DBCC Showcontig I
get these results:
DBCC SHOWCONTIG scanning 'Sales' table...
Table: 'Sales' (1228791685); index ID: 1, database ID: 16
TABLE level scan performed.
- Pages Scanned........................: 8796978
- Extents Scanned.......................: 1101956
- Extent Switches.......................: 1101956
- Avg. Pages per Extent..................: 8.0
- Scan Density [Best Count:Actual Count]......: 99.79% [1099623:1101957]
- Logical Scan Fragmentation ..............: 0.04%
- Extent Scan Fragmentation ...............: 9.80%
- Avg. Bytes Free per Page................: 647.0
- Avg. Page Density (full)................: 92.01%
DBCC execution completed. If DBCC printed error messages, contact your
system administrator.
Could someone give me an idea on how to release the 70 GB of unused space?
Thanks a lot
Juan
Do you have Text or image columns? Did you drop any recently?
Andrew J. Kelly SQL MVP
"Juan" <Juan@.discussions.microsoft.com> wrote in message
news:A6F92E41-49D3-4D64-9123-63FE8740E18E@.microsoft.com...
> Hi all
> I am having some disk space issue, and I notice that I have some tables
> with
> a lot of GB of unused space.
> Is it a way to claim and release that unused space?
> To give you a better picture, I have just one table with below information
> Rows = 131977895
> Reserved =146270344 KB
> Index_Size = 2767616 KB
> Data = 70372568 KB
> Unused = 73130160 KB <-- here is my concernd almost 70 GB unused !!!
>
> This table is reindexed every sunday (yesterday was the last reindex) and
> it
> does not look to have fragmentation issues, when I ran a DBCC Showcontig I
> get these results:
> DBCC SHOWCONTIG scanning 'Sales' table...
> Table: 'Sales' (1228791685); index ID: 1, database ID: 16
> TABLE level scan performed.
> - Pages Scanned........................: 8796978
> - Extents Scanned.......................: 1101956
> - Extent Switches.......................: 1101956
> - Avg. Pages per Extent..................: 8.0
> - Scan Density [Best Count:Actual Count]......: 99.79% [1099623:1101957]
> - Logical Scan Fragmentation ..............: 0.04%
> - Extent Scan Fragmentation ...............: 9.80%
> - Avg. Bytes Free per Page................: 647.0
> - Avg. Page Density (full)................: 92.01%
> DBCC execution completed. If DBCC printed error messages, contact your
> system administrator.
>
> Could someone give me an idea on how to release the 70 GB of unused space?
> Thanks a lot
> Juan
>
|||Juan,
Your numbers don't match.
The first data you mention is 132 million rows, and 50% unused space.
But then the DBCC SHOWCONTIG output reports 1,228 millions rows and 10%
unused space.
Gert-Jan
Juan wrote:
> Hi all
> I am having some disk space issue, and I notice that I have some tables with
> a lot of GB of unused space.
> Is it a way to claim and release that unused space?
> To give you a better picture, I have just one table with below information
> Rows = 131977895
> Reserved =146270344 KB
> Index_Size = 2767616 KB
> Data = 70372568 KB
> Unused = 73130160 KB <-- here is my concernd almost 70 GB unused !!!
> This table is reindexed every sunday (yesterday was the last reindex) and it
> does not look to have fragmentation issues, when I ran a DBCC Showcontig I
> get these results:
> DBCC SHOWCONTIG scanning 'Sales' table...
> Table: 'Sales' (1228791685); index ID: 1, database ID: 16
> TABLE level scan performed.
> - Pages Scanned........................: 8796978
> - Extents Scanned.......................: 1101956
> - Extent Switches.......................: 1101956
> - Avg. Pages per Extent..................: 8.0
> - Scan Density [Best Count:Actual Count]......: 99.79% [1099623:1101957]
> - Logical Scan Fragmentation ..............: 0.04%
> - Extent Scan Fragmentation ...............: 9.80%
> - Avg. Bytes Free per Page................: 647.0
> - Avg. Page Density (full)................: 92.01%
> DBCC execution completed. If DBCC printed error messages, contact your
> system administrator.
> Could someone give me an idea on how to release the 70 GB of unused space?
> Thanks a lot
> Juan
|||Did you get your numbers below from sp_spaceused? You cannot rely on this
for accurate information. DBCC updateusage will help.
Note that from showcontig output your pages are mostly full --> not much
unused space.
TheSQLGuru
President
Indicium Resources, Inc.
"Juan" <Juan@.discussions.microsoft.com> wrote in message
news:A6F92E41-49D3-4D64-9123-63FE8740E18E@.microsoft.com...
> Hi all
> I am having some disk space issue, and I notice that I have some tables
> with
> a lot of GB of unused space.
> Is it a way to claim and release that unused space?
> To give you a better picture, I have just one table with below information
> Rows = 131977895
> Reserved =146270344 KB
> Index_Size = 2767616 KB
> Data = 70372568 KB
> Unused = 73130160 KB <-- here is my concernd almost 70 GB unused !!!
>
> This table is reindexed every sunday (yesterday was the last reindex) and
> it
> does not look to have fragmentation issues, when I ran a DBCC Showcontig I
> get these results:
> DBCC SHOWCONTIG scanning 'Sales' table...
> Table: 'Sales' (1228791685); index ID: 1, database ID: 16
> TABLE level scan performed.
> - Pages Scanned........................: 8796978
> - Extents Scanned.......................: 1101956
> - Extent Switches.......................: 1101956
> - Avg. Pages per Extent..................: 8.0
> - Scan Density [Best Count:Actual Count]......: 99.79% [1099623:1101957]
> - Logical Scan Fragmentation ..............: 0.04%
> - Extent Scan Fragmentation ...............: 9.80%
> - Avg. Bytes Free per Page................: 647.0
> - Avg. Page Density (full)................: 92.01%
> DBCC execution completed. If DBCC printed error messages, contact your
> system administrator.
>
> Could someone give me an idea on how to release the 70 GB of unused space?
> Thanks a lot
> Juan
>
|||Ey, you are right, I ran a DBCC updateusage and it fix the numbers, I am
getting now just 300 KB of unused space, this makes sense now.
Thanks a lot, I'll keep on mind to run a DBCC UpdateUsage next time I check
the sizes of my tables.
Juan
"TheSQLGuru" wrote:

> Did you get your numbers below from sp_spaceused? You cannot rely on this
> for accurate information. DBCC updateusage will help.
> Note that from showcontig output your pages are mostly full --> not much
> unused space.
> --
> TheSQLGuru
> President
> Indicium Resources, Inc.
> "Juan" <Juan@.discussions.microsoft.com> wrote in message
> news:A6F92E41-49D3-4D64-9123-63FE8740E18E@.microsoft.com...
>
>

How to release Table unused space

Hi all
I am having some disk space issue, and I notice that I have some tables with
a lot of GB of unused space.
Is it a way to claim and release that unused space?
To give you a better picture, I have just one table with below information
Rows = 131977895
Reserved =146270344 KB
Index_Size = 2767616 KB
Data = 70372568 KB
Unused = 73130160 KB <-- here is my concernd almost 70 GB unused !!!
This table is reindexed every sunday (yesterday was the last reindex) and it
does not look to have fragmentation issues, when I ran a DBCC Showcontig I
get these results:
DBCC SHOWCONTIG scanning 'Sales' table...
Table: 'Sales' (1228791685); index ID: 1, database ID: 16
TABLE level scan performed.
- Pages Scanned........................: 8796978
- Extents Scanned.......................: 1101956
- Extent Switches.......................: 1101956
- Avg. Pages per Extent..................: 8.0
- Scan Density [Best Count:Actual Count]......: 99.79% [1099623:110
1957]
- Logical Scan Fragmentation ..............: 0.04%
- Extent Scan Fragmentation ...............: 9.80%
- Avg. Bytes Free per Page................: 647.0
- Avg. Page Density (full)................: 92.01%
DBCC execution completed. If DBCC printed error messages, contact your
system administrator.
Could someone give me an idea on how to release the 70 GB of unused space?
Thanks a lot
JuanDo you have Text or image columns? Did you drop any recently?
Andrew J. Kelly SQL MVP
"Juan" <Juan@.discussions.microsoft.com> wrote in message
news:A6F92E41-49D3-4D64-9123-63FE8740E18E@.microsoft.com...
> Hi all
> I am having some disk space issue, and I notice that I have some tables
> with
> a lot of GB of unused space.
> Is it a way to claim and release that unused space?
> To give you a better picture, I have just one table with below information
> Rows = 131977895
> Reserved =146270344 KB
> Index_Size = 2767616 KB
> Data = 70372568 KB
> Unused = 73130160 KB <-- here is my concernd almost 70 GB unused !!!
>
> This table is reindexed every sunday (yesterday was the last reindex) and
> it
> does not look to have fragmentation issues, when I ran a DBCC Showcontig I
> get these results:
> DBCC SHOWCONTIG scanning 'Sales' table...
> Table: 'Sales' (1228791685); index ID: 1, database ID: 16
> TABLE level scan performed.
> - Pages Scanned........................: 8796978
> - Extents Scanned.......................: 1101956
> - Extent Switches.......................: 1101956
> - Avg. Pages per Extent..................: 8.0
> - Scan Density [Best Count:Actual Count]......: 99.79% [1099623:1
101957]
> - Logical Scan Fragmentation ..............: 0.04%
> - Extent Scan Fragmentation ...............: 9.80%
> - Avg. Bytes Free per Page................: 647.0
> - Avg. Page Density (full)................: 92.01%
> DBCC execution completed. If DBCC printed error messages, contact your
> system administrator.
>
> Could someone give me an idea on how to release the 70 GB of unused space?
> Thanks a lot
> Juan
>|||Juan,
Your numbers don't match.
The first data you mention is 132 million rows, and 50% unused space.
But then the DBCC SHOWCONTIG output reports 1,228 millions rows and 10%
unused space.
Gert-Jan
Juan wrote:
> Hi all
> I am having some disk space issue, and I notice that I have some tables wi
th
> a lot of GB of unused space.
> Is it a way to claim and release that unused space?
> To give you a better picture, I have just one table with below information
> Rows = 131977895
> Reserved =146270344 KB
> Index_Size = 2767616 KB
> Data = 70372568 KB
> Unused = 73130160 KB <-- here is my concernd almost 70 GB unused !!!
> This table is reindexed every sunday (yesterday was the last reindex) and
it
> does not look to have fragmentation issues, when I ran a DBCC Showcontig I
> get these results:
> DBCC SHOWCONTIG scanning 'Sales' table...
> Table: 'Sales' (1228791685); index ID: 1, database ID: 16
> TABLE level scan performed.
> - Pages Scanned........................: 8796978
> - Extents Scanned.......................: 1101956
> - Extent Switches.......................: 1101956
> - Avg. Pages per Extent..................: 8.0
> - Scan Density [Best Count:Actual Count]......: 99.79% [1099623:1
101957]
> - Logical Scan Fragmentation ..............: 0.04%
> - Extent Scan Fragmentation ...............: 9.80%
> - Avg. Bytes Free per Page................: 647.0
> - Avg. Page Density (full)................: 92.01%
> DBCC execution completed. If DBCC printed error messages, contact your
> system administrator.
> Could someone give me an idea on how to release the 70 GB of unused space?
> Thanks a lot
> Juan|||Did you get your numbers below from sp_spaceused? You cannot rely on this
for accurate information. DBCC updateusage will help.
Note that from showcontig output your pages are mostly full --> not much
unused space.
TheSQLGuru
President
Indicium Resources, Inc.
"Juan" <Juan@.discussions.microsoft.com> wrote in message
news:A6F92E41-49D3-4D64-9123-63FE8740E18E@.microsoft.com...
> Hi all
> I am having some disk space issue, and I notice that I have some tables
> with
> a lot of GB of unused space.
> Is it a way to claim and release that unused space?
> To give you a better picture, I have just one table with below information
> Rows = 131977895
> Reserved =146270344 KB
> Index_Size = 2767616 KB
> Data = 70372568 KB
> Unused = 73130160 KB <-- here is my concernd almost 70 GB unused !!!
>
> This table is reindexed every sunday (yesterday was the last reindex) and
> it
> does not look to have fragmentation issues, when I ran a DBCC Showcontig I
> get these results:
> DBCC SHOWCONTIG scanning 'Sales' table...
> Table: 'Sales' (1228791685); index ID: 1, database ID: 16
> TABLE level scan performed.
> - Pages Scanned........................: 8796978
> - Extents Scanned.......................: 1101956
> - Extent Switches.......................: 1101956
> - Avg. Pages per Extent..................: 8.0
> - Scan Density [Best Count:Actual Count]......: 99.79% [1099623:1
101957]
> - Logical Scan Fragmentation ..............: 0.04%
> - Extent Scan Fragmentation ...............: 9.80%
> - Avg. Bytes Free per Page................: 647.0
> - Avg. Page Density (full)................: 92.01%
> DBCC execution completed. If DBCC printed error messages, contact your
> system administrator.
>
> Could someone give me an idea on how to release the 70 GB of unused space?
> Thanks a lot
> Juan
>|||Ey, you are right, I ran a DBCC updateusage and it fix the numbers, I am
getting now just 300 KB of unused space, this makes sense now.
Thanks a lot, I'll keep on mind to run a DBCC UpdateUsage next time I check
the sizes of my tables.
Juan
"TheSQLGuru" wrote:

> Did you get your numbers below from sp_spaceused? You cannot rely on this
> for accurate information. DBCC updateusage will help.
> Note that from showcontig output your pages are mostly full --> not much
> unused space.
> --
> TheSQLGuru
> President
> Indicium Resources, Inc.
> "Juan" <Juan@.discussions.microsoft.com> wrote in message
> news:A6F92E41-49D3-4D64-9123-63FE8740E18E@.microsoft.com...
>
>

Friday, March 23, 2012

how to register a table

I was advised to use a table in addition to the standard aspnet_ tables for keeping extra data and to register it with the rest of the database. However, when I tried to register it, I couldn't even see a database to choose from then received an error. All the tables, including the ones I created, work when I run my VSE 2005 project. If someone can give me some A-B-C guidance, I'd appreciate it.

ur query is not clear, give some of ur working example...

|||

I'm not sure how I'd give a working example since the question has to do with a database and there's not much I can show. I run aspnet_regsql so I can add a table to the standard aspnetdb.mdf. When I try to see what databases are available from the wizard that runs, I get the error "Connection failed to query a list of database names from the SQL server." On the other hand, when I use the <default> database shown in the wizard, I get the message "Setup failed. Exception: Unable to connect to SQL Server database"

This problem is quite common, I'm sure, and I don't know what other information I can give, except that I obviously need to somewhere insert a missing step that I've omitted. Do I need to run aspnet_regiis or something? I do this and only get a list of all the different options.

|||

OK, I think I'm good now. Upon reading through some more posts regarding this problem, I picked up that I needed to write \sqlexpress after the server name. Problem solved.

how to reflect schema changes at subscriber using transactional replication

Hi

I need to setup transactional replication for a production server.

The tables (articles) at publisher being replicated can expect schema changes.

Is there any way to reflect schema changes at subscriber using transactional replication.

Early Thanks,

Salman Shehbaz.

If this is SQL Server 2005, the schema changes at publisher can be automatically replicated to subscriber.

If this is SQL Server 2000, you should make schema changes through proc sp_repladdcolumn or sp_repldropcolumn when adding or dropping columns. Other schema changes can not be replicated in SQL Server 2000.

How to reference items from different database?

Hi all,

I have two databases DB_External and DB_Internal.

I am writing some stored procedures (in DB_Internal) that reads from
the tables in DB_External. I execute my stored procedures from
DB_Internal.

Everytime I want to read the tables in DB_External, I have to refer to
the table as DB_External.dbo.tableName.

Is there a better way of declaring the database DB_External up front
in the stored procedure so I don't have to pre-fix "DB_External.dbo."
in all the table names?

Thanks in advance,
June Moore."June Moore" <jungewum@.yahoo.com.au> wrote in message
news:e5dfaf21.0310100030.3e0067a5@.posting.google.c om...
> Hi all,
> I have two databases DB_External and DB_Internal.
> I am writing some stored procedures (in DB_Internal) that reads from
> the tables in DB_External. I execute my stored procedures from
> DB_Internal.
> Everytime I want to read the tables in DB_External, I have to refer to
> the table as DB_External.dbo.tableName.
> Is there a better way of declaring the database DB_External up front
> in the stored procedure so I don't have to pre-fix "DB_External.dbo."
> in all the table names?
> Thanks in advance,
> June Moore.

You always have to fully qualify the name - there's no way around that. And
it's good practice to include the dbo owner prefix, even in the 'local'
database.

If you're concerned about moving code between servers where the DB names may
be different, you could consider a pre-processor script to 'fill in' or
find/replace the DB names in a code template, then create the procedure.

Simonsql

Wednesday, March 21, 2012

How to reduce white space when object is hidden?

The report is made of one list containing 2 tables, one below each other.
Depending on the report parameters, the bottom table is made visible or
hidden.
When that table is hidden at run time, RS still reserves the same amount of
white space that the table takes in the report design layout.
This causes RS to render an extra white page at the end of the report where
it would display the table. Since the table is hidden, nothing is displayed
in this extra white page.
Does anyone know a workaround that would prevent the white page at the end
of the report?Hi Pat,
Thanks for your post.
From your descriptions, I understood the report will reserve some the
space for that hidden table when there is two tables in the Report. If I
have misunderstood your concern, please feel free to point it out.
I have tested on my side but it seems RS won't reserve the place for hidden
objects. Would you please show me a sample with AdventureWorks, with which
I could reproduce it on my side?
Sincerely yours,
Michael Cheng
Microsoft Online Partner Support
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
=====================================================
This posting is provided "AS IS" with no warranties, and confers no rights.|||Did you try the scenario where both table are contained in one list?
"Michael Cheng [MSFT]" wrote:
> Hi Pat,
> Thanks for your post.
> From your descriptions, I understood the report will reserve some the
> space for that hidden table when there is two tables in the Report. If I
> have misunderstood your concern, please feel free to point it out.
> I have tested on my side but it seems RS won't reserve the place for hidden
> objects. Would you please show me a sample with AdventureWorks, with which
> I could reproduce it on my side?
>
> Sincerely yours,
> Michael Cheng
> Microsoft Online Partner Support
> When responding to posts, please "Reply to Group" via your newsreader so
> that others may learn and benefit from your issue.
> =====================================================> This posting is provided "AS IS" with no warranties, and confers no rights.
>|||Hi Pat,
Thanks for your patience.
If you set the visibility like this:
<Visibility>
<Hidden>true</Hidden>
</Visibility>
This means that the table is always hidden. The space is preserved for
always hidden items. If you changed visibility according to Expression, it
will not reduce the white space.
However, I have tested the scenario that both table are contained in one
list as you described. It will do leave the blank and I am afraid currently
we cannot eliminate this behavior.
Sincerely yours,
Michael Cheng
Microsoft Online Partner Support
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
=====================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

How to reduce timeouts in SQL Server?

Hi,
We have designed a system which processes millions of transactions a
year. There are a couple of tables in the system which are heavily
used. These 2 tables gets about 30,000 rows inserted into them
everyday at an average. The inserts are done through stored procedures
after perfomring some complex logic. There are also a few scheduled
jobs that run every night which accesses these 2 tables. In addition,
there are a few reports that run in the web system which acces these
table for ad-hoc reporting. In the last few months, the number of
transactions have increased quite a bit and system started giving us
problems such as a lot of timeouts and a few deadlockks everyday.
I am thinking that if I can somehow replicate these tables every 6
hours or so and then use the new tables for all reporting purposes, my
timeouts might decrease. Could you suggest on how I could go about
replicating these 2 tables in an efficient way? and whether this would
help my situation?
Any other suggestions on how I could improve my system would be
appreciated.
Thanks,
Karanouch....
tough question without being able to see your database.
here are some thoughts...
1. Make sure indexes are being optimized\maintained on a regular basis (Read
up on showContig and on dbcc IndexDefrag)
2. review your critical transactions and make sure that you are not causing
excessive blocking and deadlocks (This is really hard, you'll have to read
up on troubleshooting deadlocks)
3. yes your adhoc reporting should be moved out to another database. You can
use DTS for this, replication or log shipping. We are using log shipping to
satisfy this need.
you're likely going to need to spend some serious hours pinpointing the
problem here. My guess is that option #1 above will buy you some serious
performance gains.
Cheers,
Greg Jackson
PDX, Oregon|||Articles for reference:
http://www.microsoft.com/technet/community/chats/trans/sql/sql1023.mspx
http://vyaskn.tripod.com/watch_your_timeouts.htm
"Karan" wrote:
> Hi,
> We have designed a system which processes millions of transactions a
> year. There are a couple of tables in the system which are heavily
> used. These 2 tables gets about 30,000 rows inserted into them
> everyday at an average. The inserts are done through stored procedures
> after perfomring some complex logic. There are also a few scheduled
> jobs that run every night which accesses these 2 tables. In addition,
> there are a few reports that run in the web system which acces these
> table for ad-hoc reporting. In the last few months, the number of
> transactions have increased quite a bit and system started giving us
> problems such as a lot of timeouts and a few deadlockks everyday.
> I am thinking that if I can somehow replicate these tables every 6
> hours or so and then use the new tables for all reporting purposes, my
> timeouts might decrease. Could you suggest on how I could go about
> replicating these 2 tables in an efficient way? and whether this would
> help my situation?
> Any other suggestions on how I could improve my system would be
> appreciated.
> Thanks,
> Karan
>|||I performed the DBCC SHOWCONTIG on the tables affected and the stats
look ok to me. I am pasting the stats below..
DBCC SHOWCONTIG scanning 'claims' table...
Table: 'table_name' (322100188); index ID: 1, database ID: 7
TABLE level scan performed.
- Pages Scanned........................: 202265
- Extents Scanned.......................: 25349
- Extent Switches.......................: 27598
- Avg. Pages per Extent..................: 8.0
- Scan Density [Best Count:Actual Count]......: 91.61% [25284:27599]
- Logical Scan Fragmentation ..............: 0.64%
- Extent Scan Fragmentation ...............: 10.38%
- Avg. Bytes Free per Page................: 672.6
- Avg. Page Density (full)................: 91.69%
DBCC execution completed. If DBCC printed error messages, contact your
system administrator.
Please let me know if you see anything I could improve with this
stats. All other indexes on these table have similar stats as well.
Thanks for your help.
Karan|||looks good to me.
Red Flag would be scan density LESS THAN 80%....You look fine in that
regard.
Greg Jackson
PDX, Oregonsql

How to reduce timeouts in SQL Server?

Hi,
We have designed a system which processes millions of transactions a
year. There are a couple of tables in the system which are heavily
used. These 2 tables gets about 30,000 rows inserted into them
everyday at an average. The inserts are done through stored procedures
after perfomring some complex logic. There are also a few scheduled
jobs that run every night which accesses these 2 tables. In addition,
there are a few reports that run in the web system which acces these
table for ad-hoc reporting. In the last few months, the number of
transactions have increased quite a bit and system started giving us
problems such as a lot of timeouts and a few deadlockks everyday.
I am thinking that if I can somehow replicate these tables every 6
hours or so and then use the new tables for all reporting purposes, my
timeouts might decrease. Could you suggest on how I could go about
replicating these 2 tables in an efficient way? and whether this would
help my situation?
Any other suggestions on how I could improve my system would be
appreciated.
Thanks,
Karan
ouch....
tough question without being able to see your database.
here are some thoughts...
1. Make sure indexes are being optimized\maintained on a regular basis (Read
up on showContig and on dbcc IndexDefrag)
2. review your critical transactions and make sure that you are not causing
excessive blocking and deadlocks (This is really hard, you'll have to read
up on troubleshooting deadlocks)
3. yes your adhoc reporting should be moved out to another database. You can
use DTS for this, replication or log shipping. We are using log shipping to
satisfy this need.
you're likely going to need to spend some serious hours pinpointing the
problem here. My guess is that option #1 above will buy you some serious
performance gains.
Cheers,
Greg Jackson
PDX, Oregon
|||Articles for reference:
http://www.microsoft.com/technet/com...l/sql1023.mspx
http://vyaskn.tripod.com/watch_your_timeouts.htm
"Karan" wrote:

> Hi,
> We have designed a system which processes millions of transactions a
> year. There are a couple of tables in the system which are heavily
> used. These 2 tables gets about 30,000 rows inserted into them
> everyday at an average. The inserts are done through stored procedures
> after perfomring some complex logic. There are also a few scheduled
> jobs that run every night which accesses these 2 tables. In addition,
> there are a few reports that run in the web system which acces these
> table for ad-hoc reporting. In the last few months, the number of
> transactions have increased quite a bit and system started giving us
> problems such as a lot of timeouts and a few deadlockks everyday.
> I am thinking that if I can somehow replicate these tables every 6
> hours or so and then use the new tables for all reporting purposes, my
> timeouts might decrease. Could you suggest on how I could go about
> replicating these 2 tables in an efficient way? and whether this would
> help my situation?
> Any other suggestions on how I could improve my system would be
> appreciated.
> Thanks,
> Karan
>
|||I performed the DBCC SHOWCONTIG on the tables affected and the stats
look ok to me. I am pasting the stats below..
DBCC SHOWCONTIG scanning 'claims' table...
Table: 'table_name' (322100188); index ID: 1, database ID: 7
TABLE level scan performed.
- Pages Scanned........................: 202265
- Extents Scanned.......................: 25349
- Extent Switches.......................: 27598
- Avg. Pages per Extent..................: 8.0
- Scan Density [Best Count:Actual Count]......: 91.61% [25284:27599]
- Logical Scan Fragmentation ..............: 0.64%
- Extent Scan Fragmentation ...............: 10.38%
- Avg. Bytes Free per Page................: 672.6
- Avg. Page Density (full)................: 91.69%
DBCC execution completed. If DBCC printed error messages, contact your
system administrator.
Please let me know if you see anything I could improve with this
stats. All other indexes on these table have similar stats as well.
Thanks for your help.
Karan
|||looks good to me.
Red Flag would be scan density LESS THAN 80%....You look fine in that
regard.
Greg Jackson
PDX, Oregon

How to reduce timeouts in SQL Server?

Hi,
We have designed a system which processes millions of transactions a
year. There are a couple of tables in the system which are heavily
used. These 2 tables gets about 30,000 rows inserted into them
everyday at an average. The inserts are done through stored procedures
after perfomring some complex logic. There are also a few scheduled
jobs that run every night which accesses these 2 tables. In addition,
there are a few reports that run in the web system which acces these
table for ad-hoc reporting. In the last few months, the number of
transactions have increased quite a bit and system started giving us
problems such as a lot of timeouts and a few deadlockks everyday.
I am thinking that if I can somehow replicate these tables every 6
hours or so and then use the new tables for all reporting purposes, my
timeouts might decrease. Could you suggest on how I could go about
replicating these 2 tables in an efficient way? and whether this would
help my situation?
Any other suggestions on how I could improve my system would be
appreciated.
Thanks,
Karanouch....
tough question without being able to see your database.
here are some thoughts...
1. Make sure indexes are being optimized\maintained on a regular basis (Read
up on showContig and on dbcc IndexDefrag)
2. review your critical transactions and make sure that you are not causing
excessive blocking and deadlocks (This is really hard, you'll have to read
up on troubleshooting deadlocks)
3. yes your adhoc reporting should be moved out to another database. You can
use DTS for this, replication or log shipping. We are using log shipping to
satisfy this need.
you're likely going to need to spend some serious hours pinpointing the
problem here. My guess is that option #1 above will buy you some serious
performance gains.
Cheers,
Greg Jackson
PDX, Oregon|||Articles for reference:
http://www.microsoft.com/technet/co...ql/sql1023.mspx
http://vyaskn.tripod.com/watch_your_timeouts.htm
"Karan" wrote:

> Hi,
> We have designed a system which processes millions of transactions a
> year. There are a couple of tables in the system which are heavily
> used. These 2 tables gets about 30,000 rows inserted into them
> everyday at an average. The inserts are done through stored procedures
> after perfomring some complex logic. There are also a few scheduled
> jobs that run every night which accesses these 2 tables. In addition,
> there are a few reports that run in the web system which acces these
> table for ad-hoc reporting. In the last few months, the number of
> transactions have increased quite a bit and system started giving us
> problems such as a lot of timeouts and a few deadlockks everyday.
> I am thinking that if I can somehow replicate these tables every 6
> hours or so and then use the new tables for all reporting purposes, my
> timeouts might decrease. Could you suggest on how I could go about
> replicating these 2 tables in an efficient way? and whether this would
> help my situation?
> Any other suggestions on how I could improve my system would be
> appreciated.
> Thanks,
> Karan
>|||I performed the DBCC SHOWCONTIG on the tables affected and the stats
look ok to me. I am pasting the stats below..
DBCC SHOWCONTIG scanning 'claims' table...
Table: 'table_name' (322100188); index ID: 1, database ID: 7
TABLE level scan performed.
- Pages Scanned........................: 202265
- Extents Scanned.......................: 25349
- Extent Switches.......................: 27598
- Avg. Pages per Extent..................: 8.0
- Scan Density [Best Count:Actual Count]......: 91.61% [25284:27599
]
- Logical Scan Fragmentation ..............: 0.64%
- Extent Scan Fragmentation ...............: 10.38%
- Avg. Bytes Free per Page................: 672.6
- Avg. Page Density (full)................: 91.69%
DBCC execution completed. If DBCC printed error messages, contact your
system administrator.
Please let me know if you see anything I could improve with this
stats. All other indexes on these table have similar stats as well.
Thanks for your help.
Karan|||looks good to me.
Red Flag would be scan density LESS THAN 80%....You look fine in that
regard.
Greg Jackson
PDX, Oregon

Monday, March 19, 2012

How to recreate a blank copy of dbs

I want to create a clean copy of my DB now that it is done. So it can be moved to another Server. It has some sample data in some tables that I would like to keep and some in other table that I don't. How can I do this?

Hi. I have two possible solutions:

1. You can generate a script with all the database objects: in enterprise manager right click over the database --> all tasks --> generate sql script... and in options tab include indexes, triggers, primary keys, etc.

For the tables you need with data, you could add the necessary inserts commands... but only if there are few rows.

2. Backup yuor database, and restore it in a new database with other name... delete data you don't need and use this new database as your "empty" databse

How to recover Master database table data

Hi all, I am working with SQL Server2000 and I have done a horrible thing here.
We here have an script that delete all data from all user tables of a database, and I run it in the master DATABASE.
As we don't made backups of this database, now somethings of the database aren't working.

Here is the script:

declare @.table_name sysname
declare @.alter_table_statement varchar(256)
declare @.delete_statement varchar(256)

-- definindo o cursor...
declare table_name_cursor cursor local fast_forward for
select
name
from
sysobjects
where
xtype = 'U'
and
name <> 'dtproperties'

-- desligando os vínculos...
open table_name_cursor
fetch next from table_name_cursor into @.table_name
select @.alter_table_statement = 'alter table ' + ltrim(rtrim(@.table_name)) + ' nocheck constraint all'
exec(@.alter_table_statement)
while @.@.Fetch_Status = 0
begin
fetch next from table_name_cursor into @.table_name
select @.alter_table_statement = 'alter table ' + ltrim(rtrim(@.table_name)) + ' nocheck constraint all'
exec(@.alter_table_statement)
end
close table_name_cursor

open table_name_cursor
fetch next from table_name_cursor into @.table_name
select @.delete_statement = 'delete from ' + ltrim(rtrim(@.table_name))
exec(@.delete_statement)
while @.@.Fetch_Status = 0
begin
fetch next from table_name_cursor into @.table_name
select @.delete_statement = 'delete from ' + ltrim(rtrim(@.table_name))
exec(@.delete_statement)
end
close table_name_cursor

open table_name_cursor
fetch next from table_name_cursor into @.table_name
select @.alter_table_statement = 'alter table ' + ltrim(rtrim(@.table_name)) + ' check constraint all'
exec(@.alter_table_statement)
while @.@.Fetch_Status = 0
begin
fetch next from table_name_cursor into @.table_name
select @.alter_table_statement = 'alter table ' + ltrim(rtrim(@.table_name)) + ' check constraint all'
exec(@.alter_table_statement)
end
close table_name_cursor

deallocate table_name_cursor

I have tried to restore master table with the restore function, but it doesn't work. When I try to do this I received a message informing that it can't copy the data because one file was in use. The server was in a single user mode.

Is there anyway to recover the data that I have lost?

Help me understand this.

At the beginning, you say that you don't have backups of the master database.

At the end you say that you tried to restore the master database, but had problems.

You should be able to restore the master database if you have a backup of it, and this would be the best method to use.

Can you clarify, and also let us know what method (T-SQL commands etc.) you used to attempt the restore?

|||Kevin don't have any backup of this database, I tried to rebuild it, not restore it, I used the wrong word for it.|||

There isn't any easy answer to this. If you have already rebuilt master then you should have SQL Server up and running with a clean master. Realize that by rebuilding the master database all your jobs and maintenance plans in MSDB have been lost, unless you have a backup of msdb or scripts to recreate the jobs.

The datafiles for your user databases should still be on the disk, you will need to attach those using sp_attach_db.

Once you have the databases attached you still won't have the logins. You will need to recreate the logins, then remap those new logins using the procedure outlined in http://support.microsoft.com/kb/274188/en-us

|||Ok thanks... one more explanation
In fact my real problem isn't on the databases and user logins, becaus this I doesn't lost. I lost the access to table metadata (via jdbc driver) and the permission to create diagrams. Probably this can be done by reconfiguring the server, but I doesn't know how to do it.

How to recover from dropped tempdb?

I am testing sql server 2005 in different disaster situations happened under 2000. I was reading a lot about no update on system tables, so:

sp_detach_db 'tempdb'

In sql 2000 i could issue something like the following command to recover:

use master

sp_configure 'allow updates', 1

reconfigure with override

go

insert sys.sysdatabases (name, dbid, sid, mode, status, status2, crdate, reserved, category, cmptlevel, filename, version)

values ('tempdb', 2, 0x01, 0, 8, 1090520064, '2007-01-27 13:03:10.873', '1900-01-01 00:00:00.000', 0, '90', 'D:\beep\mssql\temp\tempdb.mdf', 611)

go

sp_configure 'allow updates', 0

reconfigure with override

go

As tempdb is hardcoded db id 2, I do not have a chance to recover without a master backup.

Of course, this situation can be recovered using a master backup, but it is not always available.

Thank you for your comments.

How is it that you are detaching tempdb?

1> sp_detach_db tempdb
2> go
Msg 7940, Level 16, State 1:
System databases master, model, msdb, and tempdb cannot be detached.
1>

|||

Thanks for the fast response.

Yes, it is the normal behavior. But imagine the following situation:

The end-user tries to move the system databases to another disk. He reads the information on your site (http://support.microsoft.com/kb/224071/) and starts the server with /T3608. He executes the procedure step by step, moves msdb and model, forgets to read the remaining part and happy to move tempdb the same way.

This is the point, when you are getting involved. How to proceed?

|||Well, that script won'y work with SQL Server 2005. You can not allowed to make direct updates to system tables. You can't drop or detach a system database, so the only way of having an issue with tempdb is that it either becomes damaged while SQL Server is running in which case the SQL Server will go offline and fixing the issue is a matter of restarting SQL Server or you have an issue while starting up SQL Server in which case the SQL Server won't start and you would then need to go through the process documented in BOL for recreating tempdb.|||I have asked that the article be updated to remove SQL 2005 from the section that does the moves via detach. ALTER DATABASE should be used to change the file paths in these cases and there are detailed instructions in books online.|||

Thank you for your comment.

You _can_ detach the tempdb, if you start SQL Server 2005 with the /T3608 parameter [I have tried before posting the question]. From the point tempdb detached it cannot be re-attached, nor can be re-created using the way you mention, as the row with hardcoded database id "2" does not exists in the sysdatabases table.

|||

Thank you.

For closing the issue can we say, that the only way to recover from this situation is attaching a clean master db and re-attach the databases and re-create all global settings (logins, endpoints, etc.) stored in master?

|||

If you have a backup of master, you could restore that after putting in the clean master. Master is usually small enough that it should not be too big a burden to do full backups on a regular basis.

And if you had truly followed the article, the first step is

?

Make a current backup of all databases, especially master, from their current location.

which should allow you to recover.

How to recover from dropped tempdb?

I am testing sql server 2005 in different disaster situations happened under 2000. I was reading a lot about no update on system tables, so:

sp_detach_db 'tempdb'

In sql 2000 i could issue something like the following command to recover:

use master

sp_configure 'allow updates', 1

reconfigure with override

go

insert sys.sysdatabases (name, dbid, sid, mode, status, status2, crdate, reserved, category, cmptlevel, filename, version)

values ('tempdb', 2, 0x01, 0, 8, 1090520064, '2007-01-27 13:03:10.873', '1900-01-01 00:00:00.000', 0, '90', 'D:\beep\mssql\temp\tempdb.mdf', 611)

go

sp_configure 'allow updates', 0

reconfigure with override

go

As tempdb is hardcoded db id 2, I do not have a chance to recover without a master backup.

Of course, this situation can be recovered using a master backup, but it is not always available.

Thank you for your comments.

How is it that you are detaching tempdb?

1> sp_detach_db tempdb
2> go
Msg 7940, Level 16, State 1:
System databases master, model, msdb, and tempdb cannot be detached.
1>

|||

Thanks for the fast response.

Yes, it is the normal behavior. But imagine the following situation:

The end-user tries to move the system databases to another disk. He reads the information on your site (http://support.microsoft.com/kb/224071/) and starts the server with /T3608. He executes the procedure step by step, moves msdb and model, forgets to read the remaining part and happy to move tempdb the same way.

This is the point, when you are getting involved. How to proceed?

|||Well, that script won'y work with SQL Server 2005. You can not allowed to make direct updates to system tables. You can't drop or detach a system database, so the only way of having an issue with tempdb is that it either becomes damaged while SQL Server is running in which case the SQL Server will go offline and fixing the issue is a matter of restarting SQL Server or you have an issue while starting up SQL Server in which case the SQL Server won't start and you would then need to go through the process documented in BOL for recreating tempdb.|||I have asked that the article be updated to remove SQL 2005 from the section that does the moves via detach. ALTER DATABASE should be used to change the file paths in these cases and there are detailed instructions in books online.|||

Thank you for your comment.

You _can_ detach the tempdb, if you start SQL Server 2005 with the /T3608 parameter [I have tried before posting the question]. From the point tempdb detached it cannot be re-attached, nor can be re-created using the way you mention, as the row with hardcoded database id "2" does not exists in the sysdatabases table.

|||

Thank you.

For closing the issue can we say, that the only way to recover from this situation is attaching a clean master db and re-attach the databases and re-create all global settings (logins, endpoints, etc.) stored in master?

|||

If you have a backup of master, you could restore that after putting in the clean master. Master is usually small enough that it should not be too big a burden to do full backups on a regular basis.

And if you had truly followed the article, the first step is

?

Make a current backup of all databases, especially master, from their current location.

which should allow you to recover.

How to recover deleted data from master database

Hi all,
I made a horrible thing this week. I have an script that delete all data from all user tables, and I run it in the master database.
After this I couldn't access the metadata from the tables of my databases using a JDBC connection.
My script runs over all sysobject that are different from dtproperties.
I don't have a back up of master table. I have tried to copy the data from another master database (from another machine) but I did not work. I tried to copy from a xls, but it did'nt work too. I try do recover the master database (as it is explained in the online books) but it did not work either.

Someone know how can I recover this data?Here is the script:

declare @.table_name sysname
declare @.alter_table_statement varchar(256)
declare @.delete_statement varchar(256)

declare table_name_cursor cursor local fast_forward for
select
name
from
sysobjects
where
xtype = 'U'
and
name <> 'dtproperties'

open table_name_cursor
fetch next from table_name_cursor into @.table_name
select @.alter_table_statement = 'alter table ' + ltrim(rtrim(@.table_name)) + ' nocheck constraint all'
exec(@.alter_table_statement)
while @.@.Fetch_Status = 0
begin
fetch next from table_name_cursor into @.table_name
select @.alter_table_statement = 'alter table ' + ltrim(rtrim(@.table_name)) + ' nocheck constraint all'
exec(@.alter_table_statement)
end
close table_name_cursor

open table_name_cursor
fetch next from table_name_cursor into @.table_name
select @.delete_statement = 'delete from ' + ltrim(rtrim(@.table_name))
exec(@.delete_statement)
while @.@.Fetch_Status = 0
begin
fetch next from table_name_cursor into @.table_name
select @.delete_statement = 'delete from ' + ltrim(rtrim(@.table_name))
exec(@.delete_statement)
end
close table_name_cursor

-- ligando os vnculos...
open table_name_cursor
fetch next from table_name_cursor into @.table_name
select @.alter_table_statement = 'alter table ' + ltrim(rtrim(@.table_name)) + ' check constraint all'
exec(@.alter_table_statement)
while @.@.Fetch_Status = 0
begin
fetch next from table_name_cursor into @.table_name
select @.alter_table_statement = 'alter table ' + ltrim(rtrim(@.table_name)) + ' check constraint all'
exec(@.alter_table_statement)
end
close table_name_cursor

deallocate table_name_cursor

And here is the names of the tables of master database that this script had deleted all the containing data:
spt_monitor
spt_values
spt_fallback_db
spt_fallback_dev
spt_fallback_usg
spt_provider_types
spt_datatype_info_ext
MSreplication_options
spt_datatype_info
spt_server_info
spt_server_info

Monday, March 12, 2012

how to reconcile two tables using cursor.

Hi! I'm new here.
Here's my problem.

How can u reconcile two tables using cursor?
I would like to create a new table reflecting the matching records of the two tables I've compared.
how will i do this?
please help.Heya, ssshhh. Welcome to TSDN!

What database server software are you running?|||thnx for the welcome.
I'm using ms sql server 2000.

im doing a reconciliation report.
how can i insert the matching records into a new table?|||

Quote:

Originally Posted by ssshhh

thnx for the welcome.
I'm using ms sql server 2000.

im doing a reconciliation report.
how can i insert the matching records into a new table?


Now that we know your RDBM, we can direct you to the proper place to post.
That would be in the SQL Server/MSSQL Forum.

Also, please take some time to check out our Posting Guidelines. Thanks.|||Moving to the MS SQL forum...|||am i not in the mssql forum already?|||Use joins for your purpose and create the table based on that.

If u want to continue the process create a view instead.|||Heya, ssshhh.

Quote:

Originally Posted by ssshhh

am i not in the mssql forum already?


You are now ~_^

Wednesday, March 7, 2012

How to read block of rows from database tables

have created a Database Application in Java and display all the records in tabular format of one Table. This table have Millions of Rows, If I run Select * from Table, then my Machine not responding, so Now I wants to add paging of 1000 rows at one time.

Is there are any option/query to read block of rows at one time and then query again for next page ?

i.e In MYSQL have LIMIT clause with Select Statement

Please let me know..

Database : SQL Server 2000/2005,

Thanks in Advance
Laxmilal

Whenever you use Select statement you must use WHERE condition to limit the rows.

Eg.

Select col1,col2.... From Tablename WHERE someid between 1 and 1000

Madhu

|||

You 'Machine not responding' is most likely due to waiting for millions of rows to come back from the server.

You really need to limit the quantity of data that you are requesting from SQL Server. Without WHERE clause criteria that enforces limits, you are unnecessarily wasting bandwidth (and time) as more data than you need for the operation at hand is being transported to the client application.

With SQL 2005, you may wish to explore using the new ROW_NUMBER() function to assist in retrieving specific 'blocks' of rows.

Referring to Books Online, Topic: ROW_NUMBER()

Example:

USE AdventureWorks; GO

WITH OrderedOrders AS ( SELECT SalesOrderID, OrderDate, ROW_NUMBER() OVER (ORDER BY OrderDate) AS 'RowNumber' FROM Sales.SalesOrderHeader ) SELECT * FROM OrderedOrders WHERE RowNumber BETWEEN 50 AND 60;(You should be able to adapt this concept for your needs.)

Friday, February 24, 2012

how to raise an error and test rollbacks

I put this at the end of a stored procedure in which I update two
tables. I am trying to raise an error so that I can test whether or not
both tables will be rolled back if the event of error.
So far, this is not working to raise and error, and the therefore the
two tables are updating. I copied this out of the SQL Server Books
online
Help? And thank you.
--SQL transact that creates the procedure,
--inserts data in one table,
--retrieves value of identity column
--and then updates a second table
--and then attempts to provoke an error
--and rollback in both tables with:
RAISERROR ('sprStoredProcedureError', 16, 1)
Set @.STRERROR = @.@.ERROR
Select @.STRERROR
IF @.STRERROR = 0
COMMIT TRAN
ELSE
ROLLBACK TRAN
END
GORAISERROR is for raising the error to the calling app. At the point you are
"raising" the error, you have already determined that something is wrong in
the transaction :P This is not what you are looking for here.
In QA, if you have permissions you can right click on your proc and choose
debug to walk through it step by step. Set the values to something you know
will error and see if it goes into the rollback stage.
If you do not have permssions for debugging, just try inserting a row with
that you know will cause an error (duplicate PK for instance). In your erro
r
handler section Print 'ROLLBACK'. Then execute the proc and check messages.
HTH,
John Scragg
"JJ_377@.hotmail.com" wrote:

> I put this at the end of a stored procedure in which I update two
> tables. I am trying to raise an error so that I can test whether or not
> both tables will be rolled back if the event of error.
> So far, this is not working to raise and error, and the therefore the
> two tables are updating. I copied this out of the SQL Server Books
> online
> Help? And thank you.
> --SQL transact that creates the procedure,
> --inserts data in one table,
> --retrieves value of identity column
> --and then updates a second table
> --and then attempts to provoke an error
> --and rollback in both tables with:
> RAISERROR ('sprStoredProcedureError', 16, 1)
> Set @.STRERROR = @.@.ERROR
> Select @.STRERROR
> IF @.STRERROR = 0
> COMMIT TRAN
> ELSE
> ROLLBACK TRAN
> END
> GO
>|||Usually, it helps to include a complete script that can be used to reproduce
the problem. Below is one that I used; it is based almost entirely on your
code snippet and works correctly. There must be another issue that is not
apparent from the information you posted.
set nocount on
go
create table aa_junk1 (junk1_id int identity, more_stuff varchar(20) not
null)
go
create table aa_junk2 (junk2_id int identity, junk1_id int not null,
addl_stuff varchar(20) not null)
go
alter table aa_junk1 add constraint junk1_pk primary key (junk1_id)
go
alter table aa_junk2 add constraint junk2_pk primary key (junk2_id)
go
create procedure aa_junk as
declare @.STRERROR int
declare @.newid int
begin tran
insert aa_junk1 (more_stuff) values ('test1')
set @.newid = @.@.IDENTITY
insert aa_junk2 (junk1_id, addl_stuff)
values (@.newid, 'test1a')
select * from aa_junk1
select * from aa_junk2
RAISERROR ('sprStoredProcedureError', 16, 1)
Set @.STRERROR = @.@.ERROR
Select @.STRERROR
IF @.STRERROR = 0
BEGIN
PRINT 'COMMITTING'
COMMIT TRAN
END
ELSE
BEGIN
PRINT 'ROLLING BACK'
ROLLBACK TRAN
END
GO
exec aa_junk
go
select * from aa_junk1
select * from aa_junk2
go
drop table aa_junk2
drop table aa_junk1
drop procedure aa_junk
go|||Write correct code
begin tran
insert aa_junk1 (more_stuff) values ('test1')
SET @.STRERROR = @.@.ERROR -- @.@.ERROR must be saved immediately after DML
statement
IF @.STRERROR = 0
BEGIN
SET @.newid = SCOPE_IDENTITY() -- DONT USE @.@.IDENTITY !!!
insert aa_junk2 (junk1_id, addl_stuff) values (@.newid, 'test1a')
SET @.STRERROR = @.@.ERROR
IF @.STRERROR = 0
COMMIT TRAN
ELSE
BEGIN
RAISERROR('AA_JUNK2 ERROR NR. %d',16,1,@.STRERROR)
ROLLBACK TRAN
END
ELSE
BEGIN
RAISERROR(' AA_JUNK1 ERROR NR. %d',16,1,@.STRERROR)
ROLLBACK TRAN
END
Kuido
Scott Morris wrote:
>Usually, it helps to include a complete script that can be used to reproduc
e
>the problem. Below is one that I used; it is based almost entirely on your
>code snippet and works correctly. There must be another issue that is not
>apparent from the information you posted.
>set nocount on
>go
>create table aa_junk1 (junk1_id int identity, more_stuff varchar(20) not
>null)
>go
>create table aa_junk2 (junk2_id int identity, junk1_id int not null,
>addl_stuff varchar(20) not null)
>go
>alter table aa_junk1 add constraint junk1_pk primary key (junk1_id)
>go
>alter table aa_junk2 add constraint junk2_pk primary key (junk2_id)
>go
>create procedure aa_junk as
>declare @.STRERROR int
>declare @.newid int
>begin tran
>insert aa_junk1 (more_stuff) values ('test1')
>set @.newid = @.@.IDENTITY
>insert aa_junk2 (junk1_id, addl_stuff)
>values (@.newid, 'test1a')
>select * from aa_junk1
>select * from aa_junk2
>RAISERROR ('sprStoredProcedureError', 16, 1)
>Set @.STRERROR = @.@.ERROR
>Select @.STRERROR
>IF @.STRERROR = 0
>BEGIN
> PRINT 'COMMITTING'
> COMMIT TRAN
>END
>ELSE
>BEGIN
> PRINT 'ROLLING BACK'
> ROLLBACK TRAN
>END
>GO
>exec aa_junk
>go
>select * from aa_junk1
>select * from aa_junk2
>go
>drop table aa_junk2
>drop table aa_junk1
>drop procedure aa_junk
>go
Message posted via droptable.com
http://www.droptable.com/Uwe/Forums...server/200510/1

how to raise an error and test rollbacks

I put this at the end of a stored procedure in which I update two
tables. I am trying to raise an error so that I can test whether or not
both tables will be rolled back if the event of error.
So far, this is not working to raise and error, and the therefore the
two tables are updating. I copied this out of the SQL Server Books
online
Help? And thank you.
--SQL transact that creates the procedure,
--inserts data in one table,
--retrieves value of identity column
--and then updates a second table
--and then attempts to provoke an error
--and rollback in both tables with:
RAISERROR ('sprStoredProcedureError', 16, 1)
Set @.STRERROR = @.@.ERROR
Select @.STRERROR
IF @.STRERROR = 0
COMMIT TRAN
ELSE
ROLLBACK TRAN
END
GO
RAISERROR is for raising the error to the calling app. At the point you are
"raising" the error, you have already determined that something is wrong in
the transaction :P This is not what you are looking for here.
In QA, if you have permissions you can right click on your proc and choose
debug to walk through it step by step. Set the values to something you know
will error and see if it goes into the rollback stage.
If you do not have permssions for debugging, just try inserting a row with
that you know will cause an error (duplicate PK for instance). In your error
handler section Print 'ROLLBACK'. Then execute the proc and check messages.
HTH,
John Scragg
"JJ_377@.hotmail.com" wrote:

> I put this at the end of a stored procedure in which I update two
> tables. I am trying to raise an error so that I can test whether or not
> both tables will be rolled back if the event of error.
> So far, this is not working to raise and error, and the therefore the
> two tables are updating. I copied this out of the SQL Server Books
> online
> Help? And thank you.
> --SQL transact that creates the procedure,
> --inserts data in one table,
> --retrieves value of identity column
> --and then updates a second table
> --and then attempts to provoke an error
> --and rollback in both tables with:
> RAISERROR ('sprStoredProcedureError', 16, 1)
> Set @.STRERROR = @.@.ERROR
> Select @.STRERROR
> IF @.STRERROR = 0
> COMMIT TRAN
> ELSE
> ROLLBACK TRAN
> END
> GO
>
|||Usually, it helps to include a complete script that can be used to reproduce
the problem. Below is one that I used; it is based almost entirely on your
code snippet and works correctly. There must be another issue that is not
apparent from the information you posted.
set nocount on
go
create table aa_junk1 (junk1_id int identity, more_stuff varchar(20) not
null)
go
create table aa_junk2 (junk2_id int identity, junk1_id int not null,
addl_stuff varchar(20) not null)
go
alter table aa_junk1 add constraint junk1_pk primary key (junk1_id)
go
alter table aa_junk2 add constraint junk2_pk primary key (junk2_id)
go
create procedure aa_junk as
declare @.STRERROR int
declare @.newid int
begin tran
insert aa_junk1 (more_stuff) values ('test1')
set @.newid = @.@.IDENTITY
insert aa_junk2 (junk1_id, addl_stuff)
values (@.newid, 'test1a')
select * from aa_junk1
select * from aa_junk2
RAISERROR ('sprStoredProcedureError', 16, 1)
Set @.STRERROR = @.@.ERROR
Select @.STRERROR
IF @.STRERROR = 0
BEGIN
PRINT 'COMMITTING'
COMMIT TRAN
END
ELSE
BEGIN
PRINT 'ROLLING BACK'
ROLLBACK TRAN
END
GO
exec aa_junk
go
select * from aa_junk1
select * from aa_junk2
go
drop table aa_junk2
drop table aa_junk1
drop procedure aa_junk
go
|||Write correct code
begin tran
insert aa_junk1 (more_stuff) values ('test1')
SET @.STRERROR = @.@.ERROR -- @.@.ERROR must be saved immediately after DML
statement
IF @.STRERROR = 0
BEGIN
SET @.newid = SCOPE_IDENTITY() -- DONT USE @.@.IDENTITY !!!
insert aa_junk2 (junk1_id, addl_stuff) values (@.newid, 'test1a')
SET @.STRERROR = @.@.ERROR
IF @.STRERROR = 0
COMMIT TRAN
ELSE
BEGIN
RAISERROR('AA_JUNK2 ERROR NR. %d',16,1,@.STRERROR)
ROLLBACK TRAN
END
ELSE
BEGIN
RAISERROR(' AA_JUNK1 ERROR NR. %d',16,1,@.STRERROR)
ROLLBACK TRAN
END
Kuido
Scott Morris wrote:
>Usually, it helps to include a complete script that can be used to reproduce
>the problem. Below is one that I used; it is based almost entirely on your
>code snippet and works correctly. There must be another issue that is not
>apparent from the information you posted.
>set nocount on
>go
>create table aa_junk1 (junk1_id int identity, more_stuff varchar(20) not
>null)
>go
>create table aa_junk2 (junk2_id int identity, junk1_id int not null,
>addl_stuff varchar(20) not null)
>go
>alter table aa_junk1 add constraint junk1_pk primary key (junk1_id)
>go
>alter table aa_junk2 add constraint junk2_pk primary key (junk2_id)
>go
>create procedure aa_junk as
>declare @.STRERROR int
>declare @.newid int
>begin tran
>insert aa_junk1 (more_stuff) values ('test1')
>set @.newid = @.@.IDENTITY
>insert aa_junk2 (junk1_id, addl_stuff)
>values (@.newid, 'test1a')
>select * from aa_junk1
>select * from aa_junk2
>RAISERROR ('sprStoredProcedureError', 16, 1)
>Set @.STRERROR = @.@.ERROR
>Select @.STRERROR
>IF @.STRERROR = 0
>BEGIN
> PRINT 'COMMITTING'
> COMMIT TRAN
>END
>ELSE
>BEGIN
> PRINT 'ROLLING BACK'
> ROLLBACK TRAN
>END
>GO
>exec aa_junk
>go
>select * from aa_junk1
>select * from aa_junk2
>go
>drop table aa_junk2
>drop table aa_junk1
>drop procedure aa_junk
>go
Message posted via droptable.com
http://www.droptable.com/Uwe/Forums...erver/200510/1