

Using token ? in the format "" will rescue in case the database names in the Microsoft SQL Server instance have space characters.īut the same point is just the opposite for the undocumented sp_MSForEachTable proc syntax. 3 Answers Sorted by: 31 You can use spexecutesql with an output parameter to retrieve the scalar result. If you want to select the database name or the table name as a literal in the t-sql expression you should use double single quotes around the ? literal.Īlso the sp_MSForEachDB syntax enables the SQL Server developers or administrators to use instead of ?.

The literal ? is used as a token which is replace with database name or table name according to the executed stored procedure " sp_MSForEachDB" or " sp_MSForEachTable". The Transact-SQL statement or batch in the spexecutesql stmt parameter is not compiled until the spexecutesql statement is executed. The query for the exec is created according to the columns of the table to on which select clause if used. spexecutesql has the same behavior as EXECUTE with regard to batches, the scope of names, and database context.
Exec t sql how to#
SQL Server tutorial shows how to use sp_MSForEachDB and sp_MSForEachTable undocumented stored procedures for database programmers and SQL Server administrators to ease their daily tasks. 15 I need to use exec inside of select clause. SQL Server Undocumented Stored Procedures sp_MSForEachDB and sp_MSForEachTable in Example T-SQL Codes Insert into with EXEC : EXEC Transact SQL SQL Server / T-SQL Tutorial 1.
