Showing posts with label index. Show all posts
Showing posts with label index. Show all posts

Friday, March 23, 2012

How to get the index of each row from a SELECT query in SQL ?

Hi,
I am making as SELECT query to fill a repeater, and I need to retrieve the index of each line of the query.
ie, I want to get a dataset like :
"0", "dataCol1", "dataCol2" for the first line
"1", "dataCol1", "dataCol2" for the second line
"2", "dataCol1", "dataCol2" for the third line
etc.
Anyone knows if there is a sql statement that does it ?
Thanks
Johanncheck this|||You can also create a temp table with an int identity column, insert the first table into the temp, and return it.

Monday, March 12, 2012

How to get rid of duplicates in the table

I have a table which has quite a few duplicates and I cannot create unique
index on it.
First I want to list those duplicate so I can do something about it and
secondly, I want to delete them so I can create index.
ThanksMac,
Please post the DDL including sample data.
Thanks
Jerry
"Mac" <msamani@.comcast.net> wrote in message
news:e9Auq1l1FHA.3524@.tk2msftngp13.phx.gbl...
>I have a table which has quite a few duplicates and I cannot create unique
>index on it.
> First I want to list those duplicate so I can do something about it and
> secondly, I want to delete them so I can create index.
> Thanks
>|||Mac,
Build an index and do a select out all rows and add GROUP BY <COL>
HAVING COUNT(*) > 1. You will then get the rows which are duplicate
based on the <COL>. You can certainly add <COL1>, <COL2>. Then
remove/modify the rows and you should be able to build a unique index.
Be aware you need to do this when the system is quiet otherwise you can
have extra duplicates occur. Also your applicatio may need to be
updated to deal with a UNIQUE index collission.
Shahryar
Mac wrote:
>I have a table which has quite a few duplicates and I cannot create unique
>index on it.
>First I want to list those duplicate so I can do something about it and
>secondly, I want to delete them so I can create index.
>Thanks
>
>
Shahryar G. Hashemi | Sr. DBA Consultant
InfoSpace, Inc.
601 108th Ave NE | Suite 1200 | Bellevue, WA 98004 USA
Mobile +1 206.459.6203 | Office +1 425.201.8853 | Fax +1 425.201.6150
shashem@.infospace.com | www.infospaceinc.com
This e-mail and any attachments may contain confidential information that is legally privileged. The information is solely for the use of the intended recipient(s); any disclosure, copying, distribution, or other use of this information is strictly prohibited. If you have received this e-mail in error, please notify the sender by return e-mail and delete this message. Thank you.|||Put an identity on the table
group by your fields and Max identity value, insert that into another table
delete everything from orig table where identity is not in the other table
drop identity
create index
http://sqlservercode.blogspot.com/
"Mac" wrote:
> I have a table which has quite a few duplicates and I cannot create unique
> index on it.
> First I want to list those duplicate so I can do something about it and
> secondly, I want to delete them so I can create index.
> Thanks
>
>|||Hi Mac.
This is a 4 step solution. First you have to move all dups into a new table,
lets call TEMP_TAB. Second delete all dups from your master table, lets call
TAB_WITH_DUPS. Third move all records from TEMP_TAB to TAB_WITH_DUPS. Forth
dropTEMP_TAB.
For your understanding check following out.
---
-- STEP 0 - Preparing for the test
---
CREATE TABLE TAB_WITH_DUPS (COL_1 VARCHAR(10), COL_2 VARCHAR(10), COL_3
VARCHAR(10), )
INSERT INTO TAB_WITH_DUPS VALUES ('AA', 'BB', 'CC')
INSERT INTO TAB_WITH_DUPS VALUES ('AA', 'BB', 'CC')
INSERT INTO TAB_WITH_DUPS VALUES ('BB', 'BB', 'AA')
INSERT INTO TAB_WITH_DUPS VALUES ('CC', 'BB', 'CC')
SELECT * FROM TAB_WITH_DUPS
---
-- STEP 1 - Move dups to a new table
---
SELECT
DISTINCT
COL_1,
COL_2,
COL_3
INTO
TEMP_TAB
FROM
TAB_WITH_DUPS
GROUP BY
COL_1,
COL_2,
COL_3
HAVING
COUNT(*) > 1
SELECT * FROM TEMP_TAB
---
-- STEP 2 - Delete dups from the master table
---
DELETE a
FROM TAB_WITH_DUPS a
INNER JOIN TEMP_TAB b
ON a.COL_1 = b.COL_1
AND a.COL_2 = b.COL_2
AND a.COL_3 = b.COL_3
SELECT * FROM TAB_WITH_DUPS
---
-- STEP 3 - Bring data back to the master table.
---
INSERT INTO TAB_WITH_DUPS SELECT * FROM TEMP_TAB
SELECT * FROM TAB_WITH_DUPS
---
-- STEP 4 - Get rid of the newly created table
---
DROP TABLE TEMP_TAB
"Mac" wrote:
> I have a table which has quite a few duplicates and I cannot create unique
> index on it.
> First I want to list those duplicate so I can do something about it and
> secondly, I want to delete them so I can create index.
> Thanks
>
>|||I believe this is a simpler method:
1. SELECT * INTO #Temp1 FROM [TableName]
2. TRUNCATE TABLE [TableName]
3. CREATE UNIQUE INDEX [IndexName] ON [TableName] (ColumnNames] WITH
IGNORE_DUP_KEY
4. INSERT INTO [TableName] (ColumnNames)
SELECT (ColumnNames)
FROM #Temp1
Mike

Friday, February 24, 2012

How to get indexes?

