Showing posts with label select. Show all posts
Showing posts with label select. Show all posts

Thursday, March 22, 2012

DBCC Results into a Table Brings Errors!

If you run the script below, it will cause an error. However, if you
run the select portion after the error, it will have accomplished the
desired result. Unfortunately, the table has to be manually dropped
after the proc is run. Any ideas why'? USing the GO keyword is not
an option as it will blow the rest of the script.
CREATE TABLE #db_file_information(
fileid integer,
theFileGroup integer,
Total_Extents integer,
Used_Extents integer,
db varchar(30),
file_Path_name varchar(300)--,
-- File_Free_space decimal(15,2),
-- Percent_Free decimal(15,2)
)
-- Get the size of the datafiles
insert into #db_file_information exec('DBCC showfilestats')
-- add two columns to the temp table
alter table #db_file_information add dude as
((Total_Extents-Used_Extents)/(Total_extents*1.0))
select * from #db_file_information
drop table #db_file_informationTry,
use northwind
go
CREATE TABLE #db_file_information(
Fileid bigint,
[FileGroup] bigint,
TotalExtents bigint,
UsedExtents bigint,
[Name] sysname,
[FileName] varchar(260)
)
go
-- Get the size of the datafiles
insert into #db_file_information
exec('DBCC showfilestats with NO_INFOMSGS')
-- add two columns to the temp table
alter table #db_file_information
add dude as ((TotalExtents - UsedExtents) / nullif((TotalExtents * 1.0), 0))
go
select * from #db_file_information
go
drop table #db_file_information
go
AMB
"dpaskiet@.comcast.net" wrote:

> If you run the script below, it will cause an error. However, if you
> run the select portion after the error, it will have accomplished the
> desired result. Unfortunately, the table has to be manually dropped
> after the proc is run. Any ideas why'? USing the GO keyword is not
> an option as it will blow the rest of the script.
>
> CREATE TABLE #db_file_information(
> fileid integer,
> theFileGroup integer,
> Total_Extents integer,
> Used_Extents integer,
> db varchar(30),
> file_Path_name varchar(300)--,
> -- File_Free_space decimal(15,2),
> -- Percent_Free decimal(15,2)
> )
> -- Get the size of the datafiles
> insert into #db_file_information exec('DBCC showfilestats')
> -- add two columns to the temp table
> alter table #db_file_information add dude as
> ((Total_Extents-Used_Extents)/(Total_extents*1.0))
>
> select * from #db_file_information
>
> drop table #db_file_information
>

Sunday, March 11, 2012

DBCC InputBuffer statement to be part of a select statement

I want to use DBCC inputbuffer function for all the spids in sysprocesses table without using a cursor or loop. In other words, I want to have the value of dbcc inputbuffer part of the select statement directly or indirectly. Is there a way??SQLDBAxxx,
Try running the following:

----------
set nocount on
select 'exec DBCC INPUTBUFFER('+convert(varchar,spid)+')'
from sysprocesses
go
----------

This will generate the DBCC code you need to run; it won't run
DBCC in the TSQL mode that you need. You can save the result set
and reload it to run it. Simple, but works...

I've never been able to execute a DBCC statement using ANSI-SQL syntax( select,update, insert ).|||SQLDBAxxx,
Try running the following:

----------
set nocount on
select 'DBCC INPUTBUFFER('+convert(varchar,spid)+')'
from sysprocesses
go
----------

This will generate the DBCC code you need to run; it won't run
DBCC in the TSQL mode that you need. You can save the result set
and reload it to run it. Simple, but works...

I've never been able to execute a DBCC statement using ANSI-SQL syntax( select,update, insert ).|||Thank you Scooter. But this is not what I wanted to do. I want to capture the input buffer for a given spid. I can do it thro a cursor or loop but if my recordset is big then by the time it comes to execute the dbcc inputbuffer for the last spid the inputbuffer may have changed. That is the reason I wanted to try by some other way to narrow down the time difference.

