Dynamic association table

xiaoxiao2021-03-06  83

Originally posted: http://community.9cbs.net/expert/topic/3452/3452577.xml? Temp = .1377375

- Example Data Create Table [Table] (a sysname, b varchar (10)) INSERT [TABLE] SELECT 'TABLE_1', 'A'Union All SELECT' TABLE_2 ',' B '

Create Table Table_1 (a int) INSERT TABLE_1 SELECT 1UNION All SELECT 2

Create Table Table_2 (A INT) Insert Table_2 SELECT 3UNION All SELECT 4GO

/ * - Problem Description:

In Table, the A field stores the name A field of other tables, the maximum value of the A field of each table is obtained as follows:

Table.b C ------------------ A 1 B 2 - * /

- Treatment method DECLARE @S varchar (8000) set @S = '' SELECT @ s = @ S 'WHEN' '' '' '(SELECT MAX (A) from [' A '])' from [table] group by aexec ('SELECT B, C = Case A' @ S 'end from [Table]') Go

- Delete TABLE [TABLE], TABLE_1, TABLE_2

/*--Test Results

B C ---------- ----------- A 2b 4 - * /

转载请注明原文地址:https://www.9cbs.com/read-106233.html

New Post(0)