tsqltools icon indicating copy to clipboard operation
tsqltools copied to clipboard

Population of @SOURCE_DATABASENAME and @DESTINATION_DATABASENAME

Open Imran-imtiaz48 opened this issue 8 months ago • 0 comments

You are assigning a query to @SOURCE_SQL_DBNAME and then running sp_executesql to insert into a table variable:

SELECT @SOURCE_SQL_DBNAME = 'select name from ' + @SOURCEDBSERVER + '.master.sys.databases where database_id>4' INSERT INTO @SOURCE_DATABASENAME EXEC sp_executesql @SOURCE_SQL_DBNAME Issue is : o sp_executesql cannot insert directly into a table variable from a dynamic string like this unless the schema matches exactly, and it can get tricky. o If the dynamic SQL returns more than one column, or if there’s a typo, this will fail.

Imran-imtiaz48 avatar Jun 20 '25 23:06 Imran-imtiaz48