Wednesday, March 7, 2012

dbcc dbreindex on all tables/indexes and slow

This is what i ran and it was slow
declare reindex_cursor insensitive cursor for
select TABLE_NAME
from INFORMATION_SCHEMA.TABLES
where objectproperty(object_id(TABLE_NAME), 'IsMSShipped') = 0
and table_type = 'Base table'
declare @.table_name sysname
open reindex_cursor
fetch next from reindex_cursor into @.table_name
while (@.@.fetch_status = 0)
begin
print ('dbcc dbreindex(' + @.table_name + ')' )
exec ('dbcc dbreindex(' + @.table_name + ')' )
fetch next from reindex_cursor into @.table_name
end
close reindex_cursor
deallocate reindex_cursor
Could the problem be because of the insensitive that i mentioned in the
cursor declaration ? I can see that the DBCC is still running and not being
blocked but its way too slow. If I do cancel in between, will it rollback
everything ?For something like that I always use a STATIC cursor. You also don't need
to use dynamic sql as DBCC DBREINDEX will take a variable for the table
name. My guess why the operation is so slow is that your log and db files
probably don't have enough room in them for this and are constantly growing.
If your in full recovery mode you might want to backup your logs in between
so try something like this:
SET NOCOUNT ON
DECLARE @.TableName VARCHAR(100), @.Counter INT
SET @.Counter = 1
DECLARE curTables CURSOR STATIC LOCAL
FOR
SELECT Table_Name
FROM Information_Schema.Tables
WHERE Table_Type = 'BASE TABLE'
OPEN curTables
FETCH NEXT FROM curTables INTO @.TableName
SET @.TableName = RTRIM(@.TableName)
WHILE @.@.FETCH_STATUS = 0
BEGIN
SELECT 'Reindexing ' + @.TableName
DBCC DBREINDEX (@.TableName)
SET @.Counter = @.Counter + 1
-- Backup the Log every so often so as not to fill the log
IF @.Counter % 10 = 0
BEGIN
BACKUP LOG [Presents] TO [DD_Presents_Log] WITH NOINIT , NOUNLOAD
,
NAME = N'Presents Log Backup', NOSKIP , STATS = 10,
NOFORMAT
END
FETCH NEXT FROM curTables INTO @.TableName
END
CLOSE curTables
DEALLOCATE curTables
--
Andrew J. Kelly
SQL Server MVP
"Hassan" <fatima_ja@.hotmail.com> wrote in message
news:%23iFKndOsDHA.2252@.TK2MSFTNGP09.phx.gbl...
> This is what i ran and it was slow
> declare reindex_cursor insensitive cursor for
> select TABLE_NAME
> from INFORMATION_SCHEMA.TABLES
> where objectproperty(object_id(TABLE_NAME), 'IsMSShipped') = 0
> and table_type = 'Base table'
> declare @.table_name sysname
> open reindex_cursor
> fetch next from reindex_cursor into @.table_name
> while (@.@.fetch_status = 0)
> begin
> print ('dbcc dbreindex(' + @.table_name + ')' )
> exec ('dbcc dbreindex(' + @.table_name + ')' )
> fetch next from reindex_cursor into @.table_name
> end
> close reindex_cursor
> deallocate reindex_cursor
> Could the problem be because of the insensitive that i mentioned in the
> cursor declaration ? I can see that the DBCC is still running and not
being
> blocked but its way too slow. If I do cancel in between, will it rollback
> everything ?
>|||Why you don't use an database maintance plan?
Ronald
"Hassan" <fatima_ja@.hotmail.com> wrote in message
news:%23iFKndOsDHA.2252@.TK2MSFTNGP09.phx.gbl...
> This is what i ran and it was slow
> declare reindex_cursor insensitive cursor for
> select TABLE_NAME
> from INFORMATION_SCHEMA.TABLES
> where objectproperty(object_id(TABLE_NAME), 'IsMSShipped') = 0
> and table_type = 'Base table'
> declare @.table_name sysname
> open reindex_cursor
> fetch next from reindex_cursor into @.table_name
> while (@.@.fetch_status = 0)
> begin
> print ('dbcc dbreindex(' + @.table_name + ')' )
> exec ('dbcc dbreindex(' + @.table_name + ')' )
> fetch next from reindex_cursor into @.table_name
> end
> close reindex_cursor
> deallocate reindex_cursor
> Could the problem be because of the insensitive that i mentioned in the
> cursor declaration ? I can see that the DBCC is still running and not
being
> blocked but its way too slow. If I do cancel in between, will it rollback
> everything ?
>|||Now changing gears to the use of dynamic sql. How can i tell what needs a
dynamic sql and what can accept variables. Can you provide some examples and
how can i figure it out ? Also the definition of insensitive and static
seemed to be the same from BOL. Btw, i got the initial script from google
too.
Thanks
"Andrew J. Kelly" <sqlmvpnooospam@.shadhawk.com> wrote in message
news:uXF66WQsDHA.4088@.TK2MSFTNGP11.phx.gbl...
> For something like that I always use a STATIC cursor. You also don't need
> to use dynamic sql as DBCC DBREINDEX will take a variable for the table
> name. My guess why the operation is so slow is that your log and db files
> probably don't have enough room in them for this and are constantly
growing.
> If your in full recovery mode you might want to backup your logs in
between
> so try something like this:
>
> SET NOCOUNT ON
> DECLARE @.TableName VARCHAR(100), @.Counter INT
> SET @.Counter = 1
> DECLARE curTables CURSOR STATIC LOCAL
> FOR
> SELECT Table_Name
> FROM Information_Schema.Tables
> WHERE Table_Type = 'BASE TABLE'
> OPEN curTables
> FETCH NEXT FROM curTables INTO @.TableName
> SET @.TableName = RTRIM(@.TableName)
> WHILE @.@.FETCH_STATUS = 0
> BEGIN
> SELECT 'Reindexing ' + @.TableName
> DBCC DBREINDEX (@.TableName)
> SET @.Counter = @.Counter + 1
> -- Backup the Log every so often so as not to fill the log
> IF @.Counter % 10 = 0
> BEGIN
> BACKUP LOG [Presents] TO [DD_Presents_Log] WITH NOINIT ,
NOUNLOAD
> ,
> NAME = N'Presents Log Backup', NOSKIP , STATS = 10,
> NOFORMAT
> END
> FETCH NEXT FROM curTables INTO @.TableName
> END
> CLOSE curTables
> DEALLOCATE curTables
> --
> Andrew J. Kelly
> SQL Server MVP
>
> "Hassan" <fatima_ja@.hotmail.com> wrote in message
> news:%23iFKndOsDHA.2252@.TK2MSFTNGP09.phx.gbl...
> > This is what i ran and it was slow
> >
> > declare reindex_cursor insensitive cursor for
> > select TABLE_NAME
> > from INFORMATION_SCHEMA.TABLES
> > where objectproperty(object_id(TABLE_NAME), 'IsMSShipped') = 0
> > and table_type = 'Base table'
> >
> > declare @.table_name sysname
> >
> > open reindex_cursor
> > fetch next from reindex_cursor into @.table_name
> >
> > while (@.@.fetch_status = 0)
> > begin
> > print ('dbcc dbreindex(' + @.table_name + ')' )
> > exec ('dbcc dbreindex(' + @.table_name + ')' )
> > fetch next from reindex_cursor into @.table_name
> > end
> >
> > close reindex_cursor
> > deallocate reindex_cursor
> >
> > Could the problem be because of the insensitive that i mentioned in the
> > cursor declaration ? I can see that the DBCC is still running and not
> being
> > blocked but its way too slow. If I do cancel in between, will it
rollback
> > everything ?
> >
> >
>|||Sorry, I spaced on the Insensitive. As for when you can and when you can't
it isn't always spelled out but if you look at the entry in BOL it usually
gives a hint. If there are quotes around the option you can usually use a
variable.
DBCC DBREINDEX
( [ 'database.owner.table_name'
[ , index_name
[ , fillfactor ]
]
]
) [ WITH NO_INFOMSGS ]
Andrew J. Kelly
SQL Server MVP
"Hassan" <fatima_ja@.hotmail.com> wrote in message
news:%23CQ4liTsDHA.3416@.tk2msftngp13.phx.gbl...
> Now changing gears to the use of dynamic sql. How can i tell what needs a
> dynamic sql and what can accept variables. Can you provide some examples
and
> how can i figure it out ? Also the definition of insensitive and static
> seemed to be the same from BOL. Btw, i got the initial script from google
> too.
> Thanks
> "Andrew J. Kelly" <sqlmvpnooospam@.shadhawk.com> wrote in message
> news:uXF66WQsDHA.4088@.TK2MSFTNGP11.phx.gbl...
> > For something like that I always use a STATIC cursor. You also don't
need
> > to use dynamic sql as DBCC DBREINDEX will take a variable for the table
> > name. My guess why the operation is so slow is that your log and db
files
> > probably don't have enough room in them for this and are constantly
> growing.
> > If your in full recovery mode you might want to backup your logs in
> between
> > so try something like this:
> >
> >
> > SET NOCOUNT ON
> >
> > DECLARE @.TableName VARCHAR(100), @.Counter INT
> >
> > SET @.Counter = 1
> >
> > DECLARE curTables CURSOR STATIC LOCAL
> > FOR
> > SELECT Table_Name
> > FROM Information_Schema.Tables
> > WHERE Table_Type = 'BASE TABLE'
> >
> > OPEN curTables
> > FETCH NEXT FROM curTables INTO @.TableName
> > SET @.TableName = RTRIM(@.TableName)
> >
> > WHILE @.@.FETCH_STATUS = 0
> > BEGIN
> > SELECT 'Reindexing ' + @.TableName
> >
> > DBCC DBREINDEX (@.TableName)
> >
> > SET @.Counter = @.Counter + 1
> > -- Backup the Log every so often so as not to fill the log
> > IF @.Counter % 10 = 0
> > BEGIN
> > BACKUP LOG [Presents] TO [DD_Presents_Log] WITH NOINIT ,
> NOUNLOAD
> > ,
> > NAME = N'Presents Log Backup', NOSKIP , STATS = 10,
> > NOFORMAT
> > END
> >
> > FETCH NEXT FROM curTables INTO @.TableName
> > END
> >
> > CLOSE curTables
> > DEALLOCATE curTables
> >
> > --
> >
> > Andrew J. Kelly
> > SQL Server MVP
> >
> >
> > "Hassan" <fatima_ja@.hotmail.com> wrote in message
> > news:%23iFKndOsDHA.2252@.TK2MSFTNGP09.phx.gbl...
> > > This is what i ran and it was slow
> > >
> > > declare reindex_cursor insensitive cursor for
> > > select TABLE_NAME
> > > from INFORMATION_SCHEMA.TABLES
> > > where objectproperty(object_id(TABLE_NAME), 'IsMSShipped') = 0
> > > and table_type = 'Base table'
> > >
> > > declare @.table_name sysname
> > >
> > > open reindex_cursor
> > > fetch next from reindex_cursor into @.table_name
> > >
> > > while (@.@.fetch_status = 0)
> > > begin
> > > print ('dbcc dbreindex(' + @.table_name + ')' )
> > > exec ('dbcc dbreindex(' + @.table_name + ')' )
> > > fetch next from reindex_cursor into @.table_name
> > > end
> > >
> > > close reindex_cursor
> > > deallocate reindex_cursor
> > >
> > > Could the problem be because of the insensitive that i mentioned in
the
> > > cursor declaration ? I can see that the DBCC is still running and not
> > being
> > > blocked but its way too slow. If I do cancel in between, will it
> rollback
> > > everything ?
> > >
> > >
> >
> >
>|||On a seperate note, I'd like to encourage you to read the excellent
whitepaper at
http://www.microsoft.com/technet/treeview/default.asp?url=/technet/prodtechnol/sql/maintain/optimize/ss2kidbp.asp
It will help you determine the best way to deal with fragmentation and
whetehr you need to do anything about it at all, rather than just rebuilding
every table in the database.
Regards,
Paul.
--
Paul Randal
Dev Lead, Microsoft SQL Server Storage Engine
This posting is provided "AS IS" with no warranties, and confers no rights.
"Hassan" <fatima_ja@.hotmail.com> wrote in message
news:#iFKndOsDHA.2252@.TK2MSFTNGP09.phx.gbl...
> This is what i ran and it was slow
> declare reindex_cursor insensitive cursor for
> select TABLE_NAME
> from INFORMATION_SCHEMA.TABLES
> where objectproperty(object_id(TABLE_NAME), 'IsMSShipped') = 0
> and table_type = 'Base table'
> declare @.table_name sysname
> open reindex_cursor
> fetch next from reindex_cursor into @.table_name
> while (@.@.fetch_status = 0)
> begin
> print ('dbcc dbreindex(' + @.table_name + ')' )
> exec ('dbcc dbreindex(' + @.table_name + ')' )
> fetch next from reindex_cursor into @.table_name
> end
> close reindex_cursor
> deallocate reindex_cursor
> Could the problem be because of the insensitive that i mentioned in the
> cursor declaration ? I can see that the DBCC is still running and not
being
> blocked but its way too slow. If I do cancel in between, will it rollback
> everything ?
>|||Thanks Paul, I should have included that again for completeness but I know
that link has been referred to him several times already<g>.
--
Andrew J. Kelly
SQL Server MVP
"Paul S Randal [MS]" <prandal@.online.microsoft.com> wrote in message
news:uwswsAisDHA.2244@.TK2MSFTNGP09.phx.gbl...
> On a seperate note, I'd like to encourage you to read the excellent
> whitepaper at
>
http://www.microsoft.com/technet/treeview/default.asp?url=/technet/prodtechnol/sql/maintain/optimize/ss2kidbp.asp
> It will help you determine the best way to deal with fragmentation and
> whetehr you need to do anything about it at all, rather than just
rebuilding
> every table in the database.
> Regards,
> Paul.
> --
> Paul Randal
> Dev Lead, Microsoft SQL Server Storage Engine
>
> This posting is provided "AS IS" with no warranties, and confers no
rights.
> "Hassan" <fatima_ja@.hotmail.com> wrote in message
> news:#iFKndOsDHA.2252@.TK2MSFTNGP09.phx.gbl...
> > This is what i ran and it was slow
> >
> > declare reindex_cursor insensitive cursor for
> > select TABLE_NAME
> > from INFORMATION_SCHEMA.TABLES
> > where objectproperty(object_id(TABLE_NAME), 'IsMSShipped') = 0
> > and table_type = 'Base table'
> >
> > declare @.table_name sysname
> >
> > open reindex_cursor
> > fetch next from reindex_cursor into @.table_name
> >
> > while (@.@.fetch_status = 0)
> > begin
> > print ('dbcc dbreindex(' + @.table_name + ')' )
> > exec ('dbcc dbreindex(' + @.table_name + ')' )
> > fetch next from reindex_cursor into @.table_name
> > end
> >
> > close reindex_cursor
> > deallocate reindex_cursor
> >
> > Could the problem be because of the insensitive that i mentioned in the
> > cursor declaration ? I can see that the DBCC is still running and not
> being
> > blocked but its way too slow. If I do cancel in between, will it
rollback
> > everything ?
> >
> >
>