Showing posts with label row. Show all posts
Showing posts with label row. Show all posts

Tuesday, March 27, 2012

Create Table from Row Data

Hello,
In SQL Server 2000, is it possible to take a table with one field (column), and pivot the table so that the characters in the row data become the field (column) names of another table ( or in a View)? The number of records could vary.

If so, how would I do this?

Sample table;
Create Table dbo.MonthlyData
(
Categories varchar(30) NOT NULL
)

Sample data;

Sales Volume 2005-02
TotRefVol 2005-02
Sales Ratio 2005-02
Sales Volume 2005-03
TotRefVol 2005-03
Sales Ratio 2005-03
Sales Volume 2005-04
TotRefVol 2005-04
Sales Ratio 2005-04

If I am following you correctly; Are you wanting Sales Volume,etc.. to be a column name in a view or table?|||

First; Sorry, the subject should have been 'Create table fields from row data'. To answer your question, each row of sample data is contained within a single column called 'Categories'.

|||

Assuming you had some other value to use with the columns you reference you could do something like:

Create Table dbo.MonthlyData

(

CategoryID int IDENTITY(1,1)

,Categories varchar(30) NOT NULL

,Value varchar(50)

)

INSERT dbo.MonthlyData (Categories, [Value]) VALUES('Sales Volume 2005-02', 'Small')

INSERT dbo.MonthlyData (Categories, [Value]) VALUES('TotRefVol 2005-02', 'Medium')

INSERT dbo.MonthlyData (Categories, [Value]) VALUES('Sales Ratio 2005-02', 'Large')

INSERT dbo.MonthlyData (Categories, [Value]) VALUES('Sales Volume 2005-03', 'Extra Large')

INSERT dbo.MonthlyData (Categories, [Value]) VALUES('TotRefVol 2005-03', 'Small')

INSERT dbo.MonthlyData (Categories, [Value]) VALUES('Sales Ratio 2005-03', 'Medium')

INSERT dbo.MonthlyData (Categories, [Value]) VALUES('Sales Volume 2005-04', 'Large')

INSERT dbo.MonthlyData (Categories, [Value]) VALUES('TotRefVol 2005-04', 'Extra Large')

INSERT dbo.MonthlyData (Categories, [Value]) VALUES('Sales Ratio 2005-04', 'Small')

DECLARE @.string nvarchar(1000)

