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
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