0

I have been working on a problem for a few hours and I feel like I'm running in circles.

I am writing a script that will check the a table from and old database to a new one and it wants too see if there are differences in the tables. I have this part done but want to make the output easier to read. I ideally want an output to happen when there are records in the table, signifying there are differences. So if there are differences in tables 1, 4 , and 8 the output will say table 1, table 4, table 8. I will add a snippet of my code but I need to change some stuff because of privacy reasons.

if exists(
SELECT 'xyz' AS TableName,  1 FROM table1CD 
LEFT JOIN database2 CL ON CL.X= CAST(CD.X AS VARCHAR(100)) AND CL.Type = 'type1'
WHERE CL.Type IS NULL
)
Select TableName
Else

UNION

else if exists(
SELECT 'abc' AS TableName, 1 FROM table3 CA
LEFT JOIN database2 CL ON CL.X = CAST(CA.Y AS VARCHAR(100)) AND CL.Type = 'type2'
WHERE CL.Type IS NULL 
)
Select TableName
Else

union

more tables with repeating structure

Im still very new with this kinda SQL. I am getting errors when I run this and keep changing it around to try to figure it out but Im at a wall now.

4

1 に答える 1

1

リストが必要なだけの場合は、次のようにテーブル変数で自分で維持できます。

declare @Tables table (TableName varchar(100));


if exists(
            SELECT 'xyz' AS TableName,  1 FROM table1CD 
            LEFT JOIN database2 CL ON CL.X= CAST(CD.X AS VARCHAR(100)) AND CL.Type = 'type1'
            WHERE CL.Type IS NULL
)
insert into @Tables
    select 'Table1'

if exists(
        SELECT 'abc' AS TableName, 1 FROM table3 CA
        LEFT JOIN database2 CL ON CL.X = CAST(CA.Y AS VARCHAR(100)) AND CL.Type = 'type2'
        WHERE CL.Type IS NULL 
)
insert into @Tables
    select 'Table3'



select * from @Tables;
于 2013-05-14T23:13:21.323 に答える