How do you re-run an already executed query from 'Query History' pgadmin? - pgadmin-4

As in pgAdmin 4 screenshot, one can see a list of recently executed queries.
The play button, Execute / Refresh(F5) tool executes the most recent query instead of the selected/highlighted one.
I know I can click on a query, and from the right side pane click on copy and go to the Query Editor to run it.
My question is, is there a shortcut or some options using GUI in pgAdmin 4 to re-run the query of my choice (selected/highlighted query) from history?

Unfortunately, you cannot execute this in View/Edit table mode. However, you can copy the SQL and open a new query tool, paste the query and run it.

Related

Export multiple stored procedures to text files

In SQL Server Management Studio, I have a database with 200 stored procedures. I'm exporting each stored procedure as a script, by right clicking -> script stored procedure -> CREATE To -> File.
Is it possible to export all procedures at once, using a powershell script or anything else?
Use the Generate Scripts tool in SSMS:
Right Click Database in Object Explorer.
Tasks -> Generate Scripts.
If given the "tutorial" click Next.
Select "Select specific database objects" and tick "Stored Procedures". Click Next.
Choose export method. Likely here you want "Save as script file" with "one script file per object" selected. Ensure you choose the export location.
Click Next and Finish buttons as required.
If you are using MGT for MSSQL, then you follow these steps.
open MGT MSSQL and connect and open the list of databases
right button the the database where the stored procs are, this will open the con menu
go to "Tasks"
select "Generate Scripts"
select specific object, in your case, select "Stored Procedures" (you can select all or as you need)
then press "Next"
at the "Set Scripting Options" form, select the option; if you want all selected procs in one file or not, choose the directory where you want your files to be generated.
press "Next"
at the "Summary" form press "Next"
at the "Save or Publish Scripts" Press finish..
DONE.
There are advance options at step 7. to create each procs with drop and create option or just create options. go through those if you need.
tc.
I have developed a C# application that can accomplish this, scripting out all tables, views, stored procedures, functions, etc. to text files (one per object). This is very useful for scripting out the objects on a regular basis then keeping track of them in a Git repository. Download the DB Schema Export Tool from https://github.com/PNNL-Comp-Mass-Spec/DB-Schema-Export-Tool/releases
Example command line for automation:
DB_Schema_Export_Tool.exe
C:\Cached_DBSchema
/Server:Proteinseqs
/DBList:Manager_Control,Protein_Sequences
/Sync:"F:\Projects\Database_Schema\DMS"
/Git /Commit
/L /LogDir:Logs
/Data:ProteinSeqs_Data_Tables.txt
On Linux, use mono DB_Schema_Export_Tool.exe
Information on Mono
There are other command line switches available; see the Readme.
In addition to SQL Server, the software supports Postgres. Example output files can be found at https://github.com/PNNL-Comp-Mass-Spec/DBSchema_DMS/tree/master/DMS_Pipeline

SQL Server Management Studio 2016 → Activity monitor → Show execution plan

I have a problem when I want to see the execution plan of an expensive recent query. The problem is that the result is displayed in XML and not as a design over the execution plan.
Does anyone know how to fix this?
First of all, for me it works out of the box. Microsoft SQL Server Management Studio 13.0.15700.28. Make sure that you have the latest version. The one that I used for this test is not the very latest, but it works.
I open Activity Monitor in SSMS, expand the Recent Expensive Queries tab, right-click on a query and choose Show Execution Plan in the popup menu, then SSMS opens a new window with the graphical view of the plan.
If I right-click the graphical view of the plan there are commands "Save Execution Plan As..." and "Show Execution Plan XML" in the popup menu, which allow to save XML file with the plan.
Maybe all this works because I have SQL Sentry Plan Explorer installed.
In any case, if you have an XML file with the plan you can open it in SSMS as a graphical view. Change extension of the file from .xml to .sqlplan. Then open this file in SSMS using standard File - Open command.
I would highly recommend to use SentryOne Plan Explorer for analyzing the execution plans. It is free and significantly better than SSMS. It can open .xml and .sqlplan files with the plan.
I'm not sure if this will help but you could try execute SET SHOWPLAN_ALL OFF in a query window select the query you want to execute and press CTRL + L (by default, unless you've changed it) to view the graphical execution plan in the query window without actually executing your query.
Also, you could play around these SET commands:
SET SHOWPLAN_XML ON | OFF
SET SHOWPLAN_TEXT ON | OFF
SET SHOWPLAN_ALL ON | OFF
SET STATISTICS XML ON | OFF
SET STATISTICS PROFILE ON | OFF
SET STATISTICS IO ON | OFF
SET STATISTICS TIME ON | OFF
For further info, check this technet article: https://technet.microsoft.com/en-us/library/ms180765(v=sql.105).aspx
users must have the appropriate permissions to execute the Transact-SQL queries for which a graphical execution plan is being generated, and they must be granted the SHOWPLAN permission for all databases referenced by the query

