如何使用TSQL从连接字符串中提取目录名称?

时间:2012-06-28 21:08:32

标签: tsql pattern-matching

说我有以下字符串值:

declare @cs nvarchar(100) = 
   'Data Source=server\instance;Initial Catalog=MyDatabase;Integrated Security=True';

MyDatabase中提取字符串@cs的T-SQL是什么?

或者,我应该查看哪些T-SQL函数,以便为自己解决这个问题?

2 个答案:

答案 0 :(得分:0)

它不漂亮,我相信你可以使用regex / clr patindex&子串,但一种非常快速和简单的方法是使用下面的拆分函数做这样的事情:

使用示例:

DECLARE @cs NVARCHAR(100) = 'Data Source=server\instance;Initial Catalog=MyDatabase;Integrated Security=True';

SELECT  id ,
        Data ,
        dbo.fnParseString(2, '=', Data)
FROM    dbo.fnc_Split(@cs, ';')
WHERE   Data LIKE '%Initial Catalog%'

<强>返回:

2        Initial Catalog=MyDatabase        MyDatabase

对于split函数,第一个将字符串拆分为表,第二个将字符串拆分为列:

分成表格

CREATE FUNCTION dbo.fnc_Split
    (
      @Data VARCHAR(2000) ,
      @Sep VARCHAR(5)
    )
RETURNS @Temp TABLE
    (
      Id INT IDENTITY(1, 1) ,
      Data NVARCHAR(100)
    )
AS 
    BEGIN
        DECLARE @Cnt INT
        SET @Cnt = 1
        WHILE ( CHARINDEX(@Sep, @Data) > 0 ) 
            BEGIN
                INSERT  INTO @Temp
                        ( data
                        )
                        SELECT  Data = LTRIM(RTRIM(SUBSTRING(@Data, 1, CHARINDEX(@Sep, @Data) - 1)))
                SET @Data = SUBSTRING(@Data, CHARINDEX(@Sep, @Data) + 1, LEN(@Data))
                SET @Cnt = @Cnt + 1
            END
        INSERT  INTO @Temp
                ( data )
                SELECT  Data = LTRIM(RTRIM(@Data))
        RETURN
    END
GO

分成若干列:

CREATE FUNCTION dbo.fnParseString
    (
      @Section SMALLINT ,
      @Delimiter CHAR ,
      @Text VARCHAR(MAX)
    )
RETURNS VARCHAR(8000)
AS 
    BEGIN
        DECLARE @NextPos SMALLINT ,
            @LastPos SMALLINT ,
            @Found SMALLINT

        --#### Uncomment the following 2 lines to emulate PARSENAME functionality
        --IF @Section > 0 
        --    SELECT  @Text = REVERSE(@Text)

        SELECT  @NextPos = CHARINDEX(@Delimiter, @Text, 1) ,
                @LastPos = 0 ,
                @Found = 1

        WHILE @NextPos > 0
            AND ABS(@Section) <> @Found 
            SELECT  @LastPos = @NextPos ,
                    @NextPos = CHARINDEX(@Delimiter, @Text, @NextPos + 1) ,
                    @Found = @Found + 1

        RETURN  CASE
            WHEN @Found <> ABS(@Section) OR @Section = 0 THEN NULL
            --#### Uncomment the following lines to emulate PARSENAME functionality
            --WHEN @Section > 0 THEN REVERSE(SUBSTRING(@Text, @LastPos + 1, CASE WHEN @NextPos = 0 THEN DATALENGTH(@Text) - @LastPos ELSE @NextPos - @LastPos - 1 END))
            WHEN @Section > 0 THEN SUBSTRING(@Text, @LastPos + 1, CASE WHEN @NextPos = 0 THEN DATALENGTH(@Text) - @LastPos ELSE @NextPos - @LastPos - 1 END)

            ELSE SUBSTRING(@Text, @LastPos + 1, CASE WHEN @NextPos = 0 THEN DATALENGTH(@Text) - @LastPos ELSE @NextPos - @LastPos - 1 END)
        END
    END

答案 1 :(得分:0)

您可能会发现这更容易,只需将from_table更改为您的表,将连接字符串字段更改为您的字段。

SELECT SUBSTRING(SUBSTRING([ConnectionString], CHARINDEX('Initial Catalog=', [ConnectionString]) + LEN('Initial Catalog='), 100), 0, CHARINDEX(';', SUBSTRING([ConnectionString], CHARINDEX('Initial Catalog=', [ConnectionString]) + LEN('Initial Catalog='), 100)))
AS DbName
FROM [from_table]

我假设在拍完琴弦之后你可能想做这样的事情:
(迭代名称并查询一些dbs)

DECLARE @DbName varchar(max)
DECLARE @QaQuery NVARCHAR(MAX);

DECLARE MY_CURSOR CURSOR 
  LOCAL STATIC READ_ONLY FORWARD_ONLY
FOR 
SELECT SUBSTRING(SUBSTRING([ConnectionString], CHARINDEX('Initial Catalog=', [ConnectionString]) + LEN('Initial Catalog='), 100), 0, CHARINDEX(';', SUBSTRING([ConnectionString], CHARINDEX('Initial Catalog=', [ConnectionString]) + LEN('Initial Catalog='), 100)))
AS Connections
FROM [ConnTable]

OPEN MY_CURSOR
FETCH NEXT FROM MY_CURSOR INTO @DbName
WHILE @@FETCH_STATUS = 0
BEGIN 
    --PRINT @DbName
    SET @QaQuery = N'SELECT TOP 10 [RecordID],[SomeStuff]
                        FROM ['+@DbName+'].[dbo].[SomeTable]
                        WHERE [SomeStuff] IS NOT NULL'
  EXEC sp_executesql @QaQuery;
  FETCH NEXT FROM MY_CURSOR INTO @DbName
END
CLOSE MY_CURSOR
DEALLOCATE MY_CURSOR