在Trigger中循环所有列

时间:2013-08-29 13:25:55

标签: sql-server triggers

我在表格中有大约51列,我需要在插入触发器中的45列上应用一个数学公式。我在SQL用户定义函数中有我的公式,现在我想遍历所有插入的列并通过条件传递它们[所以我只选择45列]然后应用函数来获取我的值。有什么好办法吗?

哦,这是我的第一个触发器,我真的不知道该怎么做,我正在搜索MSDN,但是还没找到任何东西。

Create TABLE Project_5_Data (
DataID BIGINT IDENTITY(1,1) NOT NULL,
FileID BIGINT,
LogTime DATETIME,
F0_8 DECIMAL(18,2),
F1 DECIMAL(18,2),
F1_25 DECIMAL(18,2),
F1_60 DECIMAL(18,2),
F2 DECIMAL(18,2),
F2_5 DECIMAL(18,2),
F3_15 DECIMAL(18,2),
F4 DECIMAL(18,2),
F5 DECIMAL(18,2),
F6_3 DECIMAL(18,2),
F8 DECIMAL(18,2),
F10 DECIMAL(18,2),
F12_5 DECIMAL(18,2),
F16 DECIMAL(18,2),
F20 DECIMAL(18,2),
F25 DECIMAL(18,2),
F31_5 DECIMAL(18,2),
F40 DECIMAL(18,2),
F50 DECIMAL(18,2),
F63 DECIMAL(18,2),
F80 DECIMAL(18,2),
F100 DECIMAL(18,2),
F125 DECIMAL(18,2),
F160 DECIMAL(18,2),
F200 DECIMAL(18,2),
F250 DECIMAL(18,2),
F315 DECIMAL(18,2),
F400 DECIMAL(18,2),
F500 DECIMAL(18,2),
F630 DECIMAL(18,2),
F800 DECIMAL(18,2),
F1000 DECIMAL(18,2),
F1250 DECIMAL(18,2),
F1600 DECIMAL(18,2),
F2000 DECIMAL(18,2),
F2500 DECIMAL(18,2),
F3150 DECIMAL(18,2),
F4000 DECIMAL(18,2),
F5000 DECIMAL(18,2),
F6300 DECIMAL(18,2),
F8000 DECIMAL(18,2),
F10000 DECIMAL(18,2),
F12500 DECIMAL(18,2),
F16000 DECIMAL(18,2),
F20000 DECIMAL(18,2),
    TotA DECIMAL(18,2),
TotC DECIMAL(18,2),
TotZ DECIMAL(18,2),
Primary Key ( DataID)
)

我想迭代所有以F开头的列(除了FileID命名列),然后应用我的函数[dbo]。[FindLAValue],它将DEcimal输入作为参数。

我只需要知道如何循环“插入”列或该表内触发器内的所有列。谢谢。

1 个答案:

答案 0 :(得分:2)

不是答案,但我希望这可以节省您的时间 -

