Showing posts with label working. Show all posts
Showing posts with label working. Show all posts

Friday, March 30, 2012

problem in filling a dataset using dataAdapter

hi friends,

i look forward an answer that solves my problem.

iam trying too populate a DropDown list . here is the codings. Previously it was working. suddenly,

it s generating error.

strConnectionString = "Provider = SQLOLEDB;Integrated Security=False; User ID=sa;Password=;Data Source=GIREESH-AC720F7;Initial Catalog=NorthWind"

in page_load event

dim sql as string

sql = "select AthleteNameKey from athletes"

result_adap = DbAccess.ExecuteAdaP(sql)

result_adap.Fill(result_ds, "athletes")

cboAthleteName.DataSource = "athletes"

cboAthleteName.DataTextField = "AthleteNameKey"

cboAthleteName.DataValueField = "AthleteNameKey"

cboAthleteName.DataBind()

PublicFunction ExecuteAdaP(ByVal sqlsAsString)As OleDbDataAdapter

'Dim ds As New OleDbDataAdapter

Dim daAsNew OleDbDataAdapter(sqls, strConnectionString)'da.Fill(ds)Return da

EndFunction

What's the detailed error message? When the error occurs, can you do the same query using Query Analyzer?

Wednesday, March 28, 2012

Problem in Data Driven Subscription

Hi all,

I had created a Data Driven Subsripiton last week and it was working fine.

But now when i try to run the same, it is not working.

But my log file does not say anything, all it has, is this

w3wp!library!5!06/19/2006-13:34:22:: i INFO: Call to GetPermissions:/Influe Reports/Out Of Stock Report (Missing) (This is report name)
w3wp!library!5!0 6/19/2006-13:34:22:: i INFO: Call to GetSystemPermissions
w3wp!library!5!06/19/2006-13:35:12:: i INFO: Call to GetPermissions:/Influe Reports/Out Of Stock Report (Missing)
w3wp!library!5!06/19/2006-13:35:12:: i INFO: Call to GetSystemPermissions
w3wp!library!1!6/19/2006-13:39:44:: i INFO: Cleaned 0 batch records, 0 policies, 3 sessions, 4 cache entries, 2 snapshots, 12 chunks, 0 running jobs, 0 persisted streams

i am looking at the file present in this path D:\Program Files\Microsoft SQL Server\MSSQL.3\Reporting Services\LogFiles\

Can any body tell me what is the problem all about.

Moving to reporting services group

problem in crystal reports design

hi,
I want to know if its possible to design crystal reports with checkbox. Iam working in vb.net and using crystal reports.What do you want to to that?

See if you find solution here
http://support.businessobjects.com/

Monday, March 26, 2012

Problem in creating nullable columns using SELECT INTO in SQL Serv

Hi Everyone!
I have a problem that is basically a database issue in SQL Server 2000
compared to Sybase 12. I am working on a application that should support bot
h
SYbase and SQL Server as backend. My code works fine in Sybase but not in SQ
L
Server. Following is the query that will give a an idea of the issue.
Query:
--
SELECT Null as c1,
isnull( NULL,0) as c2
INTO #temp_tbl
Above query creates both c1 and c2 as nullable columns in Sybase. The same
query in SQL Server creates C1 as nullable and C2 as not null. There are
situation that result in null data and cause inserting NULL into not columns
in SQL Server. Using SET ANSI_NULL_DFLT_ON ON also did not give the required
results. I am looking for a solution which would create c1 and c2 as nullabl
e
columns in SQL Server.
Please help in finding me a solution.
Thanks in advance.
SomeshSRV wrote:
> Hi Everyone!
> I have a problem that is basically a database issue in SQL Server 2000
> compared to Sybase 12. I am working on a application that should support b
oth
> SYbase and SQL Server as backend. My code works fine in Sybase but not in
SQL
> Server. Following is the query that will give a an idea of the issue.
> Query:
> --
> SELECT Null as c1,
> isnull( NULL,0) as c2
> INTO #temp_tbl
> Above query creates both c1 and c2 as nullable columns in Sybase. The same
> query in SQL Server creates C1 as nullable and C2 as not null. There are
> situation that result in null data and cause inserting NULL into not colum
ns
> in SQL Server. Using SET ANSI_NULL_DFLT_ON ON also did not give the requir
ed
> results. I am looking for a solution which would create c1 and c2 as nulla
ble
> columns in SQL Server.
> Please help in finding me a solution.
> Thanks in advance.
> Somesh
Like this:
CREATE TABLE #temp_tbl (c1 INTEGER NULL, c2 INTEGER NULL /* !!! NO
PRIMARY KEY !!! */)
INSERT INTO #temp_tbl (c1, c2)
SELECT ...
Curiously enough, the following seems to give your desired result in
SQL Server 2000 but not in 2005. Which just goes to show the folly of
such proprietary "tricks" as this.
SELECT c1, COALESCE(c2,0) AS c2
INTO T1
FROM (SELECT NULL, NULL) AS T(c1,c2);
David Portas
SQL Server MVP
--|||David Portas wrote:
> Curiously enough, the following seems to give your desired result in
> SQL Server 2000 but not in 2005. Which just goes to show the folly of
> such proprietary "tricks" as this.
> SELECT c1, COALESCE(c2,0) AS c2
> INTO T1
> FROM (SELECT NULL, NULL) AS T(c1,c2);
>
That was just a type coercian issue. The following tests OK on both
2000 and 2005 (8.00.760 and 9.00.1399.06)
SELECT c1, COALESCE(c2,0) AS c2
INTO T1
FROM (SELECT CAST(NULL AS INTEGER),
CAST(NULL AS INTEGER)) AS T(c1,c2);
David Portas
SQL Server MVP
--

problem in connection with SQLServer using DSN

Hi all,
I am using OdbcConnection for coonectivity with SQL Server db. My code
is working fine with windows application but in ASP.NET or in
webservice its raising following exception -
"ERROR [08001] [Microsoft][ODBC SQL Server Driver][DBNETLIB]SQL
Server does not exist or access denied.
ERROR [01000] [Microsoft][ODBC SQL Server Driver]
[DBNETLIB]ConnectionOpen (Connect())."
at System.Data.Odbc.OdbcConnection.Open()
Same code is working fine for Orace DSN.
My code:
OdbcConnection conn = new
OdbcConnection("dsn=MyDsn;uid=sa;pwd=stars;");
conn.Open();
Please help me to sort out this problem
Thanks
Dharmendra
I expect the reason that no one jumped on this question (which has been
asked and answered a million times) is that you're clearly not following the
advice given here and elsewhere. I suggest you do some reading about getting
connected to SQL Server. The 4th through 6th Editions of my Hitchhiker's
Guide to Visual Basic and SQL Server discuss how to connect via ODBC to SQL
Server. However, they do not talk about the fact that recent versions of SQL
Server require that you enable connectivity before attempting to connect.
This is covered in detail in my latest book and in whitepapers posted on my
blog (see www.betav.com/blogs/billva). And be sure to stop using SA
credentials to handle your customer's data...
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
INETA Speaker
www.betav.com/blog/billva
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
Visit www.hitchhikerguides.net to get more information on my latest book:
Hitchhiker's Guide to Visual Studio and SQL Server (7th Edition)
and Hitchhiker's Guide to SQL Server 2005 Compact Edition (EBook)
------
"tomar" <dharmendratomar2000@.gmail.com> wrote in message
news:1179209227.270257.149380@.n59g2000hsh.googlegr oups.com...
> Hi all,
> I am using OdbcConnection for coonectivity with SQL Server db. My code
> is working fine with windows application but in ASP.NET or in
> webservice its raising following exception -
> "ERROR [08001] [Microsoft][ODBC SQL Server Driver][DBNETLIB]SQL
> Server does not exist or access denied.
> ERROR [01000] [Microsoft][ODBC SQL Server Driver]
> [DBNETLIB]ConnectionOpen (Connect())."
> at System.Data.Odbc.OdbcConnection.Open()
> Same code is working fine for Orace DSN.
> My code:
> OdbcConnection conn = new
> OdbcConnection("dsn=MyDsn;uid=sa;pwd=stars;");
> conn.Open();
>
> Please help me to sort out this problem
> Thanks
> Dharmendra
>
|||Thanks for response.
I have tested it dsn through Odbcad32 . It gets connected with sql
server db but when I try to connect via ASP.NET code. It gives error.
Important thing is that
when I load the application with same code on other machine. It works
fine.
Regards
Dharmendra,
================================================== =======
On May 17, 1:20 am, "William \(Bill\) Vaughn"
<billvaRemoveT...@.betav.com> wrote:
> I expect the reason that no one jumped on this question (which has been
> asked and answered a million times) is that you're clearly not following the
> advice given here and elsewhere. I suggest you do some reading about getting
> connected to SQL Server. The 4th through 6th Editions of my Hitchhiker's
> Guide to Visual Basic and SQL Server discuss how to connect via ODBC to SQL
> Server. However, they do not talk about the fact that recent versions of SQL
> Server require that you enable connectivity before attempting to connect.
> This is covered in detail in my latest book and in whitepapers posted on my
> blog (seewww.betav.com/blogs/billva). And be sure to stop using SA
> credentials to handle your customer's data...
> --
> ____________________________________
> William (Bill) Vaughn
> Author, Mentor, Consultant
> Microsoft MVP
> INETA Speakerwww.betav.com/blog/billvawww.betav.com
> Please reply only to the newsgroup so that others can benefit.
> This posting is provided "AS IS" with no warranties, and confers no rights.
> __________________________________
> Visitwww.hitchhikerguides.netto get more information on my latest book:
> Hitchhiker's Guide to Visual Studio and SQL Server (7th Edition)
> and Hitchhiker's Guide to SQL Server 2005 Compact Edition (EBook)
> ----X---
> "tomar" <dharmendratomar2...@.gmail.com> wrote in message
> news:1179209227.270257.149380@.n59g2000hsh.googlegr oups.com...
>
>
>
>
>
> - Show quoted text -

problem in connection with SQLServer using DSN

