Search Linked server name in sql server agent job - sql-server

I have around 4 servers(test1 , test2, test3, test4) and each server with 5 - 6 databases. Each server has around 60 SQL server agent jobs scheduled.
Each each job inside the servers has combination of sql statements and SSIS packages. Inside SQL statements, we have stored procs, which access tables from another servers.
Now i want to check whether server test2, is being used in sql server agent jobs of other servers.
i tried with the following query but not able to get any result
SELECT Job.name AS JobName, Job.enabled AS ActiveStatus,
JobStep.step_name AS JobStepName, JobStep.command AS JobCommand
FROM sysjobs Job INNER JOIN
sysjobsteps JobStep
ON Job.job_id = JobStep.job_id
WHERE JobStep.command LIKE '%test2%'
but when i check manually in test1 server , i can see test2 server being used in stored procs and ssis packages.
How can this be achieved ?

This is a little tricky. You will need to search the procedures, file system and SQL Server separately. The job table only contains the entry point, not the definition for each step.
SSIS Packages, stored in SQL Server
SSIS packages are stored in the msdb table sysssispackages. The definition is stored in the column packagedata. You'll need to cast the data, before you can read/search it.
-- How to read package data column.
SELECT TOP 10
[name],
packagedata AS [Before],
CAST(CAST(packagedata AS VARBINARY(MAX)) AS XML) AS [After]
FROM
sysssispackages
;
SSIS Packages, stored on the file system
These are XML text files. You can search using your favorite method. If you want to build your own solution .Net has some great file handling ablities.
Stored Procedures
There are several methods, all of which return data from the system tables. One approach is to use the object definition function.
-- Returns definition of stored procedure.
SELECT
OBJECT_DEFINITION(OBJECT_ID(N'[Your-Schema].[Your-Procedure]'))
;
If you want to bring this altogether
Using the table sysjobsteps, you could build a package/app that searches for a given server name. The final product would need to support all three search methods. You might be able to use the subsystem column, to choose which search method should be used for each step.

Related

How to automatically execute "create table" statements generated with select statement?

I have to move some data from our remote customer's mssql database to remote db2 database. On mssql server I own I created linked server to customer's source mssql database (I only have read permissions on) and created another linked server to remote db2 server.
I created some complex transformation, but for simplicity of this forum, lets simplify:
select
'create table [remotedb2server].[remotedb2db].[myschema].'
+ name
+ "COLUMS HERE AND SOME COMPLEX TRANSFORMATION"
from [remotemssqlserver].[remotemssql].[sys].[tables]
How to automatically execute above "create table" statements generated from select statement? I need a solution to be able to schedule it regularly in e.g. SQL Agent job.
I also have "drop table" select, "insert into" select etc and I would like to use the same logic.
This is known as dynamic SQL. You can execute arbitrary generated SQL by calling sp_executesql and passing in your command as a parameter. Keep in mind this has the potential to be very dangerous, as it exposes you to sql injection vulnerabilities.

Access Database - Understand Pass Through

I have been given an Access Database that I have to try to decipher what it is doing.
As a start I see that there is a Pass Through query with a command like:
Exec RefreshGLTableLatestEntries
#sourceDB = 'DB_NAME' ,
#tablePrefix = 'TableName$' ,
#logFile = 'C:\logDB.txt'
When I run it I will get something like:
Result
Success... 108 rows inserted with a total amount of $0.000000
What I don't understand is where are the rows being copied from or copied to.
In the MSSQL database I don't see a table, query, standard procedure or function called 'TableName$'. There are quite a few tables & queries called 'TableName$SomethingElse'. Is there a way to see more details on where is the data coming from?
Similarly, how can I see where are the rows being inserted to? I can not find any file named 'logDB.txt' in my hard disk to see the log. I would suspect that it might not say much more that '...108 rows insterted...'
I'm using:
Access 2016 from Office 365, Version 1609
MS SQL Server Management Studio v17.1
Any ideas on how to get more information on how to get more information on what the Pass Through do?
A Pass-Through query in Access is equivalent to running its SQL code in SQL Server Management Studio.
(In the database that is designated by the connection string of the Pass-Through query.)
The SQL is sent as-is to MSSQL and run there.
RefreshGLTableLatestEntries is the stored procedure that is executed here. You need to locate and analyze it in SQL Server.

