0

假设我有以下字符串值:

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

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

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

4

2 回答 2

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]

我假设在获取字符串后您可能想要执行以下操作:(
迭代名称并查询一些数据库)

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
于 2015-01-05T12:17:17.923 回答
0

这不是很漂亮,我相信你可以使用 regex / clr patindex & substrings 更快地做到这一点,但是一个非常快速和简单的方法是使用下面的拆分函数来做这样的事情:

示例用法:

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

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

拆分成表格:

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
于 2012-06-29T09:20:02.950 回答