2017-10-09 75 views
1

我在SQL Server中的表,我需要找到具有相同的值(稱之爲「B67」)的所有行之列,但我不現在是列名稱(因爲列的名稱經常更改)。我需要找到這一列,並要求下一列的值的另一部分(2列總是在一起)。表查找在SQL Server表中的值,並獲得下一列

例(想象中的一樣,但更'coso'):

ID | coso1 | c45 | coso2  | b63 | Coso3 | d28 
---+----------+---------+-------------+--------+--------+---- 
1 | b63 | 1  | d28  | 5  | c45 | 3 
2 | b63 | 4  | d28  | 6  | c45 | 5 
3 | b63 | 67  | d28  | 1  | c45 | 2 
4 | b63 | 34  | d28  | 5  | c45 | 6 
5 | b63 | 8  | d28  | 6  | c45 | 9 
+3

爲什麼列名更改?似乎是一個糟糕的主意。 – jarlh

+0

以下是控制數據庫,添加新列和修改所有數據的人員,例如現在的列'b67',擁有'c45'的東西, – KMG104

+0

下一個列名的確切定義是什麼? – gotqn

回答

0

這不是很清楚究竟你需要的,但看看到SQL:

create table test_columns 
(
coso1 char(3), 
c45 int, 
coso2 char(3), 
b63 int 
); 

insert into test_columns values ('b63', 1, 'd28', 5) 

DECLARE @column_id INT, 
     @name varchar(200), 
     @query varchar(max), 
     @next_column varchar(200) 
DECLARE @cursor CURSOR 
SET @cursor = CURSOR FOR 
select name, column_id from sys.columns where object_name(object_id) = 'test_columns' and name like 'coso%' 
OPEN @cursor 
FETCH NEXT 
FROM @cursor INTO @name, @column_id 
WHILE @@FETCH_STATUS = 0 
BEGIN 
    SET @query = 'SELECT 1 FROM dbo.test_columns HAVING COUNT(distinct ' + @name + ') = 1'; 
    EXEC (@query); 
    IF (@@ROWCOUNT = 1) 
    BEGIN 
     SELECT @next_column = name FROM sys.columns where object_name(object_id) = 'test_columns' and column_id = @column_id + 1; 
    SET @query = 'SELECT ' + @next_column + ', ' + @name + ' FROM dbo.test_columns'; 

    EXEC (@query); 

    END 
FETCH NEXT 
FROM @cursor INTO @name, @column_id 
END 
CLOSE @cursor 
DEALLOCATE @cursor 
0

您可以構建動態sql來測試每個列。 我用DM_EXEC_DESCRIBE_FIRST_RESULT_SET獲得列名,但你也可以使用SELECT * FROM INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = 'MYTABLE'

我保存的列數,列名和SQL查詢的臨時(變量)表,以測試列,然後循環救援人員到場偶數列測試通過價值。

它應該工作

CREATE PROCEDURE FIND_VAL (
    @ValuetoFind varchar(100) 
) 
AS 
BEGIN 

    DECLARE @TABLENAME VARCHAR(100) = 'MYTABLE' 


    DECLARE @S TABLE (ID INT PRIMARY KEY, SQL_CMD VARCHAR(255), [NAME] VARCHAR(32)) 
    DECLARE @SQL_CMD NVARCHAR(255) = '' 
    DECLARE @ID INT = 0 
    DECLARE @C INT 

    ;WITH 
    C AS (
     SELECT * FROM SYS.DM_EXEC_DESCRIBE_FIRST_RESULT_SET('SELECT * FROM '[email protected],NULL,1) 
    ), 
    S AS (
     SELECT COLUMN_ORDINAL, 'SET @CNT= CASE WHEN EXISTS(SELECT TOP 1 1 FROM '+ @TABLENAME + ' WHERE ISNULL(' + [NAME] + ', '''')<>'''[email protected]+''') THEN 0 ELSE 1 END' Q, [NAME] 
     FROM C 
    ) 
    INSERT INTO @S 
    SELECT * FROM S 

    --SELECT * FROM @S 

    WHILE EXISTS(SELECT TOP 1 1 FROM @S WHERE (ID % 2)=0 ORDER BY ID) BEGIN 

     SELECT TOP 1 @ID=ID, @SQL_CMD = SQL_CMD FROM @S WHERE (ID % 2)=0 ORDER BY ID 

     EXEC SP_EXECUTESQL @SQL_CMD, N'@CNT INT OUTPUT', @[email protected] OUTPUT 

     IF @C = 1 BEGIN 
      SELECT [NAME] FROM @S WHERE [email protected]+1 
      RETURN 
     END 

     DELETE FROM @S WHERE ID = @ID 
    END 
    SELECT NULL [NAME] 
END 

輸出

NAME 
c45 
相關問題