Automate execution of multiple SQL Server 2012 scripts in specific order

I needed some guidance with a task I have never worked with SQL Server 2012. Your help would be very much appreciated. I have inherited a data model in SQL Server.
Basically, I have 5 SQL scripts:
Script A
Script B
Script C
Script D
Script E
For running successfully script B,it needs access to tables generated by script A to perform calculation. Basically, the scripts are feeding each other. I need to run the scripts in a specific order.
My first idea was "stored procedure". So far, I have in the past only written a stored procedure to execute code from the same script that do not require executing other scripts.
My question is, what are some ideas you propose for automatically executing the above 5 scripts in a specific order? how can I do this? What would you recommend me to think when doing this?
Running the complete list of scripts takes around 10 hours.
You could easily create a new SQL Server Agent Job task like this:
Expand the SQL Server Agent node and right click the Jobs node in
SQL Server Agent and select 'New Job'
In the 'New Job' window enter the name of the job and a description
on the 'General' tab.
Select 'Steps' on the left hand side of the window and click 'New'
at the bottom.
In the 'Steps' window enter a step name and select the database you
want the query to run against.
Paste in the T-SQL command you want to run into the Command window
and click 'OK'.
Click on the 'Schedule' menu on the left of the New Job window and
enter the schedule information (e.g. daily and a time).
Click 'OK' - and that should be it.
Repeat these steps for each scripts in the order you want them to run. And, there you go!

Command(s) completed successfully but... the tables are not created

I backed up my database table's and entire schema into .sql script using Visual Studio's Database Publishing Wizard.
I then tried to re-create those tables on another PC, not before re-creating the database itself, with the same exact name and everything (using a script that I created via SSMS's Script Database as).
I then open that tables .sql file using SSMS and execute it.
SSMS reports:
Command(s) completed successfully
But examining Object Explorer reveals that no tables were created.
Why is this happening?
What have I missed?
I've just been having the exact same problem symptoms also using Visual Studio's Database Publishing Wizard, - but with a slightly different cause / fix.
Even though SQL Server Management Studio says it is connected to the correct database (in the drop down in the ribbon, and in the status bar of the window), it wasn't actually connected to anything.
To identify and fix either:
SELECT DB_NAME() AS DataBaseName
If you get
master
(or any other unexpected database name) as the result, then you are connected to the wrong database and should select the correct DB from the dropdown. This was the cause of your problem.
If you get
Command(s) completed successfully
then somehow you aren't connected at all - this happened to me.
To fix, click the "change connection" button to disconnect and reconnect.
Check whether you have selected database. Most of the times we execute query in Master db by mistake.
-- Mark as answered if this answer really answered your question
Check if you are running "Execute" or jut Parsing the code. It was a late night, I was tired, and kept running a query to create a table, successfully, but no new table. The next day with a clear mind i noticed that i was not actually running the query, i was parsing it.

How do I export my views from a database?

I have a number of views in my SQL Server database.
How do I export these as CREATE VIEW scripts?
I tried to right click the database - script database as - create to - new query editor window, but it doesn't show my my views (or tables even for that matter).
Any ideas?
In SSMS, if you right click the DB -> Tasks -> Generate Scripts... - that will take you through a wizard.
You can run through the wizard and select your DB, then the views as AdaTheDev mentioned or you can right click on each query and generate a script directly.
I prefer another way, because always I edit the views to remove "not necessary script generated SQL syntax" (depends on the point of view :)).
Right click the view, Generate script, CREATE in, new window.
See screenshot below (sorry for german version, but should work anyway).
I tried to right click the db - script database as - create to - new query editor window, but it doesn't show my my views (or tables even for that matter).
The purpose of the menu you mentioned is only to create the empty database, without any tables, views or anything else.
If you want to script anything beyond the empty database, you have to use the wizard already mentioned in AdaTheDev's answer.
Maybe you receive empty page because source of VIEW was encrypted or removed.
In older SQL editions there was a trick - after creating VIEW (SQL Server compiled it) developer could remove source of VIEW statement to protect it from "deassemblation". In current editions there is possibility to encrypt source of VIEW statement.

Resources