How do you call a SQL Server Stored Procedure in BI Publisher 11g?

I am trying to call a Microsoft SQL Server Stored Procedure that delivers data in table like format (rows / columns) in Oracle BI Publisher 11g (11.1.1.7).
Selecting procedure call as a data source for the data model does not work because BIP expects it to behave like a PL/SQL call to an Oracle database instead.
Oracle developers claim this is not supported by the software.
Is there any way around this restriction?
Although not supported out-of-the-box by BI Publisher 11g, there is a workaround to the problem. It involves tricking the software into thinking it is making a standard PL/SQL call when in fact in reality it is executing a stored procedure on the SQL Server datasource.
1) Make sure you have the native MS SQL server library installed on your weblogic server running the BIP instance.
It can be downloaded from MSDN here: http://msdn.microsoft.com/en-us/sqlserver/aa937724.aspx - depending on your JRE version you'll want to use one or the other jar file:
For JRE 1.6 and above, use sqljdbc4.jar. For 1.5 and below, use sqljdbc.jar.
You should place this in your $MIDDLEWARE_HOME\user_projects\domains\$your_domain_here$\lib\ folder and remember to restart weblogic server afterwards.
2) Inside BI Publisher administration, create a new JDBC datasource.
Our example works with following properties:
Driver Type: Microsoft SQL Server 2008 Database Driver Class: com.microsoft.sqlserver.jdbc.SQLServerDriver Connection String: jdbc:weblogic:sqlserver://[hostname]:[port];databaseName=[database name]
Fill in username/pw and test connection (if driver is installed correctly, this should work just fine)
3) Create a new datamodel.
Choose SQL query as your dataset. Here, add in these properties:
Data Source: your JDBC data source
Type of SQL: Non-standard SQL
Row Tag Name: (choose one yourself) - for now just write test.
4) Under SQL query, we now need to convince BIP that it is calling an Oracle SP when in fact it is calling an existing stored procedure on your MS SQL datasource.
This part is assuming your stored procedure delivers N amount of rows and column labels over.
Here is how we solved it for our SP that is called nrdart_get_custody_holding_headers_sp '2014-11-25' where the parameter is a date supplied by the user.
declare #var1 datetime
declare #sql varchar(255)
set #var1 = '2014-11-25'
set #sql = 'nrdart_get_custody_holding_headers_sp' +'''' + cast(#var1 as varchar) + ''''
exec (#sql)
Here, we are declaring some SQL Server datatypes, and setting them as our date parameter and as our procedure call name using some creative use of the cast function and escape characters, before finally calling exec on the stored procedure.
Parameter var1 will also work if you use a standard BIP parameter instead of our hard-coded example above
i.e. :userDate where :userDate is referring to an existing parameter called userDate in the datamodel.
Don't worry if you don't see row/column labels after clicking OK. Instead, click on "view data" and there you go. Rows and columns with data from your SP on Microsoft SQL Server. Now proceed to save this as sample data and design report layout as you would normally do. For non-date parameters you might need to play around a little bit with datatypes, but I don't see why you shouldn't get it to work with integers or varchars as well.

Data sync check among two SQL Server database

We've two SQL server database. The source server has data populated from an external system and a destination database on a remote server (used by a web app). There's an SSIS package which maps column from source tables to destination (column names differ) and populates data to maintain the sync.
Now, to ensure that both the database are in sync for which we've an SP which shows record count and for some parent-child relationships it shows child count for each parent record (i.e. Brandwise Item count). Someone has to logon to both the servers, execute the SP and get the data manually. Then compare the results to ensure that both the db are in sync.
Now, to automate this process, we've done the following-
Add the destination server as a "Linked Server"
Use "EXEC msdb.dbo.sp_send_dbmail" along with "#attach_query_result_as_file =1"
Create an SSIS job which will execute the email SP for both the servers
So, this is how we get two emails which has query results attached to
it. And then comparing the text files completes the db sync check.
I believe this can be made better - now that we're able to access the destination server as a linked server. Its my first time so I'd request some experienced guys to share their approach, probably something beyond a join query with the linked server.
Since you have access to server as Linked server you can directly run query and compare data.
Please check this
You can modify SSIS jobs to send mails based on this query result.
I'm using the following query which is a simple version and gives me differences of both the sides -
(Select s.Title, s.Description from ERPMasterBrand as s EXCEPT
Select d.Title, d.Description from MasterBrand as d)
UNION
(Select s.Title, s.Description from MasterBrand as s EXCEPT
Select d.Title, d.Description from ERPMasterBrand as d)
Any better suggestions? I've tested and it gives desired results - hope I'm not being misguided :-) by my own solution.

