2
CREATE TABLE [dbo].[MembershipModule](
 [Id] [uniqueidentifier] ROWGUIDCOL  NOT NULL,
 [ParentId] [uniqueidentifier] NULL,
 [TargetId] [int] NULL,
 [WebContentId] [uniqueidentifier] NULL,
 [Name] [varchar](35) NOT NULL,
 [NameUpper]  AS (isnull(upper([Name]),'')) PERSISTED NOT NULL,
 [UriPrefix] [varchar](max) NULL,
 [UriText] [varchar](max) NULL,
 [UriComputed]  AS ??? PERSISTED,
 [Description] [varchar](100) NULL,
 [Created] [date] NOT NULL,
 [Modified] [datetime2](7) NOT NULL,
 [MenuItem] [bit] NOT NULL,
 [Enabled] [bit] NOT NULL,
 [Position] [smallint] NULL,
 CONSTRAINT [PK_MembershipModule] PRIMARY KEY CLUSTERED 
(
 [Id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

到目前为止,UriComputed 字段的计算方式如下:

lower(replace(isnull([UriPrefix],'/')+coalesce([UriText],[Name]),' ','-'))

这会产生如下输出

结果

现在,我想用 . 终止所有 UriComputed 值'/'。这可以通过添加+ '/'到计算字段来轻松完成,除了“无文本”uris 将像 // 一样终止,这是我不希望发生的。

因为我可以放入计算字段的 sql 非常有限(而且我真的不知道这些限制的范围)我想我会在这里问如何添加它。

基本上我希望图像中的输出是

/a/login/
/a/announcements/
/a/
/

我最接近的尝试是:

isnull(convert(varchar(MAX),nullif(len(coalesce([UriText],[Name])),0)),'/')

哪个有点乱,如果应该以'/'结尾,则添加一个数字,并在应该添加'/'时添加'/',我需要的是相反的(即,'/'当长度为0时, '' 除此以外)

如果有一个内联 if 或类似的东西,我可以使用它基本上就是它,但我不知道。

谢谢!

4

1 回答 1

5

这对我有用:

[UriComputed]  AS (CASE 
                     WHEN RIGHT(lower(replace(isnull([UriPrefix],'/')+coalesce([UriText],[Name]),' ','-')), 1) = '/' THEN
                       lower(replace(isnull([UriPrefix],'/')+coalesce([UriText],[Name]),' ','-'))
                     ELSE
                       lower(replace(isnull([UriPrefix],'/')+coalesce([UriText],[Name]),' ','-')) +'/'
                   END) PERSISTED,

完整的 CREATE TABLE 语句:

CREATE TABLE [dbo].[MembershipModule](
 [Id] [uniqueidentifier] ROWGUIDCOL  NOT NULL,
 [ParentId] [uniqueidentifier] NULL,
 [TargetId] [int] NULL,
 [WebContentId] [uniqueidentifier] NULL,
 [Name] [varchar](35) NOT NULL,
 [NameUpper]  AS (isnull(upper([Name]),'')) PERSISTED NOT NULL,
 [UriPrefix] [varchar](max) NULL,
 [UriText] [varchar](max) NULL,
 [UriComputed]  AS (CASE 
                     WHEN RIGHT(lower(replace(isnull([UriPrefix],'/')+coalesce([UriText],[Name]),' ','-')), 1) = '/' THEN
                       lower(replace(isnull([UriPrefix],'/')+coalesce([UriText],[Name]),' ','-'))
                     ELSE
                       lower(replace(isnull([UriPrefix],'/')+coalesce([UriText],[Name]),' ','-')) +'/'
                    END) PERSISTED,
 [Description] [varchar](100) NULL,
 [Created] [date] NOT NULL,
 [Modified] [datetime2](7) NOT NULL,
 [MenuItem] [bit] NOT NULL,
 [Enabled] [bit] NOT NULL,
 [Position] [smallint] NULL)
于 2010-10-07T03:28:57.893 回答