I want to get a list of indexes for a certain table. I like the
sp_helpindex [table_name], but it also returns the primary key index.
How can I get what sp_helpindex returns without the primary key index?
Hi,
Query either SYSINDEXES table or use sp_help <tablename>
Thanks
Hari
SQL Server MVP
"Frank Rizzo" <none@.none.com> wrote in message
news:upXtVEmvFHA.3756@.tk2msftngp13.phx.gbl...
>I want to get a list of indexes for a certain table. I like the
>sp_helpindex [table_name], but it also returns the primary key index. How
>can I get what sp_helpindex returns without the primary key index?
|||I have created one procedure named "Proc_helpindex_without_pk". 99%
code is copied from sp_helpindex procedure and i added 1% piece of code
to meet your requirments.
Do not update sql server system tables and system procedure.
The Customizied Code is as follows.
create proc Proc_helpindex_without_pk
@.objname nvarchar(776)-- the table to check for indexes
as
-- PRELIM
set nocount on
declare @.objid int,-- the object id of the table
@.indid smallint,-- the index id of an index
@.groupid smallint, -- the filegroup id of an index
@.indname sysname,
@.groupname sysname,
@.status int,
@.keys nvarchar(2126),--Length
(16*max_identifierLength)+(15*2)+(16*3)
@.dbnamesysname
-- Check to see that the object names are local to the current
database.
select @.dbname = parsename(@.objname,3)
if @.dbname is not null and @.dbname <> db_name()
begin
raiserror(15250,-1,-1)
return (1)
end
-- Check to see the the table exists and initialize @.objid.
select @.objid = object_id(@.objname)
if @.objid is NULL
begin
select @.dbname=db_name()
raiserror(15009,-1,-1,@.objname,@.dbname)
return (1)
end
-- OPEN CURSOR OVER INDEXES (skip stats: bug shiloh_51196)
declare ms_crs_ind cursor local static for
select indid, groupid, name, status from sysindexes
where id = @.objid and indid > 0 and indid < 255 and (status & 64)=0
and name not in (select constraint_name
from INFORMATION_SCHEMA.TABLE_CONSTRAINTS
where table_name = object_name(@.objid) and
constraint_type = 'primary key')
order by indid
open ms_crs_ind
fetch ms_crs_ind into @.indid, @.groupid, @.indname, @.status
-- IF NO INDEX, QUIT
if @.@.fetch_status < 0
begin
deallocate ms_crs_ind
raiserror(15472,-1,-1) --'Object does not have any indexes.'
return (0)
end
-- create temp table
create table #spindtab
(
index_namesysnamecollate database_default NOT NULL,
statsint,
groupnamesysname collate database_default NOT NULL,
index_keysnvarchar(2126)collate database_default NOT NULL -- see
@.keys above for length descr
)
-- Now check out each index, figure out its type and keys and
--save the info in a temporary table that we'll print out at the end.
while @.@.fetch_status >= 0
begin
-- First we'll figure out what the keys are.
declare @.i int, @.thiskey nvarchar(131) -- 128+3
select @.keys = index_col(@.objname, @.indid, 1), @.i = 2
if (indexkey_property(@.objid, @.indid, 1, 'isdescending') = 1)
select @.keys = @.keys + '(-)'
select @.thiskey = index_col(@.objname, @.indid, @.i)
if ((@.thiskey is not null) and (indexkey_property(@.objid, @.indid, @.i,
'isdescending') = 1))
select @.thiskey = @.thiskey + '(-)'
while (@.thiskey is not null )
begin
select @.keys = @.keys + ', ' + @.thiskey, @.i = @.i + 1
select @.thiskey = index_col(@.objname, @.indid, @.i)
if ((@.thiskey is not null) and (indexkey_property(@.objid, @.indid,
@.i, 'isdescending') = 1))
select @.thiskey = @.thiskey + '(-)'
end
select @.groupname = groupname from sysfilegroups where groupid =
@.groupid
-- INSERT ROW FOR INDEX
insert into #spindtab values (@.indname, @.status, @.groupname, @.keys)
-- Next index
fetch ms_crs_ind into @.indid, @.groupid, @.indname, @.status
end
deallocate ms_crs_ind
-- SET UP SOME CONSTANT VALUES FOR OUTPUT QUERY
declare @.empty varchar(1) select @.empty = ''
declare @.des1varchar(35),-- 35 matches spt_values
@.des2varchar(35),
@.des4varchar(35),
@.des32varchar(35),
@.des64varchar(35),
@.des2048varchar(35),
@.des4096varchar(35),
@.des8388608varchar(35),
@.des16777216varchar(35)
select @.des1 = name from master.dbo.spt_values where type = 'I' and
number = 1
select @.des2 = name from master.dbo.spt_values where type = 'I' and
number = 2
select @.des4 = name from master.dbo.spt_values where type = 'I' and
number = 4
select @.des32 = name from master.dbo.spt_values where type = 'I' and
number = 32
select @.des64 = name from master.dbo.spt_values where type = 'I' and
number = 64
select @.des2048 = name from master.dbo.spt_values where type = 'I' and
number = 2048
select @.des4096 = name from master.dbo.spt_values where type = 'I' and
number = 4096
select @.des8388608 = name from master.dbo.spt_values where type = 'I'
and number = 8388608
select @.des16777216 = name from master.dbo.spt_values where type = 'I'
and number = 16777216
-- DISPLAY THE RESULTS
select
'index_name' = index_name,
'index_description' = convert(varchar(210), --bits 16 off, 1, 2,
16777216 on, located on group
case when (stats & 16)<>0 then 'clustered' else 'nonclustered' end
+ case when (stats & 1)<>0 then ', '+@.des1 else @.empty end
+ case when (stats & 2)<>0 then ', '+@.des2 else @.empty end
+ case when (stats & 4)<>0 then ', '+@.des4 else @.empty end
+ case when (stats & 64)<>0 then ', '+@.des64 else case when (stats
& 32)<>0 then ', '+@.des32 else @.empty end end
+ case when (stats & 2048)<>0 then ', '+@.des2048 else @.empty end
+ case when (stats & 4096)<>0 then ', '+@.des4096 else @.empty end
+ case when (stats & 8388608)<>0 then ', '+@.des8388608 else @.empty
end
+ case when (stats & 16777216)<>0 then ', '+@.des16777216 else
@.empty end
+ ' located on ' + groupname),
'index_keys' = index_keys
from #spindtab
order by index_name
return (0) -- sp_helpindex
exec Proc_helpindex_without_pk 'authors'
try. all the best
|||Thank you. This is great.
Praveen wrote:

>I have created one procedure named "Proc_helpindex_without_pk". 99%
>code is copied from sp_helpindex procedure and i added 1% piece of code
>to meet your requirments.
>Do not update sql server system tables and system procedure.
>The Customizied Code is as follows.
>create proc Proc_helpindex_without_pk
>@.objname nvarchar(776)-- the table to check for indexes
>as
>-- PRELIM
>set nocount on
>declare @.objid int,-- the object id of the table
>@.indid smallint,-- the index id of an index
>@.groupid smallint, -- the filegroup id of an index
>@.indname sysname,
>@.groupname sysname,
>@.status int,
>@.keys nvarchar(2126),--Length
>(16*max_identifierLength)+(15*2)+(16*3)
>@.dbnamesysname
>-- Check to see that the object names are local to the current
>database.
>select @.dbname = parsename(@.objname,3)
>if @.dbname is not null and @.dbname <> db_name()
>begin
>raiserror(15250,-1,-1)
>return (1)
>end
>-- Check to see the the table exists and initialize @.objid.
>select @.objid = object_id(@.objname)
>if @.objid is NULL
>begin
>select @.dbname=db_name()
>raiserror(15009,-1,-1,@.objname,@.dbname)
>return (1)
>end
>-- OPEN CURSOR OVER INDEXES (skip stats: bug shiloh_51196)
>declare ms_crs_ind cursor local static for
>select indid, groupid, name, status from sysindexes
>where id = @.objid and indid > 0 and indid < 255 and (status & 64)=0
>and name not in (select constraint_name
> from INFORMATION_SCHEMA.TABLE_CONSTRAINTS
> where table_name = object_name(@.objid) and
> constraint_type = 'primary key')
>order by indid
>open ms_crs_ind
>fetch ms_crs_ind into @.indid, @.groupid, @.indname, @.status
>-- IF NO INDEX, QUIT
>if @.@.fetch_status < 0
>begin
>deallocate ms_crs_ind
>raiserror(15472,-1,-1) --'Object does not have any indexes.'
>return (0)
>end
>-- create temp table
>create table #spindtab
>(
>index_namesysnamecollate database_default NOT NULL,
>statsint,
>groupnamesysname collate database_default NOT NULL,
>index_keysnvarchar(2126)collate database_default NOT NULL -- see
>@.keys above for length descr
>)
>-- Now check out each index, figure out its type and keys and
>--save the info in a temporary table that we'll print out at the end.
>while @.@.fetch_status >= 0
>begin
>-- First we'll figure out what the keys are.
>declare @.i int, @.thiskey nvarchar(131) -- 128+3
>select @.keys = index_col(@.objname, @.indid, 1), @.i = 2
>if (indexkey_property(@.objid, @.indid, 1, 'isdescending') = 1)
>select @.keys = @.keys + '(-)'
>select @.thiskey = index_col(@.objname, @.indid, @.i)
>if ((@.thiskey is not null) and (indexkey_property(@.objid, @.indid, @.i,
>'isdescending') = 1))
>select @.thiskey = @.thiskey + '(-)'
>while (@.thiskey is not null )
>begin
>select @.keys = @.keys + ', ' + @.thiskey, @.i = @.i + 1
>select @.thiskey = index_col(@.objname, @.indid, @.i)
>if ((@.thiskey is not null) and (indexkey_property(@.objid, @.indid,
>@.i, 'isdescending') = 1))
>select @.thiskey = @.thiskey + '(-)'
>end
>select @.groupname = groupname from sysfilegroups where groupid =
>@.groupid
>-- INSERT ROW FOR INDEX
>insert into #spindtab values (@.indname, @.status, @.groupname, @.keys)
>-- Next index
>fetch ms_crs_ind into @.indid, @.groupid, @.indname, @.status
>end
>deallocate ms_crs_ind
>-- SET UP SOME CONSTANT VALUES FOR OUTPUT QUERY
>declare @.empty varchar(1) select @.empty = ''
>declare @.des1varchar(35),-- 35 matches spt_values
>@.des2varchar(35),
>@.des4varchar(35),
>@.des32varchar(35),
>@.des64varchar(35),
>@.des2048varchar(35),
>@.des4096varchar(35),
>@.des8388608varchar(35),
>@.des16777216varchar(35)
>select @.des1 = name from master.dbo.spt_values where type = 'I' and
>number = 1
>select @.des2 = name from master.dbo.spt_values where type = 'I' and
>number = 2
>select @.des4 = name from master.dbo.spt_values where type = 'I' and
>number = 4
>select @.des32 = name from master.dbo.spt_values where type = 'I' and
>number = 32
>select @.des64 = name from master.dbo.spt_values where type = 'I' and
>number = 64
>select @.des2048 = name from master.dbo.spt_values where type = 'I' and
>number = 2048
>select @.des4096 = name from master.dbo.spt_values where type = 'I' and
>number = 4096
>select @.des8388608 = name from master.dbo.spt_values where type = 'I'
>and number = 8388608
>select @.des16777216 = name from master.dbo.spt_values where type = 'I'
>and number = 16777216
>-- DISPLAY THE RESULTS
>select
>'index_name' = index_name,
>'index_description' = convert(varchar(210), --bits 16 off, 1, 2,
>16777216 on, located on group
>case when (stats & 16)<>0 then 'clustered' else 'nonclustered' end
>+ case when (stats & 1)<>0 then ', '+@.des1 else @.empty end
>+ case when (stats & 2)<>0 then ', '+@.des2 else @.empty end
>+ case when (stats & 4)<>0 then ', '+@.des4 else @.empty end
>+ case when (stats & 64)<>0 then ', '+@.des64 else case when (stats
>& 32)<>0 then ', '+@.des32 else @.empty end end
>+ case when (stats & 2048)<>0 then ', '+@.des2048 else @.empty end
>+ case when (stats & 4096)<>0 then ', '+@.des4096 else @.empty end
>+ case when (stats & 8388608)<>0 then ', '+@.des8388608 else @.empty
>end
>+ case when (stats & 16777216)<>0 then ', '+@.des16777216 else
>@.empty end
>+ ' located on ' + groupname),
>'index_keys' = index_keys
>from #spindtab
>order by index_name
>
>return (0) -- sp_helpindex
>--
>exec Proc_helpindex_without_pk 'authors'
>try. all the best
>
>

