Click here to Skip to main content
15,889,492 members
Home / Discussions / Database
   

Database

 
GeneralRe: History table Pin
Eddy Vluggen5-Jul-11 13:19
professionalEddy Vluggen5-Jul-11 13:19 
AnswerRe: Low-level filtering of IsDeleted bit field Pin
jschell5-Jul-11 6:20
jschell5-Jul-11 6:20 
AnswerRe: Low-level filtering of IsDeleted bit field Pin
Mycroft Holmes5-Jul-11 14:21
professionalMycroft Holmes5-Jul-11 14:21 
AnswerRe: Low-level filtering of IsDeleted bit field Pin
Shameel7-Jul-11 3:20
professionalShameel7-Jul-11 3:20 
QuestionUnQuote a nvarchar() string and use it as a table name Pin
reza assar4-Jul-11 1:57
reza assar4-Jul-11 1:57 
AnswerRe: UnQuote a nvarchar() string and use it as a table name Pin
Shameel4-Jul-11 4:46
professionalShameel4-Jul-11 4:46 
AnswerRe: UnQuote a nvarchar() string and use it as a table name Pin
PIEBALDconsult4-Jul-11 6:01
mvePIEBALDconsult4-Jul-11 6:01 
AnswerRe: UnQuote a nvarchar() string and use it as a table name Pin
Niladri_Biswas4-Jul-11 17:03
Niladri_Biswas4-Jul-11 17:03 
Hi try this

Input:

Declare @tblPlaceholder table(ID int,TableName Nvarchar(20),FieldName Nvarchar(20))
insert into @tblPlaceholder 
	select 1,'spt_values','name' union all select 1,'spt_values','number' union all
	select 2,'spt_monitor','lastrun' union all select 2,'spt_monitor','cpu_busy'
--select * from @tblPlaceholder


Query:

Declare @t table(ID int,Query VARCHAR(2000))
Declare @QueryList VARCHAR(2000)
Declare @i int 
set @i = 1

-- Step 1: Build the query and insert the same into a table variable
Insert into @t 
Select ID, Query = ' Select ' + FieldNames + ' from ' + TableName
From
(
Select ID,TableName, FieldNames = Stuff((select ',' + CAST(TableName as Nvarchar(20)) + '.' + CAST(FieldName as Nvarchar(20)) 
			from @tblPlaceholder t2 where t2.ID = t1.ID
			FOR XML PATH('')),1,1,'')
From @tblPlaceholder t1
Group By t1.ID,t1.TableName)X

/* output of step 1
select * from @t

ID	Query
1	 Select spt_values.name,spt_values.number from spt_values
2	 Select spt_monitor.lastrun,spt_monitor.cpu_busy from spt_monitor
*/


-- Step 2 : loop thru the ID and execute the queries
While (@i <= 2) -- since there are two id's.. this can even be configurable as max(id)
Begin
	  SELECT @QueryList = (Select Query from @t where ID = @i)
	  exec(@QueryList)	  
	  set @i  += 1
End

/* Final output

	Output of Select spt_values.name,spt_values.number from spt_values
	
	name	number
	(rpt)	-32768
	YES OR NO	-1
	SYSREMOTELOGINS TYPES	-1
	SYSREMOTELOGINS TYPES (UPDATE)	-1
	
	
	Output of Select spt_monitor.lastrun,spt_monitor.cpu_busy from spt_monitor
	
	lastrun					cpu_busy
	2008-07-0916:46:13.877	10
*/


I have given the demonstration by using two system tables (spt_values and spt_monitor) found in the Master database.

The comments about the steps are mentioned in the code itself. Hope this will help you

Let me know in case of any concern.

Thanks
Niladri Biswas

GeneralRe: UnQuote a nvarchar() string and use it as a table name Pin
reza assar6-Jul-11 1:04
reza assar6-Jul-11 1:04 
QuestionGetDate Pin
berba4-Jul-11 0:10
berba4-Jul-11 0:10 
AnswerRe: GetDate Pin
thatraja4-Jul-11 0:25
professionalthatraja4-Jul-11 0:25 
AnswerRe: GetDate Pin
Simon_Whale4-Jul-11 0:40
Simon_Whale4-Jul-11 0:40 
GeneralRe: GetDate Pin
berba4-Jul-11 2:44
berba4-Jul-11 2:44 
GeneralRe: GetDate Pin
Simon_Whale4-Jul-11 2:49
Simon_Whale4-Jul-11 2:49 
AnswerRe: GetDate Pin
Shameel4-Jul-11 4:44
professionalShameel4-Jul-11 4:44 
QuestionA Quick SQL/Integrated Authentication issue (Looks like I am missing something trivial) Pin
Vasudevan Deepak Kumar30-Jun-11 7:24
Vasudevan Deepak Kumar30-Jun-11 7:24 
AnswerRe: A Quick SQL/Integrated Authentication issue (Looks like I am missing something trivial) Pin
Pete O'Hanlon30-Jun-11 7:44
mvePete O'Hanlon30-Jun-11 7:44 
GeneralRe: A Quick SQL/Integrated Authentication issue (Looks like I am missing something trivial) Pin
Vasudevan Deepak Kumar30-Jun-11 7:53
Vasudevan Deepak Kumar30-Jun-11 7:53 
GeneralRe: A Quick SQL/Integrated Authentication issue (Looks like I am missing something trivial) Pin
J4amieC1-Jul-11 1:18
J4amieC1-Jul-11 1:18 
AnswerRe: A Quick SQL/Integrated Authentication issue (Looks like I am missing something trivial) Pin
Mycroft Holmes30-Jun-11 12:54
professionalMycroft Holmes30-Jun-11 12:54 
GeneralRe: A Quick SQL/Integrated Authentication issue (Looks like I am missing something trivial) Pin
Vasudevan Deepak Kumar30-Jun-11 13:43
Vasudevan Deepak Kumar30-Jun-11 13:43 
GeneralRe: A Quick SQL/Integrated Authentication issue (Looks like I am missing something trivial) Pin
J4amieC30-Jun-11 23:57
J4amieC30-Jun-11 23:57 
GeneralRe: A Quick SQL/Integrated Authentication issue (Looks like I am missing something trivial) Pin
Mycroft Holmes1-Jul-11 0:54
professionalMycroft Holmes1-Jul-11 0:54 
GeneralRe: A Quick SQL/Integrated Authentication issue (Looks like I am missing something trivial) Pin
J4amieC1-Jul-11 1:11
J4amieC1-Jul-11 1:11 
GeneralRe: A Quick SQL/Integrated Authentication issue (Looks like I am missing something trivial) Pin
Mycroft Holmes1-Jul-11 1:26
professionalMycroft Holmes1-Jul-11 1:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.