Hi all,
I am using OdbcConnection for coonectivity with SQL Server db. My code
is working fine with windows application but in ASP.NET or in
webservice its raising following exception -
"ERROR [08001] [Microsoft][ODBC SQL Server Driver][DBNETLIB]
SQL
Server does not exist or access denied.
ERROR [01000] [Microsoft][ODBC SQL Server Driver]
[DBNETLIB]ConnectionOpen (Connect())."
at System.Data.Odbc.OdbcConnection.Open()
Same code is working fine for Orace DSN.
My code:
OdbcConnection conn = new
OdbcConnection("dsn=MyDsn;uid=sa;pwd=stars;");
conn.Open();
Please help me to sort out this problem
Thanks
DharmendraI expect the reason that no one jumped on this question (which has been
asked and answered a million times) is that you're clearly not following the
advice given here and elsewhere. I suggest you do some reading about getting
connected to SQL Server. The 4th through 6th Editions of my Hitchhiker's
Guide to Visual Basic and SQL Server discuss how to connect via ODBC to SQL
Server. However, they do not talk about the fact that recent versions of SQL
Server require that you enable connectivity before attempting to connect.
This is covered in detail in my latest book and in whitepapers posted on my
blog (see www.betav.com/blogs/billva). And be sure to stop using SA
credentials to handle your customer's data...
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
INETA Speaker
www.betav.com/blog/billva
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
Visit www.hitchhikerguides.net to get more information on my latest book:
Hitchhiker's Guide to Visual Studio and SQL Server (7th Edition)
and Hitchhiker's Guide to SQL Server 2005 Compact Edition (EBook)
----
---
"tomar" <dharmendratomar2000@.gmail.com> wrote in message
news:1179209227.270257.149380@.n59g2000hsh.googlegroups.com...
> Hi all,
> I am using OdbcConnection for coonectivity with SQL Server db. My code
> is working fine with windows application but in ASP.NET or in
> webservice its raising following exception -
> "ERROR [08001] [Microsoft][ODBC SQL Server Driver][DBNETLI
B]SQL
> Server does not exist or access denied.
> ERROR [01000] [Microsoft][ODBC SQL Server Driver]
> [DBNETLIB]ConnectionOpen (Connect())."
> at System.Data.Odbc.OdbcConnection.Open()
> Same code is working fine for Orace DSN.
> My code:
> OdbcConnection conn = new
> OdbcConnection("dsn=MyDsn;uid=sa;pwd=stars;");
> conn.Open();
>
> Please help me to sort out this problem
> Thanks
> Dharmendra
>|||Thanks for response.
I have tested it dsn through Odbcad32 . It gets connected with sql
server db but when I try to connect via ASP.NET code. It gives error.
Important thing is that
when I load the application with same code on other machine. It works
fine.
Regards
Dharmendra,
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D
On May 17, 1:20 am, "William \(Bill\) Vaughn"
<billvaRemoveT...@.betav.com> wrote:
> I expect the reason that no one jumped on this question (which has been
> asked and answered a million times) is that you're clearly not following =
the
> advice given here and elsewhere. I suggest you do some reading about gett=
ing
> connected to SQL Server. The 4th through 6th Editions of my Hitchhiker's
> Guide to Visual Basic and SQL Server discuss how to connect via ODBC to S=
QL
> Server. However, they do not talk about the fact that recent versions of =
SQL
> Server require that you enable connectivity before attempting to connect.
> This is covered in detail in my latest book and in whitepapers posted on =
my
> blog (seewww.betav.com/blogs/billva). And be sure to stop using SA
> credentials to handle your customer's data...
> --
> ____________________________________
> William (Bill) Vaughn
> Author, Mentor, Consultant
> Microsoft MVP
> INETA Speakerwww.betav.com/blog/billvawww.betav.com
> Please reply only to the newsgroup so that others can benefit.
> This posting is provided "AS IS" with no warranties, and confers no right=
s=2E
> __________________________________
> Visitwww.hitchhikerguides.netto get more information on my latest book:
> Hitchhiker's Guide to Visual Studio and SQL Server (7th Edition)
> and Hitchhiker's Guide to SQL Server 2005 Compact Edition (EBook)
> ----=
--=AD---
> "tomar" <dharmendratomar2...@.gmail.com> wrote in message
> news:1179209227.270257.149380@.n59g2000hsh.googlegroups.com...
>
>
>
>
>
>
>
>
> - Show quoted text -sql

problem in connecting two sqlserver

Hi,..Please help me..

We have two server on pulbilc ip A , B in other country, and there replication will working fine. But i want to replicate data from one online server(A) to my local office(in my country) server(C). means from public ip to private(A->C). then some one told me that you have to put both server on public for replication. after the i put my local server(c) on public ip. but now i am facing the problem that i am able to connect C to A(means my office server to other country server) but not able to connect A to C(means other country server to my office server). when i am connecting from A to C then error shows is "defaut sqlserver server does not allow remote connection". I have change all the sql settings..and also done required changes in firewall but not successed. my operating system 2003 and database sql2005........ please help me................ASAP

thanks and regards

Vipin yadav

Can you check the following on machine C (aka: your office server),

Open "SQL Server Configuration Manager",

- expand the "SQL Server 2005 Network Configuration" node

- select "Protocols for MSSQLSERVER"

- enable "TCP/IP" if it's not already enabled and restart MSSQLSERVER service

See if that would solve your problem.

Regards,

Gary

|||He might want to have a look on

http://www.sqlserver2005.de/Screencast/Screencast.aspx?ScreencastId=1

HTH, Jens K. Suessmeyer.

http://www.sqlserver2005.de
|||I have already done this.. still not connecting|||Check http://blogs.msdn.com/sql_protocols/archive/2005/12/22/506607.aspx for troubleshooting connectivity issues with SQL Server 2005. Also, run SQL Server Surface Area Configuration. Click Surface Area Configuration for Services and Connections and check the Remote Connections option. Make sure you have Local and remote connections option selected

Problem in connecting Sql Server to ASP.NET

I installed SQL Server in My P.C using MSDE.
it is acted as local server.
All are working fine (SQL Server and .NET)

when i am trying connect the Sql Server in .net,
i got the Error Message

"Login failed for SA/"

i am trying to Windows authentication mode,
it also gives the error like,

" login failed for Sqlserver name/asp.net"

can any one give the solution for that?

thanks in advance.

regards,
Murugavelyou need to add a user machinename/ASPNET in the users tab under your database sql server enterprise manager.

hth|||am using microsoft biztalk server but i cant see the user tab under my administration? i cant see also the server enterprise manager on my mssql. please help.|||Hi ndinakar ,

i have already created User Account in Enterpriase Manager,It is working fine in SQL SERVER
but still i got the same error message "Login failed" when connecting from ASP.NET

What is the problem?

regds,
Murugavel

Wednesday, March 21, 2012

Problem Having Old TRN Files Deleted

Greetings!
I am working with two different SQL installations on two different servers.
Both have Enterprise Mgr Maintenance Plans that specify that both the
BAK and TRN files should be deleted after 1 day. On the one server,
both deletions occur just fine. On the second, the old BAK's get deleted,
but not the old TRN's. I have reviewed the maintenance plan settings
several times, but can't see anything amiss. Anyone have any ideas?
Would Be Grateful,
Tom
As long as the MP's are not too complex, I would try deleting and rebuilding
them. I have run into similar anomalies before inwhich this was the
solution.
HTH,
Chris
"Tom Glasser" <TomGlasser@.discussions.microsoft.com> wrote in message
news:74302972-33C1-470A-A18A-F273B84B449F@.microsoft.com...
> Greetings!
> I am working with two different SQL installations on two different
servers.
> Both have Enterprise Mgr Maintenance Plans that specify that both the
> BAK and TRN files should be deleted after 1 day. On the one server,
> both deletions occur just fine. On the second, the old BAK's get deleted,
> but not the old TRN's. I have reviewed the maintenance plan settings
> several times, but can't see anything amiss. Anyone have any ideas?
> Would Be Grateful,
> Tom
>

Monday, March 12, 2012

Problem encountered with Filterparameter on SQLDataSource for GridView

Hi,

I have a GridView connected to a sqldatasource control. Everything is working great, updates, paging and filtering with one exception. When the user enters in a name like O'Reilly (with a single quote), the page errors. The error returned is:

Syntax error: Missing operand after 'Reilly' operator.

Here is the definition of the sqldatasource:

<asp:SqlDataSource ID="SqlDataSourcePersons" runat="server"
ConnectionString="<%$ ConnectionStrings:database %>"
SelectCommand="SELECT [Id], [FirstName], [LastName],Email, [PersonTypeId], [WorkerId], [_workerNTId], [Title], [City] FROM [Person]"
FilterExpression="(FirstName like '{0}%') AND (LastName like '{1}%') AND (WorkerId like '{2}%') AND (City like '{3}%')" ProviderName="System.Data.SqlClient">
<FilterParameters>
<asp:ControlParameter ControlID="TextBoxFirstName" Name="FirstName" DefaultValue="%" PropertyName="Text" Type="String" />
<asp:ControlParameter ControlID="TextBoxLastName" Name="LastName" DefaultValue="%" PropertyName="Text" Type="String" />
<asp:ControlParameter ControlID="TextBoxWorkerId" Name="WorkerId" DefaultValue="%" PropertyName="Text" Type="String" />
<asp:ControlParameter ControlID="TextBoxCity" Name="City" DefaultValue="%" PropertyName="Text" Type="String" />
</FilterParameters>
</asp:SqlDataSource>

Any suggestions would be appreciated.

Thank you, Jim

Hey

If a filter expression contains reserved characters, such as a single quotation mark, those characters must be specified using escape characters. For example, the following expression shows how to use an escape character to include an apostrophe in the expression:CompanyName = 'Margie\'s Travel'.

Following link might helpful:

http://www.aspnetresources.com/blog/apostrophe_in_rowfilter.aspx

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbtskfilteringsortingdatausingdataview.asp

Problem dropping replication support

I am working on establishing a merge replication process between SQL Server 2005 and SQL Mobile 2005. I started with a new SQL Server 2005 instance and went through the Sample for SQL Mobile merge replication. So far so good.

Today, I tried to drop all support for replication on my current SQL 2005 test instance, so that I can start from a fresh instance and do another. I used Management Studio to drop all publisher and distribution settings and had several errors occur, where some roles were not allowed to be dropped because they had membership in them.

I dropped all the users that I added to the SQL logins and tried again.

Now I am trying to run the following script:

use AdventureWorks
exec sp_replicationdboption @.dbname = N'AdventureWorks', @.optname = N'merge publish', @.value = N'false', @.ignore_distributor = 'true'

-- Dropping the distribution publishers
exec sp_dropdistpublisher @.publisher = N'XP-MIKED-LAPTOP'
GO

-- Dropping the distribution databases
use master
exec sp_dropdistributiondb @.database = N'distribution'
GO

/****** Uninstalling the server XP-MIKED-LAPTOP as a Distributor. Script Date: 1/14/2006 2:16:29 PM ******/

use master
exec sp_dropdistributor @.no_checks = 1, @.ignore_distributor = 1
GO

The error I am getting from the first batch (sp_replicationdboption) is this:

Msg 208, Level 16, State 1, Procedure sp_MSmergepublishdb, Line 103
Invalid object name 'dbo.sysmergesubscriptions'.

To me it looks like all the publication objects have been already removed from AdventureWorks, but in sysdatabases, the category column still says 4 (merge publication). Since I can't just do this anymore:

UPDATE MASTER.DBO.SYSDATABASES
set category = 0
where dbid=8

I just don't know what I can do at this point. I can't even create a new publication in AdventureWorks because it thinks there is a sysmergepublications table in there and fails when there isn't.

try sp_removedbreplication.

|||

Greg Yvkoff wrote:

try sp_removedbreplication.

Yes, that was exactly it. I got to the end of the KB article 324401 and found that stored proc. It worked like a charm.

Problem displaying report services in client browsers

Hi all

I'm working on a project where we've developed a number of reports using SQL Reporting Services. Everything works fine on the developer PCs and when they cannect to the reporting services URL they can see all the reports listed and everything works fine. When the testers connect, they can see the basic reporting services homepage with the contents and properties tabs. But when they click on the contents tab nothing is displayed.

