Avatar of BobRosas
BobRosas
 asked on

Insert partial results into table.

The attached code below works but now I'd like to insert EmployeeID and RandomDateTime into a new table.  I want Dept, Firstname and Lastname to show when it's run but I don't need to save that to the new table.

I tried the following but I get the error...
    There is already an object named 'SOGItblRandom' in the database.
I want to be able to append the new results to the same table each time it's run.  How do I do that?
 
   ...
,CURRENT_TIMESTAMP As RandomDateTime
      INSERT INTO [TCP].[dbo].SOGItblRandom
FROM [TCP].[dbo].[EmployeeList]
   ...

SELECT TOP (SELECT cast(round(25 * COUNT(EmployeeID) * 1.0/ 100, 0) as integer)
	    FROM [TCP].[dbo].[EmployeeList]
	    WHERE Department = ('SR7012') AND [Suspend] <> 1
	   ) 
       [EmployeeId]
      ,[Department] 
      ,[FirstName]
      ,[LastName]
      ,CURRENT_TIMESTAMP As RandomDateTime
FROM [TCP].[dbo].[EmployeeList]
WHERE Department  = ('SR7012') AND [Suspend] <> 1
ORDER BY NEWID()

Open in new window

Microsoft SQL Server 2008Microsoft SQL Server 2005

Avatar of undefined
Last Comment
BobRosas

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Lowfatspread

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
G Trurab Khan

Try this
SELECT TOP (SELECT cast(round(25 * COUNT(EmployeeID) * 1.0/ 100, 0) as integer)
	    FROM [TCP].[dbo].[EmployeeList]
	    WHERE Department = ('SR7012') AND [Suspend] <> 1
	   ) 
       [EmployeeId]
      ,[Department] 
      ,[FirstName]
      ,[LastName]
      ,CURRENT_TIMESTAMP As RandomDateTime
INTO [TCP].[dbo].SOGItblRandom
FROM [TCP].[dbo].[EmployeeList]
WHERE Department  = ('SR7012') AND [Suspend] <> 1
ORDER BY NEWID()

Open in new window

BobRosas

ASKER
The first solution worked great!
Thank you both for your assistance and taking the time to help.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck