Solved

Create Table Event in SQL Server 2008 R2

Posted on 2013-05-29
6
411 Views
Last Modified: 2013-05-30
I am an Access user and getting to know SQL server 2008 R2.

I have a SQL view 'dbo.Grantee_Partner'
I would like to creae a table called 'gifts' based on this view that is refreshed (i.e. overwritten) daily at 6 am based on the view 'dbo.Grantee_Partner'

What is the easiest way to accomplish this in SQL Server?
Please point me to specific examples if possible.

Thank you,
0
Comment
Question by:htamraz1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 7

Expert Comment

by:Ross Turner
ID: 39205069
I would create a sql agent job to run a script like below

SELECT *
INTO gifts
FROM dbo.Grantee_Partner

Open in new window


going to hunt out a good sql agent script for you
0
 
LVL 75

Assisted Solution

by:Aneesh Retnakaran
Aneesh Retnakaran earned 250 total points
ID: 39205070
step1: create the table
           SELECT * INTO dbo.Gifts FROM Grantee_partner where 1 =0
step2. create a sql server agent Job with the following statement  and schedule it to run daily at 6 am
          TRUNCATE TABLE dbo.Gifts
          INSERT INTO dbo.Gifts SELECT * from dbo.Grantee_Partner
0
 

Author Comment

by:htamraz1
ID: 39205150
Thanks RossTurner and aneeshattingal

aneeshattingal - can you explain the syntax 'where 1 =0'
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 39205158
>can you explain the syntax 'where 1 =0'
That will just create the table structure from the view; since '1=0' is always false, it wont populate any data
0
 
LVL 7

Accepted Solution

by:
Ross Turner earned 250 total points
ID: 39205163
Hi  htamraz1

I've created the code below which will create a sql agent job named
Create Table Gifts

i used
http://technet.microsoft.com/en-us/library/ms190268.aspx

The sql that it will intiate is found under the @command
IF OBJECT_ID(''dbo.Gifts'', ''U'') IS NOT NULL
 TRUNCATE TABLE dbo.Gifts
 INSERT INTO dbo.Gifts SELECT * from dbo.Grantee_Partner

Open in new window

pic1CODE THAT WILL CREATE THE JOB
USE msdb ;
GO
EXEC dbo.sp_add_job
    @job_name = N'Create Table Gifts' ;
GO
EXEC sp_add_jobstep
    @job_name = N'Create Table Gifts',
    @step_name = N'Truncate and Generate Table Gifts',
    @subsystem = N'TSQL',
    @command = N'IF OBJECT_ID(''dbo.Gifts'', ''U'') IS NOT NULL
 TRUNCATE TABLE dbo.Gifts
 INSERT INTO dbo.Gifts SELECT * from dbo.Grantee_Partner', 
    @retry_attempts = 0,
    @retry_interval = 0 ;
GO
EXEC dbo.sp_add_schedule
    @schedule_name = N'Generate Table Gifts',
	
    @freq_type = 4,
	@freq_interval=1, 
    @active_start_time = 060000 ;
USE msdb ;
GO
EXEC sp_attach_schedule
   @job_name = N'Create Table Gifts',

   @schedule_name = N'Generate Table Gifts';

GO
EXEC dbo.sp_add_jobserver
    @job_name = N'Create Table Gifts';
GO

Open in new window

0
 

Author Closing Comment

by:htamraz1
ID: 39208163
both expert comments offered a slightly different way of getting the job done, but were very helpful and to the point. Excellent job!
0

Featured Post

Free Webinar: AWS Backup & DR

Join our upcoming webinar with experts from AWS, CloudBerry Lab, and the Town of Edgartown IT to discuss best practices for simplifying online backup management and cutting costs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question