dnlnln / generate-sql-merge

Generate SQL MERGE statements with Table data

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Temporary Tables are not supported

Brennanb414 opened this issue · comments

Both Local Temporary tables and Global Temporary tables created in the same session as the SP are not included in the INFORMATION_SCHEMA.TABLES table, and are thus not found.

When you try to find a temp table it returns a "User table or view not found.
Make sure you have SELECT permission on that table or view" error.

Steps to recreate:

Create a table called "[test].[testTable]" then run the following:

SELECT * INTO [test].[#test123] FROM [test].[testTable] 

EXEC sp_generate_merge
                        @table_name = '#test123', --The table / view for which the MERGE statement will be generated using the existing data
                        @target_table = 'testTable', --Use this parameter to specify a different table name into which the data will be inserted/ updated / deleted
                        @from = NULL, --Use this parameter to filter the rows based on a filter condition(using WHERE)
                        @include_timestamp = 0, --Specify 1 for this parameter, if you want to include the TIMESTAMP / ROWVERSION column's data in the MERGE statement
                        @debug_mode = 0, --If @debug_mode is set to 1, the SQL statements constructed by this procedure will be printed for later examination
                        @schema = 'test', --Use this parameter if you are not the owner of the table
                        @ommit_images = 0, --Use this parameter to generate MERGE statement by omitting the 'image' columns
                        @ommit_identity = 0, --Use this parameter to ommit the identity columns
                        @top = NULL, --Use this parameter to generate a MERGE statement only for the TOP n rows
                        @cols_to_include = NULL, --List of columns to be included in the MERGE statement
                        @cols_to_exclude = NULL, --List of columns to be excluded from the MERGE statement
                        @update_only_if_changed = 1, --When 1, only performs an UPDATE operation if an included column in a matched row has changed.
                        @delete_if_not_matched = 0, --When 1, deletes unmatched source rows from target, when 0 source rows will only be used to update existing rows or insert new.
                        @disable_constraints = 0, --When 1, disables foreign key constraints and enables them after the MERGE statement
                        @ommit_computed_cols = 0, --When 1, computed columns will not be included in the MERGE statement
                        @include_use_db = 1, --When 1, includes a USE[DatabaseName] statement at the beginning of the generated batch
                        @results_to_text = 0, --When 1, outputs results to grid/ messages window.When 0, outputs MERGE statement in an XML fragment.
                        @include_rowsaffected = 0, --When 1, a section is added to the end of the batch which outputs rows affected by the MERGE
                        @nologo = 1,
                        @batch_separator = ''-- Batch separator to use

Here is the output:

(201 rows affected)
Msg 50000, Level 16, State 1, Procedure sp_generate_merge, Line 216 [Batch Start Line 0]
User table or view not found.
Make sure you have SELECT permission on that table or view.

I found information about where the temp tables are stored here

This adds some weight to resolving #17

Actually, it seems it is in INFORMATION_SCHEMA, you just need to run the utility in the context of tempdb, rather than the user db (this seems to apply even when using sys.tables to find the temp table).

The main difference when working with temp tables is that the internal name is dynamic, so it must be obtained before the utility is called:

SELECT * INTO Person.#test123 FROM AdventureWorks2017.Person.AddressType;

ALTER TABLE Person.#test123 ADD CONSTRAINT PK_Person_test123 PRIMARY KEY CLUSTERED ( AddressTypeID )

DECLARE @tempTableName NVARCHAR(MAX)
SET @tempTableName = (SELECT [name] FROM tempdb.sys.objects WHERE name LIKE N'#test123[_]%');

EXEC tempdb.dbo.sp_generate_merge
  @table_name = @tempTableName, 
  @target_table = '[Person].[AddressType]', 
  @include_use_db = 0;

Of course it would be nice if the tool did this work for the user :)

@Brennanb414 I've just raised #61 to allow temp tables to be specified for the @table_name. If you have a chance, would you be able to try the temp-table-support branch out and confirm whether this works as expected?