Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Remove parameter to make view

Author  Topic 

X-Factor
Constraint Violating Yak Guru

392 Posts

Posted - 2006-01-02 : 11:13:07
Hi,

Here's a view...

CREATE   VIEW dbo.GroupFolderPermissions
AS
SELECT
g.GroupID,
g.GroupName,
SUM(CASE FolderPermissionID WHEN 1 THEN Allow ELSE null END) AS 'FolderPermissionID1',
SUM(CASE FolderPermissionID WHEN 2 THEN Allow ELSE null END) AS 'FolderPermissionID2',
SUM(CASE FolderPermissionID WHEN 3 THEN Allow ELSE null END) AS 'FolderPermissionID3',
SUM(CASE FolderPermissionID WHEN 4 THEN Allow ELSE null END) AS 'FolderPermissionID4',
SUM(CASE FolderPermissionID WHEN 5 THEN Allow ELSE null END) AS 'FolderPermissionID5',
SUM(CASE FolderPermissionID WHEN 6 THEN Allow ELSE null END) AS 'FolderPermissionID6',
SUM(CASE FolderPermissionID WHEN 7 THEN Allow ELSE null END) AS 'FolderPermissionID7',
SUM(CASE FolderPermissionID WHEN 8 THEN Allow ELSE null END) AS 'FolderPermissionID8',
SUM(CASE FolderPermissionID WHEN 9 THEN Allow ELSE null END) AS 'FolderPermissionID9',
SUM(CASE FolderPermissionID WHEN 10 THEN Allow ELSE null END) AS 'FolderPermissionID10'
FROM
tbl_Groups g
LEFT OUTER JOIN
(
SELECT
GroupID,
FolderPermissionID,
Allow
FROM
tbl_Folders_Groups_Permissions
WHERE
FolderID = 6
) fgp
ON
g.GroupID = fgp.GroupID
GROUP BY
g.GroupID,
g.GroupName


Simple question... is it possible to make the FolderID value (6) variable without making the view into a stored procedure and supplying the FolderID as a parameter?

Kristen
Test

22859 Posts

Posted - 2006-01-02 : 12:46:04
Is there some reason that you don't just LEFT JOIN to the tbl_Folders_Groups_Permissions
table, rather than using a nested-select?

if you did that you could put

WHERE (FolderID = 6 OR FolderID IS NULL)

on the VIEW, rather than as a "constraint" on tbl_Folders_Groups_Permissions. (This assumed that FodlerID is a NOT NULL column, if it allows NULLs you will have to use a PK column from tbl_Folders_Groups_Permissions instead - e.g. WHERE (FolderID = 6 OR MyPKColumn IS NULL)

Also, would be you be better of with:

SUM(CASE FolderPermissionID WHEN 1 THEN Allow ELSE 0 END) AS 'FolderPermissionID1'
...

otherwise the application layer is going to get SQL warnings coming through ADO about the fact that the SUM includes some NULL values - which in my experience tends to muck things up!

Kristen
Go to Top of Page

X-Factor
Constraint Violating Yak Guru

392 Posts

Posted - 2006-01-02 : 13:42:54
quote:
Is there some reason that you don't just LEFT JOIN to the tbl_Folders_Groups_Permissions
table, rather than using a nested-select?



Yes its because I want all the groups ultimately returned.

If a group has been joined to a permission on a folder which isn't required and doesn't have a row to join to on a permission for the required folder then it will be filtered out altogether because it won't have been joined to a null row.

FolderId will only be null if there are no permissions for a group at all!

So I need to do the filter of the folder before the join.

Thanks for the tip.
Go to Top of Page

rockmoose
SQL Natt Alfen

3279 Posts

Posted - 2006-01-02 : 14:54:46
Have you considered a table-valued function (UDF),
they can act like "paramterized views"?

rockmoose
Go to Top of Page
   

- Advertisement -