DECLARE
      @LogTime DATETIME
    , @F0_8 DECIMAL(18,2)
    , @F1 DECIMAL(18,2)
    , @F1_25 DECIMAL(18,2)
    , @F1_60 DECIMAL(18,2)
    , @F2 DECIMAL(18,2)
    , @F2_5 DECIMAL(18,2)
    , @F3_15 DECIMAL(18,2)
    , @F4 DECIMAL(18,2)
    , @F5 DECIMAL(18,2)
    , @F6_3 DECIMAL(18,2)
    , @F8 DECIMAL(18,2)
    , @F10 DECIMAL(18,2)
    , @F12_5 DECIMAL(18,2)
    , @F16 DECIMAL(18,2)
    , @F20 DECIMAL(18,2)
    , @F25 DECIMAL(18,2)
    , @F31_5 DECIMAL(18,2)
    , @F40 DECIMAL(18,2)
    , @F50 DECIMAL(18,2)
    , @F63 DECIMAL(18,2)
    , @F80 DECIMAL(18,2)
    , @F100 DECIMAL(18,2)
    , @F125 DECIMAL(18,2)
    , @F160 DECIMAL(18,2)
    , @F200 DECIMAL(18,2)
    , @F250 DECIMAL(18,2)
    , @F315 DECIMAL(18,2)
    , @F400 DECIMAL(18,2)
    , @F500 DECIMAL(18,2)
    , @F630 DECIMAL(18,2)
    , @F800 DECIMAL(18,2)
    , @F1000 DECIMAL(18,2)
    , @F1250 DECIMAL(18,2)
    , @F1600 DECIMAL(18,2)
    , @F2000 DECIMAL(18,2)
    , @F2500 DECIMAL(18,2)
    , @F3150 DECIMAL(18,2)
    , @F4000 DECIMAL(18,2)
    , @F5000 DECIMAL(18,2)
    , @F6300 DECIMAL(18,2)
    , @F8000 DECIMAL(18,2)
    , @F10000 DECIMAL(18,2)
    , @F12500 DECIMAL(18,2)
    , @F16000 DECIMAL(18,2)
    , @F20000 DECIMAL(18,2)
    , @TotA DECIMAL(18,2)
    , @TotC DECIMAL(18,2)
    , @TotZ DECIMAL(18,2)
    , @DataID BIGINT
    , @FileID BIGINT

DECLARE project_5_data CURSOR LOCAL READ_ONLY FAST_FORWARD FOR
SELECT
      DataID
    , FileID
    , LogTime
    , F0_8
    , F1
    , F1_25
    , F1_60
    , F2
    , F2_5
    , F3_15
    , F4
    , F5
    , F6_3
    , F8
    , F10
    , F12_5
    , F16
    , F20
    , F25
    , F31_5
    , F40
    , F50
    , F63
    , F80
    , F100
    , F125
    , F160
    , F200
    , F250
    , F315
    , F400
    , F500
    , F630
    , F800
    , F1000
    , F1250
    , F1600
    , F2000
    , F2500
    , F3150
    , F4000
    , F5000
    , F6300
    , F8000
    , F10000
    , F12500
    , F16000
    , F20000
    , TotA
    , TotC
    , TotZ
FROM dbo.project_5_data

OPEN project_5_data
FETCH NEXT FROM project_5_data INTO
      @DataID
    , @FileID
    , @LogTime
    , @F0_8
    , @F1
    , @F1_25
    , @F1_60
    , @F2
    , @F2_5
    , @F3_15
    , @F4
    , @F5
    , @F6_3
    , @F8
    , @F10
    , @F12_5
    , @F16
    , @F20
    , @F25
    , @F31_5
    , @F40
    , @F50
    , @F63
    , @F80
    , @F100
    , @F125
    , @F160
    , @F200
    , @F250
    , @F315
    , @F400
    , @F500
    , @F630
    , @F800
    , @F1000
    , @F1250
    , @F1600
    , @F2000
    , @F2500
    , @F3150
    , @F4000
    , @F5000
    , @F6300
    , @F8000
    , @F10000
    , @F12500
    , @F16000
    , @F20000
    , @TotA
    , @TotC
    , @TotZ

WHILE @@FETCH_STATUS = 0 BEGIN

    -- INSERT STATEMENT HERE

    FETCH NEXT FROM project_5_data INTO
      @DataID
    , @FileID
    , @LogTime
    , @F0_8
    , @F1
    , @F1_25
    , @F1_60
    , @F2
    , @F2_5
    , @F3_15
    , @F4
    , @F5
    , @F6_3
    , @F8
    , @F10
    , @F12_5
    , @F16
    , @F20
    , @F25
    , @F31_5
    , @F40
    , @F50
    , @F63
    , @F80
    , @F100
    , @F125
    , @F160
    , @F200
    , @F250
    , @F315
    , @F400
    , @F500
    , @F630
    , @F800
    , @F1000
    , @F1250
    , @F1600
    , @F2000
    , @F2500
    , @F3150
    , @F4000
    , @F5000
    , @F6300
    , @F8000
    , @F10000
    , @F12500
    , @F16000
    , @F20000
    , @TotA
    , @TotC
    , @TotZ

