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.

Fay ADBAAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Vitor MontalvãoMSSQL Senior EngineerCommented:
So if you want the job, then Trigger isn't need anymore.
Fay ADBAAuthor Commented:
Correct, trigger doesn't seem like a good idea here
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 = 'linkedview_' + AND v.type = 'V'
 WHERE t.type = 'U' AND 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 = 'linkedview_' + AND v.type = 'V'

You'll possibly need additional logic for new/deleted columns, then you should drop the view when the underlying table is dropped etc. etc.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.