Fay A
asked on
sql job
I have a table that populates automatically with create, alter and drop view values. How can I execute these values, it needs to be executed on a different database. I can copy and paste and execute it manually but I would like to automate the process.
file.PNG
file.PNG
If the table contain actual statements, then you can loop thru it and execute those statements thru the job. Once they execute, update a flag on the original table.
ASKER
Can you show me an example please?
1. Alter Table TableContainingScriptsOnOt herDB add IsExecuted bit default 0
2. Now update the value for the "IsExecuted" for the statements you ran manually.
3. Create a stored procedure with this logic
declare @SQL varchar(1000) , @id int
select top 1 @SQL =statement, @id = id from otherdb..TableContainingSc riptsOnOth erDB where IsExecuted = 0
While @@rowcount > 0
BEGIN TRY
Execute (@SQL)
update otherdb..TableContainingSc riptsOnOth erDB set isExecuted = 1 Where id =1
select top 1 @SQL =statement, @id = id from otherdb..TableContainingSc riptsOnOth erDB where IsExecuted = 0
END TRY
BEGIN CATCH
select Error_number()
END CATCH
4. test this sp, call it on sql job
2. Now update the value for the "IsExecuted" for the statements you ran manually.
3. Create a stored procedure with this logic
declare @SQL varchar(1000) , @id int
select top 1 @SQL =statement, @id = id from otherdb..TableContainingSc
While @@rowcount > 0
BEGIN TRY
Execute (@SQL)
update otherdb..TableContainingSc
select top 1 @SQL =statement, @id = id from otherdb..TableContainingSc
END TRY
BEGIN CATCH
select Error_number()
END CATCH
4. test this sp, call it on sql job
ASKER
So, I changed the job accordingly and I am getting this back, any idea what 208 is?
(No column name)
208
here's the script, any idea what's wrong here
declare @SQL varchar(1000) , @id varchar(1000)
select top 1 @SQL = script , @id = rowid from PipelineTrueUp.dba.viewCre ationQueue where completed = 0
While @@rowcount > 0
BEGIN TRY
Execute (@SQL)
update PipelineTrueUp.dba.viewCre ationQueue set completed = 1 Where rowid =1
select top 1 @SQL = script, @id = rowid from PipelineTrueUp.dba.viewCre ationQueue where completed = 0
END TRY
BEGIN CATCH
select Error_number()
END CATCH
(No column name)
208
here's the script, any idea what's wrong here
declare @SQL varchar(1000) , @id varchar(1000)
select top 1 @SQL = script , @id = rowid from PipelineTrueUp.dba.viewCre
While @@rowcount > 0
BEGIN TRY
Execute (@SQL)
update PipelineTrueUp.dba.viewCre
select top 1 @SQL = script, @id = rowid from PipelineTrueUp.dba.viewCre
END TRY
BEGIN CATCH
select Error_number()
END CATCH
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
208 Invalid object name 'PipelineTrueup.dbo.viewte st'. CREATE VIEW pipeline.vw_viewtest as SELECT a.* From PipelineTrueup.dbo.viewtes t a WITH (NOLOCK)
makes sense
I guess 208 is the error # here
makes sense
I guess 208 is the error # here
are you able to run the same create statement on the same sql window ? it should be ran on the target database. Ensure that there are corresponding schema and ensure that the sql login has sufficient permission. Also change the variable declaration like this
declare @SQL varchar(max) , @id int
declare @SQL varchar(max) , @id int
ASKER
I am running it now, seems like it is working
ASKER
Thank you so much