SELECT @.string = ISNULL(@.string + ', ', '') + QUOTENAME([Value], '''') + QUOTENAME(Categories)

FROM dbo.MonthlyData

SET @.string = 'SELECT ' + @.string

EXEC sp_executesql @.string

This will give you Categories as your column header with associated Value column.

|||

Thank you for your help, I will try this!

cdun2

|||

Thanks again. I had a couple of questions;

-Procedure sp_executesql expects parameter '@.statement' of type 'ntext/nchar/nvarchar'; nvarchar has a 'size' limit of 4000, and ntext cannot be the datatype of local variable @.string. char will handle up to 8000 characters. How can I work around these limitations?

-Could a table be created from the results of sp_executesql @.string so that the column names become fields in the table?

cdun2

|||

Yes, you could alter the syntax

@.sql = 'select ' + @.sql

to

@.sql = 'select ' + @.sql + ' into myTable'

this would keep the columns dynamic. If the number of columns will be static you could create the table and do the following:

@.sql = 'insert myTable (valuelist) select ' + @.sql

|||I realized I didn't answer the first part of your question. If the string you are trying to pass is greater than the 4000 limit of nvarchar you can replace sp_executesql with exec() and make @.string a varchar(8000). It is better practice to use sp_executesql with dynamic sql but in this case it is your only real option.

Sunday, March 25, 2012

CREATE TABLE (starting at row ?)

Essentially what I want to do is...

Copy a table from my main SQL server database to Temp, starting at a particular row. Ex: Only include row 1,000 to 1,999 (end row.)

I've been using DTS Wizard to CREATE TABLE, and it's working fine, but I searched and searched in google and this forum and I can't find how to "start at particular row" when creating table.

Thanks,

Bill

hi Bill,

tables are created "without" rows... tables are defined by attributes implemented as columns..

rows usually are not numbered, so you can not say "start at row 1000 and go on until row 1999"... you can select data and insert it into other tables via the INSERT .. SELECT statement but, for your "requirement", you have to perform sort of paging.. that's to say skip the first "n" rows and proceed with the remaining..

a simple "solution" to get this kind of "paging" can be performed via ROW_NUMBER() new Transact-SQL 2005 function..

you project the underlying table's data adding a monotonically increasing new integer row number value..

if you write

SET NOCOUNT ON; USE tempdb; GO CREATE TABLE dbo.TestTB ( Id int NOT NULL PRIMARY KEY, dataValue varchar(10) NOT NULL ); GO DECLARE @.i int; SET @.i = 1; WHILE @.i <= 1000 BEGIN INSERT INTO dbo.TestTB VALUES ( @.i * 10 , CONVERT(varchar, @.i) + 'abc' ); SET @.i = @.i +1 END; GO WITH CTE AS ( SELECT ROW_NUMBER() OVER( ORDER BY Id ) AS rnum, Id, dataValue FROM dbo.TestTB ) SELECT rnum, Id, dataValue FROM CTE WHERE rnum > 50 AND rnum < 100; GO DROP TABLE dbo.TestTB;

you get all the rows with rnum > 50 and < 100..

and you can even project+insert that result to another destination table like

SET NOCOUNT ON; USE tempdb; GO CREATE TABLE dbo.TestTB ( Id int NOT NULL PRIMARY KEY, dataValue varchar(10) NOT NULL ); CREATE TABLE dbo.TestTB2 ( Id int NOT NULL PRIMARY KEY, dataValue varchar(10) NOT NULL ); GO DECLARE @.i int; SET @.i = 1; WHILE @.i <= 1000 BEGIN INSERT INTO dbo.TestTB VALUES ( @.i * 10 , CONVERT(varchar, @.i) + 'abc' ); SET @.i = @.i +1 END; GO WITH CTE AS (SELECT ROW_NUMBER() OVER( ORDER BY Id ) AS rnum, Id, dataValue FROM dbo.TestTB ) INSERT INTO dbo.TestTB2 SELECT Id, dataValue FROM CTE WHERE rnum > 50 AND rnum < 100; SELECT * FROM dbo.TestTB2; GO DROP TABLE dbo.TestTB, dbo.TestTB2;

regards

|||

Hi Andrea,

I was gone all day, sorry I took so long to respond. I appreciate your reply to my question.

I'm extremely new at this, and this looks like a very long statement. What saying is it's kind of over my head. Would I have to change any other parameters other than the Table names? Please excuse my lack of knowledge.

By the way, I like the name of your company... Insulin Power.

Thanks,

Bill

|||

hi Bill

Car54 wrote:

I'm extremely new at this, and this looks like a very long statement. What saying is it's kind of over my head. Would I have to change any other parameters other than the Table names? Please excuse my lack of knowledge.

as you already have "your own" tables, yes, you have to modify them..

the actual statement you have to modify only is

WITH CTE AS (SELECT ROW_NUMBER() OVER( ORDER BY [Id] ) AS rnum, -- modify the eventual order by column [Id], [dataValue] -- modify the returned columns FROM [dbo].[TestTB] -- modify the original table name ) INSERT INTO [dbo].[TestTB2] -- modify the destination table name SELECT [Id], [dataValue] -- modify the columns (returned by the previous Common Table Expression result) FROM CTE WHERE rnum > 50 AND rnum < 100; -- modify the "range" as required

By the way, I like the name of your company... Insulin Power.

I do just hope you do not suffer the same problem

regards

|||

Hi Andrea, thank you for posting this. I'm really new at this and I don't know where I would put the name of the table I'm copying, and I'm not sure where to put the number of the row to start at. I apologize for my lack of knowledge. Can you post where I need to enter the rows numbers or anything else I might have to do?

By the way, I didn't realize you were a diabetic and that was the reason for using that name for your company. I'm very sorry to hear that, I have friends that are diabetic.

Thanks,

Bill

|||

hi Bill,

Car54 wrote:

Hi Andrea, thank you for posting this. I'm really new at this and I don't know where I would put the name of the table I'm copying, and I'm not sure where to put the number of the row to start at. I apologize for my lack of knowledge. Can you post where I need to enter the rows numbers or anything else I might have to do?

WITH CTE AS (SELECT ROW_NUMBER() OVER( ORDER BY [Id] ) AS rnum, [Id], [dataValue] FROM [dbo].[TestTB] ) INSERT INTO [dbo].[TestTB2] SELECT [Id], [dataValue] FROM CTE WHERE rnum > 50 AND rnum < 100;

[Id] is the column by which you will order the resultset of the CTE you can modify accordingly to your need;

[Id], [dataValue] are the columns you need to select in the CTE to be inserted in the destination table; modify that colum list accordingly to your needs

[dbo].[TestTB] is the original table you need to get data from;

[dbo].[TestTB2] is the destination table;

50 and 100 are the "boundaries" starting from and ending to you like to export..

By the way, I didn't realize you were a diabetic and that was the reason for using that name for your company. I'm very sorry to hear that, I have friends that are diabetic.

fortunately I do have to admit I'm quiet "happy"

regards|||

Thank you Andrea, and I hope you have a great weekend.

Bill

|||

hi Bill,

Car54 wrote:

Thank you Andrea, and I hope you have a great weekend.

Bill

you too

Monday, March 19, 2012

Create row from number range

Greetings:

I would like to create a row for each number range in a table and add its associated fields with it.

Source Table has:

BEG_NUM END_NUM PRINTER USER DATE
150 153 P01 Ed 6-1-07

I would like to convert that to a table which consists of:
Number Printer User Date
150 P01 Ed 6-1-07
151 P01 Ed 6-1-07
152 P01 Ed 6-1-07
153 P01 Ed 6-1-07

How can I create this using SQL? The source table has lots of rows.

Thanks,
Ericselect min(Number) as BEG_NUM
, max(Number) as END_NUM
, Printer as PRINTER
, User as USER
, Date as DATE
from Source
group
by Printer
, User
, Date|||r937,

Thank you for your reply.

Your solution combines the rows. I want to make a row for each number between the beginning and ending number, so if beginning is 150 and ending is 153 i would have 4 rows listed from that one row. 150,151,152,153.

My goal is to have one row for each series of numbers.|||aaargh, i misunderstood, i got your tables backwards, didn't i

you will need an integers table:create table integers (i integer not null primary key);
insert into integers (i) values
(0),(1),(2),(3),(4),(5),(6),(7),(8),(9)then you can generate your range of numbers like this:select BEG_NUM + i as Number
, PRINTER as Printer
, USER as User
, DATE as Date
from integers
inner
join Source
on BEG_NUM + i <= END_NUM|||When I do the 2nd part, I get invalid column name for all except i because I am selecting fields from integer table that don't exist.

I tried adding the correct table names and selecting from both tables and then joining but it doesn't work. It says tables have the same exposed names.

When I tried another way, it gave me 10 rows for each number, which wasnt what i want either :(|||i tested my query and it works

probably your table is actually different from what you posted, or you made an error in your query

can't help you any further unless you show your query|||I apologize, Its working now.

Thank you for all your help!!|||The "filling holes in a table" problem is a well-known "difficult" one in relational databases.
If your database system supports it, you should use recursive SQL for the "integers table" part of the story:WITH integers(i) AS (VALUES (0)
UNION ALL
SELECT i+1 FROM integers
WHERE i < 9)
SELECT ... <Rudy's query>
Now this query is much more flexible in case there are bigger holes than width 10: just replace the "9" by, say, "99".
Or better yet, replace it by (SELECT MAX(End_Num-Beg_Num) FROM Source) or something like that, so that the query becomes completely unparametrized and will work on any table with any numeric column pair.
(Or with a date pair, for that matter.)|||nice trick, peter, and i mean that as a sincere compliment

it is a true "hack" and that is a good thing

:)

Sunday, March 11, 2012

Create or update with updategram

Without knowing whether a row already exists in a table, is it possible to
construct an updategram that would create the row it does not exist or
update it if it does?No.
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm
"Xerox" <info@.thinkscape.com> wrote in message
news:O1aI5TWIFHA.580@.TK2MSFTNGP15.phx.gbl...
> Without knowing whether a row already exists in a table, is it possible to
> construct an updategram that would create the row it does not exist or
> update it if it does?
>

Create or update with updategram

Without knowing whether a row already exists in a table, is it possible to
construct an updategram that would create the row it does not exist or
update it if it does?
No.
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm
"Xerox" <info@.thinkscape.com> wrote in message
news:O1aI5TWIFHA.580@.TK2MSFTNGP15.phx.gbl...
> Without knowing whether a row already exists in a table, is it possible to
> construct an updategram that would create the row it does not exist or
> update it if it does?
>

Thursday, March 8, 2012

Create new row on the fly

I have data that looks like the following

ID
=====
123456
234567
345678
456789/567890
654321/765432/876543

In some instances, the older system had to store multiple IDs in one field and used a "/" delimiter. Does anyone have any ideas on how to create new rows on the fly and split the IDs out to the new rows?

Thanks in advance.

You will need an asynchronous script component to do this. If you type that into Google you'll find loads of resources plus loads in BOL. reply here if you have any problems.

Or, if you know what the maximum number of values you will ever get in a single column are then you could use a Derived Column to split them up and an Unpivot transform to convert the values into new rows.

-Jamie

|||

Jamie Thomson wrote:

You will need an asynchronous script component to do this. If you type that into Google you'll find loads of resources plus loads in BOL. reply here if you have any problems.

I'm facing the same problem and found your article at http://blogs.conchango.com/jamiethomson/archive/2005/07/25/1841.aspx, but I'm stuck due to my ignorance of Visual Basic and the SSIS API.

If I understand correctly, all I need to do is override the Input0_ProcesssInputRow method. Here's my pseudo-code to generate multiple rows whose "code" column is populated from substrings of the "ICS" column, separated by a ",":

Public Overrides Sub Input0_ProcessInputRow(ByVal Row As Input0Buffer)

While ("," occurs in Input0Buffer.Row.ICS)

Output0Buffer.AddRow().code = substring(Input0Buffer.IC, start, length)

End While

End Sub

What does the real code to do that?

Thanks!

|||

Kevin Rodgers wrote:

Jamie Thomson wrote:

You will need an asynchronous script component to do this. If you type that into Google you'll find loads of resources plus loads in BOL. reply here if you have any problems.

I'm facing the same problem and found your article at http://blogs.conchango.com/jamiethomson/archive/2005/07/25/1841.aspx, but I'm stuck due to my ignorance of Visual Basic and the SSIS API.

If I understand correctly, all I need to do is override the Input0_ProcesssInputRow method. Here's my pseudo-code to generate multiple rows whose "code" column is populated from substrings of the "ICS" column, separated by a ",":

Public Overrides Sub Input0_ProcessInputRow(ByVal Row As Input0Buffer)

While ("," occurs in Input0Buffer.Row.ICS)

Output0Buffer.AddRow().code = substring(Input0Buffer.IC, start, length)

End While

End Sub

What does the real code to do that?

Thanks!

That won't work but the following might:

Public Overrides Sub Input0_ProcessInputRow(ByVal Row As Input0Buffer)

While ("," occurs in Input0Buffer.Row.ICS)

Row.code = substring(Input0Buffer.IC, start, length)

End While

End Sub

You don't need to call AddRow() if its a synchronous component.

-Jamie

|||

Jamie Thomson wrote:

That won't work but the following might:

Public Overrides Sub Input0_ProcessInputRow(ByVal Row As Input0Buffer)

While ("," occurs in Input0Buffer.Row.ICS)

Row.code = substring(Input0Buffer.IC, start, length)

End While

End Sub

You don't need to call AddRow() if its a synchronous component.

I may have more output rows than input rows, so it's an asynchronous component by definition, right?

Plus, I need the code that implements the ("," occurs in Input0Buffer.Row.ICS) pseudo-code and the substring(Input0Buffer.IC, start, length) pseudo-code.

|||

Just to follow through, here's the working code:

Public Overrides Sub Input0_ProcessInputRow(ByVal Row As Input0Buffer)

Dim index As Integer = 0

If Not (Row.CSV_IsNull) Then

For Each value As String In Row.CSV.ToString().Split(","c)

'Add a row to the output buffer:

Output0Buffer.AddRow()

'Preserve columns from the input buffer:

'Output0Buffer.key = Row.key

'Output0Buffer.CSV = Row.CSV

'Add output columns:

Output0Buffer.index = index

Output0Buffer.value = value

index += 1

Next

End If

End Sub

Create new row on the fly

I have data that looks like the following

ID
=====
123456
234567
345678
456789/567890
654321/765432/876543

In some instances, the older system had to store multiple IDs in one field and used a "/" delimiter. Does anyone have any ideas on how to create new rows on the fly and split the IDs out to the new rows?

Thanks in advance.

You will need an asynchronous script component to do this. If you type that into Google you'll find loads of resources plus loads in BOL. reply here if you have any problems.

Or, if you know what the maximum number of values you will ever get in a single column are then you could use a Derived Column to split them up and an Unpivot transform to convert the values into new rows.

-Jamie

|||

Jamie Thomson wrote:

You will need an asynchronous script component to do this. If you type that into Google you'll find loads of resources plus loads in BOL. reply here if you have any problems.

I'm facing the same problem and found your article at http://blogs.conchango.com/jamiethomson/archive/2005/07/25/1841.aspx, but I'm stuck due to my ignorance of Visual Basic and the SSIS API.

If I understand correctly, all I need to do is override the Input0_ProcesssInputRow method. Here's my pseudo-code to generate multiple rows whose "code" column is populated from substrings of the "ICS" column, separated by a ",":

Public Overrides Sub Input0_ProcessInputRow(ByVal Row As Input0Buffer)

While ("," occurs in Input0Buffer.Row.ICS)

Output0Buffer.AddRow().code = substring(Input0Buffer.IC, start, length)

End While

End Sub

What does the real code to do that?

Thanks!

|||

Kevin Rodgers wrote:

Jamie Thomson wrote:

You will need an asynchronous script component to do this. If you type that into Google you'll find loads of resources plus loads in BOL. reply here if you have any problems.

I'm facing the same problem and found your article at http://blogs.conchango.com/jamiethomson/archive/2005/07/25/1841.aspx, but I'm stuck due to my ignorance of Visual Basic and the SSIS API.

If I understand correctly, all I need to do is override the Input0_ProcesssInputRow method. Here's my pseudo-code to generate multiple rows whose "code" column is populated from substrings of the "ICS" column, separated by a ",":

Public Overrides Sub Input0_ProcessInputRow(ByVal Row As Input0Buffer)

While ("," occurs in Input0Buffer.Row.ICS)

Output0Buffer.AddRow().code = substring(Input0Buffer.IC, start, length)

End While

End Sub

What does the real code to do that?

Thanks!

That won't work but the following might:

Public Overrides Sub Input0_ProcessInputRow(ByVal Row As Input0Buffer)

While ("," occurs in Input0Buffer.Row.ICS)

Row.code = substring(Input0Buffer.IC, start, length)

End While

End Sub

You don't need to call AddRow() if its a synchronous component.

-Jamie

|||

Jamie Thomson wrote:

That won't work but the following might:

Public Overrides Sub Input0_ProcessInputRow(ByVal Row As Input0Buffer)

While ("," occurs in Input0Buffer.Row.ICS)

Row.code = substring(Input0Buffer.IC, start, length)

End While

End Sub

You don't need to call AddRow() if its a synchronous component.

I may have more output rows than input rows, so it's an asynchronous component by definition, right?

Plus, I need the code that implements the ("," occurs in Input0Buffer.Row.ICS) pseudo-code and the substring(Input0Buffer.IC, start, length) pseudo-code.

|||

Just to follow through, here's the working code:

Public Overrides Sub Input0_ProcessInputRow(ByVal Row As Input0Buffer)

Dim index As Integer = 0

If Not (Row.CSV_IsNull) Then

For Each value As String In Row.CSV.ToString().Split(","c)

'Add a row to the output buffer:

Output0Buffer.AddRow()

'Preserve columns from the input buffer:

'Output0Buffer.key = Row.key

'Output0Buffer.CSV = Row.CSV

'Add output columns:

Output0Buffer.index = index

Output0Buffer.value = value

index += 1

Next

End If

End Sub

Wednesday, March 7, 2012

Create N rows from 1 Row?

Hello...

I have a small question about how to realize something in SSIS.

We are rewriting an Application and we will be normalizing a table. The current Table has Data in the Format:

ID - Name - Type500 - Type1000 - Type2000

1 - Test - 2 - 1 - 0

2 - Test 2 - 0 - 2 - 1

The Targets would be:

ID - Name

1 - Test

2 - Test

TargetID , Type_ID , Date (+ ID Field omitted here)

1 - 1 - 1.1.1900

1 - 1 - 1.1.1900

1 - 2 - 1.1.1900

2 - 2 - 1.1.1900

2 - 2 - 1.1.1900

2 - 3 - 1.1.1900

So basically we need to generate N type rows for each "Count" in the Type Fields. So whats the best aproach to convert this data in an SSIS Package? Currently I am thinking about calling a SP to split the rows, but I dont "like" this aproach since it would place import/migration logic into the database and I would have to "clean up" later.

I can see two choices-

1 - Use a script component.

2 - Write a custom component.

For information, this would be an asynchronous component, as discussed in Books Online.

There is a similar problem covered here http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=511608&SiteID=1

|||

Thanks...

The link is exactly what I was looking for...

Saturday, February 25, 2012

'CREATE INDEX' Statement...Faster?

I am indexing a 32 millon row table with a unqine clustered index and
the run time for the create index statement is over 4 hours long. This
runs is too slow and may cost me a job If i cant find a faster way to
create index on large table.
Can anyone help.
Hi
What is the version of SQL Server?
<GreenHillCourt@.gmail.com> wrote in message
news:a7661fdd-12e9-4a12-969f-6ec961eab992@.n1g2000prb.googlegroups.com...
>I am indexing a 32 millon row table with a unqine clustered index and
> the run time for the create index statement is over 4 hours long. This
> runs is too slow and may cost me a job If i cant find a faster way to
> create index on large table.
> Can anyone help.
|||i don't think it would be faster. Clustered Index is the way data is stored
physically in the hard disk.
bye!
"GreenHillCourt@.gmail.com" wrote:

> I am indexing a 32 millon row table with a unqine clustered index and
> the run time for the create index statement is over 4 hours long. This
> runs is too slow and may cost me a job If i cant find a faster way to
> create index on large table.
> Can anyone help.
>
|||Setting the database to simple recovery should improve compared to full recovery. At least it will
cut down on the logging. But the data shuffling still has to occur of course...
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://sqlblog.com/blogs/tibor_karaszi
"DarthSidious" <DarthSidious@.discussions.microsoft.com> wrote in message
news:15C6F565-D67E-4912-8424-BFBDF0EC88A2@.microsoft.com...[vbcol=seagreen]
>i don't think it would be faster. Clustered Index is the way data is stored
> physically in the hard disk.
> bye!
> "GreenHillCourt@.gmail.com" wrote:
|||<GreenHillCourt@.gmail.com> wrote in message
news:a7661fdd-12e9-4a12-969f-6ec961eab992@.n1g2000prb.googlegroups.com...
>I am indexing a 32 millon row table with a unqine clustered index and
> the run time for the create index statement is over 4 hours long. This
> runs is too slow and may cost me a job If i cant find a faster way to
> create index on large table.
As others have said, if you're creating the clustered index, not much you
can do other than perhaps look at a faster disk subsystem (say raid 10 vs
RAID 5, etc.)
If you mean you're creating a non-clustered index on a table that has an
existing clustered index, one thing that can certainly help is putting the
non-clustered index on its own set of disks.

> Can anyone help.
Greg Moore
SQL Server DBA Consulting Remote and Onsite available!
Email: sql (at) greenms.com http://www.greenms.com/sqlserver.html
|||Have you tried creating the index without clustering? If I'm not mistaken,
it should then not have to physically re-order the data.
<GreenHillCourt@.gmail.com> wrote in message
news:a7661fdd-12e9-4a12-969f-6ec961eab992@.n1g2000prb.googlegroups.com...
>I am indexing a 32 millon row table with a unqine clustered index and
> the run time for the create index statement is over 4 hours long. This
> runs is too slow and may cost me a job If i cant find a faster way to
> create index on large table.
> Can anyone help.
|||"GreenHillCourt@.gmail.com" wrote:
> I am indexing a 32 millon row table with a unqine clustered index and
> the run time for the create index statement is over 4 hours long. This
> runs is too slow and may cost me a job If i cant find a faster way to
> create index on large table.
> Can anyone help.
- drop all unnecessary nonclustered indexes
- if possible, connect in single user mode
- add extra memory to the server, don't "pin" tables
- make sure you have enough I/O and bandwidth, both for your table, the
TempDB and the log file
- make sure you have enough free space before you start, at least 1.5
times the table size
- set the database recovery mode to simple
- choose an appropriate fillfactor. Any value below 70 is probably a bad
idea
4 hours seems excessive. How much space does the table occupy (before
you start)? What is your I/O system (number of disks, etc.)?
Gert-Jan
SQL Server MVP

'CREATE INDEX' Statement...Faster?

I am indexing a 32 millon row table with a unqine clustered index and
the run time for the create index statement is over 4 hours long. This
runs is too slow and may cost me a job If i cant find a faster way to
create index on large table.
Can anyone help.Hi
What is the version of SQL Server?
<GreenHillCourt@.gmail.com> wrote in message
news:a7661fdd-12e9-4a12-969f-6ec961eab992@.n1g2000prb.googlegroups.com...
>I am indexing a 32 millon row table with a unqine clustered index and
> the run time for the create index statement is over 4 hours long. This
> runs is too slow and may cost me a job If i cant find a faster way to
> create index on large table.
> Can anyone help.|||i don't think it would be faster. Clustered Index is the way data is stored
physically in the hard disk.
bye!
"GreenHillCourt@.gmail.com" wrote:
> I am indexing a 32 millon row table with a unqine clustered index and
> the run time for the create index statement is over 4 hours long. This
> runs is too slow and may cost me a job If i cant find a faster way to
> create index on large table.
> Can anyone help.
>|||Setting the database to simple recovery should improve compared to full recovery. At least it will
cut down on the logging. But the data shuffling still has to occur of course...
--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://sqlblog.com/blogs/tibor_karaszi
"DarthSidious" <DarthSidious@.discussions.microsoft.com> wrote in message
news:15C6F565-D67E-4912-8424-BFBDF0EC88A2@.microsoft.com...
>i don't think it would be faster. Clustered Index is the way data is stored
> physically in the hard disk.
> bye!
> "GreenHillCourt@.gmail.com" wrote:
>> I am indexing a 32 millon row table with a unqine clustered index and
>> the run time for the create index statement is over 4 hours long. This
>> runs is too slow and may cost me a job If i cant find a faster way to
>> create index on large table.
>> Can anyone help.|||<GreenHillCourt@.gmail.com> wrote in message
news:a7661fdd-12e9-4a12-969f-6ec961eab992@.n1g2000prb.googlegroups.com...
>I am indexing a 32 millon row table with a unqine clustered index and
> the run time for the create index statement is over 4 hours long. This
> runs is too slow and may cost me a job If i cant find a faster way to
> create index on large table.
As others have said, if you're creating the clustered index, not much you
can do other than perhaps look at a faster disk subsystem (say raid 10 vs
RAID 5, etc.)
If you mean you're creating a non-clustered index on a table that has an
existing clustered index, one thing that can certainly help is putting the
non-clustered index on its own set of disks.
> Can anyone help.
Greg Moore
SQL Server DBA Consulting Remote and Onsite available!
Email: sql (at) greenms.com http://www.greenms.com/sqlserver.html|||Have you tried creating the index without clustering? If I'm not mistaken,
it should then not have to physically re-order the data.
<GreenHillCourt@.gmail.com> wrote in message
news:a7661fdd-12e9-4a12-969f-6ec961eab992@.n1g2000prb.googlegroups.com...
>I am indexing a 32 millon row table with a unqine clustered index and
> the run time for the create index statement is over 4 hours long. This
> runs is too slow and may cost me a job If i cant find a faster way to
> create index on large table.
> Can anyone help.|||"GreenHillCourt@.gmail.com" wrote:
> I am indexing a 32 millon row table with a unqine clustered index and
> the run time for the create index statement is over 4 hours long. This
> runs is too slow and may cost me a job If i cant find a faster way to
> create index on large table.
> Can anyone help.
- drop all unnecessary nonclustered indexes
- if possible, connect in single user mode
- add extra memory to the server, don't "pin" tables
- make sure you have enough I/O and bandwidth, both for your table, the
TempDB and the log file
- make sure you have enough free space before you start, at least 1.5
times the table size
- set the database recovery mode to simple
- choose an appropriate fillfactor. Any value below 70 is probably a bad
idea
4 hours seems excessive. How much space does the table occupy (before
you start)? What is your I/O system (number of disks, etc.)?
--
Gert-Jan
SQL Server MVP

Tuesday, February 14, 2012

create corruption

Is there a way to create db corruption without deleting a row from
sysIndexes? That method still allows dbcc checkdb to return "CHECKDB found 0
allocation errors and 0 consistency errors in database bla" and I want it to
return an error.
TIA,
ChrisR
Ugh! I'm trying to get rid of it and you're trying to create it! ;-)
"ChrisR" <ChrisR@.discussions.microsoft.com> wrote in message
news:8CEFC956-B85C-4A1C-9AF4-D793C14FADB8@.microsoft.com...
> Is there a way to create db corruption without deleting a row from
> sysIndexes? That method still allows dbcc checkdb to return "CHECKDB found
> 0
> allocation errors and 0 consistency errors in database bla" and I want it
> to
> return an error.
> --
> TIA,
> ChrisR
|||Hi,
you could edit the .mdf file with a editor and enter some char's on the file.
I never done it but i think it could work : )
CC
"ChrisR" wrote:

> Is there a way to create db corruption without deleting a row from
> sysIndexes? That method still allows dbcc checkdb to return "CHECKDB found 0
> allocation errors and 0 consistency errors in database bla" and I want it to
> return an error.
> --
> TIA,
> ChrisR
|||That would leave it suspect, not corrupted. Thanks though.
TIA,
ChrisR
"CC" wrote:
[vbcol=seagreen]
> Hi,
> you could edit the .mdf file with a editor and enter some char's on the file.
> I never done it but i think it could work : )
> CC
> "ChrisR" wrote:
|||No - it would only leave it suspect if recovery runs across the page you
corrupted. Your best bet is to delete a row from sysindexes. Instead of the
script you posted in a previous thread, its far easier to create a single
non-clustered index, insert a few rows and then
delete * from sysindexes
where indid=2 and
id=object_id ('mytable')
You should get a variety of errors from CHECKDB.
Paul Randal
Dev Lead, Microsoft SQL Server Storage Engine
This posting is provided "AS IS" with no warranties, and confers no rights.
"ChrisR" <ChrisR@.discussions.microsoft.com> wrote in message
news:A16120B3-C62D-4355-A402-5F858AFEA293@.microsoft.com...[vbcol=seagreen]
> That would leave it suspect, not corrupted. Thanks though.
> --
> TIA,
> ChrisR
>
> "CC" wrote:

create corruption

Is there a way to create db corruption without deleting a row from
sysIndexes? That method still allows dbcc checkdb to return "CHECKDB found 0
allocation errors and 0 consistency errors in database bla" and I want it to
return an error.
--
TIA,
ChrisRUgh! I'm trying to get rid of it and you're trying to create it! ;-)
"ChrisR" <ChrisR@.discussions.microsoft.com> wrote in message
news:8CEFC956-B85C-4A1C-9AF4-D793C14FADB8@.microsoft.com...
> Is there a way to create db corruption without deleting a row from
> sysIndexes? That method still allows dbcc checkdb to return "CHECKDB found
> 0
> allocation errors and 0 consistency errors in database bla" and I want it
> to
> return an error.
> --
> TIA,
> ChrisR|||Hi,
you could edit the .mdf file with a editor and enter some char's on the file
.
I never done it but i think it could work : )
CC
"ChrisR" wrote:

> Is there a way to create db corruption without deleting a row from
> sysIndexes? That method still allows dbcc checkdb to return "CHECKDB found
0
> allocation errors and 0 consistency errors in database bla" and I want it
to
> return an error.
> --
> TIA,
> ChrisR|||That would leave it suspect, not corrupted. Thanks though.
--
TIA,
ChrisR
"CC" wrote:
[vbcol=seagreen]
> Hi,
> you could edit the .mdf file with a editor and enter some char's on the fi
le.
> I never done it but i think it could work : )
> CC
> "ChrisR" wrote:
>|||No - it would only leave it suspect if recovery runs across the page you
corrupted. Your best bet is to delete a row from sysindexes. Instead of the
script you posted in a previous thread, its far easier to create a single
non-clustered index, insert a few rows and then
delete * from sysindexes
where indid=2 and
id=object_id ('mytable')
You should get a variety of errors from CHECKDB.
Paul Randal
Dev Lead, Microsoft SQL Server Storage Engine
This posting is provided "AS IS" with no warranties, and confers no rights.
"ChrisR" <ChrisR@.discussions.microsoft.com> wrote in message
news:A16120B3-C62D-4355-A402-5F858AFEA293@.microsoft.com...[vbcol=seagreen]
> That would leave it suspect, not corrupted. Thanks though.
> --
> TIA,
> ChrisR
>
> "CC" wrote:
>

create corruption

Is there a way to create db corruption without deleting a row from
sysIndexes? That method still allows dbcc checkdb to return "CHECKDB found 0
allocation errors and 0 consistency errors in database bla" and I want it to
return an error.
--
TIA,
ChrisRUgh! I'm trying to get rid of it and you're trying to create it! ;-)
"ChrisR" <ChrisR@.discussions.microsoft.com> wrote in message
news:8CEFC956-B85C-4A1C-9AF4-D793C14FADB8@.microsoft.com...
> Is there a way to create db corruption without deleting a row from
> sysIndexes? That method still allows dbcc checkdb to return "CHECKDB found
> 0
> allocation errors and 0 consistency errors in database bla" and I want it
> to
> return an error.
> --
> TIA,
> ChrisR|||Hi,
you could edit the .mdf file with a editor and enter some char's on the file.
I never done it but i think it could work : )
CC
"ChrisR" wrote:
> Is there a way to create db corruption without deleting a row from
> sysIndexes? That method still allows dbcc checkdb to return "CHECKDB found 0
> allocation errors and 0 consistency errors in database bla" and I want it to
> return an error.
> --
> TIA,
> ChrisR|||That would leave it suspect, not corrupted. Thanks though.
--
TIA,
ChrisR
"CC" wrote:
> Hi,
> you could edit the .mdf file with a editor and enter some char's on the file.
> I never done it but i think it could work : )
> CC
> "ChrisR" wrote:
> > Is there a way to create db corruption without deleting a row from
> > sysIndexes? That method still allows dbcc checkdb to return "CHECKDB found 0
> > allocation errors and 0 consistency errors in database bla" and I want it to
> > return an error.
> > --
> > TIA,
> > ChrisR|||No - it would only leave it suspect if recovery runs across the page you
corrupted. Your best bet is to delete a row from sysindexes. Instead of the
script you posted in a previous thread, its far easier to create a single
non-clustered index, insert a few rows and then
delete * from sysindexes
where indid=2 and
id=object_id ('mytable')
You should get a variety of errors from CHECKDB.
--
Paul Randal
Dev Lead, Microsoft SQL Server Storage Engine
This posting is provided "AS IS" with no warranties, and confers no rights.
"ChrisR" <ChrisR@.discussions.microsoft.com> wrote in message
news:A16120B3-C62D-4355-A402-5F858AFEA293@.microsoft.com...
> That would leave it suspect, not corrupted. Thanks though.
> --
> TIA,
> ChrisR
>
> "CC" wrote:
>> Hi,
>> you could edit the .mdf file with a editor and enter some char's on the
>> file.
>> I never done it but i think it could work : )
>> CC
>> "ChrisR" wrote:
>> > Is there a way to create db corruption without deleting a row from
>> > sysIndexes? That method still allows dbcc checkdb to return "CHECKDB
>> > found 0
>> > allocation errors and 0 consistency errors in database bla" and I want
>> > it to
>> > return an error.
>> > --
>> > TIA,
>> > ChrisR