How to View Execution Plan for Query Containing a Temp Table in Toad for SQL Server? - tsql

I am trying to tune the performance of a stored procedure that contains a temp table in Toad for SQL Server. After selecting "Include Actual Execution Plan" from the 'Editor' menu, I run the query. The Results Set returns values as expected, however, the Execution Plan tab shows the following error:
Invalid object name '#temp'.
I have tried creating the temp tables first then just executing the SELECT statement that references it, I tried creating the temp tables as global temp tables and running the SELECT statement in another window, and I have messed with the SHOWPLAN_TEXT and STATISTICS PROFILE (as mentioned in this question) but I keep receiving the same error. The only thing I have not tried is using a table variable, but the changes I will be making cannot be done on table variables, so this is not really an option for me at this time.
Has anyone else come across this or have any ideas as to what I might be doing wrong?

You'll want to use the ISQL command line utility on a machine that has SQL Server client package installed. Or any other utility that can submit a query to SQL Server.
ISQL Docs and How to get an execution plan (2nd part of the post)

Related

Is there a way for a PostgresQL procedure to somehow print out its table context for test purpose?

I am a new man for PostgresQL; working in DBEaver. I have created a procedure that modifies, among others, a temp table. I would like to print out the table for testing purposes: to see what is in the table now.
In T-SQL I could just execute “select * from MyTestTable”; and this was output to SQL Studio Grid tab. This did not break the procedure.
Now on Postgres I am using DBeaver and get errors when I try to use the same approach.
A question to experienced PostgresQL: how you cope with that? Is there any way to “peek my nose” into middle of a proc and see – what data are at given moment in table. If no - how to debug large and complicated procedures without ability to look at produced data Grid?

SQL Server Query in the debug mode: querying a table?

I have a large script that I need to debug to catch an error. There is a table in the script declared as a variable. Some t-sql makes an insert into that table. I would like to select from the table while in the debug mode. I have the "locals" window opened on the screen but I cannot see the contents of the table there, just the variable itself, neither can I select from the temporary table-variable when the code execution stops at the desired breakpoint.
Is there a way I can query the table in the debug mode? Thanks!
For the purposes of debugging, you could replace the table variable (#tableName) with a global temporary table (##tableName).
Table variables and local temporary tables (#tableName) only exist within the session where they're defined, so can only be queried within that session. Global temporary tables can be accessed from other sessions and will persist until all connections to them are dropped, so you'll be able to check results from a different SSMS window as the script is executing in it's window.
You'll want to comment out the table variable definition, then add the CREATE TABLE ##... statement. After that, Find & Replace should get your script ready (and put it back when you're done).
Here's the documentation on Temporary Tables.

Sqlite dump outputting tables out of order

I'm trying to convert a Sqlite3 database to Postgres by dumping it to raw SQL, editing the SQL and then loading it via psql, summarized by this script.
The overall approach seems fine, except for one problem. When I try to run the final load with psql, I get errors like "table x does not exist", because Sqlite is outputting tables referred to by foreign keys after the tables that refer to them. This causes psql to abort all further statements with the message:
ERROR: current transaction is aborted, commands ignored until end of transaction block
The only immediately solution I see is to manually go through all the SQL and try to manually reorder the CREATE TABLE commands so that the proper order is maintained. However, this is very tedious.
Is there any way to force Sqlite to output SQL so that tables are output in order of dependency?

Statement to display create table SQL

Is there a stored procedure or some SQL that I could run that would display the SQL for creating a table from an existing table? Like sp_helptext to display the contents of a function or stored procedure. Basically, is there a way to do the Script Table As->CREATE TO method?
The answer is no. If you start profiler, and run [scirpt table]>[create to] in SSMS, then you'll see a series of sp_executesql being ran on sys.* tables. This means that no CREATE TABLE commands are stored anywhere in SQL server, and SSMS assemblies CREATE statements for a table from a lot of different sources.
On the other hand, if run [script view]>[create to], you'll see a simple query from sys.all_objects, sys.sql_modules, sys.system_sql_modules, where definitions are stored.

SQL query to list all dependent entities

An SQL table has hundreds of tables, stored procedures and functions.
I am trying to put together an SQL query that will return all the dependencies of a given set of tables. Is there a way to accomplish this using SQL Server Management Studio without writing queries?
Updated: Simplified the question to the point.
In SSMS, just right click on the table and choose "View Dependencies". As far as scripting, take a look at this article.
EDIT: In SSMS, you can only see it for one. The reason why is because of the stored procedure that is run to view them only takes one database object. So to script multiple, you'd simply need to use multiple lines of EXEC sp_depends #objname = N'DATABASE.OBJECT'; for the tables/views/stored procedures/functions that you want to get dependencies for. One approach would be to use a script like the following to get the unique list of all dependent objects that will have to be included:
CREATE TABLE #dependents (obj_name nvarchar(255), obj_type nvarchar(255))
-- Do this for every primary object you're concerned with finding dependents for
INSERT INTO #dependents (obj_name, obj_type)
EXEC sp_depends #objname = N'DATABASE.OBJECT'
-- ...
SELECT DISTINCT obj_name, obj_type
FROM #dependents
DROP TABLE #dependents
I just blog something similar to this that might help:
Knowing What to Test When Changing a SQL Server Object.
Another approach would be to right click the database and select "Tasks" and then "Generate Scripts...", check the checkbox "Script all objects in the selected database". This will give you a giant text file that you can then search.