Monday, March 26, 2012
Script to remove all extended properties?
Does anyone know of a way / script to remove all extended properties from
objects within a certain database?
Tia
James
tglThe documented way is to use sp_dropextendedproperty for each property
of each object (which can be found using fn_listextendedproperty).
An undocumented way would be:
DELETE sysproperties
(this may not work in later versions of SQL Server)
Razvan
Monday, March 12, 2012
script indexes
truncate the tables then DTS the new data in from another SQL server. I've
found that it's much faster to drop all the indexes on my tables and then
rebuild them at the end.
I'm looking to automate this process. It's easy to drop all the indexes in
a cursor - that's not really my issue. I'm currently using a vb app that I
wrote that uses DSO to generate a text file with the create index
statements, but it's not overly efficient because I don't want it to include
all tables - which it does. I've thought it might be more efficient to
query the sysindexes tables to find my indexes but I haven't found a great
way to save this information prior to dropping the indexes. I have no
objections to creating a table to store this information in.
I'm wondering if anyone has a suggestion for this?
Thanks in advance,
AndreBy DSO, do you mean SQL-DMO? If so, then why don't you iterate through
the collection of tables in your VB app, and only script out the
indexes for the tables of interest? You can do this in an ActiveX task
in the DTS package, saving the script to a global variable, and then
recreate the indexes using another ActiveX package to execute the SQL
statements.
I guess it sounds like you have all of the right elements to do what
you want to do; I'm not sure what the roadblock is.
Stu|||Yes, I do mean DMO - my mistake. You'll have to forgive me as I'm just a
rookie programmer and don't know DMO thoroughly. I don't know how to loop
through and get only the tables I'm interested in. This is a snippet of the
code I'm using to get my indexes:
For Each View In svr.Databases("MyDb").Tables
If InStr(View.Name, "sys") = 0 Then ' exclude system tables
Err.Clear
' add index info to text file
Print #1, View.Script(SQLDMOScript_Indexes)
End If
How would I modify it to get the indexes for specific tables? And how would
I store this in a global var?
Thanks for your help.
Andre|||First, I'd recommend that you get a book on DTS programming; there are
a couple of good ones out there. They'll help you get a grip on using
global variables, etc.
Second, here's a quick stab at some VBScript that may point you in the
right direction. If the tables are few, you can get by with SELECT
CASE. If you have a great deal of tables, you'll probably want to
write a function to look up the table name from an array.
Dim sql
SET SQLServer = CreateObject("SQLDMO.SqlServer")
SET Database = CreateObject("SQLDMO.Database")
SET Table = CreateObject("SQLDMO.Table")
SET Index = CreateObject("SQLDMO.Index")
SQLServer.LoginSecure = TRUE
SQLServer.Connect ServerName
For Each Database in SQLServer.Databases
If Database.SystemObject = False Then
Set filetxt = filesys.CreateTextFile(FilePAth & ServerName & "\" &
Database.Name & ".sql", True)
sql = Database.Script
sql = sql & CHAR(10) & "USE " & Database.Name & CHAR(10)
For each Table in Database.Tables
SELECT CASE Table.Name
CASE "Table1 you want"
For each Index in Table.Indexes
sql = sql & Index.script(532676612)
Next
CASE "Table You don't want"
END SELECT
Next
End if
Next
SQLServer.Disconnect
SET Index = NOTHING
SET Table = Nothing
SET Database = NOTHING
SET SQLServer = NOTHING
HTH,
Stu|||Hi Stu
I'm just now making my way back to this project and I seem to remember that
you had responded to this thread of mine with some example code. However I
don't see it anymore. I'm wondering if you could re-post it here?
Thanks very much.
Andre|||http://groups.google.com/group/micr...218b84be5a282a6
should take you to it. If not, let me know.|||Awesome, thanks Stu!
Andre
Script for sa rights and db settings
2. I also want to create a script to montor some certain db settings such as
"Use qouted identifier" etc. and notify me if someone turns them off.
Do you know how?
Thanks,
Zrichsp_dboption master, 'quoted identifier'
"Zrich" <Zrich@.discussions.microsoft.com> wrote in message
news:CF5ED5AF-578C-4680-A70D-BB9AEE29D3FB@.microsoft.com...
> 1. I want to create a script to know who has sa rights.
> 2. I also want to create a script to montor some certain db settings such
> as
> "Use qouted identifier" etc. and notify me if someone turns them off.
> Do you know how?
> Thanks,
> Zrich|||There is no something "sa rights" SA is a special and built-in login which
has "sysadmin" role right.
You can list logins which has sysadmin right by using the following command
EXEC sp_helpsrvrolemember 'sysadmin'
--
Ekrem Ã?nsoy
"Zrich" <Zrich@.discussions.microsoft.com> wrote in message
news:CF5ED5AF-578C-4680-A70D-BB9AEE29D3FB@.microsoft.com...
> 1. I want to create a script to know who has sa rights.
> 2. I also want to create a script to montor some certain db settings such
> as
> "Use qouted identifier" etc. and notify me if someone turns them off.
> Do you know how?
> Thanks,
> Zrich|||Zrich,
Here is a script I use at work that includes some of the database options. I
only run it manually, but it should be easy enough to modify it to do what
you want (on database options).
DECLARE @.DB sysname
DECLARE @.SQL nvarchar(255)
if exists ( select * from tempdb..sysobjects where name LIKE
'#FileStats__%' ) drop table #FileStats
if exists ( select * from tempdb..sysobjects where name LIKE
'#DBoption__%' ) drop table #DBoption
CREATE TABLE #DBoption (OptName VARCHAR(35), OptValue VARCHAR(10))
CREATE TABLE #FileStats(
[FileId] INT,
[FileGroup] INT,
[TotalExtents] INT,
[UsedExtents] INT,
[Name] sysname,
[Filename] varchar(255)
)
DECLARE @.FileStats TABLE (
[FileId] INT
, [FileGroup] INT
, [TotalExtents] INT
, [UsedExtents] INT
, [Name] SYSNAME
, [Filename] VARCHAR(255)
, [AutoShrink] VARCHAR(10)
, [AutoClose] VARCHAR(10)
, [QuotedID] VARCHAR(10)
)
DECLARE cDatabases CURSOR FOR
SELECT QUOTENAME(sdb.name)
FROM master.dbo.sysdatabases sdb
WHERE status & 32 != 32
AND status & 64 != 64
AND status & 128 != 128
AND status & 256 != 256
AND status & 512 != 512
AND status & 1024 != 1024
AND status & 4096 != 4096
AND status & 32768 !=32768
OPEN cDatabases
FETCH FROM cDatabases INTO @.DB
WHILE (@.@.FETCH_STATUS = 0)
BEGIN
DELETE FROM #FileStats
SET @.SQL = 'USE ' + @.DB + '; INSERT INTO #FileStats EXEC (''DBCC
SHOWFILESTATS'')'
EXEC (@.SQL)
UPDATE #FileStats SET name = @.DB
INSERT INTO @.FileStats([FileId], [FileGroup], [TotalExtents],
[UsedExtents], [Name], [Filename])
SELECT * FROM #FileStats
--
SET @.SQL = 'sp_dboption ' + @.DB + ', ''autoshrink'''
DELETE FROM #DBoption
INSERT INTO #DBoption
EXEC sp_executesql @.SQL
UPDATE @.FileStats
SET AutoShrink = (SELECT OptValue FROM #DBoption)
WHERE Name = @.DB
--
SET @.SQL = 'sp_dboption ' + @.DB + ', ''autoclose'''
DELETE FROM #DBoption
INSERT INTO #DBoption
EXEC sp_executesql @.SQL
UPDATE @.FileStats
SET AutoClose = (SELECT OptValue FROM #DBoption)
WHERE Name = @.DB
--
SET @.SQL = 'sp_dboption ' + @.DB + ', ''quoted identifier'''
DELETE FROM #DBoption
INSERT INTO #DBoption
EXEC sp_executesql @.SQL
UPDATE @.FileStats
SET QuotedID = (SELECT OptValue FROM #DBoption)
WHERE Name = @.DB
--
FETCH FROM cDatabases INTO @.DB
END
CLOSE cDatabases
DEALLOCATE cDatabases
SELECT
[Name]
,[TotalExtents]*64/1024. AS TotalExtInMB
,[UsedExtents]*64/1024. AS UsedExtInMB
,([TotalExtents] - [UsedExtents]) / 16. AS UnAllocExtInMB --* 64 / 1024. AS
UnAllocExtInMB
,CAST(FLOOR(ROUND([UsedExtents] * 100. / [TotalExtents], 0)) AS VARCHAR(3))
+ '%' AS Pct_Full
, Filename
, AutoShrink
, AutoClose
, QuotedID
FROM @.FileStats
ORDER BY TotalExtInMB DESC
DBCC sqlperf(logspace)
Friday, March 9, 2012
Script Component throws error
Hi,
I have three script component A,B, C. A reads certain data from a table. B inserts records into a .txt file when the recordtype = 1. C inserts records into same .txt file when the recordtype = 0. Now I receive a script comp error for C as follows.
[Error Desc]
************************************************************
The process cannot access the file 'D:\Documents and Settings\Administrator\Desktop\AuditLog.txt' because it is being used by another process.
************************************************************
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
at System.IO.StreamWriter.CreateFile(String path, Boolean append)
at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize)
at System.IO.StreamWriter..ctor(String path, Boolean append)
at ScriptComponent_6bda9d13fce34f90ac6315546c8d0d54.ScriptMain.PreExecute() in dts://Scripts/ScriptComponent_6bda9d13fce34f90ac6315546c8d0d54/ScriptMain:line 19
at Microsoft.SqlServer.Dts.Pipeline.ScriptComponentHost.PreExecute()
[/Error Desc]
Can anyone throw some light on it.
Thanks
If you have two script conponents trying to write to the same file at the same time, then this would seem a reasonable error. You will need to serialise the two write operations, as only one process can have a file open at a given time.|||Hi,
Thanks for your reply. After completion of first script component only next gets executed.
In my example after B completes C starts. Also I have closed and disposed the stream writer object in the post execute of the script component B
How I can get rid of this. Also suggest any alternative solutions?
|||Are all the script components in the same data-flow? If so then there is nothing to stop them trying to access the same file at the same time given that each component operates on one data buffer at a time. Each data buffer is a subset of all the data in the pipeline.
If you want to ensure that the script components are not accessing the same file concurrently put them in seperate, sequential, data flows. Or put them in 3 seperate files and then put them all together into a single file using UNION ALL component in a seperate data-flow.
-Jamie
Tuesday, February 21, 2012
Scoped Calculations and Aggregate
I have a measure which is defined as 0 and then Scope'd for certain dimensions to be a percent of the total as below:
CREATE MEMBER [Measures].[Revenue Percent] AS 0;
SCOPE ([Measures].[Revenue Percent]);
THIS = ([Measures].[Gross Revenue]) / ([Measures].[Gross Revenue], [Area].[Sales Area].[All], [Direct Response].[DRS].[All])
SCOPE (Descendants([Area].[Sales Area].&[2], 1, SELF_AND_AFTER));
THIS = ([Measures].[Gross Revenue])
/ ([Measures].[Gross Revenue], [Area].[Sales Area].&[2], [Direct Response].[DRS].[All]);
END SCOPE;
END SCOPE;
This calculation works fine when I am browsing the cube as I would expect. I can create calculated members on my date dimension and the measure is calculated correctly using Aggregate({date1, date2, date3, etc}) function. However as soon as I change this calculation to use "relative" sets of members it simply returns 0.
So if I have the calcuation,
MEMEBER [Measures].[YTD Revenue Percent] AS AGGREGATE(YTD(date4), [Measures].[Revenue Percent])
it will always return 0. Even if I create the YTD portion as a set first and then aggregate that it still returns 0. However change the ytd(Date4) to something like {date1ate4} and the calculation will work.
Has anyone else seen this sort of behaviour, or can point me in the direction of information as to why it doesn't work?
Thanks in advance
Mike
PS: The date dimension is correctly figured and the member names date1 etc are just syntax for shortening the full date in the post.
{date1ate4} Should read {date1 : date4} - Sorry just seen that once it is posted.