Using MS SQL Server I have the below SQL that updates a table that begins with 'tbl_Test%'
.
However, I have the situation where I can have multiple tables beginning with the name 'tbl_Test%'
. How would I modify the code so it will loop through and update all the tbl_Test%
tables? At the moment it only updates the first one.
DECLARE @NSQL NVARCHAR(MAX)
DECLARE @SOURCETABLE NVARCHAR(MAX)
SELECT @SOURCETABLE = TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME LIKE 'tbl_Test%' AND TABLE_TYPE = 'BASE TABLE'
SET @NSQL = 'UPDATE [' + @SOURCETABLE + '] SET [WEEKSTART] = CONVERT(NVARCHAR,convert(datetime, LEFT(WEEKSTART,10), 104),112)'
--PRINT @NSQL
EXEC SP_EXECUTESQL @NSQL