Is there any way to do some type of loop through an array of strings in SQL? I am appending data to #T_POLICY, and I want to run through the queries with one database and the run through the same queries with a different database.
Ex.
use Staging__4SBI_STG_BG
go
WITH cteEnumerate AS
(
SELECT *
,RN = ROW_NUMBER() OVER (PARTITION BY POLICY_ID ORDER BY LOADDATE DESC)
FROM dbo.STG_POLICY
)
INSERT INTO #T_POLICY
SELECT SOURCE, AGENCY_D, POL_SEQ, POLICY_ID, POL_ENDNUMBER, PRODUCT_ID,
COMPANY_ID
FROM cteEnumerate
WHERE RN = 1
ORDER BY POLICY_ID;
So the next one I would like use is use Staging__4SBI_STG_TB instead of BG, and have quite a few others to run through. Could I create a table with these names and run through them? Any help would be great.
Thanks