Solved

Create Table Event in SQL Server 2008 R2

Posted on 2013-05-29
6
415 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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

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!

Question has a verified solution.

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

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

752 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