如何将动态SQL的结果存储在变量中



我有以下理论陈述,我想使用动态SQL(在SQL Server 2016上)实现并将单个输出值存储在变量(@output)中。

@numericvar@columnname@tablename为输入参数。任何帮助都将非常感激。多谢。

SELECT @output = (
    SELECT 
        MAX(CASE WHEN ROWNUM*1.0/NUMROWS <= @numericvar THEN @columnname END)
    FROM (
        SELECT
            @columnname,
            ROW_NUMBER() OVER (ORDER BY @columnname ) AS ROWNUM,
            COUNT(*) OVER (PARTITION BY NULL) AS NUMROWS
        FROM 
            @tablename 
    ) @tablename 
); 
DECLARE @columnname SYSNAME, @tablename SYSNAME, @numericvar NUMERIC(18,2);
DECLARE @output NUMERIC(18,2);
DECLARE @sql NVARCHAR(MAX) = N'
    SET @output = (
        SELECT 
            MAX(CASE WHEN ROWNUM*1.0/NUMROWS <= @numericvar THEN '+QUOTENAME(@columnname)+N' END)
        FROM (
            SELECT
                '+QUOTENAME(@columnname)+N',
                ROW_NUMBER() OVER (ORDER BY '+QUOTENAME(@columnname)+N' ) AS ROWNUM,
                COUNT(*) OVER (PARTITION BY NULL) AS NUMROWS
            FROM 
                '+QUOTENAME(@tablename)+N'
        ) AS t
    );
';
EXECUTE sp_executesql 
    @sql,
    N'@numericvar NUMERIC(18,2), @output NUMERIC(18,2) OUTPUT', 
    @numericvar, @output OUTPUT;
SELECT @output;

更新: FLOAT输出的工作示例。该脚本使用INFORMATION_SCHEMA模式下的表,每个人都有。

看看您是否可以从这个示例中使其工作。如果你不能,我建议你编辑你的问题,并添加确切的脚本+你正在使用的参数值+ columnname类型的指示。

DECLARE @schemaname SYSNAME='INFORMATION_SCHEMA',
        @tablename  SYSNAME='COLUMNS', 
        @columnname SYSNAME='NUMERIC_PRECISION', 
        @numericvar NUMERIC(18,2)=.5;
DECLARE @output_f FLOAT;
DECLARE @sql NVARCHAR(MAX) = N'
    SET @output_f = (
            SELECT 
                MAX(CASE WHEN ROWNUM*1.0/NUMROWS<=@numericvar THEN '+QUOTENAME(@columnname)+N' END)
            FROM (
                SELECT
                    '+QUOTENAME(@columnname)+N',
                    ROW_NUMBER() OVER (ORDER BY '+QUOTENAME(@columnname)+N') AS ROWNUM,
                    COUNT(*) OVER () AS NUMROWS
                FROM 
                    '+QUOTENAME(@schemaname)+N'.'+QUOTENAME(@tablename)+N'
            ) AS t
    );
';
EXECUTE sp_executesql 
    @sql,
    N'@numericvar NUMERIC(18,2), @output_f FLOAT OUTPUT', 
    @numericvar, @output_f OUTPUT;
SELECT @output_f;

试试这个,我已经复制粘贴在这里和那里,你可能需要工作周围一点,以防万一。

declare @query nvarchar(max)
declare @output nvarchar(max)
declare @columnname nvarchar(max)
declare @tablename nvarchar(max)
declare  @numericvar NUMERIC(18,0)
    set @query ='SELECT @output = (
        SELECT 
            MAX(CASE WHEN ROWNUM*1.0/NUMROWS <= @numericvar THEN @columnname END)
        FROM (
            SELECT
                @columnname,
                ROW_NUMBER() OVER (ORDER BY @columnname ) AS ROWNUM,
                COUNT(*) OVER (PARTITION BY NULL) AS NUMROWS
            FROM 
                @tablename 
        ) @tablename 
    );'
    exec sp_executesql @query, N'@output numeric(18,2) output, 
    @columnname nvarchar(max), declare @numericvar NUMERIC(18,0),
    declare @tablename nvarchar(max)', @output= @output output, @columnname= @columnname,  @numericvar=@numericvar, @tablename=@tablename
    select @output

相关内容

  • 没有找到相关文章

最新更新