How to get indexes?

I want to get a list of indexes for a certain table. I like the
sp_helpindex [table_name], but it also returns the primary key index.
How can I get what sp_helpindex returns without the primary key index?Hi,
Query either SYSINDEXES table or use sp_help <tablename>
Thanks
Hari
SQL Server MVP
"Frank Rizzo" <none@.none.com> wrote in message
news:upXtVEmvFHA.3756@.tk2msftngp13.phx.gbl...
>I want to get a list of indexes for a certain table. I like the
>sp_helpindex [table_name], but it also returns the primary key index. How
>can I get what sp_helpindex returns without the primary key index?|||I have created one procedure named "Proc_helpindex_without_pk". 99%
code is copied from sp_helpindex procedure and i added 1% piece of code
to meet your requirments.
Do not update sql server system tables and system procedure.
The Customizied Code is as follows.
create proc Proc_helpindex_without_pk
@.objname nvarchar(776) -- the table to check for indexes
as
-- PRELIM
set nocount on
declare @.objid int, -- the object id of the table
@.indid smallint, -- the index id of an index
@.groupid smallint, -- the filegroup id of an index
@.indname sysname,
@.groupname sysname,
@.status int,
@.keys nvarchar(2126), --Length
(16*max_identifierLength)+(15*2)+(16*3)
@.dbname sysname
-- Check to see that the object names are local to the current
database.
select @.dbname = parsename(@.objname,3)
if @.dbname is not null and @.dbname <> db_name()
begin
raiserror(15250,-1,-1)
return (1)
end
-- Check to see the the table exists and initialize @.objid.
select @.objid = object_id(@.objname)
if @.objid is NULL
begin
select @.dbname=db_name()
raiserror(15009,-1,-1,@.objname,@.dbname)
return (1)
end
-- OPEN CURSOR OVER INDEXES (skip stats: bug shiloh_51196)
declare ms_crs_ind cursor local static for
select indid, groupid, name, status from sysindexes
where id = @.objid and indid > 0 and indid < 255 and (status & 64)=0
and name not in (select constraint_name
from INFORMATION_SCHEMA.TABLE_CONSTRAINTS
where table_name = object_name(@.objid) and
constraint_type = 'primary key')
order by indid
open ms_crs_ind
fetch ms_crs_ind into @.indid, @.groupid, @.indname, @.status
-- IF NO INDEX, QUIT
if @.@.fetch_status < 0
begin
deallocate ms_crs_ind
raiserror(15472,-1,-1) --'Object does not have any indexes.'
return (0)
end
-- create temp table
create table #spindtab
(
index_name sysname collate database_default NOT NULL,
stats int,
groupname sysname collate database_default NOT NULL,
index_keys nvarchar(2126) collate database_default NOT NULL -- see
@.keys above for length descr
)
-- Now check out each index, figure out its type and keys and
-- save the info in a temporary table that we'll print out at the end.
while @.@.fetch_status >= 0
begin
-- First we'll figure out what the keys are.
declare @.i int, @.thiskey nvarchar(131) -- 128+3
select @.keys = index_col(@.objname, @.indid, 1), @.i = 2
if (indexkey_property(@.objid, @.indid, 1, 'isdescending') = 1)
select @.keys = @.keys + '(-)'
select @.thiskey = index_col(@.objname, @.indid, @.i)
if ((@.thiskey is not null) and (indexkey_property(@.objid, @.indid, @.i,
'isdescending') = 1))
select @.thiskey = @.thiskey + '(-)'
while (@.thiskey is not null )
begin
select @.keys = @.keys + ', ' + @.thiskey, @.i = @.i + 1
select @.thiskey = index_col(@.objname, @.indid, @.i)
if ((@.thiskey is not null) and (indexkey_property(@.objid, @.indid,
@.i, 'isdescending') = 1))
select @.thiskey = @.thiskey + '(-)'
end
select @.groupname = groupname from sysfilegroups where groupid =@.groupid
-- INSERT ROW FOR INDEX
insert into #spindtab values (@.indname, @.status, @.groupname, @.keys)
-- Next index
fetch ms_crs_ind into @.indid, @.groupid, @.indname, @.status
end
deallocate ms_crs_ind
-- SET UP SOME CONSTANT VALUES FOR OUTPUT QUERY
declare @.empty varchar(1) select @.empty = ''
declare @.des1 varchar(35), -- 35 matches spt_values
@.des2 varchar(35),
@.des4 varchar(35),
@.des32 varchar(35),
@.des64 varchar(35),
@.des2048 varchar(35),
@.des4096 varchar(35),
@.des8388608 varchar(35),
@.des16777216 varchar(35)
select @.des1 = name from master.dbo.spt_values where type = 'I' and
number = 1
select @.des2 = name from master.dbo.spt_values where type = 'I' and
number = 2
select @.des4 = name from master.dbo.spt_values where type = 'I' and
number = 4
select @.des32 = name from master.dbo.spt_values where type = 'I' and
number = 32
select @.des64 = name from master.dbo.spt_values where type = 'I' and
number = 64
select @.des2048 = name from master.dbo.spt_values where type = 'I' and
number = 2048
select @.des4096 = name from master.dbo.spt_values where type = 'I' and
number = 4096
select @.des8388608 = name from master.dbo.spt_values where type = 'I'
and number = 8388608
select @.des16777216 = name from master.dbo.spt_values where type = 'I'
and number = 16777216
-- DISPLAY THE RESULTS
select
'index_name' = index_name,
'index_description' = convert(varchar(210), --bits 16 off, 1, 2,
16777216 on, located on group
case when (stats & 16)<>0 then 'clustered' else 'nonclustered' end
+ case when (stats & 1)<>0 then ', '+@.des1 else @.empty end
+ case when (stats & 2)<>0 then ', '+@.des2 else @.empty end
+ case when (stats & 4)<>0 then ', '+@.des4 else @.empty end
+ case when (stats & 64)<>0 then ', '+@.des64 else case when (stats
& 32)<>0 then ', '+@.des32 else @.empty end end
+ case when (stats & 2048)<>0 then ', '+@.des2048 else @.empty end
+ case when (stats & 4096)<>0 then ', '+@.des4096 else @.empty end
+ case when (stats & 8388608)<>0 then ', '+@.des8388608 else @.empty
end
+ case when (stats & 16777216)<>0 then ', '+@.des16777216 else
@.empty end
+ ' located on ' + groupname),
'index_keys' = index_keys
from #spindtab
order by index_name
return (0) -- sp_helpindex
--
exec Proc_helpindex_without_pk 'authors'
try. all the best|||Thank you. This is great.
Praveen wrote:
>I have created one procedure named "Proc_helpindex_without_pk". 99%
>code is copied from sp_helpindex procedure and i added 1% piece of code
>to meet your requirments.
>Do not update sql server system tables and system procedure.
>The Customizied Code is as follows.
>create proc Proc_helpindex_without_pk
> @.objname nvarchar(776) -- the table to check for indexes
>as
> -- PRELIM
> set nocount on
> declare @.objid int, -- the object id of the table
> @.indid smallint, -- the index id of an index
> @.groupid smallint, -- the filegroup id of an index
> @.indname sysname,
> @.groupname sysname,
> @.status int,
> @.keys nvarchar(2126), --Length
>(16*max_identifierLength)+(15*2)+(16*3)
> @.dbname sysname
> -- Check to see that the object names are local to the current
>database.
> select @.dbname = parsename(@.objname,3)
> if @.dbname is not null and @.dbname <> db_name()
> begin
> raiserror(15250,-1,-1)
> return (1)
> end
> -- Check to see the the table exists and initialize @.objid.
> select @.objid = object_id(@.objname)
> if @.objid is NULL
> begin
> select @.dbname=db_name()
> raiserror(15009,-1,-1,@.objname,@.dbname)
> return (1)
> end
> -- OPEN CURSOR OVER INDEXES (skip stats: bug shiloh_51196)
> declare ms_crs_ind cursor local static for
> select indid, groupid, name, status from sysindexes
> where id = @.objid and indid > 0 and indid < 255 and (status & 64)=0
> and name not in (select constraint_name
> from INFORMATION_SCHEMA.TABLE_CONSTRAINTS
> where table_name = object_name(@.objid) and
> constraint_type = 'primary key')
> order by indid
> open ms_crs_ind
> fetch ms_crs_ind into @.indid, @.groupid, @.indname, @.status
> -- IF NO INDEX, QUIT
> if @.@.fetch_status < 0
> begin
> deallocate ms_crs_ind
> raiserror(15472,-1,-1) --'Object does not have any indexes.'
> return (0)
> end
> -- create temp table
> create table #spindtab
> (
> index_name sysname collate database_default NOT NULL,
> stats int,
> groupname sysname collate database_default NOT NULL,
> index_keys nvarchar(2126) collate database_default NOT NULL -- see
>@.keys above for length descr
> )
> -- Now check out each index, figure out its type and keys and
> -- save the info in a temporary table that we'll print out at the end.
> while @.@.fetch_status >= 0
> begin
> -- First we'll figure out what the keys are.
> declare @.i int, @.thiskey nvarchar(131) -- 128+3
> select @.keys = index_col(@.objname, @.indid, 1), @.i = 2
> if (indexkey_property(@.objid, @.indid, 1, 'isdescending') = 1)
> select @.keys = @.keys + '(-)'
> select @.thiskey = index_col(@.objname, @.indid, @.i)
> if ((@.thiskey is not null) and (indexkey_property(@.objid, @.indid, @.i,
>'isdescending') = 1))
> select @.thiskey = @.thiskey + '(-)'
> while (@.thiskey is not null )
> begin
> select @.keys = @.keys + ', ' + @.thiskey, @.i = @.i + 1
> select @.thiskey = index_col(@.objname, @.indid, @.i)
> if ((@.thiskey is not null) and (indexkey_property(@.objid, @.indid,
>@.i, 'isdescending') = 1))
> select @.thiskey = @.thiskey + '(-)'
> end
> select @.groupname = groupname from sysfilegroups where groupid =>@.groupid
> -- INSERT ROW FOR INDEX
> insert into #spindtab values (@.indname, @.status, @.groupname, @.keys)
> -- Next index
> fetch ms_crs_ind into @.indid, @.groupid, @.indname, @.status
> end
> deallocate ms_crs_ind
> -- SET UP SOME CONSTANT VALUES FOR OUTPUT QUERY
> declare @.empty varchar(1) select @.empty = ''
> declare @.des1 varchar(35), -- 35 matches spt_values
> @.des2 varchar(35),
> @.des4 varchar(35),
> @.des32 varchar(35),
> @.des64 varchar(35),
> @.des2048 varchar(35),
> @.des4096 varchar(35),
> @.des8388608 varchar(35),
> @.des16777216 varchar(35)
> select @.des1 = name from master.dbo.spt_values where type = 'I' and
>number = 1
> select @.des2 = name from master.dbo.spt_values where type = 'I' and
>number = 2
> select @.des4 = name from master.dbo.spt_values where type = 'I' and
>number = 4
> select @.des32 = name from master.dbo.spt_values where type = 'I' and
>number = 32
> select @.des64 = name from master.dbo.spt_values where type = 'I' and
>number = 64
> select @.des2048 = name from master.dbo.spt_values where type = 'I' and
>number = 2048
> select @.des4096 = name from master.dbo.spt_values where type = 'I' and
>number = 4096
> select @.des8388608 = name from master.dbo.spt_values where type = 'I'
>and number = 8388608
> select @.des16777216 = name from master.dbo.spt_values where type = 'I'
>and number = 16777216
> -- DISPLAY THE RESULTS
> select
> 'index_name' = index_name,
> 'index_description' = convert(varchar(210), --bits 16 off, 1, 2,
>16777216 on, located on group
> case when (stats & 16)<>0 then 'clustered' else 'nonclustered' end
> + case when (stats & 1)<>0 then ', '+@.des1 else @.empty end
> + case when (stats & 2)<>0 then ', '+@.des2 else @.empty end
> + case when (stats & 4)<>0 then ', '+@.des4 else @.empty end
> + case when (stats & 64)<>0 then ', '+@.des64 else case when (stats
>& 32)<>0 then ', '+@.des32 else @.empty end end
> + case when (stats & 2048)<>0 then ', '+@.des2048 else @.empty end
> + case when (stats & 4096)<>0 then ', '+@.des4096 else @.empty end
> + case when (stats & 8388608)<>0 then ', '+@.des8388608 else @.empty
>end
> + case when (stats & 16777216)<>0 then ', '+@.des16777216 else
>@.empty end
> + ' located on ' + groupname),
> 'index_keys' = index_keys
> from #spindtab
> order by index_name
>
> return (0) -- sp_helpindex
>--
>exec Proc_helpindex_without_pk 'authors'
>try. all the best
>
>

