I tried your script and recreated your environment completely and i didn't see any error.
Ran perfectly and created all tables and transferred all data successfully
i tried running under a sql agent job and still no error was generated
but i guess you can update your insert script by making sure the column gets converted under all conditions
[BO_ID] = CASE WHEN isnumeric([BO_ID]) = 1 THEN [BO_ID] ELSE CAST([BO_ID] as DECIMAL(31,0)) END
or
[BO_ID] = CASE WHEN isnumeric([BO_ID]) = 1 THEN [BO_ID] ELSE Null END
change your script to
INSERT INTO OPENQUERY (
ZMMS_ORA
'SELECT
"COMPANYID",
"BO_ID",
"AS CM",
"AS RSM",
"AS SE",
"TS CM",
"TS RAE",
"TS RSM",
"TS SE",
"BBONE AM",
"ASM",
"WCT ASM",
"LS TSM",
"WCT TSM",
"TS TSM",
"AS PM",
"AS DEV",
"TS PM",
"TS DEV",
"TS TECH",
"BB EXEC",
"CHM",
"ANGEL Client Manager (CM)",
"ANGEL Engagement Team",
"BbOne Client Manager (CM)",
"Blackboard Executive",
"CE/Vista Hosting Mgr (ASM)",
"CE/Vista Tech Support (TSM)",
"Complex Hosting Manager (CHM)",
"Learn Client Manager (CM)",
"Learn Hosting Mgr (ASM)",
"Learn Reg Sales Mgr (RSM)",
"Learn Sales Engineer (SE)",
"Learn Services Developer",
"Learn Services ICM Mgr",
"Learn Services Project Manager",
"Learn Tech Support (TSM)",
"Transact Client Manager (CM)",
"Transact Project Manager",
"Transact Reg Acct Exec (RAE)",
"Transact Reg Sales Mgr (RSM)",
"Transact Sales Engineer (SE)",
"Transact Services Technician",
"Transact Tech Support (TSM)",
"Xythos Tech Support (TSM)" FROM ZMMSUSER.PSRPT_TEAM_ROLES_PSFT'
)
SELECT "COMPANYID"
,[BO_ID] = CASE WHEN isnumeric([BO_ID]) = 1 THEN [BO_ID] ELSE CAST([BO_ID] as DECIMAL(31,0)) END
,"AS CM"
,"AS RSM"
,"AS SE"
,"TS CM"
,"TS RAE"
,"TS RSM"
,"TS SE"
,"BBONE AM"
,"ASM"
,"WCT ASM"
,"LS TSM"
,"WCT TSM"
,"TS TSM"
,"AS PM"
,"AS DEV"
,"TS PM"
,"TS DEV"
,"TS TECH"
,"BB EXEC"
,"CHM"
,"ANGEL Client Manager (CM)"
,"ANGEL Engagement Team"
,"BbOne Client Manager (CM)"
,"Blackboard Executive"
,"CE/Vista Hosting Mgr (ASM)"
,"CE/Vista Tech Support (TSM)"
,"Complex Hosting Manager (CHM)"
,"Learn Client Manager (CM)"
,"Learn Hosting Mgr (ASM)"
,"Learn Reg Sales Mgr (RSM)"
,"Learn Sales Engineer (SE)"
,"Learn Services Developer"
,"Learn Services ICM Mgr"
,"Learn Services Project Manager"
,"Learn Tech Support (TSM)"
,"Transact Client Manager (CM)"
,"Transact Project Manager"
,"Transact Reg Acct Exec (RAE)"
,"Transact Reg Sales Mgr (RSM)"
,"Transact Sales Engineer (SE)"
,"Transact Services Technician"
,"Transact Tech Support (TSM)"
,"Xythos Tech Support (TSM)"
FROM CRMRPT..B_RPT_B_TEAM_ROLE;
I'd say the easiest possible way to do this is through PowerShell and SMO. Take the following code for instance:
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo") |
Out-Null
$SqlServerNames = "Server1", "Server2"
$SqlJobName = "YourJob"
foreach ($SqlServerName in $SqlServerNames) {
$SqlServer = New-Object Microsoft.SqlServer.Management.Smo.Server($SqlServerName)
$SqlServer.JobServer.Jobs[$SqlJobName].Start()
}
All this does it loop through a list of servers (explicitly stated at as string array variable, but can easily be obtained also from a SQL Server database table, a text file, etc.), connects to the current server, and then starts the job with the job named whatever you set the $SqlJobName
variable to.
It's also worth noting that the Start()
function will not wait for the job to complete before continuing code execution.
As you can see, in very few lines of PowerShell code you are able to accomplish this task. But I'd take it a step further, such as error handling. You don't want this to bomb out and not know what failed, when it failed, and what servers it did/didn't run on.
foreach ($SqlServerName in $SqlServerNames) {
try {
$SqlServer = New-Object Microsoft.SqlServer.Management.Smo.Server($SqlServerName)
$SqlServer.JobServer.Jobs[$SqlJobName].Start()
}
catch {
# add your error handling code here
## for instance, write the error to a text file
}
}
And, as always with any code, test this out in a non-production environment to ensure that it does what you think it will and should do. Re-work it for your needs/environment.
As per RoKa's comment:
Can this be edited to check for whether the job is currently running, error handling?
Great point, and definitely something to check for (as well as if the job actually exists by checking for a non-null value):
foreach ($SqlServerName in $SqlServerNames) {
try {
$SqlServer = New-Object Microsoft.SqlServer.Management.Smo.Server($SqlServerName)
$SqlJob = $SqlServer.JobServer.Jobs[$SqlJobName]
if (($SqlJob) -and ($SqlJob.CurrentRunStatus -eq [Microsoft.SqlServer.Management.Smo.Agent.JobExecutionStatus]::Idle)) {
$SqlJob.Start()
}
else {
# handle/log accordingly if the job doesn't exist or it's not idle
}
}
catch {
# add your error handling code here
## for instance, write the error to a text file
}
}
EDIT: While re-reading your question, I see SQL Server 2000. Are those going to be the target instances? If that is the case, and you do decide to go with the PowerShell/SMO answer I provided, then definitely definitely definitely test and ensure it is going to do what you want. Test this out very far from production and be 100% sure it does what you think it will.
EDIT: It looks like SMO versions before SQL Server 2012 (i.e. 2005, 2008, and 2008 R2) support managing SQL Server 2000 instances. I'm finding this information through this BOL reference.
Best Answer
In one of our processes we are using Powershell to achieve this.. I am not sure of the link who has written, since it resides in our process, but below script might be useful to you.
#Load the input file into an Object array
#Load the SQL Server SMO Assemly
#Create a new SqlConnection object
#For each server in the array do the following
#IF the output folder does not exist then create it
#Create a new SMO instance for this
#Script out each SQL Server Agent Job for the server
#Use the command below to output each SQL Agent Job to a separate file. Remember to comment out the line above.
#Removed backslash character, typically seen in Replication Agent jobs, to avoid invalid filepath issue
#$srv.JobServer.Jobs | foreach-object -process
}