Solved

Sorting a dynamic query

Posted on 2014-02-21
6
236 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
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.‚Äč
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

786 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