有2张桌子
Preferences
其中列出所有可能的首选项,并使用查询选择所有允许的值:
+-----------------------+-----------------------------------------------------------------+
| Preferences | Query |
+-----------------------+-----------------------------------------------------------------+
| PickingList_Template | select v,n from Options where group='PickingList_Template' |
| AWB_Template | select v,n from Options where group='AWB_Template' |
| CheckList_Template | select v,n from Options where group='CheckList_Template' |
| Manifest_Template | select v,n from Options where group='Manifest_Template' |
| NoOfCopies | select v,n from Options where group='NoOfCopies' |
...
和
UsersSelection
每个客户的记录都包含与首选项一样多的字段:
+--------+--------------------------+-------------------+-----------------------+-----------------------+----------------+
| UID | PickingList_Template | AWB_Template | CheckList_Template | Manifest_Template | NoOfCopies |
+--------+--------------------------+-------------------+-----------------------+-----------------------+----------------+
| User1 | light | DHL | portrait | light | 2 |
| User2 | full | TNT | landscape | barcoded | 0 |
...
因为我需要有完整的偏好列表,即使用户没有选择任何我使用Full Outer Join公式:
SELECT
p.LookupQuery,
su.Name,
su.Surname,
su.Admin,
us.*
FROM
eBay.dbo.UsersSelection us
FULL OUTER JOIN
dbo.Preferences p ON 1=1
FULL JOIN
dbo.Users u ON u.UID=us.UID
FULL JOIN
dbo.SubUsers su ON su.UID=u.UID
WHERE
(su.SUID=@SUID COLLATE Latin1_General_CS_AS OR su.SUID IS NULL)
AND
(su.PWD=@PWD COLLATE Latin1_General_CS_AS or su.PWD IS NULL)
ORDER
by p.SortOrder ASC
存储过程有效..但仅当UsersSelection为空或仅存在与@SUID和@PWD匹配的记录时才返回正确的值: 如果UsersSelection中有超过1条记录,则返回所有记录,而显然,如果有任何值或空值,我只需要相对于所选用户的值。
我怎样才能做到这一点?
sqlFiddle Schema
CREATE TABLE Preferencs (
Preference nvarchar(30) NULL,
Query nvarchar(300) NULL,
SortOrder int NULL
)
CREATE TABLE UsersSelection(
UID nvarchar(20),
PickingListTemplate nvarchar(30) NULL,
AWB_Template nvarchar(30) NULL,
CheckList_Template nvarchar(30) NULL,
Manifest_Template nvarchar(30) NULL,
NoOfCopies int
)
CREATE TABLE SubbUsers(
SUID nvarchar(30) NULL,
PWD nvarchar(30) NULL,
Name nvarchar(60) NULL,
Surname nvarchar(60) NULL
)
Insert into SubbUsers VALUES ('User1','pw1','John','Smith')
Insert into SubbUsers VALUES ('User2','pw2','Jack','Smith')
Insert into SubbUsers VALUES ('User3','pw3','Joe','Smith')
Insert into Preferencs VALUES ('PickingList_Template','select v,n from Options where group=''PickingList_Template''',1)
Insert into Preferencs VALUES ('AWB_Template','select v,n from Options where group=''AWB_Template''',3)
Insert into Preferencs VALUES ('CheckList_Template','select v,n from Options where group=''CheckList_Template''',5)
Insert into Preferencs VALUES ('Manifest_Template','select v,n from Options where group=''Manifest_Template''',8)
Insert into Preferencs VALUES ('NoOfCopies','select v,n from Options where group=''NoOfCopies''',6)
insert into UsersSelection VALUES ('User1','light','DHL','portrait','light',2)
insert into UsersSelection VALUES ('User2','full','TNT','landscape','barcoded',0)
insert into UsersSelection VALUES ('User3','light','FEDEX','portrait','barcoded',1)
查询:
Declare @SUID nvarchar(20)='User1', @PWD nvarchar(20)='pw1'
SELECT
p.Query,
su.Name,
su.Surname,
us.*
FROM
eBay.dbo.UsersSelection us
FULL OUTER JOIN
dbo.Preferencs p ON 1=1
FULL JOIN
dbo.SubbUsers su ON su.SUID=us.UID
WHERE
(su.SUID=@SUID COLLATE Latin1_General_CS_AS OR su.SUID IS NULL)
AND
(su.PWD=@PWD COLLATE Latin1_General_CS_AS or su.PWD IS NULL)
ORDER
by p.SortOrder ASC
很抱歉,但无法使sqlfiddle工作..运行SQL时...返回错误
此处预期输出:
1) if in UsersSelection there is a record with UID='User1' this result (and also other records related to other users)
+-----------------------+-----------------------------------------------------------------+----------+--------------------------+-------------------+-----------------------+-----------------------+----------------+
| Preference | Query | UID | PickingList_Template | AWB_Template | CheckList_Template | Manifest_Template | NoOfCopies |
+-----------------------+-----------------------------------------------------------------+----------+--------------------------+-------------------+-----------------------+-----------------------+----------------+
| PickingList_Template | select v,n from Options where group='PickingList_Template' | User1 | light | DHL | portrait | light | 2 |
| AWB_Template | select v,n from Options where group='AWB_Template' | User1 | light | DHL | portrait | light | 2 |
| CheckList_Template | select v,n from Options where group='CheckList_Template' | User1 | light | DHL | portrait | light | 2 |
| Manifest_Template | select v,n from Options where group='Manifest_Template' | User1 | light | DHL | portrait | light | 2 |
| NoOfCopies | select v,n from Options where group='NoOfCopies' | User1 | light | DHL | portrait | light | 2 |
2) if there is not a record related to user1 this result
+-----------------------+-----------------------------------------------------------------+----------+--------------------------+-------------------+-----------------------+-----------------------+----------------+
| Preference | Query | UID | PickingList_Template | AWB_Template | CheckList_Template | Manifest_Template | NoOfCopies |
+-----------------------+-----------------------------------------------------------------+----------+--------------------------+-------------------+-----------------------+-----------------------+----------------+
| PickingList_Template | select v,n from Options where group='PickingList_Template' | NULL | NULL | NULL | NULL | NULL | NULL |
| AWB_Template | select v,n from Options where group='AWB_Template' | NULL | NULL | NULL | NULL | NULL | NULL |
| CheckList_Template | select v,n from Options where group='CheckList_Template' | NULL | NULL | NULL | NULL | NULL | NULL |
| Manifest_Template | select v,n from Options where group='Manifest_Template' | NULL | NULL | NULL | NULL | NULL | NULL |
| NoOfCopies | select v,n from Options where group='NoOfCopies' | NULL | NULL | NULL | NULL | NULL | NULL |