I modified the criteria slightly so that it would work with the data that I
have within my system tables.
This version retrieves data from both tables:
SELECT
'Union 1' as ObjectType,
A.name AS ColumnName,
B.name AS TableName
--select *
FROM
dbo.syscolumns A, dbo.sysobjects B
WHERE
A.id = B.id
AND
-- (A.name like '%tax%' and A.name like '%sale%')
(A.name like '%s')
and
(B.xtype = 'V')
AND EXISTS
(SELECT
'Union 2' as ObjectType,
AA.name AS ColumnName,
BB.name AS TableName
FROM
dbo.syscolumns AA, dbo.sysobjects BB
WHERE
AA.id = BB.id
AND
AA.name like '%stat%' AND BB.name like '%const%'
and
(BB.xtype = 'V')
AND BB.name = B.name)
/***********************************************/
And this version of the query only returns the table name:
SELECT DISTINCT B.name AS TableName
--select *
FROM
dbo.syscolumns A, dbo.sysobjects B
WHERE
A.id = B.id
AND
-- (A.name like '%tax%' and A.name like '%sale%')
(A.name like '%s')
and
(B.xtype = 'V')
AND EXISTS
(SELECT
'Union 2' as ObjectType,
AA.name AS ColumnName,
BB.name AS TableName
FROM
dbo.syscolumns AA, dbo.sysobjects BB
WHERE
AA.id = BB.id
AND
AA.name like '%stat%' AND BB.name like '%const%'
and
(BB.xtype = 'V')
AND BB.name = B.name)
--
Keith
[quoted text, click to view] "Gary Rynearson" <grynearson@gfnet.com> wrote in message
news:umfDE%231yFHA.1040@TK2MSFTNGP14.phx.gbl...
> Keith
>
> Ahhh ... Yes you are right. And now that you mention it, I would actually
> want as a return either just
>
> tableName1
>
> Do you know of a way to get this?
>
> Gary
>
>
> Yes, you are right. I would be happy with a query that would return just
> what you indicated,
> I would like only the distinct values to be retrned, so in this case, my
> desired return would be just tableName1.
>
> "Keith Kratochvil" <sqlguy.back2u@comcast.net> wrote in message
> news:e$%23Mtx1yFHA.1132@TK2MSFTNGP10.phx.gbl...
>> Multiple columns appear from tableName1. Wouldn't your expected results
>> look like this:
>> tax_info tableName1
>> sales_info tableName1
>>
>>
>> --
>> Keith
>>
>>
>> "Gary Rynearson" <grynearson@gfnet.com> wrote in message
>> news:%23OwcMC1yFHA.3892@TK2MSFTNGP12.phx.gbl...
>>>I have a question on how to return only some records from my existing
>>>query (shown below). I want to return only records that occur in both
>>>sides of the union AND have the same tablename. (Maybe thier is an
>>>easier way than starting with the union, which you may suggest).
>>>
>>> So, for example, imagine the query to return the following records
>>>
>>> Union 1 tax_info tableName1
>>> Union 1 pretax tableName1
>>> Union 1 tax_info tableName2
>>> Union 2 sales_info tableName1
>>> Union 2 sales_info tableName3
>>>
>>> I would want to construct some type of additional criteria that would
>>> return only
>>>
>>> tax_info tableName1
>>>
>>> as the table 'tableName1' is the only table to be in both Union 1 and
>>> Union 2.
>>>
>>> Thanks
>>> Gary
>>>
>>>
>>> SELECT
>>> 'Union 1' as ObjectType,
>>> A.name AS ColumnName,
>>> B.name AS TableName
>>> FROM
>>> dbo.syscolumns A, dbo.sysobjects B
>>> WHERE
>>> A.id = B.id
>>> AND
>>> (A.name like '%tax%' and A.name like '%sale%')
>>> and
>>> (B.xtype = 'V')
>>>
>>> UNION
>>> SELECT
>>> 'Union 2' as ObjectType,
>>> A.name AS ColumnName,
>>> B.name AS TableName
>>> FROM
>>> dbo.syscolumns A, dbo.sysobjects B
>>> WHERE
>>> A.id = B.id
>>> AND
>>> A.name like '%Sales%'
>>> and
>>> (B.xtype = 'V')
>>>
>>>
>>
>>
>
>