Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8398913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:09:56+00:00 2026-06-09T21:09:56+00:00

I have a table like this: CREATE Table tblClassProperty ( ClassPropertyID integer IDENTITY(1,1) PRIMARY

  • 0

I have a table like this:

CREATE Table tblClassProperty (
    ClassPropertyID integer IDENTITY(1,1) PRIMARY KEY,
    Active bit DEFAULT 0 NOT NULL,
    DEL bit DEFAULT 0 NOT NULL,
    FINAL bit DEFAULT 0 NOT NULL,
    Locked bit DEFAULT 0 NOT NULL,
    _Project integer FOREIGN KEY REFERENCES tblProject(ProjectID),
    _Property integer FOREIGN KEY REFERENCES tblProperty(PropertyID),
    _Class integer FOREIGN KEY REFERENCES tblClass(ClassID)
    CONSTRAINT CClassProperty UNIQUE(_Project,_Property,_Class))

I am migrating data from an old table (in another DB) into this one. For the most of the columns I am just copying the data from the other table. The foreign key columns don’t exist in the old DB, they are just random values linked with the corresponding tables in the new DB. For your solution please take it for granted that the values for the columns _Project, _Property and _Class need to be generated the way they are now (with the SELECT TOP 1 ...)

INSERT INTO ClassDB..tblClassProperty (Active, DEL, FINAL, Locked, 
    _Project, _Property, _Class)
SELECT Active, DEL, FINAL, Locked, 
    (SELECT TOP 1 ProjectID FROM ClassDB..tblProject ORDER BY NEWID()),
    (SELECT TOP 1 PropertyID FROM ClassDB..tblProperty ORDER BY NEWID()),
    (SELECT TOP 1 ClassID FROM ClassDB..tblClass ORDER BY NEWID()),
FROM ClassDB_Access..tblClassProperty

Now, the INSERT INTO script won’t run because of the unique constraint, and the constraint must stay as it is.

My question: I need a script that copies the existing columns, generates the foreign keys as above, BUT the generated values for the tuple (_Project, _Property, _Class) will be unique as per constraint.

The script has to run in SQL Server 2008 R2, and in the future also in Oracle.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T21:09:57+00:00Added an answer on June 9, 2026 at 9:09 pm

    You might cross join all three columns, assign random row numbers to them and join them back to tblClassproperty augmented by row_number:

    ; with randomized as (
       select a.ProjectID,
              b.PropertyID,
              c.ClassID,
              row_number() over(order by newid()) rn
         from ClassDB..tblProject a
        cross join ClassDB..tblProperty b
        cross join ClassDB..tblClass c
    ),
    ordered as (
       select Active, DEL, FINAL, Locked,
              row_number() over (order by newid()) rn
         from ClassDB_Access..tblClassProperty
    )
    INSERT INTO ClassDB..tblClassProperty (Active, DEL, FINAL, Locked, 
        _Project, _Property, _Class)
    select ordered.Active, ordered.DEL, ordered.FINAL, ordered.Locked, 
           randomized.projectID, randomized.PropertyID, randomized.ClassID
      from ordered
     inner join randomized
        on ordered.rn = randomized.rn
    

    This will not work in Oracle without reshuffling cte parts – or you might rewrite this using derived tables for instant compatibility.

    Disclaimer: Other than syntax check this is not tested. Performance might be awful.

    Edit: Forgot to mention that, in case of duplicate values in IDs, you might want to do distinct before cross joins.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have something like this: create table account ( id int identity(1,1) primary key,
I have a table like this: CREATE TABLE records (_id INTEGER PRIMARY KEY, question
I have a table that's created like this: CREATE TABLE bin_test (id INTEGER PRIMARY
I have a pg table like this: CREATE TABLE order_status_history ( order_id integer NOT
I have a table like this: create table SomeTable ( tableKey int identity(1,1) not
I have a table defined like this CREATE TABLE OrderItemRoomings( OrderItemRoomingId int IDENTITY(1,1) NOT
I have a SQL lookup table like this: CREATE TABLE Product(Id INT IDENTITY PRIMARY
I have a table like this: CREATE TABLE [dbo].[Scores]( [Id] [int] IDENTITY(1,1) NOT NULL,
I have a table like this: CREATE TABLE book_info ( book_id VARCHAR(32) not null,
I have table like this: CREATE TABLE Date_Table (Year DATETIME , Month1 INTEGER, Month2

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.