TSQL - 比较表

发布于 2024-11-17 02:54:17 字数 702 浏览 1 评论 0原文

我想创建一个存储过程来比较两个查询的结果。如果第二个表的结果可以在第一个表中找到,则打印“YES”,否则打印“No”。

表 1:

SELECT     dbo.Roles.RoleName, dbo.UserRoles.RoleID
FROM         dbo.Roles LEFT OUTER JOIN
dbo.UserRoles ON dbo.Roles.RoleID = dbo.UserRoles.RoleID
WHERE     (dbo.Roles.PortalID = 0) AND (dbo.UserRoles.UserID = 2)

表 2:

Declare @RowData as nvarchar(2000)
Set @RowData = ( SELECT EditPermissions FROM vw_XMP_DMS_Documents where DocumentID = 2)
Select Data
from dbo.split(@RowData, ',')

例如。

表 1:

John
Jack
James

表 2:

John
Sally
Jane

打印“是”

这可能吗?

I want to create a stored procedure that compares the results of two queries. If the results of the 2nd table can be found in the first, print 'YES', otherwise, print 'No'.

Table 1:

SELECT     dbo.Roles.RoleName, dbo.UserRoles.RoleID
FROM         dbo.Roles LEFT OUTER JOIN
dbo.UserRoles ON dbo.Roles.RoleID = dbo.UserRoles.RoleID
WHERE     (dbo.Roles.PortalID = 0) AND (dbo.UserRoles.UserID = 2)

Table 2:

Declare @RowData as nvarchar(2000)
Set @RowData = ( SELECT EditPermissions FROM vw_XMP_DMS_Documents where DocumentID = 2)
Select Data
from dbo.split(@RowData, ',')

For example.

Table 1:

John
Jack
James

Table 2:

John
Sally
Jane

Print 'YES'

Is this possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半暖夏伤 2024-11-24 02:54:17

我正在使用临时表,因为我不知道您的表采用哪种格式。

SELECT     dbo.Roles.RoleName, dbo.UserRoles.RoleID 
INTO #t1
FROM         dbo.Roles LEFT OUTER JOIN 
dbo.UserRoles ON dbo.Roles.RoleID = dbo.UserRoles.RoleID 
WHERE     (dbo.Roles.PortalID = 0) AND (dbo.UserRoles.UserID = 2) 


Declare @RowData as nvarchar(2000) 
Set @RowData = ( SELECT EditPermissions FROM vw_XMP_DMS_Documents where DocumentID = 2) 
Select Data 
INTO #t2
from dbo.split(@RowData, ',') 


select case when exists (select 1 from #t1 where RoleName = t.Data) then 'yes' else 'no' end answer, Data 
into #tmp
from #t2 t

select * from #tmp

我不能 100% 确定这就是你想要的。所以我猜测。

了解问题后添加此内容

if exists(select 1 from #t2 t2 join #t1 t1 on t1.RoleName = t2.data) 
print 'permsGranted' 
ELSE print 'permsNotGranted'

I am using temporary tables because I dont know which format your tables has.

SELECT     dbo.Roles.RoleName, dbo.UserRoles.RoleID 
INTO #t1
FROM         dbo.Roles LEFT OUTER JOIN 
dbo.UserRoles ON dbo.Roles.RoleID = dbo.UserRoles.RoleID 
WHERE     (dbo.Roles.PortalID = 0) AND (dbo.UserRoles.UserID = 2) 


Declare @RowData as nvarchar(2000) 
Set @RowData = ( SELECT EditPermissions FROM vw_XMP_DMS_Documents where DocumentID = 2) 
Select Data 
INTO #t2
from dbo.split(@RowData, ',') 


select case when exists (select 1 from #t1 where RoleName = t.Data) then 'yes' else 'no' end answer, Data 
into #tmp
from #t2 t

select * from #tmp

I am not 100 % sure this is what you want. So I am guessing.

added this after understanding the question

if exists(select 1 from #t2 t2 join #t1 t1 on t1.RoleName = t2.data) 
print 'permsGranted' 
ELSE print 'permsNotGranted'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文