Showing posts with label duplicate. Show all posts
Showing posts with label duplicate. Show all posts

Wednesday, March 7, 2012

Alter Column to IDENTITY

I used Select Into... to duplicate a table, but lost the IDENTITY and PK
constraints in the process. How do I get those back? Thanks.
Here are the details:
Original Table:
TABLE A (
[ID] INT IDENTITY PRIMARY KEY
, ColA smallint NOT NULL
, ColB varchar(20) NOT NULL
, ColC int NULL
, ColD numeric(38, 6) NULL
)
After SELECT * INTO TABLE_B FROM TABLE_A:
TABLE B (
[ID] INT NOT NULL
, ColA smallint NOT NULL
, ColB varchar(20) NOT NULL
, ColC int NULL
, ColD numeric(38, 6) NULL
)
I tried something like this, but it didn't work:
ALTER TABLE TABLE_B
ALTER COLUMN [ID] INT IDENTITY PRIMARY KEY> I tried something like this, but it didn't work:
> ALTER TABLE TABLE_B
> ALTER COLUMN [ID] INT IDENTITY PRIMARY KEY
You can't do this; T-SQL does not allow you to add or drop the IDENTITY
property. What you need to do can be better illustrated by performing the
action in Enterprise Mangler and viewing the code that it produces. It's
rather ugly, basically it creates a work table, moves all the data over,
drops the original and renames the new. All with a huge xlock, of course.
I shouldn't have to tell you what this means on a large-ish table.|||Short answer - you can't. But you can add a *new* identity column to the
table then drop the old column. Easiest way might be to make the change via
EM - design table and save the change script.
HTH
Jerry
"Boddhicitta" <Boddhicitta@.discussions.microsoft.com> wrote in message
news:6E0241AC-792C-4D1A-92E4-9CFCF99413AF@.microsoft.com...
>I used Select Into... to duplicate a table, but lost the IDENTITY and PK
> constraints in the process. How do I get those back? Thanks.
> Here are the details:
> Original Table:
> TABLE A (
> [ID] INT IDENTITY PRIMARY KEY
> , ColA smallint NOT NULL
> , ColB varchar(20) NOT NULL
> , ColC int NULL
> , ColD numeric(38, 6) NULL
> )
> After SELECT * INTO TABLE_B FROM TABLE_A:
> TABLE B (
> [ID] INT NOT NULL
> , ColA smallint NOT NULL
> , ColB varchar(20) NOT NULL
> , ColC int NULL
> , ColD numeric(38, 6) NULL
> )
> I tried something like this, but it didn't work:
> ALTER TABLE TABLE_B
> ALTER COLUMN [ID] INT IDENTITY PRIMARY KEY|||Drop the column and add it again as an Identity column
http://sqlservercode.blogspot.com/
"Boddhicitta" wrote:

> I used Select Into... to duplicate a table, but lost the IDENTITY and PK
> constraints in the process. How do I get those back? Thanks.
> Here are the details:
> Original Table:
> TABLE A (
> [ID] INT IDENTITY PRIMARY KEY
> , ColA smallint NOT NULL
> , ColB varchar(20) NOT NULL
> , ColC int NULL
> , ColD numeric(38, 6) NULL
> )
> After SELECT * INTO TABLE_B FROM TABLE_A:
> TABLE B (
> [ID] INT NOT NULL
> , ColA smallint NOT NULL
> , ColB varchar(20) NOT NULL
> , ColC int NULL
> , ColD numeric(38, 6) NULL
> )
> I tried something like this, but it didn't work:
> ALTER TABLE TABLE_B
> ALTER COLUMN [ID] INT IDENTITY PRIMARY KEY|||Thanks everyone. These are all kind of yucky for different reasons. Is
there a better way to copy a table and retain the constraints? DTS?
"Boddhicitta" wrote:

