creating a sql server job

I need help creating a job on the sqlbox2 which checks the tables on the sqlbox1 periodically (through the Linked server) and create appropriate view when a new table appears.

Any help would be appreciated.

Thanks
Fay ADBAAsked:
Who is Participating?
 
pcelbaCommented:
And what does not work for you?

Did you create Linked server already? (You may use SSMS for this purpose.)
The SQL Job is also possible to create in SSMS easily. Everything you need is just access rights and a Stored procedure which does the job. And to create the job itself is very simple. Wizard in SSMS is self descriptive.

Let suppose the view name is derived from the table name and the linked server is defined already. Then you may obtain a list of views which do not exist by the following query in sqlbox2:

SELECT t.* FROM LinkedSQLbox1.YourDatabaseOnBox1.sys.objects t
  LEFT JOIN CurrentDatabase.sys.objects v ON v.name = 'linkedview_' + t.name AND v.type = 'V'
 WHERE t.type = 'U' AND v.name IS NULL

So you may now go through the rows and define the views. This means to define a cursor and store the CREATE VIEW command into a string and execute this string in a loop.

If you would like to check existing views then use slightly updated query:

SELECT t.* FROM LinkedSQLbox1.YourDatabaseOnBox1.sys.objects t
  LEFT JOIN CurrentDatabase.sys.objects v ON v.name = 'linkedview_' + t.name AND v.type = 'V'
 WHERE t.type = 'U' AND v.name IS NOT NULL

You'll possibly need additional logic for new/deleted columns, then you should drop the view when the underlying table is dropped etc. etc.
0
 
Vitor MontalvãoMSSQL Senior EngineerCommented:
So if you want the job, then Trigger isn't need anymore.
0
 
Fay ADBAAuthor Commented:
Correct, trigger doesn't seem like a good idea here
0
 
pcelbaCommented:
The DDL trigger on sqlbox1 could still be useful. It can generate necessary SQL commands to CREATE/ALTER views and store them to a table. The job on sqlbox2 can just read these commands and execute them...

Of course, my favorite is to do everything on one place, ie. sqlbox2.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.