Run same command on all SQL Server databases without cursors

Source: Internet
Author: User

original:https://www.mssqltips.com/sqlservertip/1414/run-same-command-on-all-sql-server-databases-without-cursors/

 -- this query would return a listing of all Tables in all databases on a SQL instance:  declare   @command  varchar  (  )  select   @command  =   " use? Select name from sysobjects WHERE xtype =   u   "  ORDER by name    exec  sp_msforeachdb  @command  
--This statement creates a stored procedure in each user database that would return a listing of all users in a database, Sorted by their modification dateDECLARE @command varchar( +)SELECT @command = 'IF"'?"'Not in ("'Master"', "'Model"', "'msdb"', "'tempdb"') BEGIN use? EXEC ("'CREATE PROCEDURE PNewProcedure1 as SELECT name, CreateDate, updatedate from Sys.sysusers ORDER by Updatedate DESC"') END'EXECSp_msforeachdb@command
-- This query would return a listing of all files in the all databases on a SQL instance: EXEC '  "? " "' , Sf.filename, sf.size from Sys.sysfiles SF '

There is no direct way to query for cross-Library stored procedure calls:

DECLARE @sql NVARCHAR (+) = '  "%p_thisisastoredprocedure%"EXEC@sql

Run same command on all SQL Server databases without cursors

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.