Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Create Table Event in SQL Server 2008 R2

Posted on 2013-05-29
6
Medium Priority
?
419 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 1000 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
Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

 
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 1000 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 learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

670 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