> I used Select Into... to duplicate a table, but lost the IDENTITY and PK
> constraints in the process. How do I get those back? Thanks.
> Here are the details:
> Original Table:
> TABLE A (
> [ID] INT IDENTITY PRIMARY KEY
> , ColA smallint NOT NULL
> , ColB varchar(20) NOT NULL
> , ColC int NULL
> , ColD numeric(38, 6) NULL
> )
> After SELECT * INTO TABLE_B FROM TABLE_A:
> TABLE B (
> [ID] INT NOT NULL
> , ColA smallint NOT NULL
> , ColB varchar(20) NOT NULL
> , ColC int NULL
> , ColD numeric(38, 6) NULL
> )
> I tried something like this, but it didn't work:
> ALTER TABLE TABLE_B
> ALTER COLUMN [ID] INT IDENTITY PRIMARY KEY|||Sure, create the table with the constraints in place, then insert the data
(specifying all columns except the identity column).
A
"Boddhicitta" <Boddhicitta@.discussions.microsoft.com> wrote in message
news:40361790-544E-4404-91B0-8F88D1F97B97@.microsoft.com...
> Thanks everyone. These are all kind of yucky for different reasons. Is
> there a better way to copy a table and retain the constraints? DTS?|||Try this:
CREATE TABLE B (
[ID] INT IDENTITY PRIMARY KEY
, ColA smallint NOT NULL
, ColB varchar(20) NOT NULL
, ColC int NULL
, ColD numeric(38, 6) NULL
)
GO
SET IDENTITY_INSERT B ON
GO
INSERT INTO B ([ID], ColA, ColB, ColC, ColD)
SELECT [ID], ColA, ColB, ColC, ColD FROM A
SET IDENTITY_INSERT B OFF
GO
"Boddhicitta" <Boddhicitta@.discussions.microsoft.com> wrote in message
news:40361790-544E-4404-91B0-8F88D1F97B97@.microsoft.com...
> Thanks everyone. These are all kind of yucky for different reasons. Is
> there a better way to copy a table and retain the constraints? DTS?
> "Boddhicitta" wrote:
>|||> Enterprise Mangler
LOL!! Thanks, I really needed that! HA HA HA HA!
Peace & happy computing,
Mike Labosh, MCSD
"When you kill a man, you're a murderer.
Kill many, and you're a conqueror.
Kill them all and you're a god." -- Dave Mustane|||Oops, typo. :-)
"Mike Labosh" <mlabosh@.hotmail.com> wrote in message
news:%23j9uk1O1FHA.3376@.TK2MSFTNGP14.phx.gbl...
> LOL!! Thanks, I really needed that! HA HA HA HA!|||While I grateful for the DDL, I hope that you are not you actaully
using IDENTITY as a key? FUNDAMENTA! WRONG ! Look for a relational
key.
Surely you had enough basic to know that code like "IDENTITY PRIMARY
KEY " means thart you have ABSOLUTELY NO CONCEPT OF RbMS.

Friday, February 24, 2012

almost duplicate records

Hi,
Is there a way to 'merge' almost duplicate records. I have records which
are the same but the other duplicate record can differ from the
other. Normally i just delete the duplicate records, but in this case i
can't.
For example (in a 1-N relation):
id | field1 | field2 | field3
1 | 11.5 | 52.0 | null
1 | null | null | yes
should be:
id | field1 | field2 | field3
1 | 11.5 | 52.0 | yes
Is this possible?Maybe like this:
SELECT id, SUM(f1), SUM(f2), MAX(f3)
FROM YourTable
GROUP BY id ;
David Portas
SQL Server MVP
--|||David Portas wrote:
> Maybe like this:
> SELECT id, SUM(f1), SUM(f2), MAX(f3)
> FROM YourTable
> GROUP BY id ;
>
Hi David,
I tried that, but that will only work with numeric values. I have
numeric as well as varchars.
Any idea how i can solve this?|||SUM will only work with numerics. MIN and MAX will work with VARCHARs
too.
The answer all depends on what you want to achieve. In you example data
what result would you have required if Field3 on the first row had been
"No" instead of NULL?. You would have to devise an expression that
makes some sense based on your knowledge of the data.
There may be other alternatives. For example you could pick the row for
each ID that has the fewest NULL attributes. Example:
ALTER TABLE YourTable ADD i INTEGER IDENTITY
GO
SELECT P.id, P.f1, P.f2, P.f3
FROM YourTable AS P,
(SELECT MIN(i) AS i
FROM YourTable AS T2
WHERE
CASE WHEN T2.f1 IS NULL THEN 1 ELSE 0 END+
CASE WHEN T2.f2 IS NULL THEN 1 ELSE 0 END+
CASE WHEN T2.f3 IS NULL THEN 1 ELSE 0 END =
(SELECT MIN(
CASE WHEN T3.f1 IS NULL THEN 1 ELSE 0 END+
CASE WHEN T3.f2 IS NULL THEN 1 ELSE 0 END+
CASE WHEN T3.f3 IS NULL THEN 1 ELSE 0 END)
FROM YourTable AS T3
WHERE T2.id = T3.id)
GROUP BY id) AS Q
WHERE P.i = Q.i ;
David Portas
SQL Server MVP
--