crack table script:
CREATE TABLE [dbo]. [KeyNames] (
[ID] [int] NOT NULL,
[KeyName] [varchar] (50) NOT NULL,
[keyOrder] [int] NOT NULL
) ON [PRIMARY]
CREATE TABLE [dbo]. [DocumentTable] (
[ID] [int] NOT NULL,
[PDF_Folder] [varchar] (50) NOT NULL,
[Key1Value] [varchar] (100) NOT NULL,
[Key2Value] [varchar] (100) NOT NULL,
[Key3Value] [varchar] (100) NOT NULL
) ON [PRIMARY]
=============== Key name table: strings ========
1 PDF_Folder 2
2 Key1Value 3
3 Key3Value 5
4 Key2Value 4
5 ID 1
=================================================== ====================================
================ Document table: rows ========
1 aa a1 a2 a3
2 bbb b1 b2 b3
3 ccc c1 c2 c3
=================================================== ====================================
the request according to below generates a dynamic order request:
DECLARE @str_column VARCHAR (100);
DECLARE @query VARCHAR (100);
SELECT @str_column = COALESCE (@str_column + ',', '') + KeyName
FROM Keynames sort by subscription;
set @query = 'select' + @str_column + 'from DocumentTable';
Exec (@query);
source share