I’m trying to write T-SQL command(s) that will copy data from a database with some normalized tables to two new tables in the same database.
The purpose of the employee database is to maintain the data used to track information about the work performed by the employees and the supervisor.
I need help writing the query that will copy data from the existing EmpTimesheet table into the new Task Occurrences table. The query will also copy data from the new Task Occurrences table and the existing Trusts and Topics tables into the new Employee Tasks table. Any help with the code will be greatly appreciated.
QUERIES
The code shown below contains comments that indicate the source and destination tables.
-- Copy data into the the new Task Occurrences table. Copy data from the existing EmpTimesheet table.DECLARE @tblTaskOccurrences TABLE ( [TaskOccurrenceID] [int] IDENTITY(1,1) PRIMARY KEY NOT NULL, [TaskOccurDate] [datetime] NULL, [TaskOccurStartTime] [datetime] NULL, [TaskOccurEndTime] [datetime] NULL)INSERT INTO @tblTaskOccurrences (TaskOccurrenceID, TaskOccurDate, TaskOccurStartTime , TaskOccurEndTime )SELECT etDate, etStartTime, etEndTimeFROM tblEmpTimesheet-- Copy data into the new Employee Tasks table. Copy data from the existing EmpTimesheet, Trusts and Topics tables into the new Employee Tasks table. Copy data from the new Task Occurrences table into the new Employee Tasks table.DECLARE @EmployeeTasks TABLE ( [TaskOccurrenceID] [int] NULL, [etEmpTimesheetID] [int] NULL, [TrustID] [int] NULL, [mstsTopicID] [int] NULL)INSERT INTO @tblEmployeeTasks (TaskOccurrenceID, etEmpTimesheetID , TrustID , mstsTopicID )--Existing Emp Timesheet tableSELECT etDate, etStartTime, etEndTimeFROM tblEmpTimesheetUNION ALL--New Task Occurrences tableSELECT TaskOccurrenceID, FROM tblTaskOccurrencesUNION ALL--Existing Trusts tableSELECT TrustID, FROM tblTrustsUNION ALL--Existing Topics tableSELECT mstsTopicID, FROM tblmtsTopics
The error message shown below occurs after the following query is executed:
-- Copy data into the the new Task Occurrences table. Copy data from the existing EmpTimesheet table.DECLARE @tblTaskOccurrences TABLE ( [TaskOccurrenceID] [int] IDENTITY(1,1) PRIMARY KEY NOT NULL, [TaskOccurDate] [datetime] NULL, [TaskOccurStartTime] [datetime] NULL, [TaskOccurEndTime] [datetime] NULL)INSERT INTO @tblTaskOccurrences (TaskOccurrenceID, TaskOccurDate, TaskOccurStartTime , TaskOccurEndTime )SELECT etDate, etStartTime, etEndTimeFROM tblEmpTimesheet
Error:
Msg 120, Level 15, State 1, Line 8
The select list for the INSERT statement contains fewer items than the insert list. The number of SELECT values must match the number of INSERT columns.
The code shown below is incomplete. I need help with the query.
-- Copy data into the new Employee Tasks table. Copy data from the existing EmpTimesheet, Trusts and Topics tables into the new Employee Tasks table. Copy data from the new Task Occurrences table into the new Employee Tasks table.DECLARE @EmployeeTasks TABLE ( [TaskOccurrenceID] [int] NULL, [etEmpTimesheetID] [int] NULL, [TrustID] [int] NULL, [mstsTopicID] [int] NULL)INSERT INTO @tblEmployeeTasks (TaskOccurrenceID, etEmpTimesheetID , TrustID , mstsTopicID )--Existing Emp Timesheet tableSELECT etDate, etStartTime, etEndTimeFROM tblEmpTimesheetUNION ALL--New Task Occurrences tableSELECT TaskOccurrenceID, FROM tblTaskOccurrencesUNION ALL--Existing Trusts tableSELECT TrustID, FROM tblTrustsUNION ALL--Existing Topics tableSELECT mstsTopicID, FROM tblmtsTopics
Also, in the UNION ALL block, all SELECT clauses in the UNION must match.
Explain in more detail what you're trying to pull off there.
Mark01
ASKER
Jim Horn, I ran the query shown below and received a "(478 row(s) affected)" message. However, when I run the select * from tblTaskOccurrences query, it shows an empty table. Why is the table empty?
Copy data from the existing EmpTimesheet table.DECLARE @tblTaskOccurrences TABLE ( [TaskOccurrenceID] [int] IDENTITY(1,1) PRIMARY KEY NOT NULL, [TaskOccurDate] [datetime] NULL, [TaskOccurStartTime] [datetime] NULL, [TaskOccurEndTime] [datetime] NULL)INSERT INTO @tblTaskOccurrences (TaskOccurDate, TaskOccurStartTime , TaskOccurEndTime )SELECT etDate, etStartTime, etEndTimeFROM tblEmpTimesheet
One of the purposes of the query shown below is to copy existing data into the new Employee Tasks table. The existing Emp Timesheet table contains the employee start times and end times. I am trying to copy the primary key (etEmpTimesheetID) for each record into the new Employee Tasks table.
I am also trying to copy the primary key for the existing Trusts and Topics tables into the new Employee Tasks table.
Finally, I am trying to copy the primary key for the new Task Occurrences table into the new Employee Tasks table.
Explain in more detail what you're trying to pull off there.