尝试从多个表中选择1个查询输出。 只有让它与CROSS JOIN一起工作,输出虽然不是我想要实现的。它似乎从另一个表中按COLUMN迭代查询。
SELECT @SQLCmd = + 'bcp ' +
'"SELECT ''<?xml version=""1.0"" encoding=""UTF-8""?>'' + ' +
' (SELECT ValID, ValSet, Id, Initials, firstname, lastname, email ' +
' FROM Employees.dbo.IDCards CROSS JOIN Employees.dbo.Value' +
' WHERE CardID = 0 ' +
' AND Value = 1 ' +
' FOR XML PATH(''Employee''), ELEMENTS, ROOT(''Employees'')) "' +
' queryout ' +
@FileName +
' -w -T -S' + @@SERVERNAME
输出就像这样。
- <Employees>
- <Employee>
<ValID>1</ValID>
<ValSet>Beach</ValSet>
<id>4</id>
<Initials>JJ</Initials>
<Firstname>Jack</Firstname>
<Lastname>Jack</Lastname>
<email>JJ@testserver.com</email>
</Employee>
- <Employee>
<ValID>2</ValID>
<ValSet>Forrest</ValSet>
<id>4</id>
<Initials>JJ</Initials>
<Firstname>Jack</Firstname>
<Lastname>Jack</Lastname>
<email>JJ@testserver.com</email>
</Employee>
- </Employees>
那么如果没有迭代的话,多个表中的一个SELECT怎么会这样?
更新 我一直在尝试不同的东西。 但这就是它现在看起来如何添加另一个表格。
IDCards
Id | Initials | firstname | lastname | email
4 | JJ | Jack | Jack | JJ@testserver.com
5 | MM | Mike | Mike | MM@testserver.com
Value
ValID | ValSet |
1 | Beach |
2 | Forrest |
CardData
CardID | CardVal |
4 | 1 |
5 | 1 |
5 | 2 |
如果有意义,我需要每个用户运行一次吗?
答案 0 :(得分:2)
你的编辑会清除一些细节,但它仍然很模糊: - )
我能想象的唯一关系(由于给定的值)就是这个。这是对的吗?
DECLARE @IDCards TABLE(Id INT,Initials VARCHAR(100),firstname VARCHAR(100),lastname VARCHAR(100),email VARCHAR(100));
INSERT INTO @IDCards VALUES
(4,'JJ','Jack','Jack','JJ@testserver.com')
,(5,'MM','Mike','Mike','MM@testserver.com');
DECLARE @Value TABLE(ValID INT, ValSet VARCHAR(100));
INSERT INTO @Value VALUES
(1,'Beach')
,(2,'Forrest');
DECLARE @CardData TABLE(CardID INT, CardVal INT);
INSERT INTO @CardData VALUES
(4,1)
,(5,1)
,(5,2);
SELECT *
FROM @Value AS v
INNER JOIN @CardData AS cd ON v.ValID=cd.CardVal
INNER JOIN @IDCards AS ic ON cd.CardID=ic.Id
结果如下:
+-------+---------+--------+---------+----+----------+-----------+----------+-------------------+
| ValID | ValSet | CardID | CardVal | Id | Initials | firstname | lastname | email |
+-------+---------+--------+---------+----+----------+-----------+----------+-------------------+
| 1 | Beach | 4 | 1 | 4 | JJ | Jack | Jack | JJ@testserver.com |
+-------+---------+--------+---------+----+----------+-----------+----------+-------------------+
| 1 | Beach | 5 | 1 | 5 | MM | Mike | Mike | MM@testserver.com |
+-------+---------+--------+---------+----+----------+-----------+----------+-------------------+
| 2 | Forrest | 5 | 2 | 5 | MM | Mike | Mike | MM@testserver.com |
+-------+---------+--------+---------+----+----------+-----------+----------+-------------------+
如果这是正确的,您可能会寻找这样的查询:
SELECT Id
,Initials
,firstname
,lastname
,email
,(
SELECT ValID
,ValSet
FROM @CardData AS cd
INNER JOIN @Value AS v On cd.CardVal=v.ValID
WHERE cd.CardID=ic.Id
FOR XML PATH('Card'),TYPE
)
FROM @IDCards AS ic
FOR XML PATH('Employee'),ROOT('Employees')
由于卡片为1:n
,您需要将它们放在子选择中
这是结果(在非常脏的水中钓鱼......):
<Employees>
<Employee>
<Id>4</Id>
<Initials>JJ</Initials>
<firstname>Jack</firstname>
<lastname>Jack</lastname>
<email>JJ@testserver.com</email>
<Card>
<ValID>1</ValID>
<ValSet>Beach</ValSet>
</Card>
</Employee>
<Employee>
<Id>5</Id>
<Initials>MM</Initials>
<firstname>Mike</firstname>
<lastname>Mike</lastname>
<email>MM@testserver.com</email>
<Card>
<ValID>1</ValID>
<ValSet>Beach</ValSet>
</Card>
<Card>
<ValID>2</ValID>
<ValSet>Forrest</ValSet>
</Card>
</Employee>
</Employees>
答案 1 :(得分:0)
使用UNION子句。
SELECT columns FROM table1
UNION
SELECT columns FROM table2
查询必须具有相同的列数