SQL Server cross database alias

I'm trying to understand how I can use an alias to reference another database in the same instance, without having to use a hardcoded name.
The scenario is as below:
I have a data db with stores data, an audit db which keeps all changes made. for various reason, i want to keep the audit data in a separate database, not least because it can get quite large and for reporting purposes.
In the data db, I don't want to reference this by a hardcoded name but an alias so that in different environments, I don't have to change the name and various sp's to reference the new name.
for example:
mydevdata
mydevaudit
If a sp exists in mydevdata such as which calls the mydevaudit, I don't want to change the sp when I go to test where the db's may be called mytestdata and mytestaudit. Again, for various reasons, the database names can change, more to do with spaces an instances etc.
So if I had procedure in mydevdata:
proc A
begin
insert into mydevaudit.table.abc(somecol)
select 1
end
when I go to test, I don't want to be change the procedure to reference another name, (assume for sake of argument that happened)
Instead I am looking to do something like:
proc A
begin
insert into AUDITEBALIAS.table.abc(somecol)
select 1
end
I am interested in finding out how I could do something like that, and the pro's and cons.
Also, dymnamic SQL is not an option.
thanks in advance for you help.
You may be able to use synonyms
CREATE SYNONYM WholeTableAliasWithDBetc FOR TheDB.dbo.TheTable
This means all object references in the local DB are local to that DB, except for synonyms that hide the other database from you.
You can also use stored procedures in the audit DB. There is a 3rd form of EXEC that is little used where you can parametrise the stored proc name
DECLARE #module_name_var varchar(100)
SET #module_name_var = 'mydevaudit.dbo.AuditProc'
-- SET #module_name_var = 'whatever.dbo.AuditProc'
EXEC #module_name_var #p1, #p2, ...
Obviously you can change module_name_var to use whatever DB you like
I've just posted this to How to create Sql Synonym or "Alias" for Database Name? which is a workaround for the same situation:
There is a way to simulate this using a linked server. This assumes you have two SQL servers with the same set of databases one for development/test and one live.
Open SQL Server Management Studio on your development/test server
Right click Server Objects > Linked Servers
Select New Linked Server...
Select the General page
Specify alias name in Linked server field - this would normally be the name of your live server
Select SQL Native Client as the provider
Enter sql_server for Product Name
In Data Source specify the name of the development server
Add Security and Server Options to taste
Click OK
The above is for SQL Server 2005 but should be similar for 2008
Once you've done that you can write SQL like this:
SELECT * FROM liveservername.databasename.dbo.tablename
Now when your scripts are run on the development server with the linked server back to itself they will work correctly pulling data from the development server and when the exact same scripts are run on the live server they will work normally.

Resources