How to get indexes?

I want to get a list of indexes for a certain table. I like the
sp_helpindex [table_name], but it also returns the primary key index.
How can I get what sp_helpindex returns without the primary key index?Hi,
Query either SYSINDEXES table or use sp_help <tablename>
Thanks
Hari
SQL Server MVP
"Frank Rizzo" <none@.none.com> wrote in message
news:upXtVEmvFHA.3756@.tk2msftngp13.phx.gbl...
>I want to get a list of indexes for a certain table. I like the
>sp_helpindex [table_name], but it also returns the primary key index.
How
>can I get what sp_helpindex returns without the primary key index?|||I have created one procedure named "Proc_helpindex_without_pk". 99%
code is copied from sp_helpindex procedure and i added 1% piece of code
to meet your requirments.
Do not update sql server system tables and system procedure.
The Customizied Code is as follows.
create proc Proc_helpindex_without_pk
@.objname nvarchar(776) -- the table to check for indexes
as
-- PRELIM
set nocount on
declare @.objid int, -- the object id of the table
@.indid smallint, -- the index id of an index
@.groupid smallint, -- the filegroup id of an index
@.indname sysname,
@.groupname sysname,
@.status int,
@.keys nvarchar(2126), --Length
(16*max_identifierLength)+(15*2)+(16*3)
@.dbname sysname
-- Check to see that the object names are local to the current
database.
select @.dbname = parsename(@.objname,3)
if @.dbname is not null and @.dbname <> db_name()
begin
raiserror(15250,-1,-1)
return (1)
end
-- Check to see the the table exists and initialize @.objid.
select @.objid = object_id(@.objname)
if @.objid is NULL
begin
select @.dbname=db_name()
raiserror(15009,-1,-1,@.objname,@.dbname)
return (1)
end
-- OPEN CURSOR OVER INDEXES (skip stats: bug shiloh_51196)
declare ms_crs_ind cursor local static for
select indid, groupid, name, status from sysindexes
where id = @.objid and indid > 0 and indid < 255 and (status & 64)=0
and name not in (select constraint_name
from INFORMATION_SCHEMA.TABLE_CONSTRAINTS
where table_name = object_name(@.objid) and
constraint_type = 'primary key')
order by indid
open ms_crs_ind
fetch ms_crs_ind into @.indid, @.groupid, @.indname, @.status
-- IF NO INDEX, QUIT
if @.@.fetch_status < 0
begin
deallocate ms_crs_ind
raiserror(15472,-1,-1) --'Object does not have any indexes.'
return (0)
end
-- create temp table
create table #spindtab
(
index_name sysname collate database_default NOT NULL,
stats int,
groupname sysname collate database_default NOT NULL,
index_keys nvarchar(2126) collate database_default NOT NULL -- see
@.keys above for length descr
)
-- Now check out each index, figure out its type and keys and
-- save the info in a temporary table that we'll print out at the end.
while @.@.fetch_status >= 0
begin
-- First we'll figure out what the keys are.
declare @.i int, @.thiskey nvarchar(131) -- 128+3
select @.keys = index_col(@.objname, @.indid, 1), @.i = 2
if (indexkey_property(@.objid, @.indid, 1, 'isdescending') = 1)
select @.keys = @.keys + '(-)'
select @.thiskey = index_col(@.objname, @.indid, @.i)
if ((@.thiskey is not null) and (indexkey_property(@.objid, @.indid, @.i,
'isdescending') = 1))
select @.thiskey = @.thiskey + '(-)'
while (@.thiskey is not null )
begin
select @.keys = @.keys + ', ' + @.thiskey, @.i = @.i + 1
select @.thiskey = index_col(@.objname, @.indid, @.i)
if ((@.thiskey is not null) and (indexkey_property(@.objid, @.indid,
@.i, 'isdescending') = 1))
select @.thiskey = @.thiskey + '(-)'
end
select @.groupname = groupname from sysfilegroups where groupid =
@.groupid
-- INSERT ROW FOR INDEX
insert into #spindtab values (@.indname, @.status, @.groupname, @.keys)
-- Next index
fetch ms_crs_ind into @.indid, @.groupid, @.indname, @.status
end
deallocate ms_crs_ind
-- SET UP SOME CONSTANT VALUES FOR OUTPUT QUERY
declare @.empty varchar(1) select @.empty = ''
declare @.des1 varchar(35), -- 35 matches spt_values
@.des2 varchar(35),
@.des4 varchar(35),
@.des32 varchar(35),
@.des64 varchar(35),
@.des2048 varchar(35),
@.des4096 varchar(35),
@.des8388608 varchar(35),
@.des16777216 varchar(35)
select @.des1 = name from master.dbo.spt_values where type = 'I' and
number = 1
select @.des2 = name from master.dbo.spt_values where type = 'I' and
number = 2
select @.des4 = name from master.dbo.spt_values where type = 'I' and
number = 4
select @.des32 = name from master.dbo.spt_values where type = 'I' and
number = 32
select @.des64 = name from master.dbo.spt_values where type = 'I' and
number = 64
select @.des2048 = name from master.dbo.spt_values where type = 'I' and
number = 2048
select @.des4096 = name from master.dbo.spt_values where type = 'I' and
number = 4096
select @.des8388608 = name from master.dbo.spt_values where type = 'I'
and number = 8388608
select @.des16777216 = name from master.dbo.spt_values where type = 'I'
and number = 16777216
-- DISPLAY THE RESULTS
select
'index_name' = index_name,
'index_description' = convert(varchar(210), --bits 16 off, 1, 2,
16777216 on, located on group
case when (stats & 16)<>0 then 'clustered' else 'nonclustered' end
+ case when (stats & 1)<>0 then ', '+@.des1 else @.empty end
+ case when (stats & 2)<>0 then ', '+@.des2 else @.empty end
+ case when (stats & 4)<>0 then ', '+@.des4 else @.empty end
+ case when (stats & 64)<>0 then ', '+@.des64 else case when (stats
& 32)<>0 then ', '+@.des32 else @.empty end end
+ case when (stats & 2048)<>0 then ', '+@.des2048 else @.empty end
+ case when (stats & 4096)<>0 then ', '+@.des4096 else @.empty end
+ case when (stats & 8388608)<>0 then ', '+@.des8388608 else @.empty
end
+ case when (stats & 16777216)<>0 then ', '+@.des16777216 else
@.empty end
+ ' located on ' + groupname),
'index_keys' = index_keys
from #spindtab
order by index_name
return (0) -- sp_helpindex
exec Proc_helpindex_without_pk 'authors'
try. all the best|||Thank you. This is great.
Praveen wrote:

