SQL从列名中提取信息

时间:2016-09-22 15:59:54

标签: sql sql-server tsql

我有一个SQL Server表,其中包含INT115INT430等列名,以及相应的数据。我想将INT115列中的数据乘以115,将INT430列中的数据乘以430,依此类推。我不知道该怎么办。

编辑:我需要解析列名并删除左边3个字符以得出乘数。

Edit2:我正在使用grandfathered表,因此我无法对数据库的外观进行更改。

5 个答案:

答案 0 :(得分:2)

不确定所需的结果应该是什么样的,但如果您不想编写所有字段名称,也许这可以帮助

Declare @YourTable table (ID int,INT115 int,INT430 int,INT550 int)
Insert into @YourTable values
(1,25,75,18),
(2,10,1,22)

Declare @XML xml
Set @XML = (Select * from @YourTable for XML RAW)

;with cteBase as (
    Select ID      = r.value('@ID','int')
          ,Item    = attr.value('local-name(.)','varchar(100)')
          ,Value   = attr.value('.','varchar(250)') 
     From  @XML.nodes('/row') as A(r)
     Cross Apply A.r.nodes('./@*[local-name(.)!="ID"]') as B(attr)
)
Select ID
      ,Item
      ,OldValue = value
      ,NewValue = cast(Right(Item,3) as int)*Value
 From cteBase
 Where Item Like 'INT___'

返回

ID  Item    OldValue    NewValue
1   INT115  25          2875
1   INT430  75          32250
1   INT550  18          9900
2   INT115  10          1150
2   INT430  1           430
2   INT550  22          12100

答案 1 :(得分:2)

这可能不是最好的方法,但它会提供所需的结果。

USE MASTER;
CREATE TABLE table1
(
INT115 int,
INT430 int,
INT1000 int
)

Insert into table1 values (1, 1, 1);
Insert into table1 values (10, 10, 10);

DECLARE @MyTable VARCHAR(100) = 'table1'
DECLARE @cmd VARCHAR(MAX)
DECLARE @COLS VARCHAR(MAX) = ''


SELECT @COLS = @COLS + ', ' + COLUMN_NAME + '*' + STUFF(COLUMN_NAME, 1, 3, '') + ' AS '+ COLUMN_NAME
FROM master.INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = @MyTable

DECLARE @COLNAMES VARCHAR(MAX) = STUFF(@COLS, 1, 1, '')

SET @cmd = 'SELECT '+ @COLNAMES+ ' FROM '  + @MyTable
EXEC (@cmd)

输出:

INT115  INT430  INT1000
---------------------
115     430     1000
1150    4300    10000

答案 2 :(得分:1)

问题更新后编辑

这根本不需要任何硬编码: 您可以浏览通过列名解析的临时表以获取乘法值,然后从表中选择SELECT,这将为您提供相乘的值。我使用'sampletable'作为你的表名。这假设每个列都以INT开头,然后立即有一个数字。

SELECT COLUMN_NAME, 
    CAST(SUBSTRING(COLUMN_NAME, 4, 10)as INT) as multiplier_number 
    INTO #values FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'sampletable'

DECLARE @sSql nvarchar(max)
DECLARE @columnname nvarchar(50)

DECLARE thing CURSOR
    FOR
        (
        SELECT COLUMN_NAME 
        FROM #values
        )
OPEN thing
FETCH NEXT FROM thing
    INTO @columnname

WHILE @@FETCH_STATUS = 0
BEGIN
    Set @sSql = 'SELECT ' + @columnname + '* (SELECT multiplier_number FROM #values where column_name = '''+ @columnname+''') as '+@columnname+' FROM sampletable'

EXEC sp_Executesql @sSql

FETCH NEXT FROM thing into @columnname
END
CLOSE THING
DEALLOCATE THING

答案 3 :(得分:1)

您可以运行一个简单的select语句,如下所示:

select col1*15 as <heading>, col2*30 as <heading> 
from <schema>.<tablename>

e.g。

$ db2 "create table test(col15 int, col30 int)"
DB20000I  The SQL command completed successfully.

db2 "insert into test values (1,1),(2,2),(3,3)"
DB20000I  The SQL command completed successfully.

db2 "select col15*15, col30*30 from test"

1           2
----------- -----------
         15          30
         30          60
         45          90

  3 record(s) selected.

希望这有帮助。

答案 4 :(得分:1)

这是一种从列名解析乘数值的方法:

SELECT RIGHT(name, LEN(name) - PATINDEX('%[0-9]%', name) + 1)
FROM sys.columns
WHERE object_id = OBJECT_ID('yourtablenamehere')

我希望我知道更多,但我希望这篇文章有所帮助!