Solved

Sorting a dynamic query

Posted on 2014-02-21
6
241 Views
Last Modified: 2014-02-21
Hi all.

This one crazy code...  It it possible to sort the content of a dynamic query..
I need to output exactacly the result as selected in the where column..


SELECT
						@DynamicQuery = ISNULL(@DynamicQuery+' UNION ','') 
						+ 'SELECT  CONVERT(varchar(max),'+COLUMN_NAME+') as varColumnName, 
						'+@VarCusHeaderID+' as fk_Header_ID,  
						'+@varProviderID+' as fk_ProviderID, 
						'+@varCurIndex+' as PositionIndex, 
						'+@varIsStatus+' as IsStatus		    
						FROM '+@TransactionTable+
						' y' +ISNULL('  WHERE '+@TableWhere,'')
					FROM INFORMATION_SCHEMA.COLUMNS
					WHERE (table_name = @TransactionTable
					AND COLUMN_NAME IN (
									'Text1',
									'Text2',
									'Text3',
									'Text4',
									'Text5',
									'Text6',
									'Text7',
									'Text8',
									'Text9',
									'Text10'
									)
					
								)

Open in new window

0
Comment
Question by:ZURINET
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 9

Expert Comment

by:QuinnDex
ID: 39876265
simple solution would be to add an extra id column incrementing by 1 this would order them by default in the order processed
0
 
LVL 12

Expert Comment

by:Harish Varghese
ID: 39876327
And here is how:

Declare another int variable @cnt and replace first 3 lines with below:
SELECT	@cnt = isnull(@cnt, 0) + 1,
@DynamicQuery = ISNULL(@DynamicQuery+' UNION  ','') 
+ 'SELECT ' + CONVERT(varchar, @cnt) + ' SlNo, CONVERT(varchar(max),'+COLUMN_NAME+') as varColumnName,'

Open in new window

-Harish
0
 

Author Comment

by:ZURINET
ID: 39876605
Hi Harish

Thanks for your feedback..
I   am having an error that says..

Conversion failed when converting the varchar value ' y' to data type int.
0
Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

 
LVL 12

Expert Comment

by:Harish Varghese
ID: 39876624
Please post your complete query
0
 
LVL 16

Accepted Solution

by:
Surendra Nath earned 500 total points
ID: 39876685
try this out

SELECT
	@DynamicQuery = ISNULL(@DynamicQuery+' UNION ','') 
	+ 'SELECT  CONVERT(varchar(max),'+COLUMN_NAME+') as varColumnName, 
	'+@VarCusHeaderID+' as fk_Header_ID,  
	'+@varProviderID+' as fk_ProviderID, 
	'+@varCurIndex+' as PositionIndex, 
	'+@varIsStatus+' as IsStatus		    
	FROM '+@TransactionTable+
	' y' +ISNULL('  WHERE '+@TableWhere,'')
FROM INFORMATION_SCHEMA.COLUMNS
WHERE (table_name = @TransactionTable
AND COLUMN_NAME IN (
				'Text1',
				'Text2',
				'Text3',
				'Text4',
				'Text5',
				'Text6',
				'Text7',
				'Text8',
				'Text9',
				'Text10'
				)
			)
ORDER BY SUBSTRING(COLUMN_NAME,5,2)

Open in new window

0
 

Author Comment

by:ZURINET
ID: 39876861
Hi Ganti

Thanks ..
It helps a bit
0

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question