The tester pcs are locked down and the user profiles wouldnt have the same access rights as the developers who are all local admins on their PCs.

My question is what would prevent the reports from being listed? Is there an activex control that is possibly being blocked by the local security policy for the tester profiles? If this is the case is there anything that can be pre installed on the client pcs to get around this problem?

Any thoughts or assistance that anyone could provide would be grately approeciated

Thanks

Mark
Make sure that these users have the proper roles assigned for the report server. There is Browser, Content Manager (that can pretty much do everything), Report Builder, My Reports and Publisher. There is also a way to define custom roles if needed. The Report server manager can set up users, groups and role assignments.

Wednesday, March 7, 2012

Problem creating script for automating ALTER INDEX maintenance

I'm new to SQL 2005, but have been working with SQL 2000 for quite some time.
In studying for the SQL 2005 test 70-431, I'm working in Chapter 12 of the
Self Study guide, working on managing index fragmentation.
What I am trying to do is collect data from sys.dt_db_index_physical_stats
DMF. I'm creating a temporary table that houses the table name in the format
"schema.table". Then I am attempting to put some of that information into a
cursor, and run an ALTER INDEX on all tables that match the criteria I need.
Following is the script I've come up with:
set ansi_nulls on
set quoted_identifier on
if exists (select * from sys.objects where name='tmp_TableIndex')
begin
drop table tmp_TableIndex
end
create table tmp_TableIndex
(
TableNamevarchar(50),
AvgFragInPercentint,
AvgPageSpaceUsedInPercentint
)
insert tmp_TableIndex(TableName, AvgFragInPercent, AvgPageSpaceUsedInPercent)
select schema_name(sc.schema_id) + '.' + object_name(dt.object_id) as
'TableName',
dt.avg_fragmentation_in_percent,
dt.avg_page_space_used_in_percent
from sys.dm_db_index_physical_stats
(
db_id(db_name()), null, null, null, 'detailed'
)
dt
join sys.objects sc
on sc.object_id=dt.object_id
join sys.indexes si
on si.object_id=dt.object_id
and si.index_id=dt.index_id
where dt.index_ID<>0
and dt.avg_fragmentation_in_percent between 10 and 15
or dt.avg_page_space_used_in_percent between 60 and 75
declare c_Indexreorg cursor
for
select distinct TableName from tmp_TableIndex
open c_Indexreorg
fetch next from c_Indexreorg
while (@.@.FETCH_STATUS=0)
begin
ALTER index ALL on [TableName]
REORGANIZE
fetch next from c_Indexreorg
end
close c_Indexreorg
deallocate c_Indexreorg
The table is created, but I get the following error:
Msg 1088, Level 16, State 9, Line 46
Cannot find the object "TableName" because it does not exist or you do not
have permissions.
I made sure the tablename in the temp table was in schema.tablename format,
but that still didn't help.
What am I doing wrong? Is ALTER INDEX not allowed in a cursor?
Richard Tocci
College Station, TX
Richard
Do you run this script under what account?
"Richard Tocci" <richardftoccijr at hotmail dot com> wrote in message
news:16DD402A-52FC-4F87-AD7E-B66EA78D7FE6@.microsoft.com...
> I'm new to SQL 2005, but have been working with SQL 2000 for quite some
> time.
> In studying for the SQL 2005 test 70-431, I'm working in Chapter 12 of the
> Self Study guide, working on managing index fragmentation.
> What I am trying to do is collect data from sys.dt_db_index_physical_stats
> DMF. I'm creating a temporary table that houses the table name in the
> format
> "schema.table". Then I am attempting to put some of that information into
> a
> cursor, and run an ALTER INDEX on all tables that match the criteria I
> need.
> Following is the script I've come up with:
> set ansi_nulls on
> set quoted_identifier on
> if exists (select * from sys.objects where name='tmp_TableIndex')
> begin
> drop table tmp_TableIndex
> end
> create table tmp_TableIndex
> (
> TableName varchar(50),
> AvgFragInPercent int,
> AvgPageSpaceUsedInPercent int
> )
> insert tmp_TableIndex(TableName, AvgFragInPercent,
> AvgPageSpaceUsedInPercent)
> select schema_name(sc.schema_id) + '.' + object_name(dt.object_id) as
> 'TableName',
> dt.avg_fragmentation_in_percent,
> dt.avg_page_space_used_in_percent
> from sys.dm_db_index_physical_stats
> (
> db_id(db_name()), null, null, null, 'detailed'
> )
> dt
> join sys.objects sc
> on sc.object_id=dt.object_id
> join sys.indexes si
> on si.object_id=dt.object_id
> and si.index_id=dt.index_id
> where dt.index_ID<>0
> and dt.avg_fragmentation_in_percent between 10 and 15
> or dt.avg_page_space_used_in_percent between 60 and 75
> declare c_Indexreorg cursor
> for
> select distinct TableName from tmp_TableIndex
> open c_Indexreorg
> fetch next from c_Indexreorg
> while (@.@.FETCH_STATUS=0)
> begin
> ALTER index ALL on [TableName]
> REORGANIZE
> fetch next from c_Indexreorg
> end
> close c_Indexreorg
> deallocate c_Indexreorg
>
> The table is created, but I get the following error:
> Msg 1088, Level 16, State 9, Line 46
> Cannot find the object "TableName" because it does not exist or you do not
> have permissions.
> I made sure the tablename in the temp table was in schema.tablename
> format,
> but that still didn't help.
> What am I doing wrong? Is ALTER INDEX not allowed in a cursor?
>
> --
> Richard Tocci
> College Station, TX
|||First, I want to recommend an already prepared script for you. See this topic in Books Online:
ms-help://MS.SQLCC.v9/MS.SQLSVR.v9.en/tsqlref9/html/d294dd8e-82d5-4628-aa2d-e57702230613.htm
If you scroll down towards the end, you will find a script that does exactly what you want to do. In
addition, the script will only reoorganize the index if it is fragmented in the first place.
To answer why your script doesn't work:

> fetch next from c_Indexreorg
You don't fetch the column from the cursor into any variables. You should have something like:

> fetch next from c_Indexreorg INTO @.v1, @.v1, ...

> ALTER index ALL on [TableName]
Here you have hardcoded the table name to "TableName", and you probably don't have a table with that
name in the database.
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"Richard Tocci" <richardftoccijr at hotmail dot com> wrote in message
news:16DD402A-52FC-4F87-AD7E-B66EA78D7FE6@.microsoft.com...
> I'm new to SQL 2005, but have been working with SQL 2000 for quite some time.
> In studying for the SQL 2005 test 70-431, I'm working in Chapter 12 of the
> Self Study guide, working on managing index fragmentation.
> What I am trying to do is collect data from sys.dt_db_index_physical_stats
> DMF. I'm creating a temporary table that houses the table name in the format
> "schema.table". Then I am attempting to put some of that information into a
> cursor, and run an ALTER INDEX on all tables that match the criteria I need.
> Following is the script I've come up with:
> set ansi_nulls on
> set quoted_identifier on
> if exists (select * from sys.objects where name='tmp_TableIndex')
> begin
> drop table tmp_TableIndex
> end
> create table tmp_TableIndex
> (
> TableName varchar(50),
> AvgFragInPercent int,
> AvgPageSpaceUsedInPercent int
> )
> insert tmp_TableIndex(TableName, AvgFragInPercent, AvgPageSpaceUsedInPercent)
> select schema_name(sc.schema_id) + '.' + object_name(dt.object_id) as
> 'TableName',
> dt.avg_fragmentation_in_percent,
> dt.avg_page_space_used_in_percent
> from sys.dm_db_index_physical_stats
> (
> db_id(db_name()), null, null, null, 'detailed'
> )
> dt
> join sys.objects sc
> on sc.object_id=dt.object_id
> join sys.indexes si
> on si.object_id=dt.object_id
> and si.index_id=dt.index_id
> where dt.index_ID<>0
> and dt.avg_fragmentation_in_percent between 10 and 15
> or dt.avg_page_space_used_in_percent between 60 and 75
> declare c_Indexreorg cursor
> for
> select distinct TableName from tmp_TableIndex
> open c_Indexreorg
> fetch next from c_Indexreorg
> while (@.@.FETCH_STATUS=0)
> begin
> ALTER index ALL on [TableName]
> REORGANIZE
> fetch next from c_Indexreorg
> end
> close c_Indexreorg
> deallocate c_Indexreorg
>
> The table is created, but I get the following error:
> Msg 1088, Level 16, State 9, Line 46
> Cannot find the object "TableName" because it does not exist or you do not
> have permissions.
> I made sure the tablename in the temp table was in schema.tablename format,
> but that still didn't help.
> What am I doing wrong? Is ALTER INDEX not allowed in a cursor?
>
> --
> Richard Tocci
> College Station, TX
|||Forgive my curiosity, but below seems surprising:

> and dt.avg_fragmentation_in_percent between 10 and 15
Why wouldn't you want to reorg if the fragmentation level is high?
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://sqlblog.com/blogs/tibor_karaszi
"Richard Tocci" <richardftoccijr at hotmail dot com> wrote in message
news:FD361A40-D2EC-448D-A29E-D5A0CBA23A8B@.microsoft.com...[vbcol=seagreen]
> After reading the Books Online help file, I modified the script and now it
> works. Here it is:
> set ansi_nulls on
> set quoted_identifier on
> if exists (select * from sys.objects where name='tmp_TableIndex')
> begin
> drop table tmp_TableIndex
> end
> create table tmp_TableIndex
> (
> TableName varchar(50),
> SchemaName varchar(50),
> AvgFragInPercent int,
> AvgPageSpaceUsedInPercent int
> )
> insert tmp_TableIndex(TableName, SchemaName, AvgFragInPercent,
> AvgPageSpaceUsedInPercent)
> select schema_name(sc.schema_id) as 'SchemaName',
> object_name(dt.object_id) as 'TableName',
> dt.avg_fragmentation_in_percent,
> dt.avg_page_space_used_in_percent
> from sys.dm_db_index_physical_stats
> (
> db_id(db_name()), null, null, null, 'detailed'
> )
> dt
> join sys.objects sc
> on sc.object_id=dt.object_id
> join sys.indexes si
> on si.object_id=dt.object_id
> and si.index_id=dt.index_id
> where dt.index_ID<>0
> and dt.avg_fragmentation_in_percent between 10 and 15
> or dt.avg_page_space_used_in_percent between 60 and 75
> declare @.table_name varchar(50),
> @.schema_name varchar(50),
> @.command varchar(100)
> declare c_Indexreorg cursor
> for
> select distinct SchemaName, TableName from tmp_TableIndex
> open c_Indexreorg
> fetch next from c_Indexreorg into @.table_name, @.schema_name
> while (@.@.FETCH_STATUS=0)
> begin
> set @.command='ALTER index ALL on ' + @.schema_name + '.' + @.table_name + '
> REORGANIZE'
> exec (@.command)
> fetch next from c_Indexreorg
> end
> close c_Indexreorg
> deallocate c_Indexreorg
>
>
> Once I created a couple more variables (I had the @.table_name variable on a
> previous iteration of the script but took it out, thinking it was making it
> to complex), and not slamming the schema name and table name together in my
> temporary table, it seemed to work better.
> THanks for all that responded.
> --
> Richard Tocci
> College Station, TX
>
> "Tibor Karaszi" wrote:
|||Actually, that should be an OR, not an AND. I was going by the 70-431 self
training book, page 453, at the bottom.
Richard Tocci
College Station, TX
"Tibor Karaszi" wrote:

