Solved

Sorting a dynamic query

Posted on 2014-02-21
6
240 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

734 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