Home > database >  Multiple SQL query results into a table
Multiple SQL query results into a table

Time:10-10

 select name col1 from syscolumns where id=object_id (' TableName1) 

 
Id
The username
The status

 select name col2 from syscolumns where id=object_id (' TableName2) 

 
Uid
"
Area
Street
Num


Query of the two tables all columns (the number of columns), how to combine two of the query results as a table, make the query results into a table of two fields,

 
Id uid
The username password
The status area
Street
Num


Consult everybody a great god

CodePudding user response:

Union all

CodePudding user response:

 
As with m (
Select row_number () over rn order by id, name, 'x
The from syscolumns where id=3
Union all
Select row_number () over (order by id) rn, 'x, name
The from sysobjects where id=55
)
Select an rn, MAX (name) c2, MAX c3 group by an rn from m (x)


Rn c2 c3
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
1 rsid sysiscols
2 rscolid
3 hbcolid
4 rcmodified
Five ti
6 cid
7 ordkey
8 maxinrowlen
The status of 9
10 offset
11 nullbit
12 bitpos
13 colguid
14 dbfragid

(14) affected


CodePudding user response:

Union all after only one column, requested the query results as two columns of the output,

CodePudding user response:

; WITH tempa AS (
SELECT the name col1, ROW_NUMBER () OVER (ORDER BY name) num
The FROM syscolumns
WHERE id=OBJECT_ID (' TableName1)
), tempb AS (
SELECT the name col1, ROW_NUMBER () OVER (ORDER BY name) num
The FROM syscolumns
WHERE id=OBJECT_ID (' TableName2)
)
The SELECT tempa col1,
Tempb. Col1
The FROM tempa
FULL JOIN tempb ON tempb. Num=tempa. Num

CodePudding user response:

Cross the apply

CodePudding user response:

refer to fifth floor riding a snail to go climbing response:
cross the apply

There is only one table

CodePudding user response:

Select *
The from (select name, colorder, object_name (id) as tbname from syscolumns where id in (object_id (' pub_articles), object_id (' pub_bids))) a
The pivot (Max (name) for tbname (pub_articles pub_bids)) in b
  • Related