> Forgive my curiosity, but below seems surprising:
>
> Why wouldn't you want to reorg if the fragmentation level is high?
> --
> Tibor Karaszi, SQL Server MVP
> http://www.karaszi.com/sqlserver/default.asp
> http://sqlblog.com/blogs/tibor_karaszi
>
> "Richard Tocci" <richardftoccijr at hotmail dot com> wrote in message
> news:FD361A40-D2EC-448D-A29E-D5A0CBA23A8B@.microsoft.com...
>
|||Guys, what book are you referring to? I am studying for the exam and am
looking for all the help I can get.
Thanks!
-Richard K
"Richard Tocci" wrote:
[vbcol=seagreen]
> Actually, that should be an OR, not an AND. I was going by the 70-431 self
> training book, page 453, at the bottom.
> --
> Richard Tocci
> College Station, TX
>
> "Tibor Karaszi" wrote:

Problem creating script for automating ALTER INDEX maintenance

I'm new to SQL 2005, but have been working with SQL 2000 for quite some time
.
In studying for the SQL 2005 test 70-431, I'm working in Chapter 12 of the
Self Study guide, working on managing index fragmentation.
What I am trying to do is collect data from sys.dt_db_index_physical_stats
DMF. I'm creating a temporary table that houses the table name in the forma
t
"schema.table". Then I am attempting to put some of that information into a
cursor, and run an ALTER INDEX on all tables that match the criteria I need.
Following is the script I've come up with:
set ansi_nulls on
set quoted_identifier on
if exists (select * from sys.objects where name='tmp_TableIndex')
begin
drop table tmp_TableIndex
end
create table tmp_TableIndex
(
TableName varchar(50),
AvgFragInPercent int,
AvgPageSpaceUsedInPercent int
)
insert tmp_TableIndex(TableName, AvgFragInPercent, AvgPageSpaceUsedInPercent
)
select schema_name(sc.schema_id) + '.' + object_name(dt.object_id) as
'TableName',
dt.avg_fragmentation_in_percent,
dt.avg_page_space_used_in_percent
from sys.dm_db_index_physical_stats
(
db_id(db_name()), null, null, null, 'detailed'
)
dt
join sys.objects sc
on sc.object_id=dt.object_id
join sys.indexes si
on si.object_id=dt.object_id
and si.index_id=dt.index_id
where dt.index_ID<>0
and dt.avg_fragmentation_in_percent between 10 and 15
or dt.avg_page_space_used_in_percent between 60 and 75
declare c_Indexreorg cursor
for
select distinct TableName from tmp_TableIndex
open c_Indexreorg
fetch next from c_Indexreorg
while (@.@.FETCH_STATUS=0)
begin
ALTER index ALL on [TableName]
REORGANIZE
fetch next from c_Indexreorg
end
close c_Indexreorg
deallocate c_Indexreorg
The table is created, but I get the following error:
Msg 1088, Level 16, State 9, Line 46
Cannot find the object "TableName" because it does not exist or you do not
have permissions.
I made sure the tablename in the temp table was in schema.tablename format,
but that still didn't help.
What am I doing wrong? Is ALTER INDEX not allowed in a cursor?
Richard Tocci
College Station, TXRichard
Do you run this script under what account?
"Richard Tocci" <richardftoccijr at hotmail dot com> wrote in message
news:16DD402A-52FC-4F87-AD7E-B66EA78D7FE6@.microsoft.com...
> I'm new to SQL 2005, but have been working with SQL 2000 for quite some
> time.
> In studying for the SQL 2005 test 70-431, I'm working in Chapter 12 of the
> Self Study guide, working on managing index fragmentation.
> What I am trying to do is collect data from sys.dt_db_index_physical_stats
> DMF. I'm creating a temporary table that houses the table name in the
> format
> "schema.table". Then I am attempting to put some of that information into
> a
> cursor, and run an ALTER INDEX on all tables that match the criteria I
> need.
> Following is the script I've come up with:
> set ansi_nulls on
> set quoted_identifier on
> if exists (select * from sys.objects where name='tmp_TableIndex')
> begin
> drop table tmp_TableIndex
> end
> create table tmp_TableIndex
> (
> TableName varchar(50),
> AvgFragInPercent int,
> AvgPageSpaceUsedInPercent int
> )
> insert tmp_TableIndex(TableName, AvgFragInPercent,
> AvgPageSpaceUsedInPercent)
> select schema_name(sc.schema_id) + '.' + object_name(dt.object_id) as
> 'TableName',
> dt.avg_fragmentation_in_percent,
> dt.avg_page_space_used_in_percent
> from sys.dm_db_index_physical_stats
> (
> db_id(db_name()), null, null, null, 'detailed'
> )
> dt
> join sys.objects sc
> on sc.object_id=dt.object_id
> join sys.indexes si
> on si.object_id=dt.object_id
> and si.index_id=dt.index_id
> where dt.index_ID<>0
> and dt.avg_fragmentation_in_percent between 10 and 15
> or dt.avg_page_space_used_in_percent between 60 and 75
> declare c_Indexreorg cursor
> for
> select distinct TableName from tmp_TableIndex
> open c_Indexreorg
> fetch next from c_Indexreorg
> while (@.@.FETCH_STATUS=0)
> begin
> ALTER index ALL on [TableName]
> REORGANIZE
> fetch next from c_Indexreorg
> end
> close c_Indexreorg
> deallocate c_Indexreorg
>
> The table is created, but I get the following error:
> Msg 1088, Level 16, State 9, Line 46
> Cannot find the object "TableName" because it does not exist or you do not
> have permissions.
> I made sure the tablename in the temp table was in schema.tablename
> format,
> but that still didn't help.
> What am I doing wrong? Is ALTER INDEX not allowed in a cursor?
>
> --
> Richard Tocci
> college Station, TX|||First, I want to recommend an already prepared script for you. See this topi
c in Books Online:
ms-help://MS.SQLCC.v9/MS.SQLSVR.v9.en/tsqlref9/html/d294dd8e-82d5-4628-aa2d-
e57702230613.htm
If you scroll down towards the end, you will find a script that does exactly
what you want to do. In
addition, the script will only reoorganize the index if it is fragmented in
the first place.
To answer why your script doesn't work:

> fetch next from c_Indexreorg
You don't fetch the column from the cursor into any variables. You should ha
ve something like:

> fetch next from c_Indexreorg INTO @.v1, @.v1, ...

> ALTER index ALL on [TableName]
Here you have hardcoded the table name to "TableName", and you probably don'
t have a table with that
name in the database.
--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"Richard Tocci" <richardftoccijr at hotmail dot com> wrote in message
news:16DD402A-52FC-4F87-AD7E-B66EA78D7FE6@.microsoft.com...
> I'm new to SQL 2005, but have been working with SQL 2000 for quite some ti
me.
> In studying for the SQL 2005 test 70-431, I'm working in Chapter 12 of the
> Self Study guide, working on managing index fragmentation.
> What I am trying to do is collect data from sys.dt_db_index_physical_stats
> DMF. I'm creating a temporary table that houses the table name in the for
mat
> "schema.table". Then I am attempting to put some of that information into
a
> cursor, and run an ALTER INDEX on all tables that match the criteria I nee
d.
> Following is the script I've come up with:
> set ansi_nulls on
> set quoted_identifier on
> if exists (select * from sys.objects where name='tmp_TableIndex')
> begin
> drop table tmp_TableIndex
> end
> create table tmp_TableIndex
> (
> TableName varchar(50),
> AvgFragInPercent int,
> AvgPageSpaceUsedInPercent int
> )
> insert tmp_TableIndex(TableName, AvgFragInPercent, AvgPageSpaceUsedInPerce
nt)
> select schema_name(sc.schema_id) + '.' + object_name(dt.object_id) as
> 'TableName',
> dt.avg_fragmentation_in_percent,
> dt.avg_page_space_used_in_percent
> from sys.dm_db_index_physical_stats
> (
> db_id(db_name()), null, null, null, 'detailed'
> )
> dt
> join sys.objects sc
> on sc.object_id=dt.object_id
> join sys.indexes si
> on si.object_id=dt.object_id
> and si.index_id=dt.index_id
> where dt.index_ID<>0
> and dt.avg_fragmentation_in_percent between 10 and 15
> or dt.avg_page_space_used_in_percent between 60 and 75
> declare c_Indexreorg cursor
> for
> select distinct TableName from tmp_TableIndex
> open c_Indexreorg
> fetch next from c_Indexreorg
> while (@.@.FETCH_STATUS=0)
> begin
> ALTER index ALL on [TableName]
> REORGANIZE
> fetch next from c_Indexreorg
> end
> close c_Indexreorg
> deallocate c_Indexreorg
>
> The table is created, but I get the following error:
> Msg 1088, Level 16, State 9, Line 46
> Cannot find the object "TableName" because it does not exist or you do not
> have permissions.
> I made sure the tablename in the temp table was in schema.tablename format
,
> but that still didn't help.
> What am I doing wrong? Is ALTER INDEX not allowed in a cursor?
>
> --
> Richard Tocci
> college Station, TX|||After reading the Books Online help file, I modified the script and now it
works. Here it is:
set ansi_nulls on
set quoted_identifier on
if exists (select * from sys.objects where name='tmp_TableIndex')
begin
drop table tmp_TableIndex
end
create table tmp_TableIndex
(
TableName varchar(50),
SchemaName varchar(50),
AvgFragInPercent int,
AvgPageSpaceUsedInPercent int
)
insert tmp_TableIndex(TableName, SchemaName, AvgFragInPercent,
AvgPageSpaceUsedInPercent)
select schema_name(sc.schema_id) as 'SchemaName',
object_name(dt.object_id) as 'TableName',
dt.avg_fragmentation_in_percent,
dt.avg_page_space_used_in_percent
from sys.dm_db_index_physical_stats
(
db_id(db_name()), null, null, null, 'detailed'
)
dt
join sys.objects sc
on sc.object_id=dt.object_id
join sys.indexes si
on si.object_id=dt.object_id
and si.index_id=dt.index_id
where dt.index_ID<>0
and dt.avg_fragmentation_in_percent between 10 and 15
or dt.avg_page_space_used_in_percent between 60 and 75
declare @.table_name varchar(50),
@.schema_name varchar(50),
@.command varchar(100)
declare c_Indexreorg cursor
for
select distinct SchemaName, TableName from tmp_TableIndex
open c_Indexreorg
fetch next from c_Indexreorg into @.table_name, @.schema_name
while (@.@.FETCH_STATUS=0)
begin
set @.command='ALTER index ALL on ' + @.schema_name + '.' + @.table_name + '
REORGANIZE'
exec (@.command)
fetch next from c_Indexreorg
end
close c_Indexreorg
deallocate c_Indexreorg
Once I created a couple more variables (I had the @.table_name variable on a
previous iteration of the script but took it out, thinking it was making it
to complex), and not slamming the schema name and table name together in my
temporary table, it seemed to work better.
THanks for all that responded.
--
Richard Tocci
College Station, TX
"Tibor Karaszi" wrote:

