INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Insert rows using CTE

Insert rows using CTE

(OP)
Hi Guys,

Given sample tables below:

CREATE TABLE [dbo].[TBL1](
[ID] [int] IDENTITY(1,1) NOT NULL PRIMARY KEY,
[ITEM_CODE] [varchar](20) NOT NULL,
[CREATED_DATETIME] [datetime] NOT NULL
);

CREATE TABLE [dbo].[TBL2](
[ID] [int] IDENTITY(1,1) NOT NULL PRIMARY KEY,
[TBL1ID] [int] NOT NULL,
[DESC] [varchar](50) NOT NULL DEFAULT 'TBL2 DESC',
[CREATED_DATETIME] [datetime] NOT NULL
);

CREATE TABLE [dbo].[TBL3](
[ID] [int] IDENTITY(1,1) NOT NULL PRIMARY KEY,
[TBL2ID] [int] NOT NULL,
[DESC] [varchar](50) NOT NULL DEFAULT 'TBL3 DESC',
[CREATED_DATETIME] [datetime] NOT NULL
);

SAMPLE TBL1 DATA:

INSERT INTO TBL1 VALUES ('SERVER1', getdate());
INSERT INTO TBL1 VALUES ('SERVER2', getdate());
INSERT INTO TBL1 VALUES ('SERVER3', getdate());
INSERT INTO TBL1 VALUES ('SERVER4', getdate());
INSERT INTO TBL1 VALUES ('SERVER5', getdate());


WHERE:

TBL2.TBL1ID = TBL1.ID
TBL3.TBL2ID = TBL2.ID

Assuming TBL1 contains data, is there a way to be able to INSERT rows on TBL2 and TBL3 using CTE?
If not possible, what other means of inserting the rows without using CURSOR.

Im using MSSQL server 2012

TIA,
yorge

RE: Insert rows using CTE

CTE meaqns common table expression and is generally used to define a query (SELECT) and name it, as if there was a table by that name, it's not used for INSERTs.

If you want data of TBL1 in TBL2, you do INSERT INTO TBL2 SELECT * FROM TBL1, in general a more precise query in regard of what fields to copy over. With typical table relations in 4NF you just copy the primary key over as foreign key into further tables to let them point to the same data and NOT copy it. If you want something else, then please sepcify what your goal is, I also don't understand what your mentioning of the relations of TBL2 to TBL1 and TBL3 to TBL2 are all about, but this might play a role in what I said about relations.

Bye, Olaf.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close