'How can I get the output of the query below into one result set in t-sql?
I only have only "read" permissions to the database. I am trying to pull data out of several tables with the same fields. I am pulling the table names from the information_schema and putting them into a variable. How can I get all the data from each SELECT into one result set?
DECLARE @acct_code AS VARCHAR(40)
,@chk_tbl AS VARCHAR(40)
DECLARE CKTable_Cursor CURSOR FOR
SELECT table_name
FROM INFORMATION_SCHEMA.Tables
WHERE table_name LIKE 'CK%'
AND table_name > 'CK000000'
ORDER BY TABLE_NAME
OPEN CKTable_Cursor
FETCH NEXT FROM CKTable_Cursor
INTO @chk_tbl
SET @acct_code = SUBSTRING(@chk_tbl,3,LEN(@chk_tbl))
WHILE @@FETCH_STATUS = 0
BEGIN
EXEC
('SELECT ck.inserteddatetime AS [Inserted Datetime]
,actcode AS [Account Code]
,actname AS [Account Name]
,chk_num AS [Check Number]
,payee AS [Payee]
,supplier AS [Supplier]
,balance AS [Balance]
FROM dbo.' + @chk_tbl + ' AS ck WITH (NOLOCK)
LEFT JOIN dbo.ChtActs AS coa WITH (NOLOCK)
ON CONVERT(VARCHAR(40),actcode) = ' + @acct_code)
FETCH NEXT FROM CKTable_Cursor
INTO @chk_tbl
SET @acct_code = SUBSTRING(@chk_tbl,3,LEN(@chk_tbl))
END
CLOSE CKTable_Cursor;
DEALLOCATE CKTable_Cursor;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|