I am wondering if there is a function in SQL Server or service in Azure to provide an opportunity to combine multiple queries into one. For example,
ALTER TABLE1
ADD RetailValue AS (CAST(QtyAvailable AS int) * CAST(UnitPrice AS int) * 1.5);
ALTER TABLE2
ADD RetailValue AS (CAST(QtyAvailable AS int) * CAST(UnitPrice AS int) * 1.5);
ALTER TABLE3
ADD RetailValue AS (CAST(QtyAvailable AS int) * CAST(UnitPrice AS int) * 1.5);
...
Is there a way to combine all these together into 1 query that can affect all the tables? the columns will be all same for every table and there are simply just too many tables to do copy and paste.
Thank you!
2
Answers
use the stored procedure
sp_msforeachtable
with filtering table.The placeholder `?’ is denoting the table.
Using Dynamic SQL you can construct the required
alter
statements and aggregate them into a single string to execute:See a demo Fiddle