>I have created one procedure named "Proc_helpindex_without_pk". 99%
>code is copied from sp_helpindex procedure and i added 1% piece of code
>to meet your requirments.
>Do not update sql server system tables and system procedure.
>The Customizied Code is as follows.
>create proc Proc_helpindex_without_pk
> @.objname nvarchar(776) -- the table to check for indexes
>as
> -- PRELIM
> set nocount on
> declare @.objid int, -- the object id of the table
> @.indid smallint, -- the index id of an index
> @.groupid smallint, -- the filegroup id of an index
> @.indname sysname,
> @.groupname sysname,
> @.status int,
> @.keys nvarchar(2126), --Length
>(16*max_identifierLength)+(15*2)+(16*3)
> @.dbname sysname
> -- Check to see that the object names are local to the current
>database.
> select @.dbname = parsename(@.objname,3)
> if @.dbname is not null and @.dbname <> db_name()
> begin
> raiserror(15250,-1,-1)
> return (1)
> end
> -- Check to see the the table exists and initialize @.objid.
> select @.objid = object_id(@.objname)
> if @.objid is NULL
> begin
> select @.dbname=db_name()
> raiserror(15009,-1,-1,@.objname,@.dbname)
> return (1)
> end
> -- OPEN CURSOR OVER INDEXES (skip stats: bug shiloh_51196)
> declare ms_crs_ind cursor local static for
> select indid, groupid, name, status from sysindexes
> where id = @.objid and indid > 0 and indid < 255 and (status & 64)=0
> and name not in (select constraint_name
> from INFORMATION_SCHEMA.TABLE_CONSTRAINTS
> where table_name = object_name(@.objid) and
> constraint_type = 'primary key')
> order by indid
> open ms_crs_ind
> fetch ms_crs_ind into @.indid, @.groupid, @.indname, @.status
> -- IF NO INDEX, QUIT
> if @.@.fetch_status < 0
> begin
> deallocate ms_crs_ind
> raiserror(15472,-1,-1) --'Object does not have any indexes.'
> return (0)
> end
> -- create temp table
> create table #spindtab
> (
> index_name sysname collate database_default NOT NULL,
> stats int,
> groupname sysname collate database_default NOT NULL,
> index_keys nvarchar(2126) collate database_default NOT NULL -- see
>@.keys above for length descr
> )
> -- Now check out each index, figure out its type and keys and
> -- save the info in a temporary table that we'll print out at the end.
> while @.@.fetch_status >= 0
> begin
> -- First we'll figure out what the keys are.
> declare @.i int, @.thiskey nvarchar(131) -- 128+3
> select @.keys = index_col(@.objname, @.indid, 1), @.i = 2
> if (indexkey_property(@.objid, @.indid, 1, 'isdescending') = 1)
> select @.keys = @.keys + '(-)'
> select @.thiskey = index_col(@.objname, @.indid, @.i)
> if ((@.thiskey is not null) and (indexkey_property(@.objid, @.indid, @.i,
>'isdescending') = 1))
> select @.thiskey = @.thiskey + '(-)'
> while (@.thiskey is not null )
> begin
> select @.keys = @.keys + ', ' + @.thiskey, @.i = @.i + 1
> select @.thiskey = index_col(@.objname, @.indid, @.i)
> if ((@.thiskey is not null) and (indexkey_property(@.objid, @.indid,
>@.i, 'isdescending') = 1))
> select @.thiskey = @.thiskey + '(-)'
> end
> select @.groupname = groupname from sysfilegroups where groupid =
>@.groupid
> -- INSERT ROW FOR INDEX
> insert into #spindtab values (@.indname, @.status, @.groupname, @.keys)
> -- Next index
> fetch ms_crs_ind into @.indid, @.groupid, @.indname, @.status
> end
> deallocate ms_crs_ind
> -- SET UP SOME CONSTANT VALUES FOR OUTPUT QUERY
> declare @.empty varchar(1) select @.empty = ''
> declare @.des1 varchar(35), -- 35 matches spt_values
> @.des2 varchar(35),
> @.des4 varchar(35),
> @.des32 varchar(35),
> @.des64 varchar(35),
> @.des2048 varchar(35),
> @.des4096 varchar(35),
> @.des8388608 varchar(35),
> @.des16777216 varchar(35)
> select @.des1 = name from master.dbo.spt_values where type = 'I' and
>number = 1
> select @.des2 = name from master.dbo.spt_values where type = 'I' and
>number = 2
> select @.des4 = name from master.dbo.spt_values where type = 'I' and
>number = 4
> select @.des32 = name from master.dbo.spt_values where type = 'I' and
>number = 32
> select @.des64 = name from master.dbo.spt_values where type = 'I' and
>number = 64
> select @.des2048 = name from master.dbo.spt_values where type = 'I' and
>number = 2048
> select @.des4096 = name from master.dbo.spt_values where type = 'I' and
>number = 4096
> select @.des8388608 = name from master.dbo.spt_values where type = 'I'
>and number = 8388608
> select @.des16777216 = name from master.dbo.spt_values where type = 'I'
>and number = 16777216
> -- DISPLAY THE RESULTS
> select
> 'index_name' = index_name,
> 'index_description' = convert(varchar(210), --bits 16 off, 1, 2,
>16777216 on, located on group
> case when (stats & 16)<>0 then 'clustered' else 'nonclustered' end
> + case when (stats & 1)<>0 then ', '+@.des1 else @.empty end
> + case when (stats & 2)<>0 then ', '+@.des2 else @.empty end
> + case when (stats & 4)<>0 then ', '+@.des4 else @.empty end
> + case when (stats & 64)<>0 then ', '+@.des64 else case when (stats
>& 32)<>0 then ', '+@.des32 else @.empty end end
> + case when (stats & 2048)<>0 then ', '+@.des2048 else @.empty end
> + case when (stats & 4096)<>0 then ', '+@.des4096 else @.empty end
> + case when (stats & 8388608)<>0 then ', '+@.des8388608 else @.empty
>end
> + case when (stats & 16777216)<>0 then ', '+@.des16777216 else
>@.empty end
> + ' located on ' + groupname),
> 'index_keys' = index_keys
> from #spindtab
> order by index_name
>
> return (0) -- sp_helpindex
>--
>exec Proc_helpindex_without_pk 'authors'
>try. all the best
>
>

