July 30, 2008

Script all indexes ( except PK ) of a table

Script all indexes ( except PK ) of a table :
SELECT OBJECT_NAME(SI.Object_ID) AS TABLE_NAME, SI.Name
FROM Sys.Indexes SI
LEFT JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC
ON SI.Name = TC.CONSTRAINT_NAME AND OBJECT_NAME(SI.Object_ID) = TC.TABLE_NAME
WHERE TC.CONSTRAINT_NAME IS NULL
AND OBJECTPROPERTY(SI.Object_ID, 'IsUserTable') = 1
AND OBJECT_NAME(SI.Object_ID)
in ('Table_Names')
ORDER BY OBJECT_NAME(SI.Object_ID), SI.Index_ID

July 17, 2008

Script all indexes as CREATE INDEX statements

SELECT
TOP 100 REPLICATE(' ',4000) AS COLNAMES , OBJECT_NAME(I.ID) AS TABLENAME,
I.ID AS TABLEID,
I.INDID AS INDEXID,
I.NAME AS INDEXNAME,
I.STATUS, INDEXPROPERTY (I.ID,I.NAME,'ISUNIQUE') AS ISUNIQUE,
INDEXPROPERTY (I.ID,I.NAME,'ISCLUSTERED') AS ISCLUSTERED,
INDEXPROPERTY (I.ID,
I.NAME,'INDEXFILLFACTOR') AS INDEXFILLFACTOR
INTO #TMP
FROM SYSINDEXES I
WHERE I.INDID > 0
AND I.INDID < 255
AND (I.STATUS & 64)=0

DECLARE
@ISQL VARCHAR(4000),
@TABLEID INT,
@INDEXID INT,
@MAXTABLELENGTH INT,
@MAXINDEXLENGTH INT

SELECT @MAXTABLELENGTH=MAX(LEN(TABLENAME))
FROM #TMP
SELECT @MAXINDEXLENGTH=MAX(LEN(INDEXNAME))
FROM #TMP

DECLARE C1 CURSOR
FOR SELECT TABLEID,INDEXID
FROM #TMP
OPEN C1
FETCH NEXT FROM C1 INTO @TABLEID,@INDEXID
WHILE @@FETCH_STATUS <> -1
BEGIN
SET @ISQL = '' SELECT @ISQL= @ISQL + ISNULL(SYSCOLUMNS.NAME,'') + ','
FROM SYSINDEXES I INNER
JOIN SYSINDEXKEYS ON I.ID=SYSINDEXKEYS.ID
AND I.INDID=SYSINDEXKEYS.INDID INNER JOIN SYSCOLUMNS ON SYSINDEXKEYS.ID=SYSCOLUMNS.ID
AND SYSINDEXKEYS.COLID=SYSCOLUMNS.COLID
WHERE I.INDID > 0 AND I.INDID < 255 AND (I.STATUS & 64)=0
AND I.ID= @TABLEID AND I.INDID= @INDEXID
ORDER BY SYSCOLUMNS.COLID

UPDATE #TMP
SET COLNAMES= @ISQL
WHERE TABLEID= @TABLEID
AND INDEXID= @INDEXID

FETCH NEXT FROM C1 INTO @TABLEID,@INDEXID
END
CLOSE C1
DEALLOCATE C1 --AT THIS POINT, THE 'COLNAMES' COLUMN HAS A TRAILING COMMA
UPDATE #TMP SET COLNAMES=LEFT(COLNAMES,LEN(COLNAMES) -1)

SELECT 'CREATE '
+ CASE
WHEN ISUNIQUE = 1
THEN ' UNIQUE '
ELSE ' '
END
+ CASE WHEN ISCLUSTERED = 1
THEN ' CLUSTERED '
ELSE ' '
END
+ ' INDEX [' + UPPER(INDEXNAME) + ']'
+ SPACE(@MAXINDEXLENGTH - LEN(INDEXNAME))
+' ON [' + UPPER(TABLENAME) + '] '
+ SPACE(@MAXTABLELENGTH - LEN(TABLENAME))
+ '(' + UPPER(COLNAMES) + ')'
+ CASE WHEN INDEXFILLFACTOR = 0 THEN '' ELSE '
WITH FILLFACTOR = ' + CONVERT(VARCHAR(10),INDEXFILLFACTOR)
END
--AS SQL FROM #TMP
--SELECT * FROM #TMP
DROP TABLE #TMP

July 11, 2008

System.Data.OracleClient requires Oracle client software version 8.1.7 or greater

Problem:
When usign System.Data.OracleClient with Oracle9i client, people get the following message while connecting to the Oracle database from and ASP.NET application.
System.Data.OracleClient requires Oracle client software version 8.1.7 or greater.

Cause:
Security permissions were not properly set when Oracle9i/10g Release 2 client was installed on Windows with NTFS. The result of this is that content of the ORACLE_HOME directory is not visible to Authenticated Users on the machine; this again causes an error while the System.Data.OracleClient is communicating with the Oracle Connectivity software from an ASP.NET using Authenticated User privileges.

Solution:
To fix the problem you have to give the Authenticated "Users group privilege" to the Oracle Home directory.
1. Log on to Windwos as a user with Administrator privileges.
2. Start Window Explorer and navigate to the ORACLE_HOME folder.
3. Choose properties on the ORACLE_HOME folder.
4. Click the “Security” tab of the “Properties” window.
5. Click on “Authenticated Users” item in the “Name” list.
6. Uncheck the “Read and Execute” box in the “Permissions” list under the “Allow” column.
7. Re-check the “Read and Execute” box under the “Allow” column
8. Click the “Advanced” button and in the “Permission Entries” verify that “Authenticated Users” are listed with permission = “Read & Execute”, and Apply To = “This folder, subfolders and files”.
9. If not, edit that line and make sure that “Apply To” drop-down box is set to “This folder, subfolders and files”. This should already be set properly but it is important that you verify it.
10. Click the “Ok” button until you close out all of the security properties windows. The cursor may present the hour glass for a few seconds as it applies the permissions you just changed to all subfolders and files.
11. Reboot, to assure that the changes have taken effect.
12. Try your application again.