2010-10-07 9 views
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] 

Jusqu'à présent, le champ UriComputed est calculé comme suit:TSQL limitations de colonne calculée

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

Ce produit la sortie suivante

Results

Maintenant, je veux mettre fin à toutes les valeurs UriComputed avec '/'. Cela serait facilement accompli en ajoutant + '/' au champ calculé, sauf pour le fait que uris "textless", serait terminé comme //, ce que je ne veux pas se produire.

puisque le sql que je peux mettre dans un champ calculé est assez limité (et je ne connais pas vraiment l'étendue de ces limitations), je pensais que je demanderais ici comment ajouter ceci.

Fondamentalement, je veux la sortie de l'image pour être

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

mon plus proche tentative de le faire a été:

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

Ce qui fait une sorte de gâchis, et ajoute un nombre s'il doit se terminer par '/', et ajoute '/' quand il le faut, ce dont j'ai besoin est le contraire (c'est-à-dire '/' quand la longueur est 0, '' sinon)

S'il y a un inline ou quelque chose comme ça que je pourrais utiliser, ce serait essentiellement, mais je ne sais pas à ce sujet.

Merci!

Répondre

5

Cela a fonctionné pour moi:

[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, 

pleine 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) 
+0

+1, a travaillé pour moi. OMG, tu m'as donné mes premiers repreps il y a quelques semaines, alors ... merci! :) –

+0

@adrift: Merci - De rien. –

+0

J'ai créé une fonction définie par l'utilisateur, puis [UriComputed] AS dbo.Func ([Id]) PERSISTED – bevacqua

Questions connexes