> First, I want to recommend an already prepared script for you. See this to
pic in Books Online:
> ms-help://MS.SQLCC.v9/MS.SQLSVR.v9.en/tsqlref9/html/d294dd8e-82d5-4628-aa2
d-e57702230613.htm
> If you scroll down towards the end, you will find a script that does exact
ly what you want to do. In
> addition, the script will only reoorganize the index if it is fragmented i
n the first place.
>
> To answer why your script doesn't work:
>
> You don't fetch the column from the cursor into any variables. You should
have something like:
>
>
> Here you have hardcoded the table name to "TableName", and you probably do
n't have a table with that
> name in the database.
> --
> Tibor Karaszi, SQL Server MVP
> http://www.karaszi.com/sqlserver/default.asp
> http://www.solidqualitylearning.com/
>
> "Richard Tocci" <richardftoccijr at hotmail dot com> wrote in message
> news:16DD402A-52FC-4F87-AD7E-B66EA78D7FE6@.microsoft.com...
>
>|||Forgive my curiosity, but below seems surprising:

> and dt.avg_fragmentation_in_percent between 10 and 15
Why wouldn't you want to reorg if the fragmentation level is high?
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://sqlblog.com/blogs/tibor_karaszi
"Richard Tocci" <richardftoccijr at hotmail dot com> wrote in message
news:FD361A40-D2EC-448D-A29E-D5A0CBA23A8B@.microsoft.com...[vbcol=seagreen]
> After reading the Books Online help file, I modified the script and now it
> works. Here it is:
> set ansi_nulls on
> set quoted_identifier on
> if exists (select * from sys.objects where name='tmp_TableIndex')
> begin
> drop table tmp_TableIndex
> end
> create table tmp_TableIndex
> (
> TableName varchar(50),
> SchemaName varchar(50),
> AvgFragInPercent int,
> AvgPageSpaceUsedInPercent int
> )
> insert tmp_TableIndex(TableName, SchemaName, AvgFragInPercent,
> AvgPageSpaceUsedInPercent)
> select schema_name(sc.schema_id) as 'SchemaName',
> object_name(dt.object_id) as 'TableName',
> dt.avg_fragmentation_in_percent,
> dt.avg_page_space_used_in_percent
> from sys.dm_db_index_physical_stats
> (
> db_id(db_name()), null, null, null, 'detailed'
> )
> dt
> join sys.objects sc
> on sc.object_id=dt.object_id
> join sys.indexes si
> on si.object_id=dt.object_id
> and si.index_id=dt.index_id
> where dt.index_ID<>0
> and dt.avg_fragmentation_in_percent between 10 and 15
> or dt.avg_page_space_used_in_percent between 60 and 75
> declare @.table_name varchar(50),
> @.schema_name varchar(50),
> @.command varchar(100)
> declare c_Indexreorg cursor
> for
> select distinct SchemaName, TableName from tmp_TableIndex
> open c_Indexreorg
> fetch next from c_Indexreorg into @.table_name, @.schema_name
> while (@.@.FETCH_STATUS=0)
> begin
> set @.command='ALTER index ALL on ' + @.schema_name + '.' + @.table_name + '
> REORGANIZE'
> exec (@.command)
> fetch next from c_Indexreorg
> end
> close c_Indexreorg
> deallocate c_Indexreorg
>
>
> Once I created a couple more variables (I had the @.table_name variable on
a
> previous iteration of the script but took it out, thinking it was making i
t
> to complex), and not slamming the schema name and table name together in m
y
> temporary table, it seemed to work better.
> THanks for all that responded.
> --
> Richard Tocci
> college Station, TX
>
> "Tibor Karaszi" wrote:
>|||Actually, that should be an OR, not an AND. I was going by the 70-431 self
training book, page 453, at the bottom.
--
Richard Tocci
College Station, TX
"Tibor Karaszi" wrote:

> Forgive my curiosity, but below seems surprising:
>
> Why wouldn't you want to reorg if the fragmentation level is high?
> --
> Tibor Karaszi, SQL Server MVP
> http://www.karaszi.com/sqlserver/default.asp
> http://sqlblog.com/blogs/tibor_karaszi
>
> "Richard Tocci" <richardftoccijr at hotmail dot com> wrote in message
> news:FD361A40-D2EC-448D-A29E-D5A0CBA23A8B@.microsoft.com...
>|||Guys, what book are you referring to? I am studying for the exam and am
looking for all the help I can get.
Thanks!
-Richard K
"Richard Tocci" wrote:
[vbcol=seagreen]
> Actually, that should be an OR, not an AND. I was going by the 70-431 sel
f
> training book, page 453, at the bottom.
> --
> Richard Tocci
> college Station, TX
>
> "Tibor Karaszi" wrote:
>

Problem creating script for automating ALTER INDEX maintenance

I'm new to SQL 2005, but have been working with SQL 2000 for quite some time.
In studying for the SQL 2005 test 70-431, I'm working in Chapter 12 of the
Self Study guide, working on managing index fragmentation.
What I am trying to do is collect data from sys.dt_db_index_physical_stats
DMF. I'm creating a temporary table that houses the table name in the format
"schema.table". Then I am attempting to put some of that information into a
cursor, and run an ALTER INDEX on all tables that match the criteria I need.
Following is the script I've come up with:
set ansi_nulls on
set quoted_identifier on
if exists (select * from sys.objects where name='tmp_TableIndex')
begin
drop table tmp_TableIndex
end
create table tmp_TableIndex
(
TableName varchar(50),
AvgFragInPercent int,
AvgPageSpaceUsedInPercent int
)
insert tmp_TableIndex(TableName, AvgFragInPercent, AvgPageSpaceUsedInPercent)
select schema_name(sc.schema_id) + '.' + object_name(dt.object_id) as
'TableName',
dt.avg_fragmentation_in_percent,
dt.avg_page_space_used_in_percent
from sys.dm_db_index_physical_stats
(
db_id(db_name()), null, null, null, 'detailed'
)
dt
join sys.objects sc
on sc.object_id=dt.object_id
join sys.indexes si
on si.object_id=dt.object_id
and si.index_id=dt.index_id
where dt.index_ID<>0
and dt.avg_fragmentation_in_percent between 10 and 15
or dt.avg_page_space_used_in_percent between 60 and 75
declare c_Indexreorg cursor
for
select distinct TableName from tmp_TableIndex
open c_Indexreorg
fetch next from c_Indexreorg
while (@.@.FETCH_STATUS=0)
begin
ALTER index ALL on [TableName]
REORGANIZE
fetch next from c_Indexreorg
end
close c_Indexreorg
deallocate c_Indexreorg
The table is created, but I get the following error:
Msg 1088, Level 16, State 9, Line 46
Cannot find the object "TableName" because it does not exist or you do not
have permissions.
I made sure the tablename in the temp table was in schema.tablename format,
but that still didn't help.
What am I doing wrong? Is ALTER INDEX not allowed in a cursor?
--
Richard Tocci
College Station, TXRichard
Do you run this script under what account?
"Richard Tocci" <richardftoccijr at hotmail dot com> wrote in message
news:16DD402A-52FC-4F87-AD7E-B66EA78D7FE6@.microsoft.com...
> I'm new to SQL 2005, but have been working with SQL 2000 for quite some
> time.
> In studying for the SQL 2005 test 70-431, I'm working in Chapter 12 of the
> Self Study guide, working on managing index fragmentation.
> What I am trying to do is collect data from sys.dt_db_index_physical_stats
> DMF. I'm creating a temporary table that houses the table name in the
> format
> "schema.table". Then I am attempting to put some of that information into
> a
> cursor, and run an ALTER INDEX on all tables that match the criteria I
> need.
> Following is the script I've come up with:
> set ansi_nulls on
> set quoted_identifier on
> if exists (select * from sys.objects where name='tmp_TableIndex')
> begin
> drop table tmp_TableIndex
> end
> create table tmp_TableIndex
> (
> TableName varchar(50),
> AvgFragInPercent int,
> AvgPageSpaceUsedInPercent int
> )
> insert tmp_TableIndex(TableName, AvgFragInPercent,
> AvgPageSpaceUsedInPercent)
> select schema_name(sc.schema_id) + '.' + object_name(dt.object_id) as
> 'TableName',
> dt.avg_fragmentation_in_percent,
> dt.avg_page_space_used_in_percent
> from sys.dm_db_index_physical_stats
> (
> db_id(db_name()), null, null, null, 'detailed'
> )
> dt
> join sys.objects sc
> on sc.object_id=dt.object_id
> join sys.indexes si
> on si.object_id=dt.object_id
> and si.index_id=dt.index_id
> where dt.index_ID<>0
> and dt.avg_fragmentation_in_percent between 10 and 15
> or dt.avg_page_space_used_in_percent between 60 and 75
> declare c_Indexreorg cursor
> for
> select distinct TableName from tmp_TableIndex
> open c_Indexreorg
> fetch next from c_Indexreorg
> while (@.@.FETCH_STATUS=0)
> begin
> ALTER index ALL on [TableName]
> REORGANIZE
> fetch next from c_Indexreorg
> end
> close c_Indexreorg
> deallocate c_Indexreorg
>
> The table is created, but I get the following error:
> Msg 1088, Level 16, State 9, Line 46
> Cannot find the object "TableName" because it does not exist or you do not
> have permissions.
> I made sure the tablename in the temp table was in schema.tablename
> format,
> but that still didn't help.
> What am I doing wrong? Is ALTER INDEX not allowed in a cursor?
>
> --
> Richard Tocci
> College Station, TX|||First, I want to recommend an already prepared script for you. See this topic in Books Online:
ms-help://MS.SQLCC.v9/MS.SQLSVR.v9.en/tsqlref9/html/d294dd8e-82d5-4628-aa2d-e57702230613.htm
If you scroll down towards the end, you will find a script that does exactly what you want to do. In
addition, the script will only reoorganize the index if it is fragmented in the first place.
To answer why your script doesn't work:
> fetch next from c_Indexreorg
You don't fetch the column from the cursor into any variables. You should have something like:
> fetch next from c_Indexreorg INTO @.v1, @.v1, ...
> ALTER index ALL on [TableName]
Here you have hardcoded the table name to "TableName", and you probably don't have a table with that
name in the database.
--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"Richard Tocci" <richardftoccijr at hotmail dot com> wrote in message
news:16DD402A-52FC-4F87-AD7E-B66EA78D7FE6@.microsoft.com...
> I'm new to SQL 2005, but have been working with SQL 2000 for quite some time.
> In studying for the SQL 2005 test 70-431, I'm working in Chapter 12 of the
> Self Study guide, working on managing index fragmentation.
> What I am trying to do is collect data from sys.dt_db_index_physical_stats
> DMF. I'm creating a temporary table that houses the table name in the format
> "schema.table". Then I am attempting to put some of that information into a
> cursor, and run an ALTER INDEX on all tables that match the criteria I need.
> Following is the script I've come up with:
> set ansi_nulls on
> set quoted_identifier on
> if exists (select * from sys.objects where name='tmp_TableIndex')
> begin
> drop table tmp_TableIndex
> end
> create table tmp_TableIndex
> (
> TableName varchar(50),
> AvgFragInPercent int,
> AvgPageSpaceUsedInPercent int
> )
> insert tmp_TableIndex(TableName, AvgFragInPercent, AvgPageSpaceUsedInPercent)
> select schema_name(sc.schema_id) + '.' + object_name(dt.object_id) as
> 'TableName',
> dt.avg_fragmentation_in_percent,
> dt.avg_page_space_used_in_percent
> from sys.dm_db_index_physical_stats
> (
> db_id(db_name()), null, null, null, 'detailed'
> )
> dt
> join sys.objects sc
> on sc.object_id=dt.object_id
> join sys.indexes si
> on si.object_id=dt.object_id
> and si.index_id=dt.index_id
> where dt.index_ID<>0
> and dt.avg_fragmentation_in_percent between 10 and 15
> or dt.avg_page_space_used_in_percent between 60 and 75
> declare c_Indexreorg cursor
> for
> select distinct TableName from tmp_TableIndex
> open c_Indexreorg
> fetch next from c_Indexreorg
> while (@.@.FETCH_STATUS=0)
> begin
> ALTER index ALL on [TableName]
> REORGANIZE
> fetch next from c_Indexreorg
> end
> close c_Indexreorg
> deallocate c_Indexreorg
>
> The table is created, but I get the following error:
> Msg 1088, Level 16, State 9, Line 46
> Cannot find the object "TableName" because it does not exist or you do not
> have permissions.
> I made sure the tablename in the temp table was in schema.tablename format,
> but that still didn't help.
> What am I doing wrong? Is ALTER INDEX not allowed in a cursor?
>
> --
> Richard Tocci
> College Station, TX|||After reading the Books Online help file, I modified the script and now it
works. Here it is:
set ansi_nulls on
set quoted_identifier on
if exists (select * from sys.objects where name='tmp_TableIndex')
begin
drop table tmp_TableIndex
end
create table tmp_TableIndex
(
TableName varchar(50),
SchemaName varchar(50),
AvgFragInPercent int,
AvgPageSpaceUsedInPercent int
)
insert tmp_TableIndex(TableName, SchemaName, AvgFragInPercent,
AvgPageSpaceUsedInPercent)
select schema_name(sc.schema_id) as 'SchemaName',
object_name(dt.object_id) as 'TableName',
dt.avg_fragmentation_in_percent,
dt.avg_page_space_used_in_percent
from sys.dm_db_index_physical_stats
(
db_id(db_name()), null, null, null, 'detailed'
)
dt
join sys.objects sc
on sc.object_id=dt.object_id
join sys.indexes si
on si.object_id=dt.object_id
and si.index_id=dt.index_id
where dt.index_ID<>0
and dt.avg_fragmentation_in_percent between 10 and 15
or dt.avg_page_space_used_in_percent between 60 and 75
declare @.table_name varchar(50),
@.schema_name varchar(50),
@.command varchar(100)
declare c_Indexreorg cursor
for
select distinct SchemaName, TableName from tmp_TableIndex
open c_Indexreorg
fetch next from c_Indexreorg into @.table_name, @.schema_name
while (@.@.FETCH_STATUS=0)
begin
set @.command='ALTER index ALL on ' + @.schema_name + '.' + @.table_name + '
REORGANIZE'
exec (@.command)
fetch next from c_Indexreorg
end
close c_Indexreorg
deallocate c_Indexreorg
Once I created a couple more variables (I had the @.table_name variable on a
previous iteration of the script but took it out, thinking it was making it
to complex), and not slamming the schema name and table name together in my
temporary table, it seemed to work better.
THanks for all that responded.
--
Richard Tocci
College Station, TX
"Tibor Karaszi" wrote:
> First, I want to recommend an already prepared script for you. See this topic in Books Online:
> ms-help://MS.SQLCC.v9/MS.SQLSVR.v9.en/tsqlref9/html/d294dd8e-82d5-4628-aa2d-e57702230613.htm
> If you scroll down towards the end, you will find a script that does exactly what you want to do. In
> addition, the script will only reoorganize the index if it is fragmented in the first place.
>
> To answer why your script doesn't work:
> > fetch next from c_Indexreorg
> You don't fetch the column from the cursor into any variables. You should have something like:
> > fetch next from c_Indexreorg INTO @.v1, @.v1, ...
> > ALTER index ALL on [TableName]
> Here you have hardcoded the table name to "TableName", and you probably don't have a table with that
> name in the database.
> --
> Tibor Karaszi, SQL Server MVP
> http://www.karaszi.com/sqlserver/default.asp
> http://www.solidqualitylearning.com/
>
> "Richard Tocci" <richardftoccijr at hotmail dot com> wrote in message
> news:16DD402A-52FC-4F87-AD7E-B66EA78D7FE6@.microsoft.com...
> > I'm new to SQL 2005, but have been working with SQL 2000 for quite some time.
> > In studying for the SQL 2005 test 70-431, I'm working in Chapter 12 of the
> > Self Study guide, working on managing index fragmentation.
> >
> > What I am trying to do is collect data from sys.dt_db_index_physical_stats
> > DMF. I'm creating a temporary table that houses the table name in the format
> > "schema.table". Then I am attempting to put some of that information into a
> > cursor, and run an ALTER INDEX on all tables that match the criteria I need.
> >
> > Following is the script I've come up with:
> >
> > set ansi_nulls on
> > set quoted_identifier on
> >
> > if exists (select * from sys.objects where name='tmp_TableIndex')
> > begin
> > drop table tmp_TableIndex
> > end
> >
> > create table tmp_TableIndex
> > (
> > TableName varchar(50),
> > AvgFragInPercent int,
> > AvgPageSpaceUsedInPercent int
> > )
> >
> > insert tmp_TableIndex(TableName, AvgFragInPercent, AvgPageSpaceUsedInPercent)
> > select schema_name(sc.schema_id) + '.' + object_name(dt.object_id) as
> > 'TableName',
> > dt.avg_fragmentation_in_percent,
> > dt.avg_page_space_used_in_percent
> > from sys.dm_db_index_physical_stats
> > (
> > db_id(db_name()), null, null, null, 'detailed'
> > )
> > dt
> > join sys.objects sc
> > on sc.object_id=dt.object_id
> > join sys.indexes si
> > on si.object_id=dt.object_id
> > and si.index_id=dt.index_id
> > where dt.index_ID<>0
> > and dt.avg_fragmentation_in_percent between 10 and 15
> > or dt.avg_page_space_used_in_percent between 60 and 75
> >
> > declare c_Indexreorg cursor
> > for
> > select distinct TableName from tmp_TableIndex
> >
> > open c_Indexreorg
> >
> > fetch next from c_Indexreorg
> >
> > while (@.@.FETCH_STATUS=0)
> > begin
> > ALTER index ALL on [TableName]
> > REORGANIZE
> > fetch next from c_Indexreorg
> > end
> >
> > close c_Indexreorg
> >
> > deallocate c_Indexreorg
> >
> >
> > The table is created, but I get the following error:
> > Msg 1088, Level 16, State 9, Line 46
> > Cannot find the object "TableName" because it does not exist or you do not
> > have permissions.
> >
> > I made sure the tablename in the temp table was in schema.tablename format,
> > but that still didn't help.
> >
> > What am I doing wrong? Is ALTER INDEX not allowed in a cursor?
> >
> >
> >
> > --
> > Richard Tocci
> > College Station, TX
>
>|||Forgive my curiosity, but below seems surprising:
> and dt.avg_fragmentation_in_percent between 10 and 15
Why wouldn't you want to reorg if the fragmentation level is high?
--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://sqlblog.com/blogs/tibor_karaszi
"Richard Tocci" <richardftoccijr at hotmail dot com> wrote in message
news:FD361A40-D2EC-448D-A29E-D5A0CBA23A8B@.microsoft.com...
> After reading the Books Online help file, I modified the script and now it
> works. Here it is:
> set ansi_nulls on
> set quoted_identifier on
> if exists (select * from sys.objects where name='tmp_TableIndex')
> begin
> drop table tmp_TableIndex
> end
> create table tmp_TableIndex
> (
> TableName varchar(50),
> SchemaName varchar(50),
> AvgFragInPercent int,
> AvgPageSpaceUsedInPercent int
> )
> insert tmp_TableIndex(TableName, SchemaName, AvgFragInPercent,
> AvgPageSpaceUsedInPercent)
> select schema_name(sc.schema_id) as 'SchemaName',
> object_name(dt.object_id) as 'TableName',
> dt.avg_fragmentation_in_percent,
> dt.avg_page_space_used_in_percent
> from sys.dm_db_index_physical_stats
> (
> db_id(db_name()), null, null, null, 'detailed'
> )
> dt
> join sys.objects sc
> on sc.object_id=dt.object_id
> join sys.indexes si
> on si.object_id=dt.object_id
> and si.index_id=dt.index_id
> where dt.index_ID<>0
> and dt.avg_fragmentation_in_percent between 10 and 15
> or dt.avg_page_space_used_in_percent between 60 and 75
> declare @.table_name varchar(50),
> @.schema_name varchar(50),
> @.command varchar(100)
> declare c_Indexreorg cursor
> for
> select distinct SchemaName, TableName from tmp_TableIndex
> open c_Indexreorg
> fetch next from c_Indexreorg into @.table_name, @.schema_name
> while (@.@.FETCH_STATUS=0)
> begin
> set @.command='ALTER index ALL on ' + @.schema_name + '.' + @.table_name + '
> REORGANIZE'
> exec (@.command)
> fetch next from c_Indexreorg
> end
> close c_Indexreorg
> deallocate c_Indexreorg
>
>
> Once I created a couple more variables (I had the @.table_name variable on a
> previous iteration of the script but took it out, thinking it was making it
> to complex), and not slamming the schema name and table name together in my
> temporary table, it seemed to work better.
> THanks for all that responded.
> --
> Richard Tocci
> College Station, TX
>
> "Tibor Karaszi" wrote:
>> First, I want to recommend an already prepared script for you. See this topic in Books Online:
>> ms-help://MS.SQLCC.v9/MS.SQLSVR.v9.en/tsqlref9/html/d294dd8e-82d5-4628-aa2d-e57702230613.htm
>> If you scroll down towards the end, you will find a script that does exactly what you want to do.
>> In
>> addition, the script will only reoorganize the index if it is fragmented in the first place.
>>
>> To answer why your script doesn't work:
>> > fetch next from c_Indexreorg
>> You don't fetch the column from the cursor into any variables. You should have something like:
>> > fetch next from c_Indexreorg INTO @.v1, @.v1, ...
>> > ALTER index ALL on [TableName]
>> Here you have hardcoded the table name to "TableName", and you probably don't have a table with
>> that
>> name in the database.
>> --
>> Tibor Karaszi, SQL Server MVP
>> http://www.karaszi.com/sqlserver/default.asp
>> http://www.solidqualitylearning.com/
>>
>> "Richard Tocci" <richardftoccijr at hotmail dot com> wrote in message
>> news:16DD402A-52FC-4F87-AD7E-B66EA78D7FE6@.microsoft.com...
>> > I'm new to SQL 2005, but have been working with SQL 2000 for quite some time.
>> > In studying for the SQL 2005 test 70-431, I'm working in Chapter 12 of the
>> > Self Study guide, working on managing index fragmentation.
>> >
>> > What I am trying to do is collect data from sys.dt_db_index_physical_stats
>> > DMF. I'm creating a temporary table that houses the table name in the format
>> > "schema.table". Then I am attempting to put some of that information into a
>> > cursor, and run an ALTER INDEX on all tables that match the criteria I need.
>> >
>> > Following is the script I've come up with:
>> >
>> > set ansi_nulls on
>> > set quoted_identifier on
>> >
>> > if exists (select * from sys.objects where name='tmp_TableIndex')
>> > begin
>> > drop table tmp_TableIndex
>> > end
>> >
>> > create table tmp_TableIndex
>> > (
>> > TableName varchar(50),
>> > AvgFragInPercent int,
>> > AvgPageSpaceUsedInPercent int
>> > )
>> >
>> > insert tmp_TableIndex(TableName, AvgFragInPercent, AvgPageSpaceUsedInPercent)
>> > select schema_name(sc.schema_id) + '.' + object_name(dt.object_id) as
>> > 'TableName',
>> > dt.avg_fragmentation_in_percent,
>> > dt.avg_page_space_used_in_percent
>> > from sys.dm_db_index_physical_stats
>> > (
>> > db_id(db_name()), null, null, null, 'detailed'
>> > )
>> > dt
>> > join sys.objects sc
>> > on sc.object_id=dt.object_id
>> > join sys.indexes si
>> > on si.object_id=dt.object_id
>> > and si.index_id=dt.index_id
>> > where dt.index_ID<>0
>> > and dt.avg_fragmentation_in_percent between 10 and 15
>> > or dt.avg_page_space_used_in_percent between 60 and 75
>> >
>> > declare c_Indexreorg cursor
>> > for
>> > select distinct TableName from tmp_TableIndex
>> >
>> > open c_Indexreorg
>> >
>> > fetch next from c_Indexreorg
>> >
>> > while (@.@.FETCH_STATUS=0)
>> > begin
>> > ALTER index ALL on [TableName]
>> > REORGANIZE
>> > fetch next from c_Indexreorg
>> > end
>> >
>> > close c_Indexreorg
>> >
>> > deallocate c_Indexreorg
>> >
>> >
>> > The table is created, but I get the following error:
>> > Msg 1088, Level 16, State 9, Line 46
>> > Cannot find the object "TableName" because it does not exist or you do not
>> > have permissions.
>> >
>> > I made sure the tablename in the temp table was in schema.tablename format,
>> > but that still didn't help.
>> >
>> > What am I doing wrong? Is ALTER INDEX not allowed in a cursor?
>> >
>> >
>> >
>> > --
>> > Richard Tocci
>> > College Station, TX
>>|||Actually, that should be an OR, not an AND. I was going by the 70-431 self
training book, page 453, at the bottom.
--
Richard Tocci
College Station, TX
"Tibor Karaszi" wrote:
> Forgive my curiosity, but below seems surprising:
> > and dt.avg_fragmentation_in_percent between 10 and 15
> Why wouldn't you want to reorg if the fragmentation level is high?
> --
> Tibor Karaszi, SQL Server MVP
> http://www.karaszi.com/sqlserver/default.asp
> http://sqlblog.com/blogs/tibor_karaszi
>
> "Richard Tocci" <richardftoccijr at hotmail dot com> wrote in message
> news:FD361A40-D2EC-448D-A29E-D5A0CBA23A8B@.microsoft.com...
> > After reading the Books Online help file, I modified the script and now it
> > works. Here it is:
> >
> > set ansi_nulls on
> > set quoted_identifier on
> >
> > if exists (select * from sys.objects where name='tmp_TableIndex')
> > begin
> > drop table tmp_TableIndex
> > end
> >
> > create table tmp_TableIndex
> > (
> > TableName varchar(50),
> > SchemaName varchar(50),
> > AvgFragInPercent int,
> > AvgPageSpaceUsedInPercent int
> > )
> >
> > insert tmp_TableIndex(TableName, SchemaName, AvgFragInPercent,
> > AvgPageSpaceUsedInPercent)
> > select schema_name(sc.schema_id) as 'SchemaName',
> > object_name(dt.object_id) as 'TableName',
> > dt.avg_fragmentation_in_percent,
> > dt.avg_page_space_used_in_percent
> > from sys.dm_db_index_physical_stats
> > (
> > db_id(db_name()), null, null, null, 'detailed'
> > )
> > dt
> > join sys.objects sc
> > on sc.object_id=dt.object_id
> > join sys.indexes si
> > on si.object_id=dt.object_id
> > and si.index_id=dt.index_id
> > where dt.index_ID<>0
> > and dt.avg_fragmentation_in_percent between 10 and 15
> > or dt.avg_page_space_used_in_percent between 60 and 75
> >
> > declare @.table_name varchar(50),
> > @.schema_name varchar(50),
> > @.command varchar(100)
> >
> > declare c_Indexreorg cursor
> > for
> > select distinct SchemaName, TableName from tmp_TableIndex
> >
> > open c_Indexreorg
> >
> > fetch next from c_Indexreorg into @.table_name, @.schema_name
> >
> > while (@.@.FETCH_STATUS=0)
> > begin
> > set @.command='ALTER index ALL on ' + @.schema_name + '.' + @.table_name + '
> > REORGANIZE'
> > exec (@.command)
> > fetch next from c_Indexreorg
> > end
> >
> > close c_Indexreorg
> >
> > deallocate c_Indexreorg
> >
> >
> >
> >
> > Once I created a couple more variables (I had the @.table_name variable on a
> > previous iteration of the script but took it out, thinking it was making it
> > to complex), and not slamming the schema name and table name together in my
> > temporary table, it seemed to work better.
> >
> > THanks for all that responded.
> > --
> > Richard Tocci
> > College Station, TX
> >
> >
> > "Tibor Karaszi" wrote:
> >
> >> First, I want to recommend an already prepared script for you. See this topic in Books Online:
> >> ms-help://MS.SQLCC.v9/MS.SQLSVR.v9.en/tsqlref9/html/d294dd8e-82d5-4628-aa2d-e57702230613.htm
> >>
> >> If you scroll down towards the end, you will find a script that does exactly what you want to do.
> >> In
> >> addition, the script will only reoorganize the index if it is fragmented in the first place.
> >>
> >>
> >> To answer why your script doesn't work:
> >>
> >> > fetch next from c_Indexreorg
> >>
> >> You don't fetch the column from the cursor into any variables. You should have something like:
> >>
> >> > fetch next from c_Indexreorg INTO @.v1, @.v1, ...
> >>
> >> > ALTER index ALL on [TableName]
> >>
> >> Here you have hardcoded the table name to "TableName", and you probably don't have a table with
> >> that
> >> name in the database.
> >> --
> >> Tibor Karaszi, SQL Server MVP
> >> http://www.karaszi.com/sqlserver/default.asp
> >> http://www.solidqualitylearning.com/
> >>
> >>
> >> "Richard Tocci" <richardftoccijr at hotmail dot com> wrote in message
> >> news:16DD402A-52FC-4F87-AD7E-B66EA78D7FE6@.microsoft.com...
> >> > I'm new to SQL 2005, but have been working with SQL 2000 for quite some time.
> >> > In studying for the SQL 2005 test 70-431, I'm working in Chapter 12 of the
> >> > Self Study guide, working on managing index fragmentation.
> >> >
> >> > What I am trying to do is collect data from sys.dt_db_index_physical_stats
> >> > DMF. I'm creating a temporary table that houses the table name in the format
> >> > "schema.table". Then I am attempting to put some of that information into a
> >> > cursor, and run an ALTER INDEX on all tables that match the criteria I need.
> >> >
> >> > Following is the script I've come up with:
> >> >
> >> > set ansi_nulls on
> >> > set quoted_identifier on
> >> >
> >> > if exists (select * from sys.objects where name='tmp_TableIndex')
> >> > begin
> >> > drop table tmp_TableIndex
> >> > end
> >> >
> >> > create table tmp_TableIndex
> >> > (
> >> > TableName varchar(50),
> >> > AvgFragInPercent int,
> >> > AvgPageSpaceUsedInPercent int
> >> > )
> >> >
> >> > insert tmp_TableIndex(TableName, AvgFragInPercent, AvgPageSpaceUsedInPercent)
> >> > select schema_name(sc.schema_id) + '.' + object_name(dt.object_id) as
> >> > 'TableName',
> >> > dt.avg_fragmentation_in_percent,
> >> > dt.avg_page_space_used_in_percent
> >> > from sys.dm_db_index_physical_stats
> >> > (
> >> > db_id(db_name()), null, null, null, 'detailed'
> >> > )
> >> > dt
> >> > join sys.objects sc
> >> > on sc.object_id=dt.object_id
> >> > join sys.indexes si
> >> > on si.object_id=dt.object_id
> >> > and si.index_id=dt.index_id
> >> > where dt.index_ID<>0
> >> > and dt.avg_fragmentation_in_percent between 10 and 15
> >> > or dt.avg_page_space_used_in_percent between 60 and 75
> >> >
> >> > declare c_Indexreorg cursor
> >> > for
> >> > select distinct TableName from tmp_TableIndex
> >> >
> >> > open c_Indexreorg
> >> >
> >> > fetch next from c_Indexreorg
> >> >
> >> > while (@.@.FETCH_STATUS=0)
> >> > begin
> >> > ALTER index ALL on [TableName]
> >> > REORGANIZE
> >> > fetch next from c_Indexreorg
> >> > end
> >> >
> >> > close c_Indexreorg
> >> >
> >> > deallocate c_Indexreorg
> >> >
> >> >
> >> > The table is created, but I get the following error:
> >> > Msg 1088, Level 16, State 9, Line 46
> >> > Cannot find the object "TableName" because it does not exist or you do not
> >> > have permissions.
> >> >
> >> > I made sure the tablename in the temp table was in schema.tablename format,
> >> > but that still didn't help.
> >> >
> >> > What am I doing wrong? Is ALTER INDEX not allowed in a cursor?
> >> >
> >> >
> >> >
> >> > --
> >> > Richard Tocci
> >> > College Station, TX
> >>
> >>
> >>
>|||Guys, what book are you referring to? I am studying for the exam and am
looking for all the help I can get.
Thanks!
-Richard K
"Richard Tocci" wrote:
> Actually, that should be an OR, not an AND. I was going by the 70-431 self
> training book, page 453, at the bottom.
> --
> Richard Tocci
> College Station, TX
>
> "Tibor Karaszi" wrote:
> > Forgive my curiosity, but below seems surprising:
> >
> > > and dt.avg_fragmentation_in_percent between 10 and 15
> >
> > Why wouldn't you want to reorg if the fragmentation level is high?
> >
> > --
> > Tibor Karaszi, SQL Server MVP
> > http://www.karaszi.com/sqlserver/default.asp
> > http://sqlblog.com/blogs/tibor_karaszi
> >
> >
> > "Richard Tocci" <richardftoccijr at hotmail dot com> wrote in message
> > news:FD361A40-D2EC-448D-A29E-D5A0CBA23A8B@.microsoft.com...
> > > After reading the Books Online help file, I modified the script and now it
> > > works. Here it is:
> > >
> > > set ansi_nulls on
> > > set quoted_identifier on
> > >
> > > if exists (select * from sys.objects where name='tmp_TableIndex')
> > > begin
> > > drop table tmp_TableIndex
> > > end
> > >
> > > create table tmp_TableIndex
> > > (
> > > TableName varchar(50),
> > > SchemaName varchar(50),
> > > AvgFragInPercent int,
> > > AvgPageSpaceUsedInPercent int
> > > )
> > >
> > > insert tmp_TableIndex(TableName, SchemaName, AvgFragInPercent,
> > > AvgPageSpaceUsedInPercent)
> > > select schema_name(sc.schema_id) as 'SchemaName',
> > > object_name(dt.object_id) as 'TableName',
> > > dt.avg_fragmentation_in_percent,
> > > dt.avg_page_space_used_in_percent
> > > from sys.dm_db_index_physical_stats
> > > (
> > > db_id(db_name()), null, null, null, 'detailed'
> > > )
> > > dt
> > > join sys.objects sc
> > > on sc.object_id=dt.object_id
> > > join sys.indexes si
> > > on si.object_id=dt.object_id
> > > and si.index_id=dt.index_id
> > > where dt.index_ID<>0
> > > and dt.avg_fragmentation_in_percent between 10 and 15
> > > or dt.avg_page_space_used_in_percent between 60 and 75
> > >
> > > declare @.table_name varchar(50),
> > > @.schema_name varchar(50),
> > > @.command varchar(100)
> > >
> > > declare c_Indexreorg cursor
> > > for
> > > select distinct SchemaName, TableName from tmp_TableIndex
> > >
> > > open c_Indexreorg
> > >
> > > fetch next from c_Indexreorg into @.table_name, @.schema_name
> > >
> > > while (@.@.FETCH_STATUS=0)
> > > begin
> > > set @.command='ALTER index ALL on ' + @.schema_name + '.' + @.table_name + '
> > > REORGANIZE'
> > > exec (@.command)
> > > fetch next from c_Indexreorg
> > > end
> > >
> > > close c_Indexreorg
> > >
> > > deallocate c_Indexreorg
> > >
> > >
> > >
> > >
> > > Once I created a couple more variables (I had the @.table_name variable on a
> > > previous iteration of the script but took it out, thinking it was making it
> > > to complex), and not slamming the schema name and table name together in my
> > > temporary table, it seemed to work better.
> > >
> > > THanks for all that responded.
> > > --
> > > Richard Tocci
> > > College Station, TX
> > >
> > >
> > > "Tibor Karaszi" wrote:
> > >
> > >> First, I want to recommend an already prepared script for you. See this topic in Books Online:
> > >> ms-help://MS.SQLCC.v9/MS.SQLSVR.v9.en/tsqlref9/html/d294dd8e-82d5-4628-aa2d-e57702230613.htm
> > >>
> > >> If you scroll down towards the end, you will find a script that does exactly what you want to do.
> > >> In
> > >> addition, the script will only reoorganize the index if it is fragmented in the first place.
> > >>
> > >>
> > >> To answer why your script doesn't work:
> > >>
> > >> > fetch next from c_Indexreorg
> > >>
> > >> You don't fetch the column from the cursor into any variables. You should have something like:
> > >>
> > >> > fetch next from c_Indexreorg INTO @.v1, @.v1, ...
> > >>
> > >> > ALTER index ALL on [TableName]
> > >>
> > >> Here you have hardcoded the table name to "TableName", and you probably don't have a table with
> > >> that
> > >> name in the database.
> > >> --
> > >> Tibor Karaszi, SQL Server MVP
> > >> http://www.karaszi.com/sqlserver/default.asp
> > >> http://www.solidqualitylearning.com/
> > >>
> > >>
> > >> "Richard Tocci" <richardftoccijr at hotmail dot com> wrote in message
> > >> news:16DD402A-52FC-4F87-AD7E-B66EA78D7FE6@.microsoft.com...
> > >> > I'm new to SQL 2005, but have been working with SQL 2000 for quite some time.
> > >> > In studying for the SQL 2005 test 70-431, I'm working in Chapter 12 of the
> > >> > Self Study guide, working on managing index fragmentation.
> > >> >
> > >> > What I am trying to do is collect data from sys.dt_db_index_physical_stats
> > >> > DMF. I'm creating a temporary table that houses the table name in the format
> > >> > "schema.table". Then I am attempting to put some of that information into a
> > >> > cursor, and run an ALTER INDEX on all tables that match the criteria I need.
> > >> >
> > >> > Following is the script I've come up with:
> > >> >
> > >> > set ansi_nulls on
> > >> > set quoted_identifier on
> > >> >
> > >> > if exists (select * from sys.objects where name='tmp_TableIndex')
> > >> > begin
> > >> > drop table tmp_TableIndex
> > >> > end
> > >> >
> > >> > create table tmp_TableIndex
> > >> > (
> > >> > TableName varchar(50),
> > >> > AvgFragInPercent int,
> > >> > AvgPageSpaceUsedInPercent int
> > >> > )
> > >> >
> > >> > insert tmp_TableIndex(TableName, AvgFragInPercent, AvgPageSpaceUsedInPercent)
> > >> > select schema_name(sc.schema_id) + '.' + object_name(dt.object_id) as
> > >> > 'TableName',
> > >> > dt.avg_fragmentation_in_percent,
> > >> > dt.avg_page_space_used_in_percent
> > >> > from sys.dm_db_index_physical_stats
> > >> > (
> > >> > db_id(db_name()), null, null, null, 'detailed'
> > >> > )
> > >> > dt
> > >> > join sys.objects sc
> > >> > on sc.object_id=dt.object_id
> > >> > join sys.indexes si
> > >> > on si.object_id=dt.object_id
> > >> > and si.index_id=dt.index_id
> > >> > where dt.index_ID<>0
> > >> > and dt.avg_fragmentation_in_percent between 10 and 15
> > >> > or dt.avg_page_space_used_in_percent between 60 and 75
> > >> >
> > >> > declare c_Indexreorg cursor
> > >> > for
> > >> > select distinct TableName from tmp_TableIndex
> > >> >
> > >> > open c_Indexreorg
> > >> >
> > >> > fetch next from c_Indexreorg
> > >> >
> > >> > while (@.@.FETCH_STATUS=0)
> > >> > begin
> > >> > ALTER index ALL on [TableName]
> > >> > REORGANIZE
> > >> > fetch next from c_Indexreorg
> > >> > end
> > >> >
> > >> > close c_Indexreorg
> > >> >
> > >> > deallocate c_Indexreorg
> > >> >
> > >> >
> > >> > The table is created, but I get the following error:
> > >> > Msg 1088, Level 16, State 9, Line 46
> > >> > Cannot find the object "TableName" because it does not exist or you do not
> > >> > have permissions.
> > >> >
> > >> > I made sure the tablename in the temp table was in schema.tablename format,
> > >> > but that still didn't help.
> > >> >
> > >> > What am I doing wrong? Is ALTER INDEX not allowed in a cursor?
> > >> >
> > >> >
> > >> >
> > >> > --
> > >> > Richard Tocci
> > >> > College Station, TX
> > >>
> > >>
> > >>
> >
> >

