How to execute insert queries in One database for another database in Azure - azure-sql-database

I need to execute update & insert queries in multiple database of azure in one stored procedure. please help

Actually, if you want to execute the same stored procedure in two different databases, you can keep the same stored procedure in both the databases. If you are referring to cross database queries, then it is not possible directly. However, you can make use of ADF/Databricks to achieve the same.

Related

SQL Server : inserting multi-table output from stored procedure into separate tables

I have a stored procedure that returns several tables (currently consumed by a web service). I now need to insert that output into a SQL Server database.
I can insert a single table if I know the existing definition via
INSERT INTO #temp
EXEC ('myProcSQlhere')
But for this I need to know the definition of the output table in advance.
SELECT *
INTO #temp
FROM OPENQUERY(SERVERNAME, 'EXEC myProcSQlhere')
Which gets around knowing the table definition, but only allows entry into one table. So how do I insert data into multiple tables?
A stored procedure can only return one value. What your stored procedure does is performing multiple selects and so giving multiple result sets. This works for systems that can consume multiple result sets but you are stuck in SQL-Server.
Check if the stored proc doesn't already call independent stored procedures for each result set and if so, call those. Or use the definition of the stored procedure and call each query used within sequentially.
If you have no access to the system providing the stored procedure, you will need to implement a CLR stored procedure and fetch each result set to store the results into different tables.

Stored procedure not compiling with different USE clauses

I have several views in Database 1 and I wrote a stored procedure in database 2. The stored procedure in database 2 references several tables in database 1.
For some reason when I have:
USE Database1
GO
while testing, it works completely fine. But when I use
USE Database2
GO
the stored procedure doesn't compile. No warnings, just continues to spin. The first case only takes about 1 second to run.
Anyone know what could possibly be the issue? When I attempt to run similar stored procedures in database2 that use the same references to database1 it works fine. Also, they are on the same server in SQL Server.
Sorry I am unable to post the code.
SQL Server has to take out locks on the objects so it can create a query plan. It either cannot connect to the database or cannot take the locks it needs.

How to run more than one query at a time in SQL Server

I have to generate a big report consists of 45 insert statements. How can I Run more than one insert statement at a time it by splitting the queries into groups.
USe Stored Procedure for that and by using it u can return value also.
You can write 45 insert statements in stored procedure.
Try using multiple stored procedures where each stored procedure handles several insert statement. Even if you execute these procedures one by one they should be executed in parallel on SQL server as long as you’re using different connections.
You might succeed in running multiple sessions (i.e. logins).[EDIT] I wrote, that an insert locks the table, which is wrong. Thanks #marc_s. [/EDIT]
However, if your insert precedes a complex query, you might be successful, as the queries could be carried out in parallel.
But, it greatly depends on the code.
Isn't there anything you can improve using the existing code? Usually, there is enough room for a performance boost just by looking at the statements.

How to print select results from within a stored procedure

I have a stored procedure that I'm trying to debug (T-SQL).
It contains creates a temporary table, and has several update statements to update it with various data.
How can I insert statements to view the contents of this table at various points during the running of the stored procedure.
Ideally, I'll be running this directly from MS SQL Server Management Studio, and simple output to the Message frame will suffice.
Wouldn't it be possible to SELECT what you need? Sorry if I misunderstood the question. Selected variables/tables will be returned as results

Possible to create a SQL stored procedure for use for all databases

I have a stored procedure, in one database within my SQL server, that sets permissions to all stored procedures at once for that particulat database. Is there a way to create this stored procedure in a way were I can call it easily from any database within the SQL server and if so how do I go about doing such a thing
While the best solution to this specific question of granting execute to all procedures is the one provided by marc_s, the actual question was is there a way to create a single stored procedure and make it available to all databases.
The way to do this is documented at https://nickstips.wordpress.com/2010/10/18/sql-making-a-stored-procedure-available-to-all-databases/:
Create the stored procedure in the master database.
It must be named to start with sp_, e.g. sp_MyCustomProcedure
Execute sys.sp_MS_marksystemobject passing the name of the procedure, e.g. EXEC sys.sp_MS_marksystemobject sp_MyCustomProcedure
Here is a simple example which just selects the name of the current database:
use master
go
create procedure dbo.sp_SelectCurrentDatabaseName as begin
select db_name()
end
go
execute sys.sp_MS_marksystemobject sp_SelectCurrentDatabaseName
go
Calling exec dbo.sp_SelectCurrentDatabaseName on any database will then work.
To mark the procedure as not a system object, there a some nasty hacks suggested at https://social.msdn.microsoft.com/Forums/sqlserver/en-US/793d0add-6fd9-43ea-88aa-c0b3b89b8d70/how-do-i-undo-spmsmarksystemobject?forum=sqltools but it is safest and easiest to just drop and re-create the procedure.
Caveat
Of course creating system procedures like this is breaking the common rule of not naming your own procedures as sp_xxx, due to the possibility of them conflicting with built-in procedures in future versions of SQL Server. Therefore this should be done with care and not just create a load of randomly named procedures which you find useful.
A common simple way to avoid this is to add your own company/personal prefix to the procedure which Microsoft is unlikely to use, e.g. sp_MyCompany_MyCustomProcedure.
I have a stored procedure, in one database within my SQL server, that
sets permissions to all stored procedures at once for that particular
database.
You could archive the same result much easier:
create a new role, e.g. db_executor
CREATE ROLE db_executor
grant that role execute permissions without specifying any objects:
GRANT EXECUTE TO db_executor
This role now has execute permissions on all stored procedures and functions - and it will even get the same permissions for any future stored procedure that you add to your database!
Just assign this role to the users you need and you're done....
Have you tried a 3 or 4 part name?
InstanceName.DatabaseName.dbo.usp_Name
That procedure could in turn reference objects in other databases using the same conventions. So you could parameterize the name of the database to be operated on and use dynamic SQL to generate 4 part names to reference objects such as system tables.