Sunday, February 19, 2012

How to get Full-Text to not ingore special characters

We just implemented a full-text index on our product master table,
however the users are now screaming because they cannot search on some
of the special characters that are commonly found in our product
descriptions, specifically the #, %, and period (.)

These characters are not in the Noise file, so no luck in just
deleting them from there, but somehow, the full-text is automatically
ignoring those characters, and we would like for the full-text to not
ignore these characters.

Any insight or help would be appreciated.

Thanks"dotnetprogrammer" <tim_60173@.yahoo.com> wrote in message
news:e856daff.0309050524.703c112@.posting.google.co m...
> We just implemented a full-text index on our product master table,
> however the users are now screaming because they cannot search on some
> of the special characters that are commonly found in our product
> descriptions, specifically the #, %, and period (.)
> These characters are not in the Noise file, so no luck in just
> deleting them from there, but somehow, the full-text is automatically
> ignoring those characters, and we would like for the full-text to not
> ignore these characters.

You're probably better off asking in
news:microsoft.public.sqlserver.fulltext

The experts there can help you.

Off the top of my head, I don't believe you can search on those, but they
will be able to tell you for sure.

> Any insight or help would be appreciated.
> Thanks

How to get FILL FACTOR

Hi,
How can i get the current "Fill Factor" for an existing
index
Thank you !
DonUse the INDEXPROPERTY function. For example:
SELECT INDEXPROPERTY(OBJECT_ID('authors'), 'UPKCL_auidind',
'IndexFillFactor')
--
Carlos E. Rojas
SQL Server MVP
Co-Author SQL Server 2000 Programming by Example
"Donald" <Donald.huppe@.bocenor.com> wrote in message
news:0d2101c37230$66d7ef10$a501280a@.phx.gbl...
> Hi,
> How can i get the current "Fill Factor" for an existing
> index
> Thank you !
> Don|||This is a multi-part message in MIME format.
--=_NextPart_000_0166_01C3720F.D6745550
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
Check out INDEXPROPERTY() in the BOL.
-- Tom
---
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Columnist, SQL Server Professional
Toronto, ON Canada
www.pinnaclepublishing.com/sql
"Donald" <Donald.huppe@.bocenor.com> wrote in message =news:0d2101c37230$66d7ef10$a501280a@.phx.gbl...
Hi,
How can i get the current "Fill Factor" for an existing index
Thank you !
Don
--=_NextPart_000_0166_01C3720F.D6745550
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
&

Check out INDEXPROPERTY() in the =BOL.
-- Tom
---T=homas A. Moreau, BSc, PhD, MCSE, MCDBASQL Server MVPColumnist, SQL =Server ProfessionalToronto, ON Canadahttp://www.pinnaclepublishing.com/sql">www.pinnaclepublishing.com=/sql
"Donald" = wrote in message news:0d2101c37230$66=d7ef10$a501280a@.phx.gbl...Hi,How can i get the current "Fill Factor" for an existing =indexThank you !Don

--=_NextPart_000_0166_01C3720F.D6745550--