Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 select from a list of tables

Author  Topic 

jkockz
Starting Member

4 Posts

Posted - 2008-01-23 : 15:32:36
I have a number of tables. Some of them have a column named 'status'. What I would like is a query that would output the name of each table that has a column named status, the type of that column, and the values that exist in the column for that table.

for instance:

table1 tinyint 0
table1 tinyint 2
table1 tinyint 3
table2 bit 0
table2 bit 1

so far I have a query that outputs the table and type, but I cannot seem to figure out how to implement the last piece.

SELECT
[TableName] = so.name,
[Type] = st.name
FROM
sysobjects so,
syscolumns sc,
systypes st
WHERE
so.xtype = 'U'
AND
sc.id = OBJECT_ID(so.name)
AND
sc.name = 'status'
AND st.type = sc.type

nathans
Aged Yak Warrior

938 Posts

Posted - 2008-01-23 : 21:11:48
How is this useful? Is this homework?

To achieve, you will need to use dynamic sql for that last part. Basically, iterate through the resultset you already have, construct a dynamic select statement, and exec into a temp table.





Nathan Skerl
Go to Top of Page

nathans
Aged Yak Warrior

938 Posts

Posted - 2008-01-23 : 21:24:21
Give that a shot, if you still have troubles please post back and I will show you an example.

Nathan Skerl
Go to Top of Page

jkockz
Starting Member

4 Posts

Posted - 2008-01-24 : 11:38:57
It's not for anything in particular. I just ran across something I could not figure out how to do. I don't think I've tried dynamic sql before. If you could help out or post an example I would greatly appreciate it.

Thanks
Go to Top of Page

nathans
Aged Yak Warrior

938 Posts

Posted - 2008-01-24 : 12:59:02
try this, it returns 3 rows from each table:


set nocount on

declare @ColumnName varchar(50),
@StageID int,
@Cmd varchar(8000)

set @ColumnName = 'Status'

-- stage the TableName, etc of each table that has the column
declare @Stage table (StageID int identity(1,1), TableName varchar(100), ColumnName varchar(100), DataType varchar(100))
insert into @Stage (TableName, ColumnName, DataType)
select st.name,
@ColumnName,
stt.name
from sys.columns sc
join sys.tables st on
sc.object_id = st.object_id
join sys.types stt on
sc.system_type_id = stt.system_type_id
where is_ms_shipped = 0 and
sc.name = @ColumnName

-- create temp table to hold select results
create table #Result (TableName varchar(100), DataType varchar(100), RowValue varchar(100))


-- loop through, construct insert statements for first 3 rows of each table
select @StageID = min(StageID) from @Stage

while @StageID is not null
begin
select @Cmd = 'set rowcount 3 insert into #Result select ''' + TableName + ''', ''' + DataType + ''', cast(' + ColumnName + ' as varchar(100)) from ' + TableName + ' (nolock) set rowcount 0'
from @Stage
where StageID = @StageID

--print @Cmd
exec(@Cmd)

select @StageID = min(StageID) from @Stage where StageID > @StageID
end

-- return results
select * from #Result

-- clean up
drop table #Result


Nathan Skerl
Go to Top of Page

jkockz
Starting Member

4 Posts

Posted - 2008-01-24 : 15:33:18
What I actually meant was to show all the distinct values that exist for each table on the status column. I should have been more specific

Your query actually does what I was looking for if I add 'distinct' after select in @Cmd as well as removing 'set rowcount 3'

Thanks for the help!
Go to Top of Page

jkockz
Starting Member

4 Posts

Posted - 2008-01-24 : 15:36:01
Also, this might be really simple, but I've just never been exposed to it. What are 'set nocount on' and '(nolock) set rowcount 0' for?
Go to Top of Page

nathans
Aged Yak Warrior

938 Posts

Posted - 2008-01-24 : 15:45:28
No problem.

set nocount on
just prevents the server from returning messages to the client, for ex "5 Rows Affected"
Using it is a good practice as it reduces network traffic.


Read more about dynamic sql here:
http://www.sqlteam.com/article/introduction-to-dynamic-sql-part-1

and set rowcount here:
http://www.sqlteam.com/article/dynamic-sql-or-how-do-i-select-top-var-records



Nathan Skerl
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2008-01-24 : 17:33:00
The NOLOCK is a table hint to set the TRANSACTION ISOLATION LEVEL to READ UNCOMMITTED (check out BOL).

The SET ROWCOUNT XXX - Is used to limit the number of rows returned (In this example 3) and then set to 0 after the query which resets the rowcount to return all rows.
Go to Top of Page
   

- Advertisement -