Results 1 to 3 of 3

Thread: I appreciate If anyone Help me to write this query

  1. #1
    Join Date
    Mar 2006
    Posts
    14

    I appreciate If anyone Help me to write this query

    User Page Name Permission
    vijay customer.aspx 1
    vijay customer.aspx 2
    vijay customer.aspx 3
    vijay user.aspx 2
    Rajashekar customer.aspx 1
    Rajashekar customer.aspx 2

    Where Permission 1 = SAVE
    2 = UPDATE
    3 = DELLETE

    Where I query on User and PageName I want the output as

    User Page Name Permission
    vijay customer.aspx 1,2,3
    vijay user.aspx 2
    Rajashekar customer.aspx 1,2

  2. #2
    Join Date
    Apr 2006
    Location
    Ukraine
    Posts
    1
    At first you must crate function, which returns table...

    create function selPerms()
    returns @retPermissions table
    (
    User nvarchar(100),
    PageName nvarchar(100),
    Permission nvarchar(100)
    )
    as
    begin
    declare @lUser nvarchar(100),
    @lPageName nvarchar(100),
    @lPermission nvarchar(100),
    @lTempPerm nvarchar(100)

    declare selPerm cursor local fast_forward read_only for
    Select User, PageName, Permission from tblName

    open selPerm
    fetch next from selPerm into @lUser, @lPageName, @lPermission

    while (@@fetch_status > -1)
    begin

    if exists(Select * from @retPermissions where User = @lUser and PageName = @lPageName)
    begin
    Select @lTempPerm = Permission from @retPermissions
    where User = @lUser and PageName = @lPageName
    Update @retPermissions set Permission = @lTempPerm + ', ' + @lPermission
    end
    else
    Insert into @lTempPerm (User, PageName, Permission)
    values (@lUser, @lPageName, @lPermission)

    fetch next from selPerm into @lUser, @lPageName, @lPermission
    end

    close selPerm
    deallocate selPerm

    end

    In this function I use for all User, PageName, Permission fields type nvarchar(100) because I don't know what type it is in your table... and table name which your filds are placed in - tblName...

    You must correct these for your table name and fileds types..

    Then you just select all from created function

    Select * from selPerms()

  3. #3
    Join Date
    Sep 2005
    Posts
    168
    --Replace {Mytable} with the appropriate table name.
    --Suppose Permission is numeric
    --...not the best implementation

    SELECT DISTINCT ISNULL(clone.[User], ISNULL(clone1.[User], , clone2.[User], )) AS [USER],
    ISNULL(clone.[Page_Name], ISNULL(clone1.[Page_Name], clone2.[Page_Name])) AS [PAGE_NAME],
    ISNULL(CAST(clone.[Permission] AS VARCHAR(1)), '')+ ISNULL(CASE ISNULL(clone.[Permission], 0) WHEN 0 THEN '' ELSE ',' END + CAST(clone1.[Permission] AS VARCHAR(1)), '') + ISNULL(CASE ISNULL(clone.[Permission], 0)+ISNULL(clone1.[Permission], 0) WHEN 0 THEN '' ELSE ',' END+CAST(clone2.[Permission]AS VARCHAR(1)), '') AS [PERMISSION]
    FROM {Mytable} tmp
    LEFT JOIN {Mytable} clone ON clone.[User], = tmp.[User], AND clone.[Page_Name] = tmp.[Page_Name] AND clone.[Permission]= 1
    LEFT JOIN {Mytable} clone1 ON clone1.[User], = tmp.[User], AND clone1.[Page_Name] = tmp.[Page_Name] AND clone1.[Permission]= 2
    LEFT JOIN @tmp clone2 ON clone2.[User], = tmp.[User], AND clone2.[Page_Name] = tmp.[Page_Name] AND clone2.[Permission]= 3
    ORDER BY [USER], [PAGE_NAME]

    --HTH--

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •