Passing params to stored procedure located on another server - sql

I have a stored procedure on tempdb database (under System Databases) on server A. The stored procedure has 3 parameters, Param1, Param2, and Param3 which all accept varchars.
I would like to execute this stored procedure on server B for a database called SomeDB.
With the stored procedure, I'd like to pull data from different tables on SomeDB, and then put the results in a new table that will be created called SomeNewTable, which again will be located on SomeDb
Let's assume that the servers are linked.
How should I approach this?

Write the stored procedure on ServerB . Test the stored proc so that it can run completely on ServerB when you are connected to server B. So everything is happening in Server B.
Now just call the Server B stored proc from Server A.
On Server B exec SomeDb.dbo.myStoredProc(p1,p2,p3)
On Server A exec LinkedServerB.SomeDb.dbo.myStoredProc(p1,p2,p3)
Do not use tempDb for anything - you can not reliably put a stored proc in TempDb*
The fact that it will work in temp db for awhile is misleading.
If you are doing a coding exercise they may have you use tempDb but that is a special situation.
You will have to deal with the security jump from ServerA to ServerB depending on how the linked server was setup.
*(yes I know how to do it permanently - but I doubt it works in azure)

Related

How can i creat a stored procedure on mirroring database using SQL Server?

I have two servers: server1 includes the primary_DB and server2 includes the mirror_DB, I created a database in another server3, then I created two servers linked to the servers 1 and 2.
The objectif is to create a stored procedure to collect data from server 1 or 2 (depending on the active databse) and insert data into server 3 using the likned server.
I wanted create two jobs using two stored procedure, one with linked1 and second with the linked2 in case of server failover, but creation is not allowed when the database acts as a mirror database.
My question is how to retrieve data using a stored procedure from a database that can become a mirror database?
i found the solution, i used Dynamic SQL on creation the stored procedure

Can I check the dependencies of a stored procedure with other database?

Thank you for reading.
Temp_server
- Temp_db1
. Table : Temp_table
. stored procedure (they refer Temp_table of Temp_db1)
- Temp_db2
. stored procedure (they refer Temp_table of Temp_db1)
Assume that:
there is a server (called Temp_server)
there are two databases (called Temp_db1, Temp_db2)
In Temp_db1, there is a Temp_table and some stored procedures that refer to Temp_table.
In this situation, I can view the list of stored procedure which refer to Temp_table.
But, in SSMS, it shows only the stored procedure of Temp_db1. This means, if there is a stored procedure that is saved on Temp_db2 and refers to Temp_table of Temp_db1, it doesn't show up.
Can I view this, too, somehow?
Can I view this, too, somehow?
You can use sys.sql_expression_dependencies
It shows you dependencies in other databases and even other(linked) servers.
Here is an example when it shows up my historical triggers that writes to another database, Storico.
You can see also test_powershell stored procedure that uses xp_cmdshell from master
and sp_write_execution_log procedure that uses loopback server to execute another sp, sp_write_execution_log_lnk.

Call stored procedure from Elastic Database in Azure

Here is the scenario:
Elastic Database A connected to database B
Database B contains stored procedure that returns result set needed in database A
As far as I can tell the title can't be done. Only tables or views seem to work from my testing.
So the next though was to yield the stored procedure as a view in database B then call the view from DB A.
But views cant call stored procedures, even tried looking into table valued functions between the view and the stored procedure, but that's not permitted either.
How can I get the result set from a stored procedure in DB B into DB A?
Currently the only way to execute remote Stored Procedure calls using Elastic Query is when your External Data Source is defined using a "sharded" setup. E.g. you have defined an external source with
CREATE EXTERNAL DATA SOURCE MyElasticDBQueryDataSrc WITH
(TYPE = SHARD_MAP_MANAGER, ... )
In that case, you have access to a utility procedure called SP_Execute_Fanout which can be used to invoke a stored procedure (or perform ANY SQL operation) on each shard, and return a UNION ALL result set. This utility proc is detailed here
This capability is not yet available with Elastic Query's "Vertically Partitioned" scenario (e.g. TYPE = RDBMS), however we will be adding this type of functionality as the preview continues, so that invoking a remote stored proc in a single DB becomes simple.

Multi Server Queries

Currently we are having 5 servers and they will be expanded to more servers in coming months. Now we are facing problem of handling them and syncing all servers. So we are using SQL Compare 10. But is there any procedure by which we can connect to these server and update those Stored Procedures at one go.
Suppose we are having 3 servers, with their IPs and Username and Passwords
Now if i change one procedure in one database in one of the server and reflect it to all the db on different server how to accomplish the same.
I was thinking of creating a procedure for which the parameters needs to be passed including the stored procedure name and then in that SP it will connect to databases and either ALTER or DELETE followed by CREATE.

Running synchronous commands to between two sql servers

I'm running a stored procedure on server1 from my application. The stored procedure does a bunch of stuff and populate a table on server2 with the result from the procedure.
I'm using linked server to accomplish this.
When the stored procedure is done running the application continues and tries to do some manipulation of the result from the stored procedure.
My problem is that the results from the stored procedure has not been completely inserted into the tables yet, so the manipulation of the tables fails.
So my question is. Is it possible to ensure the insert into on the linked server is done synchronous? I would like to have the stored procedure not return until the tables on the linked server actually is done.
You can use an output parameter of the first procedure. When the table is create on the second server the output parameter value will be return to your application and indicates the operation is ready.
If the things are difficult then this you can try setting a different isolation level of your store procedure:
http://msdn.microsoft.com/en-us/library/ms173763.aspx
I found the reason for this strange behavior. There was a line of code in my stored procedure added during debug that did a select on a temporary mem table before the data in the same table was written to the linked server.
When the select statement was run, the control was given back to my application and at the same time the stored procedure continued running. I guess the stored procedure was running synchronously from the start.