END

CLOSE project_5_data
DEALLOCATE project_5_data

脚本生成器 -

DECLARE 
      @Name SYSNAME = 'schema_name.table_name' -- dbo.test_table
    , @SQL NVARCHAR(MAX)

SELECT @SQL = 'DECLARE' 
    + CHAR(13) + STUFF((
        SELECT CHAR(9) + ', @' + c.name + ' ' + UPPER(t.name) + 
            CASE WHEN t.name IN ('nvarchar', 'varchar', 'nchar', 'char', 'varbinary', 'binary')
                   THEN '(' + CASE WHEN c.max_length = -1 THEN 'MAX' ELSE CAST(c.max_length AS VARCHAR(5)) END + ')'
                 WHEN t.name IN ('datetime2', 'time2', 'datetimeoffset') 
                   THEN '(' + CAST(c.scale AS NVARCHAR(5)) + ')'
                 WHEN t.name = 'decimal' 
                   THEN '(' + CAST(c.[precision] AS NVARCHAR(5)) + ',' + CAST(c.scale AS VARCHAR(5)) + ')'
                ELSE ''
            END + CHAR(13)
        FROM sys.columns c
        JOIN sys.types t ON c.system_type_id = t.system_type_id  AND c.system_type_id = t.user_type_id
        WHERE c.[object_id] = ob.[object_id]
        ORDER BY c.column_id
        FOR XML PATH(''), TYPE).value('.', 'VARCHAR(MAX)'), 1, 2, CHAR(9) + ' ')
    + CHAR(13) + 'DECLARE ' + ob.name + ' CURSOR LOCAL READ_ONLY FAST_FORWARD FOR' 
    + CHAR(13) + 'SELECT' + CHAR(13) + STUFF((
        SELECT CHAR(9) + ', ' + c.name + CHAR(13)
        FROM sys.columns c
        WHERE c.[object_id] = ob.[object_id]
        FOR XML PATH(''), TYPE).value('.', 'VARCHAR(MAX)'), 1, 2, CHAR(9) + ' ')
    + 'FROM ' + ob.sname + '.' + ob.oname
    + CHAR(13) + CHAR(13) + 'OPEN ' + ob.name
    + CHAR(13) + b.FetchBlock
    + CHAR(13) + 'WHILE @@FETCH_STATUS = 0 BEGIN'
    + CHAR(13) + CHAR(13) + CHAR(9) + '-- INSERT STATEMENT HERE'
    + CHAR(13) + CHAR(13) + CHAR(9) + b.FetchBlock
    + CHAR(13) + 'END'
    + CHAR(13) + CHAR(13) + 'CLOSE ' + ob.name
    + CHAR(13) + 'DEALLOCATE ' + ob.name
FROM (
    SELECT 
          o.[object_id]
        , oname = o.name
        , sname = SCHEMA_NAME(o.[schema_id])
        , name = LOWER(o.name) 
    FROM sys.objects o 
    WHERE o.[type] IN ('V', 'U')
        AND SCHEMA_NAME(o.[schema_id]) + '.' + o.name = @Name
) ob
OUTER APPLY (
    SELECT FetchBlock = 'FETCH NEXT FROM ' + ob.name + ' INTO' + CHAR(13) + 
    STUFF((
        SELECT CHAR(9) + ', @' + c.name + CHAR(13)
        FROM sys.columns c
        WHERE c.[object_id] = ob.[object_id]
        FOR XML PATH(''), TYPE).value('.', 'VARCHAR(MAX)'), 1, 2, CHAR(9) + ' ')
) b

PRINT @SQL