Saturday, February 25, 2012

Problem converting VS2003 code to VS2005

Hi:

I am trying to convert my VS2003 project to VS2005 beta 2. The actual conversion has gone fine, but some code that was working fine under to old IDE is now giving me an exception when run under the VS2005 generated code.

The code is:

using (SqlConnection conn = new SqlConnection("Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=GestionNET00001;Data Source=localhost")

{
...
}

And the error I'm getting is:
{"The type initializer for 'System.Data.SqlClient.SqlConnection' threw an exception."}

Has anyone any idea why this is happening?

TIA,
MartinH.

You've got invalid parenthesis in that code sample you know?

Anyway, have you tried a simple declaration instead of using the using statement?
i.e.

SqlConnection conn;
conn = new SqlConnection("Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=GestionNET00001;Data Source=localhost");

Does it fail on either of those lines if it's written out like that?
If not, then merge them together into the same statement and see if that works.
If it does, try putting them back into the using statement and see what happens...|||

plenderj wrote:

You've got invalid parenthesis in that code sample you know?


Yes that was a typo, sorry, but in my code it was correct and compiled correctly.

plenderj wrote:

Anyway, have you tried a simple declaration instead of using the using statement?
i.e.

SqlConnection conn;
conn = new SqlConnection("Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=GestionNET00001;Data Source=localhost");

Does it fail on either of those lines if it's written out like that?


I have since tried to declare and instantiate the connection without a connection string, but still have the same problem. I have also tried to eliminate the 'using' block, still the same.
There seems to be a problem at runtime, but I have checked the 'references' and Syatem.Data is included, I don't think I need anything else for SqlConnection.
Finally, when I installed VS2005 b2, the SqlConnection, SqlCommand and other family members were not installed on the tool pallete, I have to install them manuallly. Is this standard procedure for VS2005? If not this may be related to the problem I have.
TIA,
MartinH.|||I had a problem with VS2005 not automatically including the System.data namespace references for me to use and had to manually add it to the project properties.

Try also including the System.Data.SqlClient namespace|||Just for the record, I have found the solution to the afore mentioned problem.
It was 2 fold. First, I was using the Microsoft Enterprise Library for January 2005, and once I removed this from my project it worked fine. Maybe the June update will work okay, I will have to try it.
Secondly, I use Developer Express's XPO and had the 'trace' feature enabled (within the App.config file). Turning this option off also solved the problem.
I have tried each option individually and they both cause the problem to reappear.
HTH,
MartinH.|||I am having the same issue. The designer generated code is throwing the same error from the InitConnection function of one of my datasets. This only happens when I have a listener configured in the app.config file. If I comment out the Systems.Diagnotics section of the config file, I no longer get the error. The highlighted line below is throwing the error.

This seems to be a bug in how the config file is being processed.

Private Sub InitConnection()

Me.m_connection = New System.Data.SqlClient.SqlConnection

Me.m_connection.ConnectionString = DnD.Settings.Default.DnDConnectionString

End Sub

|||It's not a bug -- the confusing thing was where the error was thrown. Drilling into the exception made it clear that I had a typo in the diagnostics section of the config file.