• <small id='ikbTA'></small><noframes id='ikbTA'>

    <legend id='ikbTA'><style id='ikbTA'><dir id='ikbTA'><q id='ikbTA'></q></dir></style></legend>
    <tfoot id='ikbTA'></tfoot>

          <bdo id='ikbTA'></bdo><ul id='ikbTA'></ul>

      1. <i id='ikbTA'><tr id='ikbTA'><dt id='ikbTA'><q id='ikbTA'><span id='ikbTA'><b id='ikbTA'><form id='ikbTA'><ins id='ikbTA'></ins><ul id='ikbTA'></ul><sub id='ikbTA'></sub></form><legend id='ikbTA'></legend><bdo id='ikbTA'><pre id='ikbTA'><center id='ikbTA'></center></pre></bdo></b><th id='ikbTA'></th></span></q></dt></tr></i><div id='ikbTA'><tfoot id='ikbTA'></tfoot><dl id='ikbTA'><fieldset id='ikbTA'></fieldset></dl></div>

      2. SQL Server SELECT,其中任何列包含“x"

        时间:2023-10-10
      3. <i id='T88FP'><tr id='T88FP'><dt id='T88FP'><q id='T88FP'><span id='T88FP'><b id='T88FP'><form id='T88FP'><ins id='T88FP'></ins><ul id='T88FP'></ul><sub id='T88FP'></sub></form><legend id='T88FP'></legend><bdo id='T88FP'><pre id='T88FP'><center id='T88FP'></center></pre></bdo></b><th id='T88FP'></th></span></q></dt></tr></i><div id='T88FP'><tfoot id='T88FP'></tfoot><dl id='T88FP'><fieldset id='T88FP'></fieldset></dl></div>
              <tbody id='T88FP'></tbody>

            <legend id='T88FP'><style id='T88FP'><dir id='T88FP'><q id='T88FP'></q></dir></style></legend>

            <tfoot id='T88FP'></tfoot>

              <small id='T88FP'></small><noframes id='T88FP'>

                <bdo id='T88FP'></bdo><ul id='T88FP'></ul>

                1. 本文介绍了SQL Server SELECT,其中任何列包含“x"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  使用 SQL Server 2008,假设我有一个名为 testing 的表,有 80 列,我想找到一个名为 foo 的值.

                  Using SQL Server 2008, say I have a table called testing with 80 columns and I want to find a value called foo.

                  我能做到:

                  SELECT * 
                  FROM testing 
                  WHERE COLNAME = 'foo'
                  

                  是否可以查询所有 80 列并返回所有结果,其中 foo 包含在 80 列中的任何一列中?

                  Is it possible I can query all 80 columns and return all the results where foo is contained in any of the 80 columns?

                  提前致谢.

                  推荐答案

                  第一种方法(已测试)首先获取以逗号分隔的字符串变量中的列列表,然后您可以使用 IN

                  First Method(Tested) First get list of columns in string variable separated by commas and then you can search 'foo' using that variable by use of IN

                  检查下面的存储过程,首先获取列然后搜索字符串:

                  Check stored procedure below which first gets columns and then searches for string:

                  DECLARE @TABLE_NAME VARCHAR(128)
                  DECLARE @SCHEMA_NAME VARCHAR(128)
                  
                  -----------------------------------------------------------------------
                  
                  -- Set up the name of the table here :
                  SET @TABLE_NAME = 'testing'
                  -- Set up the name of the schema here, or just leave set to 'dbo' :
                  SET @SCHEMA_NAME = 'dbo'
                  
                  -----------------------------------------------------------------------
                  
                  DECLARE @vvc_ColumnName VARCHAR(128)
                  DECLARE @vvc_ColumnList VARCHAR(MAX)
                  
                  IF @SCHEMA_NAME =''
                    BEGIN
                    PRINT 'Error : No schema defined!'
                    RETURN
                    END
                  
                  IF NOT EXISTS (SELECT * FROM sys.tables T JOIN sys.schemas S
                        ON T.schema_id=S.schema_id
                        WHERE T.Name=@TABLE_NAME AND S.name=@SCHEMA_NAME)
                    BEGIN
                    PRINT 'Error : The table '''+@TABLE_NAME+''' in schema '''+
                        @SCHEMA_NAME+''' does not exist in this database!' 
                    RETURN
                   END
                  
                  DECLARE TableCursor CURSOR FAST_FORWARD FOR
                  SELECT   CASE WHEN PATINDEX('% %',C.name) > 0 
                       THEN '['+ C.name +']' 
                       ELSE C.name 
                       END
                  FROM     sys.columns C
                  JOIN     sys.tables T
                  ON       C.object_id  = T.object_id
                  JOIN     sys.schemas S
                  ON       S.schema_id  = T.schema_id
                  WHERE    T.name    = @TABLE_NAME
                  AND      S.name    = @SCHEMA_NAME
                  ORDER BY column_id
                  
                  
                  SET @vvc_ColumnList=''
                  
                  OPEN TableCursor
                  FETCH NEXT FROM TableCursor INTO @vvc_ColumnName
                  
                  WHILE @@FETCH_STATUS=0
                    BEGIN
                     SET @vvc_ColumnList = @vvc_ColumnList + @vvc_ColumnName
                  
                      -- get the details of the next column
                     FETCH NEXT FROM TableCursor INTO @vvc_ColumnName
                  
                    -- add a comma if we are not at the end of the row
                     IF @@FETCH_STATUS=0
                      SET @vvc_ColumnList = @vvc_ColumnList + ','
                     END
                  
                   CLOSE TableCursor
                   DEALLOCATE TableCursor
                  
                  -- Now search for `foo`
                  
                  
                  SELECT *
                  FROM testing 
                  WHERE 'foo' in (@vvc_ColumnList );
                  

                  第二种方法在 sql server 中,您可以获得表的对象 id,然后使用该对象 id 可以获取列.在这种情况下,它将如下所示:

                  2nd Method In sql server you can get object id of table then using that object id you can fetch columns. In that case it will be as below:

                  第 1 步:首先获取表的对象 ID

                  Step 1: First get Object Id of table

                  select * from sys.tables order by name    
                  

                  第 2 步:现在获取表格的列并在其中进行搜索:

                  Step 2: Now get columns of your table and search in it:

                   select * from testing where 'foo' in (select name from sys.columns  where  object_id =1977058079)
                  

                  注意:object_id 是您在第一步中为您获取的相关表

                  Note: object_id is what you get fetch in first step for you relevant table

                  这篇关于SQL Server SELECT,其中任何列包含“x"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何在 sql server 中对行号进行内部联接 下一篇:逗号分隔值与 SQL 查询

                  相关文章

                  <legend id='slcrs'><style id='slcrs'><dir id='slcrs'><q id='slcrs'></q></dir></style></legend>
                2. <tfoot id='slcrs'></tfoot>
                  1. <small id='slcrs'></small><noframes id='slcrs'>

                    1. <i id='slcrs'><tr id='slcrs'><dt id='slcrs'><q id='slcrs'><span id='slcrs'><b id='slcrs'><form id='slcrs'><ins id='slcrs'></ins><ul id='slcrs'></ul><sub id='slcrs'></sub></form><legend id='slcrs'></legend><bdo id='slcrs'><pre id='slcrs'><center id='slcrs'></center></pre></bdo></b><th id='slcrs'></th></span></q></dt></tr></i><div id='slcrs'><tfoot id='slcrs'></tfoot><dl id='slcrs'><fieldset id='slcrs'></fieldset></dl></div>

                        <bdo id='slcrs'></bdo><ul id='slcrs'></ul>